ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
ansilogger.cpp
1//##################################################################################################
2// ALib C++ 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;
23 import ALib.Strings.Tokenizer;
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
38void AnsiLogger::AcknowledgeLox( detail::LoxImpl* lox, lang::ContainerOp op ) {
41 return;
42
43 auto& fmt= GetFormatMetaInfo();
44
45 // set msg suffix to "reset"
46 fmt.MsgSuffix.Reset(ANSI_RESET);
47
48 // evaluate environment variable "ALOX_CONSOLE_LIGHT_COLORS"
49 // If default, we assume dark background, hence use light color on foreground
51 (void) useLightColors.Define();
55
56 // If the meta-information came from the defaults (resources) we parse different ones
57 // dedicated to ANSI consoles.
58 if( varFormatMetaInfo.GetPriority() == Priority::DefaultValues ) {
59 // This moves the verbosity information to the end to colorize the whole line
60 auto ansiLoggerDefaultFormat= ALOX.GetResource("Var_D21A");
61 varFormatMetaInfo.Import(ansiLoggerDefaultFormat, Priority::DefaultValues );
62} }
63
64//##################################################################################################
65// logText
66//##################################################################################################
68 Verbosity ,
69 AString& msg,
70 ScopeInfo& ,
71 int ,
72 bool isRecursion) {
73 // Acquire ostream (empty in single-threaded applications)
74 LocalAllocator4K tempAllocator;
75 OStreamWriter<nchar, MonoAllocator, true> writer(os, tempAllocator);
76 if (isRecursion)
77 writer.DisableSync= true;
78
79 // loop over message, print the parts between the escape sequences
80 integer column= 0;
81 Tokenizer msgParts( msg, '\x1B' );
82 Substring& actual= msgParts.Actual;
83 Substring& rest= msgParts.Rest;
84 for(;;) {
85 msgParts.Next( lang::Whitespaces::Keep );
86
87 // check if end of actual msg part is an ANSI sequence. If yes, we extend the actual msg
88 if ( rest.CharAtStart() == '[' ) {
89 // read the 'm'
90 integer idx= rest.IndexOf( 'm' );
91 if ( idx < 0 ) { // unknown ANSI Code
92 ALIB_WARNING( "ALOX", "Unknown ANSI ESC Code \"{}...\"", rest.Substring(0, 10 ) )
93 writer.Write( actual );
94 continue;
95 }
96
97 column+= actual.WStringLength();
98
99 actual= Substring( actual.Buffer(), actual.Length() + idx + 2 );
100 rest.ConsumeChars<NC>( idx + 1 );
101
102 writer.Write( actual );
103
104 continue;
105 }
106
107 if ( actual.IsNotEmpty() ) {
108 writer.Write( actual );
109 column+= actual.WStringLength();
110 }
111
112 // end of loop?
113 if ( !msgParts.HasNext() )
114 break;
115
116 // found an ESC sequence
117 character c= rest.ConsumeChar();
118
119 // Colors
120 if( c == 'C' || c == 'c' ) {
121 bool isForeGround= ( c== 'c' );
122
123 c= rest.ConsumeChar();
124 int colNo= c - '0';
125 ALIB_ASSERT_WARNING( colNo >=0 && colNo <=9, "ALOX", "AnsiLogger: Unknown ESC-c code" )
126
127 // add bg
128 colNo+= isForeGround ? 0 : 10;
129
130 // add light
133 colNo+= 20;
134
135 String ansiCol;
136 switch( colNo ) {
137 case 0: ansiCol= AnsiLogger::ANSI_RED ; break;
138 case 1: ansiCol= AnsiLogger::ANSI_GREEN ; break;
139 case 2: ansiCol= AnsiLogger::ANSI_YELLOW ; break;
140 case 3: ansiCol= AnsiLogger::ANSI_BLUE ; break;
141 case 4: ansiCol= AnsiLogger::ANSI_MAGENTA ; break;
142 case 5: ansiCol= AnsiLogger::ANSI_CYAN ; break;
143 case 6: ansiCol= AnsiLogger::ANSI_BLACK ; break;
144 case 7: ansiCol= AnsiLogger::ANSI_WHITE ; break;
145 case 8: ansiCol= AnsiLogger::ANSI_GRAY ; break;
146 case 9: ansiCol= AnsiLogger::ANSI_STD_COL ; break;
147
148 case 10: ansiCol= AnsiLogger::ANSI_BG_RED ; break;
149 case 11: ansiCol= AnsiLogger::ANSI_BG_GREEN ; break;
150 case 12: ansiCol= AnsiLogger::ANSI_BG_YELLOW ; break;
151 case 13: ansiCol= AnsiLogger::ANSI_BG_BLUE ; break;
152 case 14: ansiCol= AnsiLogger::ANSI_BG_MAGENTA ; break;
153 case 15: ansiCol= AnsiLogger::ANSI_BG_CYAN ; break;
154 case 16: ansiCol= AnsiLogger::ANSI_BG_BLACK ; break;
155 case 17: ansiCol= AnsiLogger::ANSI_BG_WHITE ; break;
156 case 18: ansiCol= AnsiLogger::ANSI_BG_GRAY ; break;
157 case 19: ansiCol= AnsiLogger::ANSI_BG_STD_COL ; break;
158
159 case 20: ansiCol= AnsiLogger::ANSI_LIGHT_RED ; break;
160 case 21: ansiCol= AnsiLogger::ANSI_LIGHT_GREEN ; break;
161 case 22: ansiCol= AnsiLogger::ANSI_LIGHT_YELLOW ; break;
162 case 23: ansiCol= AnsiLogger::ANSI_LIGHT_BLUE ; break;
163 case 24: ansiCol= AnsiLogger::ANSI_LIGHT_MAGENTA ; break;
164 case 25: ansiCol= AnsiLogger::ANSI_LIGHT_CYAN ; break;
165 case 26: ansiCol= AnsiLogger::ANSI_BG_BLACK ; break;
166 case 27: ansiCol= AnsiLogger::ANSI_BG_WHITE ; break;
167 case 28: ansiCol= AnsiLogger::ANSI_LIGHT_GRAY ; break;
168 case 29: ansiCol= AnsiLogger::ANSI_STD_COL ; break;
169
170 case 30: ansiCol= AnsiLogger::ANSI_BG_LIGHT_RED ; break;
171 case 31: ansiCol= AnsiLogger::ANSI_BG_LIGHT_GREEN ; break;
172 case 32: ansiCol= AnsiLogger::ANSI_BG_LIGHT_YELLOW ; break;
173 case 33: ansiCol= AnsiLogger::ANSI_BG_LIGHT_BLUE ; break;
174 case 34: ansiCol= AnsiLogger::ANSI_BG_LIGHT_MAGENTA ; break;
175 case 35: ansiCol= AnsiLogger::ANSI_BG_LIGHT_CYAN ; break;
176 case 36: ansiCol= AnsiLogger::ANSI_BG_BLACK ; break;
177 case 37: ansiCol= AnsiLogger::ANSI_BG_WHITE ; break;
178 case 38: ansiCol= AnsiLogger::ANSI_BG_LIGHT_GRAY ; break;
179 case 39: ansiCol= AnsiLogger::ANSI_BG_STD_COL ; break;
180 default: ALIB_ERROR( "ALOX", "Error in ANSI Code" )
181 ansiCol= A_CHAR("");
182 break;
183 }
184
185 writer.Write( ansiCol );
186 }
187
188 // Styles
189 else if ( c == 's' ) {
190 c= rest.ConsumeChar();
191 writer.Write(const_cast<const character*>( c== 'B' ? ANSI_BOLD
192 : c== 'I' ? ANSI_ITALICS
193 : ANSI_RESET ) );
194 }
195
196 // auto tab / end of meta
197 else if ( c == 't' || c == 'A' ) {
198 c= rest.ConsumeChar();
199 int extraSpace= c >= '0' && c <= '9' ? ( c - '0' )
200 : ( c - 'A' ) + 10;
201
202 // tab stop (write spaces using a growing buffer)
204 AutoSizes::Types::Tabstop, column, extraSpace );
205 integer qtySpaces= tabStop - column;
206 writer.Fill(' ', qtySpaces );
207 column+= qtySpaces;
208 }
209
210 // Link (we just colorize links here)
211 else if ( c == 'l' )
212 writer.Write( rest.ConsumeChar() == 'S'
214 ? static_cast<const String&>( ANSI_LIGHT_BLUE )
215 : static_cast<const String&>( ANSI_BLUE ) )
216 : static_cast<const String&>( ANSI_STD_COL ) );
217
218 else {
219 rest.ConsumeChar();
220 ALIB_WARNING( "ALOX", "Unknown ESC code {!Q'}.", c )
221 }
222
223 } // write loop
224
225 writer.GetStream() << std::endl;
226}
227
228}}} // namespace [alib::lox::loggers]
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_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.
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.
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:316
TChar CharAtStart() const
Definition string.inl:433
integer IndexOf(TChar needle, integer startIdx=0) const
Definition string.inl:815
constexpr bool IsNotEmpty() const
Definition string.inl:369
constexpr const TChar * Buffer() const
Definition string.inl:311
integer WStringLength() const
TString< TChar > Substring(integer regionStart, integer regionLength=MAX_LEN) const
Definition string.inl:384
integer ConsumeChars(integer regionLength, TSubstring *target=nullptr)
void Write(const NString &src, integer *printedWidth=nullptr)
void Fill(const TChar fillChar, integer count)
@ 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:67
ALIB_DLL bool Define(Priority requestedPriority=Priority::Standard)
Definition variable.cpp:261
#define A_CHAR(STR)
#define ALIB_WARNING(domain,...)
Definition alib.inl:1063
#define ALIB_ASSERT_WARNING(cond, domain,...)
Definition alib.inl:1067
#define ALIB_ERROR(domain,...)
Definition alib.inl:1062
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:344
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
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:2189
strings::compatibility::std::OStreamWriter< TChar, TAllocator, TSynced, TTargetLF > OStreamWriter
Type alias in namespace alib.
characters::character character
Type alias in namespace alib.
monomem::TLocalAllocator< 4 > LocalAllocator4K
Type alias in namespace alib. Allocates 4kB of stack memory.
strings::TSubstring< character > Substring
Type alias in namespace alib.
Parameters specific to colorful loggers. As of today, this simply has one attribute.