AzeorthCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
AppenderConsole Class Reference

#include "AppenderConsole.h"

Inheritance diagram for AppenderConsole:
Appender

Public Member Functions

 AppenderConsole (uint8 _id, std::string const &name, LogLevel level, AppenderFlags flags, std::vector< std::string_view > const &args)
 
void InitColors (std::string const &name, std::string_view init_str)
 
AppenderType getType () const override
 
- Public Member Functions inherited from Appender
 Appender (uint8 _id, std::string const &name, LogLevel level=LOG_LEVEL_DISABLED, AppenderFlags flags=APPENDER_FLAGS_NONE)
 
virtual ~Appender ()
 
uint8 getId () const
 
std::string const & getName () const
 
virtual AppenderType getType () const =0
 
LogLevel getLogLevel () const
 
AppenderFlags getFlags () const
 
void setLogLevel (LogLevel)
 
void write (LogMessage *message)
 
virtual void setRealmId (uint32)
 

Static Public Attributes

static constexpr AppenderType type = APPENDER_CONSOLE
 

Private Member Functions

void SetColor (bool stdout_stream, ColorTypes color)
 
void ResetColor (bool stdout_stream)
 
void _write (LogMessage const *message) override
 

Private Attributes

bool _colored
 
ColorTypes _colors [NUM_ENABLED_LOG_LEVELS]
 

Additional Inherited Members

- Static Public Member Functions inherited from Appender
static char const * getLogLevelString (LogLevel level)
 

Detailed Description

Constructor & Destructor Documentation

◆ AppenderConsole()

AppenderConsole::AppenderConsole ( uint8  _id,
std::string const &  name,
LogLevel  level,
AppenderFlags  flags,
std::vector< std::string_view > const &  args 
)
31 : Appender(id, name, level, flags), _colored(false)
32{
33 for (uint8 i = 0; i < NUM_ENABLED_LOG_LEVELS; ++i)
34 {
36 }
37
38 if (3 < args.size())
39 {
40 InitColors(name, args[3]);
41 }
42}
std::uint8_t uint8
Definition: Define.h:110
ColorTypes
Definition: AppenderConsole.h:25
@ NUM_COLOR_TYPES
Definition: AppenderConsole.h:41
@ NUM_ENABLED_LOG_LEVELS
Definition: LogCommon.h:34
Definition: Appender.h:30
std::string name
Definition: Appender.h:50
LogLevel level
Definition: Appender.h:51
AppenderFlags flags
Definition: Appender.h:52
bool _colored
Definition: AppenderConsole.h:57
void InitColors(std::string const &name, std::string_view init_str)
Definition: AppenderConsole.cpp:44
ColorTypes _colors[NUM_ENABLED_LOG_LEVELS]
Definition: AppenderConsole.h:58

References _colors, InitColors(), Appender::name, NUM_COLOR_TYPES, and NUM_ENABLED_LOG_LEVELS.

Member Function Documentation

◆ _write()

void AppenderConsole::_write ( LogMessage const *  message)
overrideprivatevirtual

Implements Appender.

