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