ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
textfilelogger.inl
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/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software 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 //################################################################################################
26 // Internal fields
27 //################################################################################################
28 protected:
29 /// Allocator used for the \alib{strings::compatibility::std;OStreamWriter}.
31
32 /// The \b OStreamWriter encapsulated in a placeholder. The instance is constructed
33 /// and destructed with #notifyPlainTextLogOp. With that, synchronized output is
34 /// guaranteed.
36
37 /// Encapsulates the text file stream in a system-dependent way.
38 std::ofstream* ofs =nullptr;
39
40 /// Flag to prevent file open/close operations when multi-line text logging is performed.
42
43
44 //################################################################################################
45 // Public fields
46 //################################################################################################
47 public:
48 /// The path and fileName to the log file.
50
51 /// Errors that usually indicate i/o problems. With construction of the logger,
52 /// the file is tried to be opened and closed, which might indicate major problems
53 /// (permissions, path, etc) with this public field very early.
54 SystemErrors LastSystemError =SystemErrors::None;
55
56
57 //################################################################################################
58 // Constructor/destructor
59 //################################################################################################
60 public:
61 /// Creates a TextFileLogger.
62 /// @param fileName The filename (potentially including a path) of the output log file.
63 /// @param loggerName The name of the \e Logger. Defaults to "TEXTFILE".
65 explicit TextFileLogger( const alib::String& fileName,
66 const alib::NString& loggerName =nullptr );
67
68 /// Destructs a TextFileLogger
69 virtual ~TextFileLogger() override {}
70
71 //################################################################################################
72 // Protected methods
73 //################################################################################################
74 protected:
75 /// Opens the file.
77 void openFile();
78
79 /// Closes the file.
81 void closeFile();
82
83 //################################################################################################
84 // Abstract method implementations
85 //################################################################################################
86 protected:
87 /// Starts/ends log line. Appends a new-line character sequence to previously logged lines.
88 ///
89 /// @param phase Indicates the beginning or end of a log line.
90 /// @return Always returns true.
92 virtual bool notifyPlainTextLogOp( lang::Phase phase ) override;
93
94 /// Writes the given region of the given string to the console.
95 ///
96 /// @param buffer The string to write a portion of.
97 /// @param start The start of the portion in \p{buffer} to write out.
98 /// @param length The length of the portion in \p{buffer} to write out.
99 /// @return The number of characters written, -1 on error.
101 virtual integer logPlainTextPart( const String& buffer,
102 integer start, integer length ) override;
103
104
105 /// Empty implementation, not needed for this class
106 /// @param phase Indicates the beginning or end of a multi-line operation.
108 virtual void notifyMultiLineOp ( lang::Phase phase ) override;
109
110}; // class TextFileLogger
111
112
113}} // namespace alib[::lox::loggers]
114
115/// Type alias in namespace \b alib.
117
118} // namespace [alib]
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
virtual ~TextFileLogger() override
Destructs a TextFileLogger.
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.
#define ALIB_DLL
Definition alib.inl:503
#define ALIB_EXPORT
Definition alib.inl:497
Phase
Denotes a phase, e.g.,of a transaction.
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
lox::loggers::TextFileLogger TextFileLogger
Type alias in namespace alib.
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
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2189
system::SystemErrors SystemErrors
Type alias in namespace alib.