ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
textfilelogger.cpp
1//##################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6//##################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14//========================================= Global Fragment ========================================
15#ifndef H_ALIB_ALOX
18#endif
19#include <fstream>
20//============================================== Module ============================================
21#if ALIB_C20_MODULES
22 module ALib.ALox.Impl;
23 import ALib.Lang;
24 import ALib.Strings;
25#else
26# include "ALib.Lang.H"
27# include "ALib.Strings.H"
28# include "ALib.ALox.H"
29# include "ALib.ALox.Impl.H"
30#endif
31//========================================== Implementation ========================================
32using namespace alib;
33
35 const alib::NString& loggerName )
36: PlainTextLogger( loggerName, "TEXTFILE" )
37, ma( ALIB_DBG("TextFileLogger",) 8 ) {
38 FileName << fileName;
39
40 // test open file for test
41 ALIB_STRINGS_TO_NARROW(FileName,nFileName,1024)
42 auto* os= new std::ofstream( nFileName, std::ios::app );
43 if ( !os->is_open() ) {
45 ALIB_WARNING( "ALOX", "Could not open file: \"{}\". System error code: {}",
47 }
48 else
49 // test run OK
50 os->close();
51
52 delete os;
53}
54
56 ALIB_STRINGS_TO_NARROW(FileName,nFileName,1024)
57 ofs= new std::ofstream( nFileName, std::ios::app );
58 if ( !ofs->is_open() ) {
60 ALIB_WARNING( "ALOX", "Could not open file: \"{}\". System error code: {}",
62 delete ofs; ofs= nullptr;
63 return;
64 }
65
66 LastSystemError= SystemErrors::None;
67 writer.Construct( *ofs, ma );
68}
69
71 if( ofs == nullptr )
72 return;
73
74 writer.Destruct();
75 ofs->close();
76 delete ofs;
77}
78
79
81 // save state (to have it in logText)
83
84 // open/close the file
85 if ( phase == lang::Phase::Begin )
86 openFile();
87 else if( ofs != nullptr )
88 closeFile();
89}
90
92 if ( ofs != nullptr && phase == lang::Phase::End )
93 (*ofs) << std::endl;
94
95 // open/close
97 if ( phase == lang::Phase::Begin )
98 openFile();
99 else
100 closeFile();
101 }
102
103 return LastSystemError == SystemErrors::None;
104}
105
107 if (ofs == nullptr || LastSystemError != SystemErrors::None)
108 return 0;
109 integer printedWidth;
110 writer->Write( buffer.Substring<NC>( start, length ), &printedWidth );
111 return printedWidth;
112}
virtual ALIB_DLL void notifyMultiLineOp(lang::Phase phase) override
lang::Placeholder< OStreamWriter< nchar, MonoAllocator, true > > writer
ALIB_DLL void openFile()
Opens the file.
virtual ALIB_DLL bool notifyPlainTextLogOp(lang::Phase phase) override
bool currentlyInMultiLineOp
Flag to prevent file open/close operations when multi-line text logging is performed.
ALIB_DLL void closeFile()
Closes the file.
std::ofstream * ofs
Encapsulates the text file stream in a system-dependent way.
MonoAllocator ma
Allocator used for the OStreamWriter.
virtual ALIB_DLL integer logPlainTextPart(const String &buffer, integer start, integer length) override
ALIB_DLL TextFileLogger(const alib::String &fileName, const alib::NString &loggerName=nullptr)
alib::AString FileName
The path and fileName to the log file.
PlainTextLogger(const NString &name, const NString &typeName)
TString< TChar > Substring(integer regionStart, integer regionLength=MAX_LEN) const
Definition string.inl:384
#define ALIB_STRINGS_TO_NARROW( src, dest, bufSize)
#define ALIB_WARNING(domain,...)
Definition alib.inl:1063
#define ALIB_DBG(...)
Definition alib.inl:853
Phase
Denotes a phase, e.g.,of a transaction.
@ Begin
The start of a transaction.
@ End
The end of a transaction.
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
strings::TString< nchar > NString
Type alias in namespace alib.
Definition string.inl:2198
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2189
system::SystemErrors SystemErrors
Type alias in namespace alib.