ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
textfilelogger.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_alox of the \aliblong.
4///
5/// Copyright 2013-2026 A-Worx GmbH, Germany.
6/// Published under #"mainpage_license".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace lox { namespace loggers {
9
10//==================================================================================================
11/// This is a very simple file logger for textual log outputs. The file name string provided
12/// in the constructor is not verified.
13/// The fileName may be changed by simply setting the public member #".FileName".
14///
15/// In alignment with \alox principles (\alox does not throw!), the logger does not throw i/o
16/// exceptions. In debug-compilations, \alib warnings are issued.
17/// Recent system io/ errors are stored in public field# LastSystemError.
18/// The constructor tries to open/close the file with write permissions.
19/// For many applications, it should be enough to check this field after construction and
20/// take according actions on failure. Otherwise, errors may periodically be checked by an
21/// application (e.g., no space left on device, etc.).
22//==================================================================================================
24 //################################################################################################
25 // Internal fields
26 //################################################################################################
27 protected:
28 /// Allocator used for the #"std::OStreamWriter".
30
31 /// The #"%OStreamWriter" encapsulated in a placeholder. The instance is constructed
32 /// and destructed with #"notifyPlainTextLogOp". With that, synchronized output is
33 /// guaranteed.
35
36 /// Encapsulates the text file stream in a system-dependent way.
37 std::ofstream* ofs =nullptr;
38
39 /// Flag to prevent file open/close operations when multi-line text logging is performed.
41
42
43 //################################################################################################
44 // Public fields
45 //################################################################################################
46 public:
47 /// The path and fileName to the log file.
49
50 /// Errors that usually indicate i/o problems. With construction of the logger,
51 /// the file is tried to be opened and closed, which might indicate major problems
52 /// (permissions, path, etc) with this public field very early.
53 std::errc LastSystemError =std::errc(0);
54
55
56 //################################################################################################
57 // Constructor/destructor
58 //################################################################################################
59 public:
60 /// Creates a TextFileLogger.
61 /// @param fileName The filename (potentially including a path) of the output log file.
62 /// @param loggerName The name of the \e Logger. Defaults to "TEXTFILE".
64 explicit TextFileLogger( const alib::String& fileName,
65 const alib::NString& loggerName =nullptr );
66
67 /// Destructs a TextFileLogger
68 virtual ~TextFileLogger() override {}
69
70 //################################################################################################
71 // Protected methods
72 //################################################################################################
73 protected:
74 /// Opens the file.
76 void openFile();
77
78 /// Closes the file.
80 void closeFile();
81
82 //################################################################################################
83 // Abstract method implementations
84 //################################################################################################
85 protected:
86 /// Starts/ends log line. Appends a new-line character sequence to previously logged lines.
87 ///
88 /// @param phase Indicates the beginning or end of a log line.
89 /// @return Always returns true.
91 virtual bool notifyPlainTextLogOp( lang::Phase phase ) override;
92
93 /// Writes the given region of the given string to the console.
94 ///
95 /// @param buffer The string to write a portion of.
96 /// @param start The start of the portion in \p{buffer} to write out.
97 /// @param length The length of the portion in \p{buffer} to write out.
98 /// @return The number of characters written, -1 on error.
100 virtual integer logPlainTextPart( const String& buffer,
101 integer start, integer length ) override;
102
103
104 /// Empty implementation, not needed for this class
105 /// @param phase Indicates the beginning or end of a multi-line operation.
107 virtual void notifyMultiLineOp ( lang::Phase phase ) override;
108
109}; // class TextFileLogger
110
111
112}} // namespace alib[::lox::loggers]
113
114/// Type alias in namespace #"%alib".
116
117} // namespace [alib]
#define ALIB_DLL
#define ALIB_EXPORT
virtual void notifyMultiLineOp(lang::Phase phase) override
lang::Placeholder< OStreamWriter< nchar, MonoAllocator, true > > writer
virtual bool notifyPlainTextLogOp(lang::Phase phase) override
virtual ~TextFileLogger() override
Destructs a TextFileLogger.
bool currentlyInMultiLineOp
Flag to prevent file open/close operations when multi-line text logging is performed.
std::ofstream * ofs
Encapsulates the text file stream in a system-dependent way.
MonoAllocator ma
Allocator used for the #"std::OStreamWriter".
virtual integer logPlainTextPart(const String &buffer, integer start, integer length) override
TextFileLogger(const alib::String &fileName, const alib::NString &loggerName=nullptr)
alib::AString FileName
The path and fileName to the log file.
Phase
Denotes a phase, e.g.,of a transaction.
Definition alox.cpp:14
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
strings::TString< nchar > NString
Type alias in namespace #"%alib".
Definition string.hpp:2174
lox::loggers::TextFileLogger TextFileLogger
Type alias in namespace #"%alib".
lang::integer integer
Type alias in namespace #"%alib".
Definition integers.hpp:149
strings::TString< character > String
Type alias in namespace #"%alib".
Definition string.hpp:2165
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace #"%alib".