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