173{
174 bool stdout_stream = !(message->level == LOG_LEVEL_ERROR || message->level == LOG_LEVEL_FATAL);
175
176 if (_colored)
177 {
178 uint8 index;
179
180 switch (message->level)
181 {
182 case LOG_LEVEL_TRACE:
183 index = 5;
184 break;
185 case LOG_LEVEL_DEBUG:
186 index = 4;
187 break;
188 case LOG_LEVEL_INFO:
189 index = 3;
190 break;
191 case LOG_LEVEL_WARN:
192 index = 2;
193 break;
194 case LOG_LEVEL_FATAL:
195 index = 0;
196 break;
197 case LOG_LEVEL_ERROR:
198 [[fallthrough]];
199 default:
200 index = 1;
201 break;
202 }
203
204 SetColor(stdout_stream, _colors[index]);
205 utf8printf(stdout_stream ? stdout : stderr, "%s%s\n", message->prefix.c_str(), message->text.c_str());
206 ResetColor(stdout_stream);
207 }
208 else
209 {
210 utf8printf(stdout_stream ? stdout : stderr, "%s%s\n", message->prefix.c_str(), message->text.c_str());
211 }
212}
@ LOG_LEVEL_DEBUG
Definition: LogCommon.h:31
@ LOG_LEVEL_ERROR
Definition: LogCommon.h:28
@ LOG_LEVEL_FATAL
Definition: LogCommon.h:27
@ LOG_LEVEL_TRACE
Definition: LogCommon.h:32
@ LOG_LEVEL_WARN
Definition: LogCommon.h:29
@ LOG_LEVEL_INFO
Definition: LogCommon.h:30
void utf8printf(FILE *out, const char *str,...)
Definition: Util.cpp:497
void ResetColor(bool stdout_stream)
Definition: AppenderConsole.cpp:162
void SetColor(bool stdout_stream, ColorTypes color)
Definition: AppenderConsole.cpp:75

References _colored, _colors, LogMessage::level, LOG_LEVEL_DEBUG, LOG_LEVEL_ERROR, LOG_LEVEL_FATAL, LOG_LEVEL_INFO, LOG_LEVEL_TRACE, LOG_LEVEL_WARN, LogMessage::prefix, ResetColor(), SetColor(), LogMessage::text, and utf8printf().

◆ getType()

AppenderType AppenderConsole::getType ( ) const
inlineoverridevirtual

Implements Appender.

51{ return type; }
static constexpr AppenderType type
Definition: AppenderConsole.h:47

References type.

◆ InitColors()

void AppenderConsole::InitColors ( std::string const &  name,
std::string_view  init_str 
)
45{
46 if (str.empty())
47 {
48 _colored = false;
49 return;
50 }
51
52 std::vector<std::string_view> colorStrs = Acore::Tokenize(str, ' ', false);
53 if (colorStrs.size() != NUM_ENABLED_LOG_LEVELS)
54 {
55 throw InvalidAppenderArgsException(Acore::StringFormatFmt("Log::CreateAppenderFromConfig: Invalid color data '{}' for console appender {} (expected {} entries, got {})",
56 str, name, NUM_ENABLED_LOG_LEVELS, colorStrs.size()));
57 }
58
59 for (uint8 i = 0; i < NUM_ENABLED_LOG_LEVELS; ++i)
60 {
61 if (Optional<uint8> color = Acore::StringTo<uint8>(colorStrs[i]); color && EnumUtils::IsValid<ColorTypes>(*color))
62 {
63 _colors[i] = static_cast<ColorTypes>(*color);
64 }
65 else
66 {
67 throw InvalidAppenderArgsException(Acore::StringFormatFmt("Log::CreateAppenderFromConfig: Invalid color '{}' for log level {} on console appender {}",
68 colorStrs[i], EnumUtils::ToTitle(static_cast<LogLevel>(i)), name));
69 }
70 }
71
72 _colored = true;
73}
LogLevel
Definition: LogCommon.h:25
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:24
std::string StringFormatFmt(std::string_view fmt, Args &&... args)
Definition: StringFormat.h:44
std::vector< std::string_view > Tokenize(std::string_view str, char sep, bool keepEmpty)
Definition: Tokenize.cpp:20
Definition: Appender.h:56
static char const * ToTitle(Enum value)
Definition: SmartEnum.h:127

References _colored, _colors, Appender::name, NUM_ENABLED_LOG_LEVELS, Acore::StringFormatFmt(), Acore::Tokenize(), and EnumUtils::ToTitle().

Referenced by AppenderConsole().

◆ ResetColor()

void AppenderConsole::ResetColor ( bool  stdout_stream)
private
163{
164#if AC_PLATFORM == AC_PLATFORM_WINDOWS
165 HANDLE hConsole = GetStdHandle(stdout_stream ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE);
166 SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
167#else
168 fprintf((stdout_stream ? stdout : stderr), "\x1b[0m");
169#endif
170}

