ALib C++ Library
Library Version: 2412 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/// \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#ifndef HPP_ALIB_LOX_TEXT_FILE_LOGGER
9#define HPP_ALIB_LOX_TEXT_FILE_LOGGER 1
10#pragma once
14
15namespace alib { namespace lox { namespace loggers {
16
17//==================================================================================================
18/// This is a very simple file logger for textual log outputs. The file name string provided
19/// in the constructor is not verified.
20/// The fileName may be changed by simply setting the public member #FileName.
21///
22/// In alignment with \alox principles (\alox does not throw!), the logger does not throw i/o
23/// exceptions. In debug-compilations, \alib warnings are issued.
24/// Recent system io/ errors are stored in public field# LastSystemError.
25/// The constructor tries to open/close the file with write permissions.
26/// For many applications, it should be enough to check this field after construction and
27/// take according actions on failure. Otherwise, errors may periodically be checked by an
28/// application (e.g., no space left on device, etc.).
29//==================================================================================================
31{
32 // #############################################################################################
33 // Internal fields
34 // #############################################################################################
35 protected:
36
37 /// Encapsulates the text file stream in a system dependent way.
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 //==========================================================================================
62 /// Creates a TextFileLogger.
63 /// @param fileName The filename (potentially including a path) of the output log file.
64 /// @param loggerName The name of the \e Logger. Defaults to "TEXTFILE".
65 //==========================================================================================
67 explicit TextFileLogger( const alib::String& fileName,
68 const alib::NString& loggerName =nullptr );
69
70 //==========================================================================================
71 /// Destructs a TextFileLogger
72 //==========================================================================================
73 virtual ~TextFileLogger() override {}
74
75 // #############################################################################################
76 // Protected methods
77 // #############################################################################################
78 protected:
79 //==========================================================================================
80 /// Opens the file.
81 //==========================================================================================
83 void openFile();
84
85 //==========================================================================================
86 /// Closes the file.
87 //==========================================================================================
89 void closeFile();
90
91 // #############################################################################################
92 // Abstract method implementations
93 // #############################################################################################
94 protected:
95 //==========================================================================================
96 /// Starts/ends log line. Appends a new-line character sequence to previously logged lines.
97 ///
98 /// @param phase Indicates the beginning or end of a log line.
99 /// @return Always returns true.
100 //==========================================================================================
102 virtual bool notifyLogOp( lang::Phase phase ) override;
103
104 //==========================================================================================
105 /// Writes the given region of the given string to the console.
106 ///
107 /// @param buffer The string to write a portion of.
108 /// @param start The start of the portion in \p{buffer} to write out.
109 /// @param length The length of the portion in \p{buffer} to write out.
110 /// @return The number of characters written, -1 on error.
111 //==========================================================================================
113 virtual integer logSubstring( const String& buffer,
114 integer start, integer length ) override;
115
116
117 //==========================================================================================
118 /// Empty implementation, not needed for this class
119 /// @param phase Indicates the beginning or end of a multi-line operation.
120 //==========================================================================================
122 virtual void notifyMultiLineOp ( lang::Phase phase ) override;
123
124}; // class TextFileLogger
125
126
127}} // namespace alib[::lox::loggers]
128
129/// Type alias in namespace \b alib.
131
132} // namespace [alib]
133
134#endif // HPP_ALIB_LOX_TEXT_FILE_LOGGER
135
virtual ~TextFileLogger() override
Destructs a TextFileLogger.
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
#define ALIB_API
Definition alib.hpp:639
SystemErrors
Denotes result values returned by system functions (glibc, etc).
Phase
Denotes a phase, e.g.,of a transaction.
Definition alib.cpp:69
lox::loggers::TextFileLogger TextFileLogger
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:273