ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
ansilogger.cpp
1// #################################################################################################
2// alib::lox::loggers - ALox Logging Library
3//
4// Copyright 2013-2024 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
8
9#if !defined(ALIB_DOX)
10# if !defined(HPP_ALOX_ANSI_LOGGER)
12# endif
13
14# if !defined(HPP_ALIB_STRINGS_UTIL_SPACES)
16# endif
17
18# if !defined (HPP_ALIB_ALOXMODULE)
20# endif
21# if !defined (HPP_ALIB_STRINGS_UTIL_TOKENIZER)
23# endif
24# if !defined(HPP_ALIB_ENUMS_SERIALIZATION)
26# endif
27
28# if !defined(HPP_ALIB_CAMP_MESSAGE_REPORT)
30# endif
31#endif // !defined(ALIB_DOX)
32
33using namespace alib::lox::detail;
34
35namespace alib { namespace lox { namespace loggers {
36
37// #################################################################################################
38// Constructor/Destructor
39// #################################################################################################
40AnsiLogger::AnsiLogger( const NString& name, const NString& typeName )
41: TextLogger( name, typeName, true )
42{
43 construct();
44}
45
46AnsiLogger::AnsiLogger( std::ostream* pOStream,
47 const NString& name, const NString& typeName )
48: TextLogger( name, typeName, false )
49{
50 writer.SetStream( pOStream );
51
52 construct();
53}
54
56{
57 // set msg suffix to "reset"
59
60 // evaluate environment variable "ALOX_CONSOLE_LIGHT_COLORS"
63 if ( ALOX.GetConfig().Load( variable ) != Priorities::NONE && variable.Size() > 0)
64 {
65 Substring p= variable.GetString();
66 if(p.Trim().IsNotEmpty())
67 {
68 if( !enums::Parse<LightColorUsage>( p, UseLightColors ) )
69 {
70 ALIB_WARNING( "ALOX", "Unknown value specified in variable: {} = {!Q'}.",
71 variable.Fullname(), variable.GetString() )
72 }
73 }
74 }
75
77 {
78 // default: dark background, hence use light color on foreground
80 }
81
82 // move verbosity information to the end to colorize the whole line
84 MetaInfo->Format._("%V");
89}
90
94
95// #################################################################################################
96// logText
97// #################################################################################################
98
99
101 AString& msg,
102 ScopeInfo& , int )
103{
104 // loop over message, print the parts between the escape sequences
105 integer column= 0;
106 Tokenizer msgParts( msg, '\x1B' );
107 Substring& actual= msgParts.Actual;
108 Substring& rest= msgParts.Rest;
109 for(;;)
110 {
111 msgParts.Next( lang::Whitespaces::Keep );
112
113 // check if end of actual msg part is an ANSI sequence. If yes, we extend the actual msg
114 if ( rest.CharAtStart() == '[' )
115 {
116 // read the 'm'
117 integer idx= rest.IndexOf( 'm' );
118 if ( idx < 0 ) // unknown ANSI Code
119 {
120 ALIB_WARNING( "ALOX", "Unknown ANSI ESC Code \"{}...\"", rest.Substring(0, 10 ) )
121 writer.Write( actual );
122 continue;
123 }
124
125 column+= actual.WStringLength();
126
127 actual= Substring( actual.Buffer(), actual.Length() + idx + 2 );
128 rest.ConsumeChars<false>( idx + 1 );
129
130 writer.Write( actual );
131
132 continue;
133 }
134
135 if ( actual.IsNotEmpty() )
136 {
137 writer.Write( actual );
138 column+= actual.WStringLength();
139 }
140
141 // end of loop?
142 if ( !msgParts.HasNext() )
143 break;
144
145 // found an ESC sequence
146 character c= rest.ConsumeChar();
147
148 // Colors
149 if( c == 'C' || c == 'c' )
150 {
151 bool isForeGround= ( c== 'c' );
152
153 c= rest.ConsumeChar();
154 int colNo= c - '0';
155 ALIB_ASSERT_WARNING( colNo >=0 && colNo <=9, "ALOX", "AnsiLogger: Unknown ESC-c code" )
156
157 // add bg
158 colNo+= isForeGround ? 0 : 10;
159
160 // add light
162 colNo+= 20;
163
164
165 String ansiCol;
166 switch( colNo )
167 {
168 case 0: ansiCol= AnsiLogger::ANSI_RED ; break;
169 case 1: ansiCol= AnsiLogger::ANSI_GREEN ; break;
170 case 2: ansiCol= AnsiLogger::ANSI_YELLOW ; break;
171 case 3: ansiCol= AnsiLogger::ANSI_BLUE ; break;
172 case 4: ansiCol= AnsiLogger::ANSI_MAGENTA ; break;
173 case 5: ansiCol= AnsiLogger::ANSI_CYAN ; break;
174 case 6: ansiCol= AnsiLogger::ANSI_BLACK ; break;
175 case 7: ansiCol= AnsiLogger::ANSI_WHITE ; break;
176 case 8: ansiCol= AnsiLogger::ANSI_GRAY ; break;
177 case 9: ansiCol= AnsiLogger::ANSI_STD_COL ; break;
178
179 case 10: ansiCol= AnsiLogger::ANSI_BG_RED ; break;
180 case 11: ansiCol= AnsiLogger::ANSI_BG_GREEN ; break;
181 case 12: ansiCol= AnsiLogger::ANSI_BG_YELLOW ; break;
182 case 13: ansiCol= AnsiLogger::ANSI_BG_BLUE ; break;
183 case 14: ansiCol= AnsiLogger::ANSI_BG_MAGENTA ; break;
184 case 15: ansiCol= AnsiLogger::ANSI_BG_CYAN ; break;
185 case 16: ansiCol= AnsiLogger::ANSI_BG_BLACK ; break;
186 case 17: ansiCol= AnsiLogger::ANSI_BG_WHITE ; break;
187 case 18: ansiCol= AnsiLogger::ANSI_BG_GRAY ; break;
188 case 19: ansiCol= AnsiLogger::ANSI_BG_STD_COL ; break;
189
190 case 20: ansiCol= AnsiLogger::ANSI_LIGHT_RED ; break;
191 case 21: ansiCol= AnsiLogger::ANSI_LIGHT_GREEN ; break;
192 case 22: ansiCol= AnsiLogger::ANSI_LIGHT_YELLOW ; break;
193 case 23: ansiCol= AnsiLogger::ANSI_LIGHT_BLUE ; break;
194 case 24: ansiCol= AnsiLogger::ANSI_LIGHT_MAGENTA ; break;
195 case 25: ansiCol= AnsiLogger::ANSI_LIGHT_CYAN ; break;
196 case 26: ansiCol= AnsiLogger::ANSI_BG_BLACK ; break;
197 case 27: ansiCol= AnsiLogger::ANSI_BG_WHITE ; break;
198 case 28: ansiCol= AnsiLogger::ANSI_LIGHT_GRAY ; break;
199 case 29: ansiCol= AnsiLogger::ANSI_STD_COL ; break;
200
201 case 30: ansiCol= AnsiLogger::ANSI_BG_LIGHT_RED ; break;
202 case 31: ansiCol= AnsiLogger::ANSI_BG_LIGHT_GREEN ; break;
203 case 32: ansiCol= AnsiLogger::ANSI_BG_LIGHT_YELLOW ; break;
204 case 33: ansiCol= AnsiLogger::ANSI_BG_LIGHT_BLUE ; break;
205 case 34: ansiCol= AnsiLogger::ANSI_BG_LIGHT_MAGENTA ; break;
206 case 35: ansiCol= AnsiLogger::ANSI_BG_LIGHT_CYAN ; break;
207 case 36: ansiCol= AnsiLogger::ANSI_BG_BLACK ; break;
208 case 37: ansiCol= AnsiLogger::ANSI_BG_WHITE ; break;
209 case 38: ansiCol= AnsiLogger::ANSI_BG_LIGHT_GRAY ; break;
210 case 39: ansiCol= AnsiLogger::ANSI_BG_STD_COL ; break;
211 default: ALIB_ERROR( "ALOX", "Error in ANSI Code" )
212 ansiCol= A_CHAR("");
213 break;
214 }
215
216 writer.Write( ansiCol );
217 }
218
219 // Styles
220 else if ( c == 's' )
221 {
222 c= rest.ConsumeChar();
223 writer.Write(const_cast<const character*>( c== 'B' ? ANSI_BOLD
224 : c== 'I' ? ANSI_ITALICS
225 : ANSI_RESET ) );
226 }
227
228 // auto tab / end of meta
229 else if ( c == 't' || c == 'A' )
230 {
231 c= rest.ConsumeChar();
232 int extraSpace= c >= '0' && c <= '9' ? ( c - '0' )
233 : ( c - 'A' ) + 10;
234
235 // tab stop (write spaces using a growing buffer)
236 integer tabStop= AutoSizes.Next( AutoSizes::Types::Tabstop, column, extraSpace );
237 integer qtySpaces= tabStop - column;
238 if( qtySpaces > 0 )
239 {
240 Spaces::Write( *writer.GetStream(), qtySpaces );
241 column+= qtySpaces;
242 }
243
244 }
245
246 // Link (we just colorize links here)
247 else if ( c == 'l' )
248 writer.Write( rest.ConsumeChar() == 'S'
250 ? static_cast<const String&>( ANSI_LIGHT_BLUE )
251 : static_cast<const String&>( ANSI_BLUE ) )
252 : static_cast<const String&>( ANSI_STD_COL ) );
253
254 else
255 {
256 rest.ConsumeChar();
257 ALIB_WARNING( "ALOX", "Unknown ESC code {!Q'}.", c )
258 }
259
260 } // write loop
261
262 *writer.GetStream() << std::endl;
263}
264
265
266// #################################################################################################
267// AnsiConsoleLogger
268// #################################################################################################
270: AnsiLogger( name, "ANSI_CONSOLE" )
271{
272}
273
277
278}}} // namespace [alib::lox::loggers]
ALIB_API Priorities Load(Variable &variable)
integer Size() const
Definition variable.hpp:708
ALIB_API const String & Fullname()
Definition variable.cpp:266
const String & GetString(int idx=0)
Definition variable.hpp:780
config::Configuration & GetConfig()
Definition camp.hpp:231
static constexpr character RED[4]
Select red color for foreground.
static constexpr character GRAY[4]
Select gray color for foreground.
static constexpr character BLUE[4]
Select blue color for foreground.
virtual ALIB_API ~AnsiConsoleLogger() override
ALIB_API AnsiConsoleLogger(const NString &name=nullptr)
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 ALIB_API ~AnsiLogger() override
ALIB_API AnsiLogger(const NString &name=nullptr, const NString &typeName="ANSI")
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_YELLOW[11]
Select light yellow as background color.
static constexpr character ANSI_CYAN[6]
Select cyan as 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.
TAString & _(const TString< TChar > &src, integer regionStart, integer regionLength=MAX_LEN)
Definition astring.hpp:1056
ALIB_API integer SearchAndReplace(TChar needle, TChar replacement, integer startIdx=0)
Definition astring.cpp:330
integer IndexOf(TChar needle, integer startIdx=0) const
Definition string.hpp:889
constexpr bool IsNotEmpty() const
Definition string.hpp:420
constexpr integer Length() const
Definition string.hpp:357
TChar CharAtStart() const
Definition string.hpp:459
TString< TChar > Substring(integer regionStart, integer regionLength=MAX_LEN) const
Definition string.hpp:314
constexpr const TChar * Buffer() const
Definition string.hpp:350
integer WStringLength() const
TSubstring & Trim(const TCString< TChar > &whiteSpaces=TT_StringConstants< TChar >::DefaultWhitespaces())
integer ConsumeChars(integer regionLength, TSubstring *target=nullptr)
@ Tabstop
denotes a tab stop entry.
integer Next(Types type, integer requestedSize, integer growthPadding)
static ALIB_API void Write(std::basic_ostream< char > &os, integer qty)
Definition spaces.cpp:49
ALIB_API TSubstring< TChar > & Next(lang::Whitespaces trimming=lang::Whitespaces::Trim, TChar newDelim='\0')
Definition tokenizer.cpp:18
TSubstring< TChar > Actual
Definition tokenizer.hpp:84
#define ALIB_WARNING(...)
Definition alib.hpp:981
#define A_CHAR(STR)
#define ALIB_ERROR(...)
Definition alib.hpp:980
#define ALIB_ASSERT_RESULT_NOT_EQUALS( func, value)
Definition alib.hpp:1000
#define ALIB_ASSERT_WARNING(cond,...)
Definition alib.hpp:985
@ Keep
Keep whitespaces in string.
Definition alib.cpp:57
lox::ALox ALOX
strings::TSubstring< character > Substring
Type alias in namespace alib.
characters::character character
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286