ALib C++ Framework
by
Library Version: 2605 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/// Copyright 2013-2026 A-Worx GmbH, Germany.
6/// Published under #"mainpage_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 #"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 #"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/// runtime 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/// #"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 #"%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 #"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 // ANSI Escape Code definitions
55 //################################################################################################
56 public:
57 static constexpr character ANSI_RED [6] { A_CHAR("\033[31m") }; ///< Select red as foreground color
58 static constexpr character ANSI_GREEN [6] { A_CHAR("\033[32m") }; ///< Select green as foreground color
59 static constexpr character ANSI_YELLOW [6] { A_CHAR("\033[33m") }; ///< Select yellow as foreground color
60 static constexpr character ANSI_BLUE [6] { A_CHAR("\033[34m") }; ///< Select blue as foreground color
61 static constexpr character ANSI_MAGENTA [6] { A_CHAR("\033[35m") }; ///< Select magenta as foreground color
62 static constexpr character ANSI_CYAN [6] { A_CHAR("\033[36m") }; ///< Select cyan as foreground color
63 static constexpr character ANSI_BLACK [6] { A_CHAR("\033[30m") }; ///< Select black as foreground color
64 static constexpr character ANSI_WHITE [11] { A_CHAR("\033[38;5;15m") }; ///< Select white as foreground color
65 static constexpr character ANSI_GRAY [12] { A_CHAR("\033[38;5;240m")}; ///< Select gray as foreground color
66 static constexpr character ANSI_STD_COL [6] { A_CHAR("\033[39m") }; ///< Select standard foreground color
67
68 static constexpr character ANSI_BG_RED [6] { A_CHAR("\033[41m") }; ///< Select red as background color
69 static constexpr character ANSI_BG_GREEN [6] { A_CHAR("\033[42m") }; ///< Select green as background color
70 static constexpr character ANSI_BG_YELLOW [6] { A_CHAR("\033[43m") }; ///< Select yellow as background color
71 static constexpr character ANSI_BG_BLUE [6] { A_CHAR("\033[44m") }; ///< Select blue as background color
72 static constexpr character ANSI_BG_MAGENTA [6] { A_CHAR("\033[45m") }; ///< Select magenta as background color
73 static constexpr character ANSI_BG_CYAN [6] { A_CHAR("\033[46m") }; ///< Select cyan as background color
74 static constexpr character ANSI_BG_BLACK [6] { A_CHAR("\033[40m") }; ///< Select black as background color
75 static constexpr character ANSI_BG_WHITE [11] { A_CHAR("\033[48;5;15m") }; ///< Select white as background color
76 static constexpr character ANSI_BG_GRAY [12] { A_CHAR("\033[48;5;240m")}; ///< Select gray as background color
77 static constexpr character ANSI_BG_STD_COL [6] { A_CHAR("\033[49m") }; ///< Select standard background color
78
79 static constexpr character ANSI_LIGHT_RED [11] { A_CHAR("\033[38;5;09m") }; ///< Select light red as foreground color
80 static constexpr character ANSI_LIGHT_GREEN [11] { A_CHAR("\033[38;5;10m") }; ///< Select light green as foreground color
81 static constexpr character ANSI_LIGHT_YELLOW [11] { A_CHAR("\033[38;5;11m") }; ///< Select light yellow as foreground color
82 static constexpr character ANSI_LIGHT_BLUE [11] { A_CHAR("\033[38;5;12m") }; ///< Select light blue as foreground color
83 static constexpr character ANSI_LIGHT_MAGENTA [11] { A_CHAR("\033[38;5;13m") }; ///< Select light magenta as foreground color
84 static constexpr character ANSI_LIGHT_CYAN [11] { A_CHAR("\033[38;5;14m") }; ///< Select light cyan as foreground color
85 static constexpr character ANSI_LIGHT_GRAY [12] { A_CHAR("\033[38;5;250m")}; ///< Select light gray as foreground color
86 static constexpr character ANSI_LIGHT_STD_COL [6] { A_CHAR("\033[39m") }; ///< Select standard foreground color
87
88 static constexpr character ANSI_BG_LIGHT_RED [11] { A_CHAR("\033[48;5;09m") }; ///< Select light red as background color
89 static constexpr character ANSI_BG_LIGHT_GREEN [11] { A_CHAR("\033[48;5;10m") }; ///< Select light green as background color
90 static constexpr character ANSI_BG_LIGHT_YELLOW [11] { A_CHAR("\033[48;5;11m") }; ///< Select light yellow as background color
91 static constexpr character ANSI_BG_LIGHT_BLUE [11] { A_CHAR("\033[48;5;12m") }; ///< Select light blue as background color
92 static constexpr character ANSI_BG_LIGHT_MAGENTA[11] { A_CHAR("\033[48;5;13m") }; ///< Select light magenta as background color
93 static constexpr character ANSI_BG_LIGHT_CYAN [11] { A_CHAR("\033[48;5;14m") }; ///< Select light cyan as background color
94 static constexpr character ANSI_BG_LIGHT_GRAY [12] { A_CHAR("\033[48;5;250m")}; ///< Select light gray as background color
95 static constexpr character ANSI_BG_LIGHT_STD_COL[6] { A_CHAR("\033[49m") }; ///< Select standard background color
96
97 static constexpr character ANSI_BOLD [5] { A_CHAR("\033[1m") }; ///< Select bold font style
98 static constexpr character ANSI_ITALICS [5] { A_CHAR("\033[3m") }; ///< Select italics font style
99 static constexpr character ANSI_STD_STYLE [5] { A_CHAR("\033[0m") }; ///< Select standard font style
100 static constexpr character ANSI_RESET [5] { A_CHAR("\033[0m") }; ///< Reset colors and font style
101
102
103
104 //################################################################################################
105 // protected fields
106 //################################################################################################
107 protected:
108 /// The output stream to use, provided with construction.
109 std::ostream& os;
110
111 //################################################################################################
112 // public fields
113 //################################################################################################
114 public:
115
116 /// As of today, this parameter struct has only one value of type
117 /// #"ColorfulLoggerParameters;LightColorUsage". With this logger,
118 /// this flag is used as follows.
119 ///
120 /// Foreground and background colors chosen by this class might differ in their intensity.
121 /// This increases the overall readability by increasing the contrast.
122 /// If the background color of a console window is dark, then the background colors of
123 /// colored log output should be darker colors than the foreground colors - and vice versa.
124 ///
125 /// Depending on the setting of this field, \alox
126 /// #"alib::lox::ESC;escape codes" for colors are translated to normal ANSI colors or
127 /// lighter ones:
128 /// - If this field evaluates to
129 /// #"LightColorUsage::Never", light colors
130 /// are never used.
131 /// - If this field is
132 /// #"LightColorUsage::Foreground",
133 /// colors will be light colors and background colors dark ones. This is the default.
134 /// - With
135 /// #"LightColorUsage::Background",
136 /// background colors will be light colors and foreground colors dark ones.
137 ///
138 /// The configuration variable #"alxcvALOX_CONSOLE_LIGHT_COLORS" allows externally
139 /// modifying this flag. It is read once within the constructor.
141
142 //################################################################################################
143 // Constructor/destructor
144 //################################################################################################
145 public:
146 /// Creates an AnsiLogger using the given output stream.
147 /// @param pOStream The output stream to write into. Defaults to \c std::cout.
148 /// @param name The name of the \e Logger, defaults to what is provided with
149 /// parameter \p{typeName}.
150 /// @param typeName The type of the \e Logger, defaults to "ANSI".
151 ALIB_DLL explicit
152 AnsiLogger( std::ostream& pOStream = std::cout,
153 const NString& name = nullptr,
154 const NString& typeName = "ANSI" )
155 : TextLogger( name, typeName )
156 , os(pOStream) {}
157
158 /// Destructs an AnsiLogger
159 virtual ~AnsiLogger() override {}
160
161 //################################################################################################
162 // Interface
163 //################################################################################################
164 public:
165
166 //################################################################################################
167 // Abstract method implementations
168 //################################################################################################
169 protected:
170 /// Implementation of the abstract method of parent class TextLogger.
171 /// Logs messages to the basic output stream provided in the constructor. Replaces
172 /// \alox ESC escape sequences with ANSI escape sequences.
173 ///
174 /// @param domain The <em>Log Domain</em>.
175 /// @param verbosity The verbosity. This has been checked to be active already on this
176 /// stage and is provided to be able to be logged out only.
177 /// @param msg The log message.
178 /// @param scope Information about the scope of the <em>Log Statement</em>..
179 /// @param lineNumber The line number of a multi-line message, starting with 0. For
180 /// single line messages this is -1.
181 /// @param isRecursion If \c true, a recursive logging operation was detected. A logger might
182 /// use this information, for example, to prevent recursive acquisitions
183 /// of resources.
185 virtual void logText( detail::Domain& domain,
186 Verbosity verbosity,
187 AString& msg,
188 detail::ScopeInfo& scope,
189 int lineNumber,
190 bool isRecursion ) override;
191
192 /// Empty implementation.
193 void notifyMultiLineOp ( lang::Phase ) override {}
194
195
196 /// Invokes parent method. Then reads and interprets configuration variable
197 /// #"alxcvALOX_CONSOLE_LIGHT_COLORS". Finally it is checked if variable
198 /// #"alxcvALOX_LOGGERNAME_FORMAT" has priority of default values, and if yes, such
199 /// values are overwritten to include ANSI color codes. These color codes are retrieved from
200 /// resources named <c>"Var_D21A"</c>.
201 ///
202 /// @param lox The #"%Lox" to acknowledge insertion or removal
203 /// @param op The operation. Either #"%ContainerOp::Insert" or #"%ContainerOp::Remove".
206
207}; // class AnsiLogger
208
209//==================================================================================================
210/// A #"AnsiLogger" that logs all messages to <em>std::cout</em>.
211/// The name of the \e Logger defaults to "ANSI_CONSOLE".
212///
213/// \note To avoid misunderstandings: This class cannot enable the output console (which receives
214/// \alox log data) to support ANSI Escape Codes. The opposite is right: this class should be
215/// used only if the console supports ANSI Escape Codes.
216///
217/// \see Class #"AnsiLogger" for more information on ANSI escape sequences and their use.
218//==================================================================================================
220 public:
221 /// Creates an AnsiConsoleLogger.
222 /// @param name The name of the \e Logger. Optional and defaults to the logger type name.
223 /// "ANSI_CONSOLE".
224 explicit AnsiConsoleLogger( const NString& name= nullptr )
225 : AnsiLogger( std::cout, name, "ANSI_CONSOLE" ) {}
226
227 /// Destructs an AnsiConsoleLogger
228 virtual ~AnsiConsoleLogger() override {}
229}; // class AnsiConsoleLogger
230
231
232}} // namespace alib[::lox::loggers]
233
234/// Type alias in namespace #"%alib".
236
237/// Type alias in namespace #"%alib".
239
240} // namespace [alib]
#define ALIB_DLL
#define A_CHAR(STR)
#define ALIB_EXPORT
virtual ~AnsiConsoleLogger() override
Destructs an AnsiConsoleLogger.
AnsiConsoleLogger(const NString &name=nullptr)
AnsiLogger(std::ostream &pOStream=std::cout, const NString &name=nullptr, const NString &typeName="ANSI")
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 void logText(detail::Domain &domain, Verbosity verbosity, AString &msg, detail::ScopeInfo &scope, int lineNumber, bool isRecursion) override
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.
std::ostream & os
The output stream to use, provided with construction.
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.
void notifyMultiLineOp(lang::Phase) override
Empty implementation.
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
virtual ~AnsiLogger() override
Destructs an AnsiLogger.
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.
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.
static constexpr character ANSI_BG_BLUE[6]
Select blue as background color.
void AcknowledgeLox(detail::LoxImpl *lox, lang::ContainerOp op) override
Definition ansilogger.cpp:5
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.
TextLogger(const NString &pName, const NString &typeName)
ContainerOp
Denotes standard container operations.
Phase
Denotes a phase, e.g.,of a transaction.
Definition alox.cpp:14
strings::TString< nchar > NString
Type alias in namespace #"%alib".
Definition string.hpp:2174
lox::loggers::AnsiLogger AnsiLogger
Type alias in namespace #"%alib".
lox::loggers::AnsiConsoleLogger AnsiConsoleLogger
Type alias in namespace #"%alib".
strings::TAString< character, lang::HeapAllocator > AString
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.