Referenced by _write().

◆ SetColor()

void AppenderConsole::SetColor ( bool  stdout_stream,
ColorTypes  color 
)
private
76{
77#if AC_PLATFORM == AC_PLATFORM_WINDOWS
78 static WORD WinColorFG[NUM_COLOR_TYPES] =
79 {
80 0, // BLACK
81 FOREGROUND_RED, // RED
82 FOREGROUND_GREEN, // GREEN
83 FOREGROUND_RED | FOREGROUND_GREEN, // BROWN
84 FOREGROUND_BLUE, // BLUE
85 FOREGROUND_RED | FOREGROUND_BLUE, // MAGENTA
86 FOREGROUND_GREEN | FOREGROUND_BLUE, // CYAN
87 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, // WHITE
88 // YELLOW
89 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
90 // RED_BOLD
91 FOREGROUND_RED | FOREGROUND_INTENSITY,
92 // GREEN_BOLD
93 FOREGROUND_GREEN | FOREGROUND_INTENSITY,
94 FOREGROUND_BLUE | FOREGROUND_INTENSITY, // BLUE_BOLD
95 // MAGENTA_BOLD
96 FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
97 // CYAN_BOLD
98 FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
99 // WHITE_BOLD
100 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY
101 };
102
103 HANDLE hConsole = GetStdHandle(stdout_stream ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE);
104 SetConsoleTextAttribute(hConsole, WinColorFG[color]);
105#else
106 enum ANSITextAttr
107 {
108 TA_NORMAL = 0,
109 TA_BOLD = 1,
110 TA_BLINK = 5,
111 TA_REVERSE = 7
112 };
113
114 enum ANSIFgTextAttr
115 {
116 FG_BLACK = 30,
117 FG_RED,
118 FG_GREEN,
119 FG_BROWN,
120 FG_BLUE,
121 FG_MAGENTA,
122 FG_CYAN,
123 FG_WHITE,
124 FG_YELLOW
125 };
126
127 enum ANSIBgTextAttr
128 {
129 BG_BLACK = 40,
130 BG_RED,
131 BG_GREEN,
132 BG_BROWN,
133 BG_BLUE,
134 BG_MAGENTA,
135 BG_CYAN,
136 BG_WHITE
137 };
138
139 static uint8 UnixColorFG[NUM_COLOR_TYPES] =
140 {
141 FG_BLACK, // BLACK
142 FG_RED, // RED
143 FG_GREEN, // GREEN
144 FG_BROWN, // BROWN
145 FG_BLUE, // BLUE
146 FG_MAGENTA, // MAGENTA
147 FG_CYAN, // CYAN
148 FG_WHITE, // WHITE
149 FG_YELLOW, // YELLOW
150 FG_RED, // LRED
151 FG_GREEN, // LGREEN
152 FG_BLUE, // LBLUE
153 FG_MAGENTA, // LMAGENTA
154 FG_CYAN, // LCYAN
155 FG_WHITE // LWHITE
156 };
157
158 fprintf((stdout_stream ? stdout : stderr), "\x1b[%d%sm", UnixColorFG[color], (color >= YELLOW && color < NUM_COLOR_TYPES ? ";1" : ""));
159#endif
160}
@ YELLOW
Definition: AppenderConsole.h:34

References NUM_COLOR_TYPES, and YELLOW.

Referenced by _write().

Member Data Documentation

◆ _colored

bool AppenderConsole::_colored
private

Referenced by _write(), and InitColors().

◆ _colors

ColorTypes AppenderConsole::_colors[NUM_ENABLED_LOG_LEVELS]
private

Referenced by _write(), AppenderConsole(), and InitColors().

◆ type

constexpr AppenderType AppenderConsole::type = APPENDER_CONSOLE
staticconstexpr

Referenced by getType().