diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index 3563e7b6..905d270a 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -38,7 +38,7 @@ public: , mutex_(ConsoleMutex::mutex()) { - set_color_mode(mode); + set_color_mode_(mode); colors_[level::trace] = white; colors_[level::debug] = cyan; colors_[level::info] = green; @@ -139,22 +139,28 @@ public: } void set_color_mode(color_mode mode) + { + std::lock_guard lock(mutex_); + set_color_mode_(mode); + } + +private: + void set_color_mode_(color_mode mode) { switch (mode) { case color_mode::always: should_do_colors_ = true; - return; + break; case color_mode::automatic: should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal(); - return; + break; case color_mode::never: should_do_colors_ = false; - return; + break; } } -private: void print_ccode_(const std::string &color_code) { fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_); diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index 2ee3efd7..3c248192 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -41,7 +41,7 @@ public: : out_handle_(OutHandle::handle()) , mutex_(ConsoleMutex::mutex()) { - set_color_mode(mode); + set_color_mode_(mode); colors_[level::trace] = WHITE; colors_[level::debug] = CYAN; colors_[level::info] = GREEN; @@ -108,21 +108,28 @@ public: } void set_color_mode(color_mode mode) + { + std::lock_guard lock(mutex_); + set_color_mode_(mode); + } + +private: + using mutex_t = typename ConsoleMutex::mutex_t; + + void set_color_mode_(color_mode mode) { switch (mode) { case color_mode::always: case color_mode::automatic: should_do_colors_ = true; - return; + break case color_mode::never: should_do_colors_ = false; - return; + break } } -private: - using mutex_t = typename ConsoleMutex::mutex_t; // set color and return the orig console attributes (for resetting later) WORD set_console_attribs(WORD attribs) {