ALib C++ Library
Library Version: 2402 R1
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
9#ifndef HPP_ALOX_TEXT_FILE_LOGGER
10#define HPP_ALOX_TEXT_FILE_LOGGER 1
11
12#if !defined (HPP_ALOX_DETAIL_TEXTLOGGER_PLAINTEXTLOGGER)
14#endif
15
16#if !defined (HPP_ALIB_COMPATIBILITY_STD_STRINGS_IOSTREAM)
18#endif
19
20#if !defined(HPP_ALIB_CAMP_ERRORS)
22#endif
23
24namespace alib { namespace lox { namespace loggers {
25
26/** ************************************************************************************************
27 * This is a very simple file logger for textual log outputs. The file name string provided
28 * in the constructor is not verified.
29 * The fileName may be changed by simply setting the public member #FileName.
30 *
31 * In alignment with \alox principles (\alox does not throw!), the logger does not throw i/o
32 * exceptions. In debug-compilations, \alib warnings are issued.
33 * Recent system io/ errors are stored in public field# LastSystemError.
34 * The constructor tries to open/close the file with write permissions.
35 * For many applications, it should be enough to check this field after construction and
36 * take according actions on failure. Otherwise, errors may periodically be checked by an
37 * application (e.g. no space left on device, etc.).
38 **************************************************************************************************/
40{
41 // #############################################################################################
42 // Internal fields
43 // #############################################################################################
44 protected:
45
46 /** Encapsulates the text file stream in a system dependent way. */
48
49 /** Flag to prevent file open/close operations when multi line text logging is performed. */
51
52
53 // #############################################################################################
54 // Public fields
55 // #############################################################################################
56 public:
57 /** The path and fileName to the log file. */
59
60 /** Errors that usually indicate i/o problems. With construction of the logger,
61 * the file is tried to be opened and closed, which might indicate major problems
62 * (permissions, path, etc) with this public field very early. */
63 SystemErrors LastSystemError = SystemErrors::None;
64
65
66 // #############################################################################################
67 // Constructor/destructor
68 // #############################################################################################
69 public:
70 /** ****************************************************************************************
71 * Creates a TextFileLogger.
72 * @param fileName The filename (potentially including a path) of the output log file.
73 * @param loggerName The name of the \e Logger. Defaults to "TEXTFILE".
74 ******************************************************************************************/
76 explicit TextFileLogger( const alib::String& fileName,
77 const alib::NString& loggerName =nullptr );
78
79 /** ****************************************************************************************
80 * Destructs a TextFileLogger
81 ******************************************************************************************/
82 virtual ~TextFileLogger() override {}
83
84 // #############################################################################################
85 // Protected methods
86 // #############################################################################################
87 protected:
88 /** ****************************************************************************************
89 * Opens the file.
90 ******************************************************************************************/
92 void openFile();
93
94 /** ****************************************************************************************
95 * Closes the file.
96 ******************************************************************************************/
98 void closeFile();
99
100 // #############################################################################################
101 // Abstract method implementations
102 // #############################################################################################
103 protected:
104 /** ****************************************************************************************
105 * Starts/ends log line. Appends a new-line character sequence to previously logged lines.
106 *
107 * @param phase Indicates the beginning or end of a log line.
108 * @return Always returns true.
109 ******************************************************************************************/
111 virtual bool notifyLogOp( lang::Phase phase ) override;
112
113 /** ****************************************************************************************
114 * Writes the given region of the given string to the console.
115 *
116 * @param buffer The string to write a portion of.
117 * @param start The start of the portion in \p{buffer} to write out.
118 * @param length The length of the portion in \p{buffer} to write out.
119 * @return The number of characters written, -1 on error.
120 ******************************************************************************************/
122 virtual integer logSubstring( const String& buffer,
123 integer start, integer length ) override;
124
125
126 /** ****************************************************************************************
127 * Empty implementation, not needed for this class
128 * @param phase Indicates the beginning or end of a multi-line operation.
129 ******************************************************************************************/
131 virtual void notifyMultiLineOp ( lang::Phase phase ) override;
132
133}; // class TextFileLogger
134
135
136}} // namespace alib[::lox::loggers]
137
138/// Type alias in namespace \b alib.
140
141} // namespace [alib]
142
143#endif // HPP_ALOX_TEXT_FILE_LOGGER
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
#define ALIB_API
Definition alib.hpp:538
Definition alib.cpp:57
lox::loggers::TextFileLogger TextFileLogger
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286