ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
ansilogger.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/// A logger that logs all messages to the <em>std::basic_ostream</em> instance provided in the
12/// constructor. The name of the \e Logger defaults to "ANSI_LOGGER".
13///
14/// ALox text logger escape sequences (see class \ref alib::lox::ESC "ESC")
15/// are translated to ANSI escape sequences.
16/// Support for ANSI escape sequences (also referred to as <em>VT100 terminal emulation</em>)
17/// is available on most unix terminal windows. Besides text colors, bold and italics font style
18/// can be set.
19/// ANSI escape sequences are also available in various IDE output windows.
20///
21/// Foreground and background colors can be set to be either light/dark or dark/light. This improves
22/// the readability of log output a lot and even allows reading if foreground and background colors
23/// are the same (they then still differ). However, the right setting for this is dependent on
24/// the color scheme of the final output device (window). To manipulate the right setting, see field
25/// #CFP and also configuration variable \ref alxcvALOX_CONSOLE_LIGHT_COLORS.
26///
27/// In the method #AcknowledgeLox, the default meta info string and color settings are changed
28/// to fit this logger.
29///
30/// \note
31/// Instead of using ANSI sequences in the format strings directly, which would lower the
32/// run-time cost a little, ESC sequences are used because this way the light/dark color
33/// selection, which depends on the field #CFP is performed correctly.
34///
35/// There is not 100% match between the ANSI sequences and the definitions in
36/// \ref alib::lox::ESC "ESC".
37/// For example ESC does not provide all ANSI colors and no blinking. On the other hand,
38/// ANSI does not allow to reset the style without resetting the colors.
39/// Of course, it is no problem to log other ANSI codes directly into an \b %AnsiLogger.
40/// In this case, other Loggers that might be attached to the same Lox and that do not
41/// support ANSI must be equipped with corresponding replacement information.
42/// In other words: To support the same log output into different loggers, it is
43/// recommended to use \ref alib::lox::ESC "ESC" sequences instead of
44/// directly using ANSI codes.
45///
46/// The ANSI codes used by this class are exposed through a list of fields.
47/// They might be useful for manipulating the attributes of the various inherited format
48/// variables, which of course might contain native ANSI sequences.
49/// (In contrast to the log messages themselves, this meta-information is specific to a logger
50/// instance and this way it does not need to be replaced in other loggers).
51//==================================================================================================
53{
54 // #############################################################################################
55 // ANSI Escape Code definitions
56 // #############################################################################################
57 public:
58 static constexpr character ANSI_RED [6] { A_CHAR("\033[31m") }; ///< Select red as foreground color
59 static constexpr character ANSI_GREEN [6] { A_CHAR("\033[32m") }; ///< Select green as foreground color
60 static constexpr character ANSI_YELLOW [6] { A_CHAR("\033[33m") }; ///< Select yellow as foreground color
61 static constexpr character ANSI_BLUE [6] { A_CHAR("\033[34m") }; ///< Select blue as foreground color
62 static constexpr character ANSI_MAGENTA [6] { A_CHAR("\033[35m") }; ///< Select magenta as foreground color
63 static constexpr character ANSI_CYAN [6] { A_CHAR("\033[36m") }; ///< Select cyan as foreground color
64 static constexpr character ANSI_BLACK [6] { A_CHAR("\033[30m") }; ///< Select black as foreground color
65 static constexpr character ANSI_WHITE [11] { A_CHAR("\033[38;5;15m") }; ///< Select white as foreground color
66 static constexpr character ANSI_GRAY [12] { A_CHAR("\033[38;5;240m")}; ///< Select gray as foreground color
67 static constexpr character ANSI_STD_COL [6] { A_CHAR("\033[39m") }; ///< Select standard foreground color
68
69 static constexpr character ANSI_BG_RED [6] { A_CHAR("\033[41m") }; ///< Select red as background color
70 static constexpr character ANSI_BG_GREEN [6] { A_CHAR("\033[42m") }; ///< Select green as background color
71 static constexpr character ANSI_BG_YELLOW [6] { A_CHAR("\033[43m") }; ///< Select yellow as background color
72 static constexpr character ANSI_BG_BLUE [6] { A_CHAR("\033[44m") }; ///< Select blue as background color
73 static constexpr character ANSI_BG_MAGENTA [6] { A_CHAR("\033[45m") }; ///< Select magenta as background color
74 static constexpr character ANSI_BG_CYAN [6] { A_CHAR("\033[46m") }; ///< Select cyan as background color
75 static constexpr character ANSI_BG_BLACK [6] { A_CHAR("\033[40m") }; ///< Select black as background color
76 static constexpr character ANSI_BG_WHITE [11] { A_CHAR("\033[48;5;15m") }; ///< Select white as background color
77 static constexpr character ANSI_BG_GRAY [12] { A_CHAR("\033[48;5;240m")}; ///< Select gray as background color
78 static constexpr character ANSI_BG_STD_COL [6] { A_CHAR("\033[49m") }; ///< Select standard background color
79
80 static constexpr character ANSI_LIGHT_RED [11] { A_CHAR("\033[38;5;09m") }; ///< Select light red as foreground color
81 static constexpr character ANSI_LIGHT_GREEN [11] { A_CHAR("\033[38;5;10m") }; ///< Select light green as foreground color
82 static constexpr character ANSI_LIGHT_YELLOW [11] { A_CHAR("\033[38;5;11m") }; ///< Select light yellow as foreground color
83 static constexpr character ANSI_LIGHT_BLUE [11] { A_CHAR("\033[38;5;12m") }; ///< Select light blue as foreground color
84 static constexpr character ANSI_LIGHT_MAGENTA [11] { A_CHAR("\033[38;5;13m") }; ///< Select light magenta as foreground color
85 static constexpr character ANSI_LIGHT_CYAN [11] { A_CHAR("\033[38;5;14m") }; ///< Select light cyan as foreground color
86 static constexpr character ANSI_LIGHT_GRAY [12] { A_CHAR("\033[38;5;250m")}; ///< Select light gray as foreground color
87 static constexpr character ANSI_LIGHT_STD_COL [6] { A_CHAR("\033[39m") }; ///< Select standard foreground color
88
89 static constexpr character ANSI_BG_LIGHT_RED [11] { A_CHAR("\033[48;5;09m") }; ///< Select light red as background color
90 static constexpr character ANSI_BG_LIGHT_GREEN [11] { A_CHAR("\033[48;5;10m") }; ///< Select light green as background color
91 static constexpr character ANSI_BG_LIGHT_YELLOW [11] { A_CHAR("\033[48;5;11m") }; ///< Select light yellow as background color
92 static constexpr character ANSI_BG_LIGHT_BLUE [11] { A_CHAR("\033[48;5;12m") }; ///< Select light blue as background color
93 static constexpr character ANSI_BG_LIGHT_MAGENTA[11] { A_CHAR("\033[48;5;13m") }; ///< Select light magenta as background color
94 static constexpr character ANSI_BG_LIGHT_CYAN [11] { A_CHAR("\033[48;5;14m") }; ///< Select light cyan as background color
95 static constexpr character ANSI_BG_LIGHT_GRAY [12] { A_CHAR("\033[48;5;250m")}; ///< Select light gray as background color
96 static constexpr character ANSI_BG_LIGHT_STD_COL[6] { A_CHAR("\033[49m") }; ///< Select standard background color
97
98 static constexpr character ANSI_BOLD [5] { A_CHAR("\033[1m") }; ///< Select bold font style
99 static constexpr character ANSI_ITALICS [5] { A_CHAR("\033[3m") }; ///< Select italics font style
100 static constexpr character ANSI_STD_STYLE [5] { A_CHAR("\033[0m") }; ///< Select standard font style
101 static constexpr character ANSI_RESET [5] { A_CHAR("\033[0m") }; ///< Reset colors and font style
102
103
104
105 // #############################################################################################
106 // protected fields
107 // #############################################################################################
108 protected:
109 /// Encapsulates \c std::cout, \c std::wcout or the output stream provided in the
110 /// corresponding constructor.
112
113 // #############################################################################################
114 // public fields
115 // #############################################################################################
116 public:
117
118 /// As of today, this parameter struct has only one value of type
119 /// \alib{lox::textlogger::ColorfulLoggerParameters;LightColorUsage}. With this logger,
120 /// this flag is used as follows.
121 ///
122 /// Foreground and background colors chosen by this class might differ in their intensity.
123 /// This increases the overall readability by increasing the contrast.
124 /// If the background color of a console window is dark, then the background colors of
125 /// colored log output should be darker colors than the foreground colors - and vice versa.
126 ///
127 /// Depending on the setting of this field, \alox
128 /// \ref alib::lox::ESC "escape codes" for colors are translated to normal ANSI colors or
129 /// lighter ones:
130 /// - If this field evaluates to
131 /// \alib{lox::textlogger::ColorfulLoggerParameters;LightColorUsage;Never}, light colors
132 /// are never used.
133 /// - If this field is
134 /// \alib{lox::textlogger::ColorfulLoggerParameters;LightColorUsage;Foreground},
135 /// colors will be light colors and background colors dark ones. This is the default.
136 /// - With
137 /// \alib{lox::textlogger::ColorfulLoggerParameters;LightColorUsage;Background},
138 /// background colors will be light colors and foreground colors dark ones.
139 ///
140 /// The configuration variable \ref alxcvALOX_CONSOLE_LIGHT_COLORS allows externally
141 /// modifying this flag. It is read once within the constructor.
143
144 // #############################################################################################
145 // Constructor/destructor
146 // #############################################################################################
147 public:
148 //==========================================================================================
149 /// Creates an AnsiLogger using \c std::cout respectively \c std::wcout.
150 /// @param name The name of the \e Logger, defaults to what is provided with
151 /// parameter \p{typeName}.
152 /// @param typeName The type of the \e Logger, defaults to "ANSI".
153 //==========================================================================================
154 ALIB_DLL explicit
155 AnsiLogger( const NString& name= nullptr, const NString& typeName= "ANSI" )
156 : TextLogger( name, typeName, true ) {}
157
158 //==========================================================================================
159 /// Creates an AnsiLogger using the given output stream.
160 /// \note If \c std::cout respectively \c std::wcout must not be provided here. Instead, the
161 /// alternative constructor omitting parameter \p{pOStream} is to be used.
162 /// @param pOStream The output stream to write into.
163 /// @param name The name of the \e Logger, defaults to what is provided with
164 /// parameter \p{typeName}.
165 /// @param typeName The type of the \e Logger, defaults to "ANSI".
166 //==========================================================================================
167 ALIB_DLL explicit
168 AnsiLogger( std::ostream* pOStream,
169 const NString& name = nullptr,
170 const NString& typeName = "ANSI" )
171 : TextLogger( name, typeName, false ) { writer.SetStream( pOStream ); }
172
173
174 //==========================================================================================
175 /// Destructs an AnsiLogger
176 //==========================================================================================
178 virtual ~AnsiLogger() override;
179
180 // #############################################################################################
181 // Interface
182 // #############################################################################################
183 public:
184
185 // #############################################################################################
186 // Abstract method implementations
187 // #############################################################################################
188 protected:
189 //==========================================================================================
190 /// Implementation of the abstract method of parent class TextLogger.
191 /// Logs messages to the basic output stream provided in the constructor. Replaces
192 /// \alox ESC escape sequences with ANSI escape sequences.
193 ///
194 /// @param domain The <em>Log Domain</em>.
195 /// @param verbosity The verbosity. This has been checked to be active already on this
196 /// stage and is provided to be able to be logged out only.
197 /// @param msg The log message.
198 /// @param scope Information about the scope of the <em>Log Statement</em>..
199 /// @param lineNumber The line number of a multi-line message, starting with 0. For
200 /// single line messages this is -1.
201 //==========================================================================================
203 virtual void logText( detail::Domain& domain,
204 Verbosity verbosity,
205 AString& msg,
206 detail::ScopeInfo& scope,
207 int lineNumber) override;
208
209 //==========================================================================================
210 /// Empty implementation.
211 //==========================================================================================
212 virtual void notifyMultiLineOp ( lang::Phase ) override {}
213
214
215 //==========================================================================================
216 /// Invokes parent method. Then reads and interprets configuration variable
217 /// \ref alxcvALOX_CONSOLE_LIGHT_COLORS. Finally it is checked if variable
218 /// \ref alxcvALOX_LOGGERNAME_FORMAT has priority of default values, and if yes, such
219 /// values are overwritten to include ANSI color codes. These color codes are retrieved from
220 /// resources named <c>"Var_D21A"</c>.
221 ///
222 /// @param lox The \b %Lox to acknowledge insertion or removal
223 /// @param op The operation. Either \b ContainerOp::Insert or \b ContainerOp::Remove.
224 //==========================================================================================
226 virtual void AcknowledgeLox( detail::LoxImpl* lox, lang::ContainerOp op ) override;
227
228
229}; // class AnsiLogger
230
231//==================================================================================================
232/// A #AnsiLogger that logs all messages to the standard output <em>cout</em>.
233/// The name of the \e Logger defaults to "ANSI_CONSOLE".
234///
235/// Provides 'cout' to the constructor of its parent class %AnsiLogger.
236/// See class #AnsiLogger for more information on ANSI escape sequences and their use.
237///
238/// \note To avoid misunderstandings: This class cannot enable the output console (which receives
239/// \alox log data) to support ANSI Escape Codes. The opposite is right: this class should be
240/// used only if the console supports ANSI Escape Codes.
241//==================================================================================================
243{
244 // #############################################################################################
245 // Constructor/destructor
246 // #############################################################################################
247 public:
248 //==========================================================================================
249 /// Creates an AnsiConsoleLogger.
250 /// @param name (Optional) The name of the \e Logger, defaults to "CONSOLE".
251 //==========================================================================================
253 explicit AnsiConsoleLogger( const NString& name= nullptr );
254
255 //==========================================================================================
256 /// Destructs an AnsiConsoleLogger
257 //==========================================================================================
259 virtual ~AnsiConsoleLogger() override;
260}; // class AnsiConsoleLogger
261
262
263}} // namespace alib[::lox::loggers]
264
265/// Type alias in namespace \b alib.
267
268/// Type alias in namespace \b alib.
270
271} // namespace [alib]
virtual ALIB_DLL ~AnsiConsoleLogger() override
Destructs an AnsiConsoleLogger.
ALIB_DLL AnsiConsoleLogger(const NString &name=nullptr)
virtual ALIB_DLL void logText(detail::Domain &domain, Verbosity verbosity, AString &msg, detail::ScopeInfo &scope, int lineNumber) override
static constexpr character ANSI_BLACK[6]
Select black as foreground color.
static constexpr character ANSI_BG_LIGHT_CYAN[11]
Select light cyan as background color.
static constexpr character ANSI_CYAN[6]
Select cyan as foreground color.
virtual ALIB_DLL ~AnsiLogger() override
Destructs an AnsiLogger.
static constexpr character ANSI_BG_BLACK[6]
Select black as background color.
static constexpr character ANSI_BG_MAGENTA[6]
Select magenta as background color.
static constexpr character ANSI_WHITE[11]
Select white as foreground color.
static constexpr character ANSI_BG_YELLOW[6]
Select yellow as background color.
virtual void notifyMultiLineOp(lang::Phase) override
Empty implementation.
static constexpr character ANSI_LIGHT_STD_COL[6]
Select standard foreground color.
static constexpr character ANSI_BG_LIGHT_YELLOW[11]
Select light yellow as background color.
static constexpr character ANSI_BG_WHITE[11]
Select white as background color.
static constexpr character ANSI_YELLOW[6]
Select yellow as foreground color.
static constexpr character ANSI_BG_GREEN[6]
Select green as background color.
static constexpr character ANSI_BG_STD_COL[6]
Select standard background color.
static constexpr character ANSI_GRAY[12]
Select gray as foreground color.
static constexpr character ANSI_BG_GRAY[12]
Select gray as background color.
static constexpr character ANSI_GREEN[6]
Select green as foreground color.
static constexpr character ANSI_BG_RED[6]
Select red as background color.
textlogger::ColorfulLoggerParameters CFP
static constexpr character ANSI_MAGENTA[6]
Select magenta as foreground color.
static constexpr character ANSI_LIGHT_BLUE[11]
Select light blue as foreground color.
static constexpr character ANSI_BG_LIGHT_GRAY[12]
Select light gray as background color.
static constexpr character ANSI_BLUE[6]
Select blue as foreground color.
ALIB_DLL AnsiLogger(const NString &name=nullptr, const NString &typeName="ANSI")
static constexpr character ANSI_LIGHT_MAGENTA[11]
Select light magenta as foreground color.
static constexpr character ANSI_LIGHT_GRAY[12]
Select light gray as foreground color.
static constexpr character ANSI_BOLD[5]
Select bold font style.
static constexpr character ANSI_BG_LIGHT_MAGENTA[11]
Select light magenta as background color.
static constexpr character ANSI_RESET[5]
Reset colors and font style.
static constexpr character ANSI_BG_CYAN[6]
Select cyan as background color.
static constexpr character ANSI_STD_STYLE[5]
Select standard font style.
static constexpr character ANSI_BG_LIGHT_BLUE[11]
Select light blue as background color.
static constexpr character ANSI_STD_COL[6]
Select standard foreground color.
static constexpr character ANSI_LIGHT_RED[11]
Select light red as foreground color.
static constexpr character ANSI_LIGHT_GREEN[11]
Select light green as foreground color.
static constexpr character ANSI_BG_LIGHT_STD_COL[6]
Select standard background color.
static constexpr character ANSI_RED[6]
Select red as foreground color.
static constexpr character ANSI_BG_LIGHT_RED[11]
Select light red as background color.
static constexpr character ANSI_LIGHT_CYAN[11]
Select light cyan as foreground color.
ALIB_DLL AnsiLogger(std::ostream *pOStream, const NString &name=nullptr, const NString &typeName="ANSI")
static constexpr character ANSI_BG_BLUE[6]
Select blue as background color.
virtual ALIB_DLL void AcknowledgeLox(detail::LoxImpl *lox, lang::ContainerOp op) override
static constexpr character ANSI_BG_LIGHT_GREEN[11]
Select light green as background color.
static constexpr character ANSI_LIGHT_YELLOW[11]
Select light yellow as foreground color.
static constexpr character ANSI_ITALICS[5]
Select italics font style.
ALIB_DLL TextLogger(const NString &pName, const NString &typeName, bool pUsesStdStreams)
#define ALIB_DLL
Definition alib.inl:496
#define A_CHAR(STR)
#define ALIB_EXPORT
Definition alib.inl:488
ContainerOp
Denotes standard container operations.
Phase
Denotes a phase, e.g.,of a transaction.
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
lox::loggers::AnsiConsoleLogger AnsiConsoleLogger
Type alias in namespace alib.
strings::TString< nchar > NString
Type alias in namespace alib.
Definition string.inl:2390
lox::loggers::AnsiLogger AnsiLogger
Type alias in namespace alib.
strings::compatibility::std::StringWriter StringWriter
Type alias in namespace alib.
characters::character character
Type alias in namespace alib.
Parameters specific to colorful loggers. As of today, this simply has one attribute.