TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
AppenderConsole Class Reference

#include <AppenderConsole.h>

Public Types

typedef std::integral_constant
< AppenderType,
APPENDER_CONSOLE >::type 
TypeIndex
 

Public Member Functions

 AppenderConsole (uint8 _id, std::string const &name, LogLevel level, AppenderFlags flags, ExtraAppenderArgs extraArgs)
 
void InitColors (const std::string &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 constgetName () const
 
LogLevel getLogLevel () const
 
AppenderFlags getFlags () const
 
void setLogLevel (LogLevel)
 
void write (LogMessage *message)
 
virtual void setRealmId (uint32)
 

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 [MaxLogLevels]
 

Additional Inherited Members

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

Member Typedef Documentation

typedef std::integral_constant<AppenderType, APPENDER_CONSOLE>::type AppenderConsole::TypeIndex

Constructor & Destructor Documentation

AppenderConsole::AppenderConsole ( uint8  _id,
std::string const name,
LogLevel  level,
AppenderFlags  flags,
ExtraAppenderArgs  extraArgs 
)
29  : Appender(id, name, level, flags), _colored(false)
30 {
31  for (uint8 i = 0; i < MaxLogLevels; ++i)
33 
34  if (!extraArgs.empty())
35  InitColors(extraArgs[0]);
36 }
bool _colored
Definition: AppenderConsole.h:58
const uint8 MaxColors
Definition: AppenderConsole.h:43
std::string name
Definition: Appender.h:109
AppenderFlags flags
Definition: Appender.h:111
const uint8 MaxLogLevels
Definition: Appender.h:42
Appender(uint8 _id, std::string const &name, LogLevel level=LOG_LEVEL_DISABLED, AppenderFlags flags=APPENDER_FLAGS_NONE)
Definition: Appender.cpp:40
LogLevel level
Definition: Appender.h:110
uint8_t uint8
Definition: Define.h:152
ColorTypes _colors[MaxLogLevels]
Definition: AppenderConsole.h:59
void InitColors(const std::string &init_str)
Definition: AppenderConsole.cpp:38
ColorTypes
Definition: AppenderConsole.h:24

+ Here is the call graph for this function:

Member Function Documentation

void AppenderConsole::_write ( LogMessage const message)
overrideprivatevirtual

Implements Appender.

165 {
166  bool stdout_stream = !(message->level == LOG_LEVEL_ERROR || message->level == LOG_LEVEL_FATAL);
167 
168  if (_colored)
169  {
170  uint8 index;
171  switch (message->level)
172  {
173  case LOG_LEVEL_TRACE:
174  index = 5;
175  break;
176  case LOG_LEVEL_DEBUG:
177  index = 4;
178  break;
179  case LOG_LEVEL_INFO:
180  index = 3;
181  break;
182  case LOG_LEVEL_WARN:
183  index = 2;
184  break;
185  case LOG_LEVEL_FATAL:
186  index = 0;
187  break;
188  case LOG_LEVEL_ERROR: // No break on purpose
189  default:
190  index = 1;
191  break;
192  }
193 
194  SetColor(stdout_stream, _colors[index]);
195  utf8printf(stdout_stream ? stdout : stderr, "%s%s\n", message->prefix.c_str(), message->text.c_str());
196  ResetColor(stdout_stream);
197  }
198  else
199  utf8printf(stdout_stream ? stdout : stderr, "%s%s\n", message->prefix.c_str(), message->text.c_str());
200 }
bool _colored
Definition: AppenderConsole.h:58
void SetColor(bool stdout_stream, ColorTypes color)
Definition: AppenderConsole.cpp:67
void ResetColor(bool stdout_stream)
Definition: AppenderConsole.cpp:154
Definition: Appender.h:35
Definition: Appender.h:34
Definition: Appender.h:36
Definition: Appender.h:37
Definition: Appender.h:38
void utf8printf(FILE *out, const char *str,...)
Definition: Util.cpp:469
Definition: Appender.h:39
uint8_t uint8
Definition: Define.h:152
ColorTypes _colors[MaxLogLevels]
Definition: AppenderConsole.h:59

+ Here is the call graph for this function:

AppenderType AppenderConsole::getType ( ) const
inlineoverridevirtual

Implements Appender.

52 { return TypeIndex::value; }
const FieldDescriptor value
Definition: descriptor.h:1522
void AppenderConsole::InitColors ( const std::string &  init_str)
39 {
40  if (str.empty())
41  {
42  _colored = false;
43  return;
44  }
45 
46  int color[MaxLogLevels];
47 
48  std::istringstream ss(str);
49 
50  for (uint8 i = 0; i < MaxLogLevels; ++i)
51  {
52  ss >> color[i];
53 
54  if (!ss)
55  return;
56 
57  if (color[i] < 0 || color[i] >= MaxColors)
58  return;
59  }
60 
61  for (uint8 i = 0; i < MaxLogLevels; ++i)
62  _colors[i] = ColorTypes(color[i]);
63 
64  _colored = true;
65 }
bool _colored
Definition: AppenderConsole.h:58
const uint8 MaxColors
Definition: AppenderConsole.h:43
const uint8 MaxLogLevels
Definition: Appender.h:42
uint8_t uint8
Definition: Define.h:152
ColorTypes _colors[MaxLogLevels]
Definition: AppenderConsole.h:59
ColorTypes
Definition: AppenderConsole.h:24

+ Here is the caller graph for this function:

void AppenderConsole::ResetColor ( bool  stdout_stream)
private
155 {
156  #if PLATFORM == PLATFORM_WINDOWS
157  HANDLE hConsole = GetStdHandle(stdout_stream ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE);
158  SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
159  #else
160  fprintf((stdout_stream ? stdout : stderr), "\x1b[0m");
161  #endif
162 }
void * HANDLE
Definition: CascPort.h:146
FMT_API int fprintf(std::FILE *f, CStringRef format, ArgList args)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

bool AppenderConsole::_colored
private
ColorTypes AppenderConsole::_colors[MaxLogLevels]
private

The documentation for this class was generated from the following files: