ALib C++ Library
Library Version: 2412 R0
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 !DOXYGEN
11# include "alib/alox/aloxcamp.hpp"
15#endif // !DOXYGEN
16
17using namespace alib::lox::detail;
18
19namespace alib { namespace lox { namespace loggers {
20
22{
25 return;
26
27 auto& fmt= GetFormatMetaInfo();
28
29 // set msg suffix to "reset"
30 fmt.MsgSuffix.Reset(ANSI_RESET);
31
32 // evaluate environment variable "ALOX_CONSOLE_LIGHT_COLORS"
33 // If default, we assume dark background, hence use light color on foreground
36 (void) useLightColors.Define();
40
41 // If the meta-information came from the defaults (resources) we parse different ones
42 // dedicated to ANSI consoles.
43 if( varFormatMetaInfo.GetPriority() == Priority::DefaultValues )
44 {
45 // This moves the verbosity information to the end to colorize the whole line
46 auto ansiLoggerDefaultFormat= ALOX.GetResource("Var_D21A");
47 varFormatMetaInfo.Import(ansiLoggerDefaultFormat, Priority::DefaultValues );
48 }
49}
50
54
55// #################################################################################################
56// logText
57// #################################################################################################
58
59
61 AString& msg,
62 ScopeInfo& , int )
63{
64 // loop over message, print the parts between the escape sequences
65 integer column= 0;
66 Tokenizer msgParts( msg, '\x1B' );
67 Substring& actual= msgParts.Actual;
68 Substring& rest= msgParts.Rest;
69 for(;;)
70 {
71 msgParts.Next( lang::Whitespaces::Keep );
72
73 // check if end of actual msg part is an ANSI sequence. If yes, we extend the actual msg
74 if ( rest.CharAtStart() == '[' )
75 {
76 // read the 'm'
77 integer idx= rest.IndexOf( 'm' );
78 if ( idx < 0 ) // unknown ANSI Code
79 {
80 ALIB_WARNING( "ALOX", "Unknown ANSI ESC Code \"{}...\"", rest.Substring(0, 10 ) )
81 writer.Write( actual );
82 continue;
83 }
84
85 column+= actual.WStringLength();
86
87 actual= Substring( actual.Buffer(), actual.Length() + idx + 2 );
88 rest.ConsumeChars<NC>( idx + 1 );
89
90 writer.Write( actual );
91
92 continue;
93 }
94
95 if ( actual.IsNotEmpty() )
96 {
97 writer.Write( actual );
98 column+= actual.WStringLength();
99 }
100
101 // end of loop?
102 if ( !msgParts.HasNext() )
103 break;
104
105 // found an ESC sequence
106 character c= rest.ConsumeChar();
107
108 // Colors
109 if( c == 'C' || c == 'c' )
110 {
111 bool isForeGround= ( c== 'c' );
112
113 c= rest.ConsumeChar();
114 int colNo= c - '0';
115 ALIB_ASSERT_WARNING( colNo >=0 && colNo <=9, "ALOX", "AnsiLogger: Unknown ESC-c code" )
116
117 // add bg
118 colNo+= isForeGround ? 0 : 10;
119
120 // add light
123 colNo+= 20;
124
125 String ansiCol;
126 switch( colNo )
127 {
128 case 0: ansiCol= AnsiLogger::ANSI_RED ; break;
129 case 1: ansiCol= AnsiLogger::ANSI_GREEN ; break;
130 case 2: ansiCol= AnsiLogger::ANSI_YELLOW ; break;
131 case 3: ansiCol= AnsiLogger::ANSI_BLUE ; break;
132 case 4: ansiCol= AnsiLogger::ANSI_MAGENTA ; break;
133 case 5: ansiCol= AnsiLogger::ANSI_CYAN ; break;
134 case 6: ansiCol= AnsiLogger::ANSI_BLACK ; break;
135 case 7: ansiCol= AnsiLogger::ANSI_WHITE ; break;
136 case 8: ansiCol= AnsiLogger::ANSI_GRAY ; break;
137 case 9: ansiCol= AnsiLogger::ANSI_STD_COL ; break;
138
139 case 10: ansiCol= AnsiLogger::ANSI_BG_RED ; break;
140 case 11: ansiCol= AnsiLogger::ANSI_BG_GREEN ; break;
141 case 12: ansiCol= AnsiLogger::ANSI_BG_YELLOW ; break;
142 case 13: ansiCol= AnsiLogger::ANSI_BG_BLUE ; break;
143 case 14: ansiCol= AnsiLogger::ANSI_BG_MAGENTA ; break;
144 case 15: ansiCol= AnsiLogger::ANSI_BG_CYAN ; break;
145 case 16: ansiCol= AnsiLogger::ANSI_BG_BLACK ; break;
146 case 17: ansiCol= AnsiLogger::ANSI_BG_WHITE ; break;
147 case 18: ansiCol= AnsiLogger::ANSI_BG_GRAY ; break;
148 case 19: ansiCol= AnsiLogger::ANSI_BG_STD_COL ; break;
149
150 case 20: ansiCol= AnsiLogger::ANSI_LIGHT_RED ; break;
151 case 21: ansiCol= AnsiLogger::ANSI_LIGHT_GREEN ; break;
152 case 22: ansiCol= AnsiLogger::ANSI_LIGHT_YELLOW ; break;
153 case 23: ansiCol= AnsiLogger::ANSI_LIGHT_BLUE ; break;
154 case 24: ansiCol= AnsiLogger::ANSI_LIGHT_MAGENTA ; break;
155 case 25: ansiCol= AnsiLogger::ANSI_LIGHT_CYAN ; break;
156 case 26: ansiCol= AnsiLogger::ANSI_BG_BLACK ; break;
157 case 27: ansiCol= AnsiLogger::ANSI_BG_WHITE ; break;
158 case 28: ansiCol= AnsiLogger::ANSI_LIGHT_GRAY ; break;
159 case 29: ansiCol= AnsiLogger::ANSI_STD_COL ; break;
160
161 case 30: ansiCol= AnsiLogger::ANSI_BG_LIGHT_RED ; break;
162 case 31: ansiCol= AnsiLogger::ANSI_BG_LIGHT_GREEN ; break;
163 case 32: ansiCol= AnsiLogger::ANSI_BG_LIGHT_YELLOW ; break;
164 case 33: ansiCol= AnsiLogger::ANSI_BG_LIGHT_BLUE ; break;
165 case 34: ansiCol= AnsiLogger::ANSI_BG_LIGHT_MAGENTA ; break;
166 case 35: ansiCol= AnsiLogger::ANSI_BG_LIGHT_CYAN ; break;
167 case 36: ansiCol= AnsiLogger::ANSI_BG_BLACK ; break;
168 case 37: ansiCol= AnsiLogger::ANSI_BG_WHITE ; break;
169 case 38: ansiCol= AnsiLogger::ANSI_BG_LIGHT_GRAY ; break;
170 case 39: ansiCol= AnsiLogger::ANSI_BG_STD_COL ; break;
171 default: ALIB_ERROR( "ALOX", "Error in ANSI Code" )
172 ansiCol= A_CHAR("");
173 break;
174 }
175
176 writer.Write( ansiCol );
177 }
178
179 // Styles
180 else if ( c == 's' )
181 {
182 c= rest.ConsumeChar();
183 writer.Write(const_cast<const character*>( c== 'B' ? ANSI_BOLD
184 : c== 'I' ? ANSI_ITALICS
185 : ANSI_RESET ) );
186 }
187
188 // auto tab / end of meta
189 else if ( c == 't' || c == 'A' )
190 {
191 c= rest.ConsumeChar();
192 int extraSpace= c >= '0' && c <= '9' ? ( c - '0' )
193 : ( c - 'A' ) + 10;
194
195 // tab stop (write spaces using a growing buffer)
197 AutoSizes::Types::Tabstop, column, extraSpace );
198 integer qtySpaces= tabStop - column;
199 writer.WriteChars(' ', qtySpaces );
200 column+= qtySpaces;
201 }
202
203 // Link (we just colorize links here)
204 else if ( c == 'l' )
205 writer.Write( rest.ConsumeChar() == 'S'
207 ? static_cast<const String&>( ANSI_LIGHT_BLUE )
208 : static_cast<const String&>( ANSI_BLUE ) )
209 : static_cast<const String&>( ANSI_STD_COL ) );
210
211 else
212 {
213 rest.ConsumeChar();
214 ALIB_WARNING( "ALOX", "Unknown ESC code {!Q'}.", c )
215 }
216
217 } // write loop
218
219 *writer.GetStream() << std::endl;
220}
221
222
223// #################################################################################################
224// AnsiConsoleLogger
225// #################################################################################################
227: AnsiLogger( name, "ANSI_CONSOLE" )
228{
229}
230
234
235}}} // namespace [alib::lox::loggers]
236
ALIB_API bool Define(Priority requestedPriority=Priority::Standard)
Definition variable.cpp:272
Priority GetPriority() const
ALIB_API void Import(const String &src, Priority priority, const StringEscaper *escaper=nullptr)
Definition variable.cpp:349
threads::SharedLock & GetConfigLock()
Definition camp.hpp:219
const String & GetResource(const NString &name)
virtual ALIB_API ~AnsiConsoleLogger() override
Destructs an AnsiConsoleLogger.
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
Destructs an AnsiLogger.
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.
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.
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.
FormatMetaInfo & GetFormatMetaInfo()
virtual ALIB_API void AcknowledgeLox(detail::LoxImpl *lox, lang::ContainerOp op) override
integer IndexOf(TChar needle, integer startIdx=0) const
Definition string.hpp:896
constexpr bool IsNotEmpty() const
Definition string.hpp:389
constexpr integer Length() const
Definition string.hpp:326
TChar CharAtStart() const
Definition string.hpp:466
TString< TChar > Substring(integer regionStart, integer regionLength=MAX_LEN) const
Definition string.hpp:406
constexpr const TChar * Buffer() const
Definition string.hpp:319
integer WStringLength() const
integer ConsumeChars(integer regionLength, TSubstring *target=nullptr)
ALIB_API void Write(const NString &src)
ALIB_API void WriteChars(const nchar fillChar, integer count)
@ Tabstop
denotes a tab stop entry.
ALIB_API TSubstring< TChar > & Next(lang::Whitespaces trimming=lang::Whitespaces::Trim, TChar newDelim='\0')
Definition tokenizer.cpp:16
TSubstring< TChar > Actual
Definition tokenizer.hpp:73
#define ALIB_WARNING(...)
Definition alib.hpp:1268
#define A_CHAR(STR)
#define ALIB_ERROR(...)
Definition alib.hpp:1267
#define ALIB_ASSERT_WARNING(cond,...)
Definition alib.hpp:1272
#define ALIB_LOCK_WITH(lock)
Definition owner.hpp:456
@ Keep
Keep whitespaces in string.
ContainerOp
Denotes standard container operations.
@ Insert
Denotes insertions.
@ CONSOLE_LIGHT_COLORS
Denotes configuration variable ALOX/CONSOLE_LIGHT_COLORS used by colorful specializations of class Te...
Definition alib.cpp:69
lox::ALoxCamp ALOX
The singleton instance of ALib Camp class ALoxCamp.
Definition aloxcamp.cpp:33
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:273
Parameters specific to colorful loggers. As of today, this simply has one attribute.