ansiocolor_sink now works correctly on windows

pull/194/head^2
Christopher Torres 10 years ago
parent 083d6c0d2f
commit f1e3f676e3
No known key found for this signature in database
GPG Key ID: 8728E0631469D094

@ -33,6 +33,40 @@ public:
virtual void log(const details::log_msg& msg) override;
virtual void flush() override;
#ifdef _WIN32
void set_color(level::level_enum level, const short& color);
/// Formatting codes
const short reset = 0;
const short bold = FOREGROUND_INTENSITY;
const short dark = reset; // Not implemented in windows
const short underline = reset; // Not implemented in windows
const short blink = reset; // Not implemented in windows
const short reverse = FOREGROUND_RED | FOREGROUND_GREEN |
FOREGROUND_BLUE | BACKGROUND_RED |
BACKGROUND_GREEN | BACKGROUND_BLUE; // XOR to use this
const short concealed = reset; // Not implemented in windows
// Foreground colors
const short grey = bold;
const short red = FOREGROUND_RED;
const short green = FOREGROUND_GREEN;
const short yellow = FOREGROUND_RED | FOREGROUND_GREEN;
const short blue = FOREGROUND_BLUE;
const short magenta = FOREGROUND_RED | FOREGROUND_BLUE;
const short cyan = FOREGROUND_GREEN | FOREGROUND_BLUE;
const short white = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
/// Background colors
const short on_grey = BACKGROUND_INTENSITY;
const short on_red = BACKGROUND_RED;
const short on_green = BACKGROUND_GREEN;
const short on_yellow = BACKGROUND_RED | BACKGROUND_GREEN;
const short on_blue = BACKGROUND_BLUE;
const short on_magenta = BACKGROUND_RED | BACKGROUND_BLUE;
const short on_cyan = BACKGROUND_GREEN | BACKGROUND_BLUE;
const short on_white = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
#else
void set_color(level::level_enum level, const std::string& color);
/// Formatting codes
@ -63,15 +97,31 @@ public:
const std::string on_magenta = "\033[45m";
const std::string on_cyan = "\033[46m";
const std::string on_white = "\033[47m";
#endif
protected:
sink_ptr sink_;
#ifdef _WIN32
std::map<level::level_enum, short> colors_;
#else
std::map<level::level_enum, std::string> colors_;
#endif
};
inline ansicolor_sink::ansicolor_sink(sink_ptr wrapped_sink) : sink_(wrapped_sink)
{
#ifdef _WIN32
colors_[level::trace] = cyan;
colors_[level::debug] = cyan;
colors_[level::info] = white;
colors_[level::notice] = bold | white;
colors_[level::warn] = bold | yellow;
colors_[level::err] = red;
colors_[level::critical] = bold | red;
colors_[level::alert] = bold | white | on_red;
colors_[level::emerg] = bold | yellow | on_red;
colors_[level::off] = reset;
#else
colors_[level::trace] = cyan;
colors_[level::debug] = cyan;
colors_[level::info] = white;
@ -82,25 +132,38 @@ inline ansicolor_sink::ansicolor_sink(sink_ptr wrapped_sink) : sink_(wrapped_sin
colors_[level::alert] = bold + white + on_red;
colors_[level::emerg] = bold + yellow + on_red;
colors_[level::off] = reset;
#endif
}
inline void ansicolor_sink::log(const details::log_msg& msg)
{
// Wrap the originally formatted message in color codes
const std::string& prefix = colors_[msg.level];
const std::string& s = msg.formatted.str();
const std::string& suffix = reset;
details::log_msg m;
#ifdef _WIN32
m.formatted << s;
SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ), colors_[msg.level]);
sink_->log( m );
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), reset );
#else
const std::string& prefix = colors_[msg.level];
const std::string& suffix = reset;
m.formatted << prefix << s << suffix;
sink_->log(m);
sink_->log( m );
#endif
}
inline void ansicolor_sink::flush()
{
sink_->flush();
}
#ifdef _WIN32
inline void ansicolor_sink::set_color(level::level_enum level, const short& color)
#else
inline void ansicolor_sink::set_color(level::level_enum level, const std::string& color)
#endif
{
colors_[level] = color;
}

Loading…
Cancel
Save