wincolor color formatting support

This commit is contained in:
gabime 2018-04-06 03:04:18 +03:00
parent c8610d9a86
commit d040ab93ea
3 changed files with 29 additions and 9 deletions

View File

@ -58,8 +58,8 @@ int main(int, char *[])
daily_logger->info(123.44); daily_logger->info(123.44);
// Customize msg format for all messages // Customize msg format for all messages
spd::set_pattern("*** [%H:%M:%S %z] [thread %t] %v ***"); spd::set_pattern("[%^+++%$] [%H:%M:%S %z] [thread %t] %v");
rotating_logger->info("This is another message with custom format"); console->info("This a message with custom format (and custom color range between the '%^' and '%$')");
// Runtime log levels // Runtime log levels
spd::set_level(spd::level::info); // Set global log level to info spd::set_level(spd::level::info); // Set global log level to info

View File

@ -28,7 +28,7 @@ public:
: target_file_(file) : target_file_(file)
{ {
should_do_colors_ = details::os::in_terminal(file) && details::os::is_color_terminal(); should_do_colors_ = details::os::in_terminal(file) && details::os::is_color_terminal();
colors_[level::trace] = magenta; colors_[level::trace] = white;
colors_[level::debug] = cyan; colors_[level::debug] = cyan;
colors_[level::info] = green; colors_[level::info] = green;
colors_[level::warn] = yellow + bold; colors_[level::warn] = yellow + bold;

View File

@ -25,6 +25,7 @@ class wincolor_sink : public base_sink<Mutex>
public: public:
const WORD BOLD = FOREGROUND_INTENSITY; const WORD BOLD = FOREGROUND_INTENSITY;
const WORD RED = FOREGROUND_RED; const WORD RED = FOREGROUND_RED;
const WORD GREEN = FOREGROUND_GREEN;
const WORD CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE; const WORD CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE;
const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN; const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
@ -32,9 +33,9 @@ public:
wincolor_sink(HANDLE std_handle) wincolor_sink(HANDLE std_handle)
: out_handle_(std_handle) : out_handle_(std_handle)
{ {
colors_[level::trace] = CYAN; colors_[level::trace] = WHITE;
colors_[level::debug] = CYAN; colors_[level::debug] = CYAN;
colors_[level::info] = WHITE | BOLD; colors_[level::info] = GREEN;
colors_[level::warn] = YELLOW | BOLD; colors_[level::warn] = YELLOW | BOLD;
colors_[level::err] = RED | BOLD; // red bold colors_[level::err] = RED | BOLD; // red bold
colors_[level::critical] = BACKGROUND_RED | WHITE | BOLD; // white bold on red background colors_[level::critical] = BACKGROUND_RED | WHITE | BOLD; // white bold on red background
@ -59,10 +60,22 @@ public:
protected: protected:
void _sink_it(const details::log_msg &msg) override void _sink_it(const details::log_msg &msg) override
{ {
auto color = colors_[msg.level]; if (msg.color_range_end > msg.color_range_start)
auto orig_attribs = set_console_attribs(color); {
WriteConsoleA(out_handle_, msg.formatted.data(), static_cast<DWORD>(msg.formatted.size()), nullptr, nullptr); // before color range
SetConsoleTextAttribute(out_handle_, orig_attribs); // reset to orig colors _print_range(msg, 0, msg.color_range_start);
// in color range
auto orig_attribs = set_console_attribs(colors_[msg.level]);
_print_range(msg, msg.color_range_start, msg.color_range_end);
::SetConsoleTextAttribute(out_handle_, orig_attribs); // reset to orig colors
// after color range
_print_range(msg, msg.color_range_end, msg.formatted.size());
}
else // print without colors if color range is invalid
{
_print_range(msg, 0, msg.formatted.size());
}
} }
void _flush() override void _flush() override
@ -86,6 +99,13 @@ private:
SetConsoleTextAttribute(out_handle_, attribs | back_color); SetConsoleTextAttribute(out_handle_, attribs | back_color);
return orig_buffer_info.wAttributes; // return orig attribs return orig_buffer_info.wAttributes; // return orig attribs
} }
// print a range of formatted message to console
void _print_range(const details::log_msg &msg, size_t start, size_t end)
{
DWORD size = static_cast<DWORD>(end - start);
WriteConsoleA(out_handle_, msg.formatted.data() + start, size, nullptr, nullptr);
}
}; };
// //