ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
ansilogger.cpp
1// #################################################################################################
2// alib::lox::loggers - ALox Logging Library
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14// ====================================== Global Fragment ======================================
16#include <ostream>
17
18// =========================================== Module ==========================================
19#if ALIB_C20_MODULES
20 module ALib.ALox.Impl;
21 import ALib.Lang;
22 import ALib.Strings;
24 import ALib.Camp;
25#else
26# include "ALib.Lang.H"
27# include "ALib.Strings.H"
29# include "ALib.Camp.H"
30# include "ALib.ALox.H"
31# include "ALib.ALox.Impl.H"
32#endif
33// ====================================== Implementation =======================================
34using namespace alib::lox::detail;
35
36namespace alib { namespace lox { namespace loggers {
37
39{
42 return;
43
44 auto& fmt= GetFormatMetaInfo();
45
46 // set msg suffix to "reset"
47 fmt.MsgSuffix.Reset(ANSI_RESET);
48
49 // evaluate environment variable "ALOX_CONSOLE_LIGHT_COLORS"
50 // If default, we assume dark background, hence use light color on foreground
52 (void) useLightColors.Define();
56
57 // If the meta-information came from the defaults (resources) we parse different ones
58 // dedicated to ANSI consoles.
59 if( varFormatMetaInfo.GetPriority() == Priority::DefaultValues )
60 {
61 // This moves the verbosity information to the end to colorize the whole line
62 auto ansiLoggerDefaultFormat= ALOX.GetResource("Var_D21A");
63 varFormatMetaInfo.Import(ansiLoggerDefaultFormat, Priority::DefaultValues );
64 }
65}
66
70
71// #################################################################################################
72// logText
73// #################################################################################################
74
75
77 AString& msg,
78 ScopeInfo& , int )
79{
80 // loop over message, print the parts between the escape sequences
81 integer column= 0;
82 Tokenizer msgParts( msg, '\x1B' );
83 Substring& actual= msgParts.Actual;
84 Substring& rest= msgParts.Rest;
85 for(;;)
86 {
87 msgParts.Next( lang::Whitespaces::Keep );
88
89 // check if end of actual msg part is an ANSI sequence. If yes, we extend the actual msg
90 if ( rest.CharAtStart() == '[' )
91 {
92 // read the 'm'
93 integer idx= rest.IndexOf( 'm' );
94 if ( idx < 0 ) // unknown ANSI Code
95 {
96 ALIB_WARNING( "ALOX", "Unknown ANSI ESC Code \"{}...\"", rest.Substring(0, 10 ) )
97 writer.Write( actual );
98 continue;
99 }
100
101 column+= actual.WStringLength();
102
103 actual= Substring( actual.Buffer(), actual.Length() + idx + 2 );
104 rest.ConsumeChars<NC>( idx + 1 );
105
106 writer.Write( actual );
107
108 continue;
109 }
110
111 if ( actual.IsNotEmpty() )
112 {
113 writer.Write( actual );
114 column+= actual.WStringLength();
115 }
116
117 // end of loop?
118 if ( !msgParts.HasNext() )
119 break;
120
121 // found an ESC sequence
122 character c= rest.ConsumeChar();
123
124 // Colors
125 if( c == 'C' || c == 'c' )
126 {
127 bool isForeGround= ( c== 'c' );
128
129 c= rest.ConsumeChar();
130 int colNo= c - '0';
131 ALIB_ASSERT_WARNING( colNo >=0 && colNo <=9, "ALOX", "AnsiLogger: Unknown ESC-c code" )
132
133 // add bg
134 colNo+= isForeGround ? 0 : 10;
135
136 // add light
139 colNo+= 20;
140
141 String ansiCol;
142 switch( colNo )
143 {
144 case 0: ansiCol= AnsiLogger::ANSI_RED ; break;
145 case 1: ansiCol= AnsiLogger::ANSI_GREEN ; break;
146 case 2: ansiCol= AnsiLogger::ANSI_YELLOW ; break;
147 case 3: ansiCol= AnsiLogger::ANSI_BLUE ; break;
148 case 4: ansiCol= AnsiLogger::ANSI_MAGENTA ; break;
149 case 5: ansiCol= AnsiLogger::ANSI_CYAN ; break;
150 case 6: ansiCol= AnsiLogger::ANSI_BLACK ; break;
151 case 7: ansiCol= AnsiLogger::ANSI_WHITE ; break;
152 case 8: ansiCol= AnsiLogger::ANSI_GRAY ; break;
153 case 9: ansiCol= AnsiLogger::ANSI_STD_COL ; break;
154
155 case 10: ansiCol= AnsiLogger::ANSI_BG_RED ; break;
156 case 11: ansiCol= AnsiLogger::ANSI_BG_GREEN ; break;
157 case 12: ansiCol= AnsiLogger::ANSI_BG_YELLOW ; break;
158 case 13: ansiCol= AnsiLogger::ANSI_BG_BLUE ; break;
159 case 14: ansiCol= AnsiLogger::ANSI_BG_MAGENTA ; break;
160 case 15: ansiCol= AnsiLogger::ANSI_BG_CYAN ; break;
161 case 16: ansiCol= AnsiLogger::ANSI_BG_BLACK ; break;
162 case 17: ansiCol= AnsiLogger::ANSI_BG_WHITE ; break;
163 case 18: ansiCol= AnsiLogger::ANSI_BG_GRAY ; break;
164 case 19: ansiCol= AnsiLogger::ANSI_BG_STD_COL ; break;
165
166 case 20: ansiCol= AnsiLogger::ANSI_LIGHT_RED ; break;
167 case 21: ansiCol= AnsiLogger::ANSI_LIGHT_GREEN ; break;
168 case 22: ansiCol= AnsiLogger::ANSI_LIGHT_YELLOW ; break;
169 case 23: ansiCol= AnsiLogger::ANSI_LIGHT_BLUE ; break;
170 case 24: ansiCol= AnsiLogger::ANSI_LIGHT_MAGENTA ; break;
171 case 25: ansiCol= AnsiLogger::ANSI_LIGHT_CYAN ; break;
172 case 26: ansiCol= AnsiLogger::ANSI_BG_BLACK ; break;
173 case 27: ansiCol= AnsiLogger::ANSI_BG_WHITE ; break;
174 case 28: ansiCol= AnsiLogger::ANSI_LIGHT_GRAY ; break;
175 case 29: ansiCol= AnsiLogger::ANSI_STD_COL ; break;
176
177 case 30: ansiCol= AnsiLogger::ANSI_BG_LIGHT_RED ; break;
178 case 31: ansiCol= AnsiLogger::ANSI_BG_LIGHT_GREEN ; break;
179 case 32: ansiCol= AnsiLogger::ANSI_BG_LIGHT_YELLOW ; break;
180 case 33: ansiCol= AnsiLogger::ANSI_BG_LIGHT_BLUE ; break;
181 case 34: ansiCol= AnsiLogger::ANSI_BG_LIGHT_MAGENTA ; break;
182 case 35: ansiCol= AnsiLogger::ANSI_BG_LIGHT_CYAN ; break;
183 case 36: ansiCol= AnsiLogger::ANSI_BG_BLACK ; break;
184 case 37: ansiCol= AnsiLogger::ANSI_BG_WHITE ; break;
185 case 38: ansiCol= AnsiLogger::ANSI_BG_LIGHT_GRAY ; break;
186 case 39: ansiCol= AnsiLogger::ANSI_BG_STD_COL ; break;
187 default: ALIB_ERROR( "ALOX", "Error in ANSI Code" )
188 ansiCol= A_CHAR("");
189 break;
190 }
191
192 writer.Write( ansiCol );
193 }
194
195 // Styles
196 else if ( c == 's' )
197 {
198 c= rest.ConsumeChar();
199 writer.Write(const_cast<const character*>( c== 'B' ? ANSI_BOLD
200 : c== 'I' ? ANSI_ITALICS
201 : ANSI_RESET ) );
202 }
203
204 // auto tab / end of meta
205 else if ( c == 't' || c == 'A' )
206 {
207 c= rest.ConsumeChar();
208 int extraSpace= c >= '0' && c <= '9' ? ( c - '0' )
209 : ( c - 'A' ) + 10;
210
211 // tab stop (write spaces using a growing buffer)
213 AutoSizes::Types::Tabstop, column, extraSpace );
214 integer qtySpaces= tabStop - column;
215 writer.WriteChars(' ', qtySpaces );
216 column+= qtySpaces;
217 }
218
219 // Link (we just colorize links here)
220 else if ( c == 'l' )
221 writer.Write( rest.ConsumeChar() == 'S'
223 ? static_cast<const String&>( ANSI_LIGHT_BLUE )
224 : static_cast<const String&>( ANSI_BLUE ) )
225 : static_cast<const String&>( ANSI_STD_COL ) );
226
227 else
228 {
229 rest.ConsumeChar();
230 ALIB_WARNING( "ALOX", "Unknown ESC code {!Q'}.", c )
231 }
232
233 } // write loop
234
235 *writer.GetStream() << std::endl;
236}
237
238
239// #################################################################################################
240// AnsiConsoleLogger
241// #################################################################################################
243: AnsiLogger( name, "ANSI_CONSOLE" )
244{
245}
246
250
251}}} // namespace [alib::lox::loggers]
252
virtual ALIB_DLL ~AnsiConsoleLogger() override
Destructs an AnsiConsoleLogger.
ALIB_DLL AnsiConsoleLogger(const NString &name=nullptr)
virtual ALIB_DLL void logText(detail::Domain &domain, Verbosity verbosity, AString &msg, detail::ScopeInfo &scope, int lineNumber) override
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 ~AnsiLogger() override
Destructs an AnsiLogger.
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.
static constexpr character ANSI_BG_LIGHT_YELLOW[11]
Select light yellow as background color.
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
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.
ALIB_DLL AnsiLogger(const NString &name=nullptr, const NString &typeName="ANSI")
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_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_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.
virtual 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.
virtual ALIB_DLL void AcknowledgeLox(detail::LoxImpl *lox, lang::ContainerOp op) override
FormatMetaInfo & GetFormatMetaInfo()
constexpr integer Length() const
Definition string.inl:318
TChar CharAtStart() const
Definition string.inl:440
integer IndexOf(TChar needle, integer startIdx=0) const
Definition string.inl:844
constexpr bool IsNotEmpty() const
Definition string.inl:371
constexpr const TChar * Buffer() const
Definition string.inl:313
integer WStringLength() const
TString< TChar > Substring(integer regionStart, integer regionLength=MAX_LEN) const
Definition string.inl:386
integer ConsumeChars(integer regionLength, TSubstring *target=nullptr)
@ Tabstop
denotes a tab stop entry.
Definition autosizes.inl:57
ALIB_DLL TSubstring< TChar > & Next(lang::Whitespaces trimming=lang::Whitespaces::Trim, TChar newDelim='\0')
Definition tokenizer.cpp:26
TSubstring< TChar > Actual
Definition tokenizer.inl:66
ALIB_DLL bool Define(Priority requestedPriority=Priority::Standard)
Definition variable.cpp:280
#define A_CHAR(STR)
#define ALIB_WARNING(domain,...)
Definition alib.inl:1046
#define ALIB_ASSERT_WARNING(cond, domain,...)
Definition alib.inl:1050
#define ALIB_ERROR(domain,...)
Definition alib.inl:1045
ContainerOp
Denotes standard container operations.
@ Insert
Denotes insertions.
@ Keep
Keep whitespaces in string.
@ CONSOLE_LIGHT_COLORS
Denotes configuration variable ALOX/CONSOLE_LIGHT_COLORS used by colorful specializations of class Te...
Definition aloxcamp.inl:67
variables::Variable CampVariable(camp::Camp &camp)
Definition camp.inl:349
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
strings::util::TTokenizer< character > Tokenizer
Type alias in namespace alib.
variables::Variable Variable
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
strings::TString< nchar > NString
Type alias in namespace alib.
Definition string.inl:2390
lox::ALoxCamp ALOX
The singleton instance of ALib Camp class ALoxCamp.
Definition aloxcamp.cpp:53
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2381
characters::character character
Type alias in namespace alib.
strings::TSubstring< character > Substring
Type alias in namespace alib.
Parameters specific to colorful loggers. As of today, this simply has one attribute.