Commit 91046e6c authored by Gabi Melman's avatar Gabi Melman

Simplify wincolor sink color mode management

parent 17e1ba8a
...@@ -22,12 +22,7 @@ SPDLOG_INLINE wincolor_sink<ConsoleMutex>::wincolor_sink(void *out_handle, color ...@@ -22,12 +22,7 @@ SPDLOG_INLINE wincolor_sink<ConsoleMutex>::wincolor_sink(void *out_handle, color
, formatter_(details::make_unique<spdlog::pattern_formatter>()) , formatter_(details::make_unique<spdlog::pattern_formatter>())
{ {
// check if out_handle is points to the actual console.
// ::GetConsoleMode() should return 0 if it is redirected or not valid console handle.
DWORD console_mode;
in_console_ = ::GetConsoleMode(static_cast<HANDLE>(out_handle_), &console_mode) != 0;
set_color_mode_impl(mode); set_color_mode_impl(mode);
// set level colors // set level colors
colors_[level::trace] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; // white colors_[level::trace] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; // white
colors_[level::debug] = FOREGROUND_GREEN | FOREGROUND_BLUE; // cyan colors_[level::debug] = FOREGROUND_GREEN | FOREGROUND_BLUE; // cyan
...@@ -66,16 +61,10 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg) ...@@ -66,16 +61,10 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
msg.color_range_end = 0; msg.color_range_end = 0;
memory_buf_t formatted; memory_buf_t formatted;
formatter_->format(msg, formatted); formatter_->format(msg, formatted);
if (!in_console_)
{
write_to_file_(formatted);
return;
}
if (should_do_colors_ && msg.color_range_end > msg.color_range_start) if (should_do_colors_ && msg.color_range_end > msg.color_range_start)
{ {
// before color range // before color range
print_range_(formatted, 0, msg.color_range_start); print_range_(formatted, 0, msg.color_range_start);
// in color range // in color range
auto orig_attribs = static_cast<WORD>(set_foreground_color_(colors_[msg.level])); auto orig_attribs = static_cast<WORD>(set_foreground_color_(colors_[msg.level]));
print_range_(formatted, msg.color_range_start, msg.color_range_end); print_range_(formatted, msg.color_range_start, msg.color_range_end);
...@@ -85,7 +74,7 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg) ...@@ -85,7 +74,7 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::log(const details::log_msg &msg)
} }
else // print without colors if color range is invalid (or color is disabled) else // print without colors if color range is invalid (or color is disabled)
{ {
print_range_(formatted, 0, formatted.size()); write_to_file_(formatted);
} }
} }
...@@ -119,7 +108,21 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color_mode(color_mode mode) ...@@ -119,7 +108,21 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color_mode(color_mode mode)
template<typename ConsoleMutex> template<typename ConsoleMutex>
void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color_mode_impl(color_mode mode) void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::set_color_mode_impl(color_mode mode)
{ {
should_do_colors_ = mode != color_mode::never; switch (mode)
{
case color_mode::always:
should_do_colors_ = true;
break;
case color_mode::never:
should_do_colors_ = false;
break;
default:
// should do colors only if out_handle_ points to actual console.
DWORD console_mode;
bool in_console = ::GetConsoleMode(static_cast<HANDLE>(out_handle_), &console_mode) != 0;
should_do_colors_ = in_console;
break;
}
} }
// set foreground color and return the orig console attributes (for resetting later) // set foreground color and return the orig console attributes (for resetting later)
......
...@@ -42,7 +42,6 @@ protected: ...@@ -42,7 +42,6 @@ protected:
using mutex_t = typename ConsoleMutex::mutex_t; using mutex_t = typename ConsoleMutex::mutex_t;
void *out_handle_; void *out_handle_;
mutex_t &mutex_; mutex_t &mutex_;
bool in_console_;
bool should_do_colors_; bool should_do_colors_;
std::unique_ptr<spdlog::formatter> formatter_; std::unique_ptr<spdlog::formatter> formatter_;
std::array<std::uint16_t, level::n_levels> colors_; std::array<std::uint16_t, level::n_levels> colors_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment