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