20 #include <unordered_map> 29 template<
typename OutHandle,
typename ConsoleMutex>
33 const WORD
BOLD = FOREGROUND_INTENSITY;
34 const WORD
RED = FOREGROUND_RED;
35 const WORD
GREEN = FOREGROUND_GREEN;
36 const WORD
CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE;
37 const WORD
WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
38 const WORD
YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
41 : out_handle_(OutHandle::handle())
42 , mutex_(ConsoleMutex::mutex())
64 std::lock_guard<mutex_t> lock(mutex_);
70 std::lock_guard<mutex_t> lock(mutex_);
73 if (msg.color_range_end > msg.color_range_start)
76 print_range_(formatted, 0, msg.color_range_start);
79 auto orig_attribs = set_console_attribs(colors_[msg.level]);
80 print_range_(formatted, msg.color_range_start, msg.color_range_end);
81 ::SetConsoleTextAttribute(out_handle_,
84 print_range_(formatted, msg.color_range_end, formatted.
size());
88 print_range_(formatted, 0, formatted.
size());
99 std::lock_guard<mutex_t> lock(mutex_);
103 void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
override final 105 std::lock_guard<mutex_t> lock(mutex_);
110 using mutex_t =
typename ConsoleMutex::mutex_t;
112 WORD set_console_attribs(WORD attribs)
114 CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info;
115 ::GetConsoleScreenBufferInfo(out_handle_, &orig_buffer_info);
116 WORD back_color = orig_buffer_info.wAttributes;
118 back_color &=
static_cast<WORD
>(~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY));
120 ::SetConsoleTextAttribute(out_handle_, attribs | back_color);
121 return orig_buffer_info.wAttributes;
127 auto size =
static_cast<DWORD
>(end - start);
128 ::WriteConsoleA(out_handle_, formatted.
data() + start, size,
nullptr,
nullptr);
133 std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_;
wincolor_sink & operator=(const wincolor_sink &other)=delete
std::unique_ptr< spdlog::formatter > formatter_
void set_pattern(const std::string &pattern) overridefinal
~wincolor_sink() override
void flush() finaloverride
void set_formatter(std::unique_ptr< spdlog::formatter > sink_formatter) overridefinal
level::level_enum level() const
void set_color(level::level_enum level, WORD color)
void log(const details::log_msg &msg) finaloverride
auto end(const C &c) -> decltype(c.end())