From 317342278604608daab719a9953241ea9dc928d8 Mon Sep 17 00:00:00 2001 From: Gabriel Vaduva Date: Sat, 29 Jul 2017 23:07:02 +0300 Subject: [PATCH] Background color unchanged Keep the background color unchaged on Windows console when using colored loggers. --- include/spdlog/sinks/wincolor_sink.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index 66111249..5b92bf8a 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -80,7 +80,11 @@ private: { CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info; GetConsoleScreenBufferInfo(out_handle_, &orig_buffer_info); - SetConsoleTextAttribute(out_handle_, attribs); + WORD back_color = orig_buffer_info.wAttributes; + // retrieve the current background color + back_color &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY); + // keep the background color unchanged + SetConsoleTextAttribute(out_handle_, attribs | back_color); return orig_buffer_info.wAttributes; //return orig attribs } };