ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
textfilelogger.cpp
1// #################################################################################################
2// alib::lox::loggers - ALox Logging Library
3//
4// Copyright 2013-2024 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
8
9#if !DOXYGEN
12# include <fstream>
13#endif // !DOXYGEN
14
15
16using namespace alib;
17
19 const alib::NString& loggerName )
20: PlainTextLogger( loggerName, "TEXTFILE", false )
21{
22 FileName << fileName;
23
24 // test open file for test
25 ALIB_STRINGS_TO_NARROW(FileName,nFileName,1024)
26 auto* os= new std::ofstream( nFileName, std::ios::app );
27 if ( !os->is_open() )
28 {
30 ALIB_WARNING( "ALOX", "Could not open file: {!Q}. System error code: {}",
32 }
33 else
34 // test run OK
35 os->close();
36
37 delete os;
38}
39
41{
42 ALIB_STRINGS_TO_NARROW(FileName,nFileName,1024)
43 auto* os= new std::ofstream( nFileName, std::ios::app );
44 if ( !os->is_open() )
45 {
46 writer.SetStream( nullptr );
48 ALIB_WARNING( "ALOX", "Could not open file: {!Q}. System error code: {}",
50 delete os; os= nullptr;
51 return;
52 }
53 else
54 LastSystemError= SystemErrors::None;
55
56 writer.SetStream( os );
57}
58
60{
61 auto* stream= writer.GetStream();
62 if( stream == nullptr )
63 return;
64
65 auto* os= dynamic_cast<std::ofstream*>(stream);
66 os->close();
67 delete os;
68 writer.SetStream( nullptr );
69}
70
71
73{
74 // save state (to have it in logText)
76
77 // open/close the file
78 if ( phase == lang::Phase::Begin )
79 openFile();
80 else if( writer.GetStream() != nullptr )
81 closeFile();
82}
83
85{
86 auto* stream= writer.GetStream();
87 if ( stream != nullptr && phase == lang::Phase::End )
88 *stream << std::endl;
89
90 // open/close
92 {
93 if ( phase == lang::Phase::Begin )
94 openFile();
95 else
96 closeFile();
97 }
98
99 return LastSystemError == SystemErrors::None;
100}
101
103{
104 if (writer.GetStream() != nullptr && LastSystemError == SystemErrors::None)
105 return writer.WriteAndGetWideLength( buffer.Substring<NC>( start, length ) );
106 return 0;
107}
108
virtual ALIB_API void notifyMultiLineOp(lang::Phase phase) override
virtual ALIB_API integer logSubstring(const String &buffer, integer start, integer length) override
StringWriter writer
Encapsulates the text file stream in a system dependent way.
bool currentlyInMultiLineOp
Flag to prevent file open/close operations when multi line text logging is performed.
alib::AString FileName
The path and fileName to the log file.
ALIB_API void closeFile()
Closes the file.
ALIB_API void openFile()
Opens the file.
virtual ALIB_API bool notifyLogOp(lang::Phase phase) override
ALIB_API TextFileLogger(const alib::String &fileName, const alib::NString &loggerName=nullptr)
TString< TChar > Substring(integer regionStart, integer regionLength=MAX_LEN) const
Definition string.hpp:406
ALIB_API integer WriteAndGetWideLength(const NString &src)
#define ALIB_WARNING(...)
Definition alib.hpp:1268
#define ALIB_STRINGS_TO_NARROW( src, dest, bufSize)
Phase
Denotes a phase, e.g.,of a transaction.
@ Begin
The start of a transaction.
@ End
The end of a transaction.
Definition alib.cpp:69
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:273
lang::system::SystemErrors SystemErrors
Type alias in namespace alib.