ALib C++ Library
Library Version: 2402 R1
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 !defined(ALIB_DOX)
10#if !defined (HPP_ALOX_TEXT_FILE_LOGGER)
12#endif
13
14#if !defined(HPP_ALIB_CAMP_MESSAGE_REPORT)
16#endif
17
18#if !defined (_GLIBCXX_FSTREAM) && !defined(_FSTREAM_)
19# include <fstream>
20#endif
21#endif // !defined(ALIB_DOX)
22
23
24using namespace alib;
25
27 const alib::NString& loggerName )
28: PlainTextLogger( loggerName, "TEXTFILE", false )
29{
30 FileName << fileName;
31
32 // test open file for test
33 ALIB_STRINGS_TO_NARROW(FileName,nFileName,1024)
34 auto* os= new std::ofstream( nFileName, std::ios::app );
35 if ( !os->is_open() )
36 {
38 ALIB_WARNING( "ALOX", "Could not open file: {!Q}. System error code: {}",
40 }
41 else
42 // test run OK
43 os->close();
44
45 delete os;
46}
47
49{
50 ALIB_STRINGS_TO_NARROW(FileName,nFileName,1024)
51 auto* os= new std::ofstream( nFileName, std::ios::app );
52 if ( !os->is_open() )
53 {
54 writer.SetStream( nullptr );
56 ALIB_WARNING( "ALOX", "Could not open file: {!Q}. System error code: {}",
58 delete os; os= nullptr;
59 return;
60 }
61 else
62 LastSystemError= SystemErrors::None;
63
64 writer.SetStream( os );
65}
66
68{
69 auto* stream= writer.GetStream();
70 if( stream == nullptr )
71 return;
72
73 auto* os= dynamic_cast<std::ofstream*>(stream);
74 os->close();
75 delete os;
76 writer.SetStream( nullptr );
77}
78
79
81{
82 // save state (to have it in logText)
84
85 // open/close the file
86 if ( phase == lang::Phase::Begin )
87 openFile();
88 else if( writer.GetStream() != nullptr )
89 closeFile();
90}
91
93{
94 auto* stream= writer.GetStream();
95 if ( stream != nullptr && phase == lang::Phase::End )
96 *stream << std::endl;
97
98 // open/close
100 {
101 if ( phase == lang::Phase::Begin )
102 openFile();
103 else
104 closeFile();
105 }
106
107 return LastSystemError == SystemErrors::None;
108}
109
111{
112 if (writer.GetStream() != nullptr && LastSystemError == SystemErrors::None)
113 return writer.WriteAndGetWideLength( buffer.Substring<false>( start, length ) );
114 return 0;
115}
virtual ALIB_API void notifyMultiLineOp(lang::Phase phase) override
virtual ALIB_API integer logSubstring(const String &buffer, integer start, integer length) override
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:314
#define ALIB_WARNING(...)
Definition alib.hpp:981
#define ALIB_STRINGS_TO_NARROW( src, dest, bufSize)
@ Begin
The start of a transaction.
@ End
The end of a transaction.
Definition alib.cpp:57
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286
lang::system::SystemErrors SystemErrors
Type alias in namespace alib.