ALib C++ Library
Library Version: 2511 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 /// The output stream to use, provided with construction.
110 std::ostream& os;
111
112 //################################################################################################
113 // public fields
114 //################################################################################################
115 public:
116
117 /// As of today, this parameter struct has only one value of type
118 /// \alib{lox::textlogger::ColorfulLoggerParameters;LightColorUsage}. With this logger,
119 /// this flag is used as follows.
120 ///
121 /// Foreground and background colors chosen by this class might differ in their intensity.
122 /// This increases the overall readability by increasing the contrast.
123 /// If the background color of a console window is dark, then the background colors of
124 /// colored log output should be darker colors than the foreground colors - and vice versa.
125 ///
126 /// Depending on the setting of this field, \alox
127 /// \ref alib::lox::ESC "escape codes" for colors are translated to normal ANSI colors or
128 /// lighter ones:
129 /// - If this field evaluates to
130 /// \alib{lox::textlogger::ColorfulLoggerParameters;LightColorUsage;Never}, light colors
131 /// are never used.
132 /// - If this field is
133 /// \alib{lox::textlogger::ColorfulLoggerParameters;LightColorUsage;Foreground},
134 /// colors will be light colors and background colors dark ones. This is the default.
135 /// - With
136 /// \alib{lox::textlogger::ColorfulLoggerParameters;LightColorUsage;Background},
137 /// background colors will be light colors and foreground colors dark ones.
138 ///
139 /// The configuration variable \ref alxcvALOX_CONSOLE_LIGHT_COLORS allows externally
140 /// modifying this flag. It is read once within the constructor.
142
143 //################################################################################################
144 // Constructor/destructor
145 //################################################################################################
146 public:
147 /// Creates an AnsiLogger using the given output stream.
148 /// @param pOStream The output stream to write into. Defaults to \c std::cout.
149 /// @param name The name of the \e Logger, defaults to what is provided with
150 /// parameter \p{typeName}.
151 /// @param typeName The type of the \e Logger, defaults to "ANSI".
152 ALIB_DLL explicit
153 AnsiLogger( std::ostream& pOStream = std::cout,
154 const NString& name = nullptr,
155 const NString& typeName = "ANSI" )
156 : TextLogger( name, typeName )
157 , os(pOStream) {}
158
159 /// Destructs an AnsiLogger
160 virtual ~AnsiLogger() override {}
161
162 //################################################################################################
163 // Interface
164 //################################################################################################
165 public:
166
167 //################################################################################################
168 // Abstract method implementations
169 //################################################################################################
170 protected:
171 /// Implementation of the abstract method of parent class TextLogger.
172 /// Logs messages to the basic output stream provided in the constructor. Replaces
173 /// \alox ESC escape sequences with ANSI escape sequences.
174 ///
175 /// @param domain The <em>Log Domain</em>.
176 /// @param verbosity The verbosity. This has been checked to be active already on this
177 /// stage and is provided to be able to be logged out only.
178 /// @param msg The log message.
179 /// @param scope Information about the scope of the <em>Log Statement</em>..
180 /// @param lineNumber The line number of a multi-line message, starting with 0. For
181 /// single line messages this is -1.
182 /// @param isRecursion If \c true, a recursive logging operation was detected. A logger might
183 /// use this information, for example, to prevent recursive acquisitions
184 /// of resources.
186 virtual void logText( detail::Domain& domain,
187 Verbosity verbosity,
188 AString& msg,
189 detail::ScopeInfo& scope,
190 int lineNumber,
191 bool isRecursion ) override;
192
193 /// Empty implementation.
194 void notifyMultiLineOp ( lang::Phase ) override {}
195
196
197 /// Invokes parent method. Then reads and interprets configuration variable
198 /// \ref alxcvALOX_CONSOLE_LIGHT_COLORS. Finally it is checked if variable
199 /// \ref alxcvALOX_LOGGERNAME_FORMAT has priority of default values, and if yes, such
200 /// values are overwritten to include ANSI color codes. These color codes are retrieved from
201 /// resources named <c>"Var_D21A"</c>.
202 ///
203 /// @param lox The \b %Lox to acknowledge insertion or removal
204 /// @param op The operation. Either \b ContainerOp::Insert or \b ContainerOp::Remove.
206 void AcknowledgeLox( detail::LoxImpl* lox, lang::ContainerOp op ) override;
207
208}; // class AnsiLogger
209
210//==================================================================================================
211/// A #AnsiLogger that logs all messages to <em>std::cout</em>.
212/// The name of the \e Logger defaults to "ANSI_CONSOLE".
213///
214/// \note To avoid misunderstandings: This class cannot enable the output console (which receives
215/// \alox log data) to support ANSI Escape Codes. The opposite is right: this class should be
216/// used only if the console supports ANSI Escape Codes.
217///
218/// \see Class #AnsiLogger for more information on ANSI escape sequences and their use.
219//==================================================================================================
221{
222 public:
223 /// Creates an AnsiConsoleLogger.
224 /// @param name The name of the \e Logger. Optional and defaults to the logger type name.
225 /// "ANSI_CONSOLE".
226 explicit AnsiConsoleLogger( const NString& name= nullptr )
227 : AnsiLogger( std::cout, name, "ANSI_CONSOLE" ) {}
228
229 /// Destructs an AnsiConsoleLogger
230 virtual ~AnsiConsoleLogger() override {}
231}; // class AnsiConsoleLogger
232
233
234}} // namespace alib[::lox::loggers]
235
236/// Type alias in namespace \b alib.
238
239/// Type alias in namespace \b alib.
241
242} // namespace [alib]
virtual ~AnsiConsoleLogger() override
Destructs an AnsiConsoleLogger.
AnsiConsoleLogger(const NString &name=nullptr)
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 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.
ALIB_DLL AnsiLogger(std::ostream &pOStream=std::cout, const NString &name=nullptr, const NString &typeName="ANSI")
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.
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)
#define ALIB_DLL
Definition alib.inl:503
#define A_CHAR(STR)
#define ALIB_EXPORT
Definition alib.inl:497
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:2198
lox::loggers::AnsiLogger AnsiLogger
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.