From 5e856c6b4da77ca7d4c15dc494ffbfc489a50d48 Mon Sep 17 00:00:00 2001 From: Philip Salzmann Date: Thu, 9 May 2019 13:16:38 +0200 Subject: [PATCH] Add mode enum to control output of color sinks This adds a new "color_mode" enum that can be used to control the color code output behavior of sinks with color support. It can be one of three values: always, automatic and never. --- include/spdlog/common.h | 10 ++++++++++ include/spdlog/sinks/ansicolor_sink.h | 20 ++++++++++++++++++-- include/spdlog/sinks/stdout_color_sinks.h | 16 ++++++++-------- include/spdlog/sinks/wincolor_sink.h | 22 +++++++++++++++++++--- 4 files changed, 55 insertions(+), 13 deletions(-) diff --git a/include/spdlog/common.h b/include/spdlog/common.h index dd9a4785..85e3cecd 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -159,6 +159,16 @@ inline spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCE using level_hasher = std::hash; } // namespace level +// +// Color mode used by sinks with color support. +// +enum class color_mode +{ + always, + automatic, + never +}; + // // Pattern time - specific time getting to use for pattern_formatter. // local time by default diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index 1cb797ba..3563e7b6 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -33,12 +33,12 @@ class ansicolor_sink final : public sink { public: using mutex_t = typename ConsoleMutex::mutex_t; - ansicolor_sink() + ansicolor_sink(color_mode mode = color_mode::automatic) : target_file_(TargetStream::stream()) , mutex_(ConsoleMutex::mutex()) { - should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal(); + set_color_mode(mode); colors_[level::trace] = white; colors_[level::debug] = cyan; colors_[level::info] = green; @@ -138,6 +138,22 @@ public: return should_do_colors_; } + void set_color_mode(color_mode mode) + { + switch (mode) + { + case color_mode::always: + should_do_colors_ = true; + return; + case color_mode::automatic: + should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal(); + return; + case color_mode::never: + should_do_colors_ = false; + return; + } + } + private: void print_ccode_(const std::string &color_code) { diff --git a/include/spdlog/sinks/stdout_color_sinks.h b/include/spdlog/sinks/stdout_color_sinks.h index 5accda44..89271666 100644 --- a/include/spdlog/sinks/stdout_color_sinks.h +++ b/include/spdlog/sinks/stdout_color_sinks.h @@ -31,26 +31,26 @@ using stderr_color_sink_st = ansicolor_stderr_sink_st; } // namespace sinks template -inline std::shared_ptr stdout_color_mt(const std::string &logger_name) +inline std::shared_ptr stdout_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic) { - return Factory::template create(logger_name); + return Factory::template create(logger_name, mode); } template -inline std::shared_ptr stdout_color_st(const std::string &logger_name) +inline std::shared_ptr stdout_color_st(const std::string &logger_name, color_mode mode = color_mode::automatic) { - return Factory::template create(logger_name); + return Factory::template create(logger_name, mode); } template -inline std::shared_ptr stderr_color_mt(const std::string &logger_name) +inline std::shared_ptr stderr_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic) { - return Factory::template create(logger_name); + return Factory::template create(logger_name, mode); } template -inline std::shared_ptr stderr_color_st(const std::string &logger_name) +inline std::shared_ptr stderr_color_st(const std::string &logger_name, color_mode mode = color_mode::automatic) { - return Factory::template create(logger_name); + return Factory::template create(logger_name, mode); } } // namespace spdlog diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index 1fdf8c56..2ee3efd7 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -37,10 +37,11 @@ public: const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN; - wincolor_sink() + wincolor_sink(color_mode mode = color_mode::automatic) : out_handle_(OutHandle::handle()) , mutex_(ConsoleMutex::mutex()) { + set_color_mode(mode); colors_[level::trace] = WHITE; colors_[level::debug] = CYAN; colors_[level::info] = GREEN; @@ -70,7 +71,7 @@ public: std::lock_guard lock(mutex_); fmt::memory_buffer formatted; formatter_->format(msg, formatted); - if (msg.color_range_end > msg.color_range_start) + if (should_do_colors_ && msg.color_range_end > msg.color_range_start) { // before color range print_range_(formatted, 0, msg.color_range_start); @@ -83,7 +84,7 @@ public: // after color range print_range_(formatted, msg.color_range_end, formatted.size()); } - else // print without colors if color range is invalid + else // print without colors if color range is invalid (or color is disabled) { print_range_(formatted, 0, formatted.size()); } @@ -106,6 +107,20 @@ public: formatter_ = std::move(sink_formatter); } + void set_color_mode(color_mode mode) + { + switch (mode) + { + case color_mode::always: + case color_mode::automatic: + should_do_colors_ = true; + return; + case color_mode::never: + should_do_colors_ = false; + return; + } + } + private: using mutex_t = typename ConsoleMutex::mutex_t; // set color and return the orig console attributes (for resetting later) @@ -130,6 +145,7 @@ private: HANDLE out_handle_; mutex_t &mutex_; + bool should_do_colors_; std::unordered_map colors_; };