diff --git a/include/spdlog/common.h b/include/spdlog/common.h index ccc1d990..905f469b 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -156,6 +156,16 @@ spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT; 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-inl.h b/include/spdlog/sinks/ansicolor_sink-inl.h index 7f7cd751..c7c3fa2d 100644 --- a/include/spdlog/sinks/ansicolor_sink-inl.h +++ b/include/spdlog/sinks/ansicolor_sink-inl.h @@ -10,12 +10,12 @@ #include "spdlog/details/os.h" template -SPDLOG_INLINE spdlog::sinks::ansicolor_sink::ansicolor_sink() +SPDLOG_INLINE spdlog::sinks::ansicolor_sink::ansicolor_sink(color_mode mode) : 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; @@ -88,6 +88,23 @@ SPDLOG_INLINE bool spdlog::sinks::ansicolor_sink::sh return should_do_colors_; } +template +SPDLOG_INLINE void spdlog::sinks::ansicolor_sink::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; + } +} + template SPDLOG_INLINE void spdlog::sinks::ansicolor_sink::print_ccode_(const std::string &color_code) { diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index 08a30aae..f940255e 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -29,7 +29,7 @@ class ansicolor_sink final : public sink { public: using mutex_t = typename ConsoleMutex::mutex_t; - ansicolor_sink(); + ansicolor_sink(color_mode mode = color_mode::automatic); ~ansicolor_sink() override = default; ansicolor_sink(const ansicolor_sink &other) = delete; @@ -40,6 +40,7 @@ public: void set_pattern(const std::string &pattern) final; void set_formatter(std::unique_ptr sink_formatter) override; bool should_color(); + void set_color_mode(color_mode mode); /// Formatting codes const std::string reset = "\033[m"; @@ -91,4 +92,4 @@ using ansicolor_stderr_sink_st = ansicolor_sink -SPDLOG_INLINE std::shared_ptr stdout_color_mt(const std::string &logger_name) +SPDLOG_INLINE std::shared_ptr stdout_color_mt(const std::string &logger_name, color_mode mode) { - return Factory::template create(logger_name); + return Factory::template create(logger_name, mode); } template -SPDLOG_INLINE std::shared_ptr stdout_color_st(const std::string &logger_name) +SPDLOG_INLINE std::shared_ptr stdout_color_st(const std::string &logger_name, color_mode mode) { - return Factory::template create(logger_name); + return Factory::template create(logger_name, mode); } template -SPDLOG_INLINE std::shared_ptr stderr_color_mt(const std::string &logger_name) +SPDLOG_INLINE std::shared_ptr stderr_color_mt(const std::string &logger_name, color_mode mode) { - return Factory::template create(logger_name); + return Factory::template create(logger_name, mode); } template -SPDLOG_INLINE std::shared_ptr stderr_color_st(const std::string &logger_name) +SPDLOG_INLINE std::shared_ptr stderr_color_st(const std::string &logger_name, color_mode mode) { - return Factory::template create(logger_name); + return Factory::template create(logger_name, mode); } } // namespace spdlog \ No newline at end of file diff --git a/include/spdlog/sinks/stdout_color_sinks.h b/include/spdlog/sinks/stdout_color_sinks.h index fe1452e5..7812f5e2 100644 --- a/include/spdlog/sinks/stdout_color_sinks.h +++ b/include/spdlog/sinks/stdout_color_sinks.h @@ -29,19 +29,19 @@ using stderr_color_sink_st = ansicolor_stderr_sink_st; } // namespace sinks template -std::shared_ptr stdout_color_mt(const std::string &logger_name); +std::shared_ptr stdout_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic); template -std::shared_ptr stdout_color_st(const std::string &logger_name); +std::shared_ptr stdout_color_st(const std::string &logger_name, color_mode mode = color_mode::automatic); template -std::shared_ptr stderr_color_mt(const std::string &logger_name); +std::shared_ptr stderr_color_mt(const std::string &logger_name, color_mode mode = color_mode::automatic); template -std::shared_ptr stderr_color_st(const std::string &logger_name); +std::shared_ptr stderr_color_st(const std::string &logger_name, color_mode mode = color_mode::automatic); } // namespace spdlog #ifdef SPDLOG_HEADER_ONLY #include "stdout_color_sinks-inl.h" -#endif \ No newline at end of file +#endif diff --git a/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h index 912f2a6f..e107d1a2 100644 --- a/include/spdlog/sinks/wincolor_sink-inl.h +++ b/include/spdlog/sinks/wincolor_sink-inl.h @@ -13,10 +13,11 @@ namespace spdlog { namespace sinks { template -SPDLOG_INLINE wincolor_sink::wincolor_sink() +SPDLOG_INLINE wincolor_sink::wincolor_sink(color_mode mode) : out_handle_(TargetStream::handle()) , mutex_(ConsoleMutex::mutex()) { + set_color_mode(mode); colors_[level::trace] = WHITE; colors_[level::debug] = CYAN; colors_[level::info] = GREEN; @@ -46,7 +47,7 @@ void SPDLOG_INLINE wincolor_sink::log(const details: 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); @@ -59,7 +60,7 @@ void SPDLOG_INLINE wincolor_sink::log(const details: // 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()); } @@ -85,6 +86,23 @@ void SPDLOG_INLINE wincolor_sink::set_formatter(std: formatter_ = std::move(sink_formatter); } +template +void SPDLOG_INLINE wincolor_sink::set_color_mode(color_mode mode) +{ + switch (mode) + { + case color_mode::always: + case color_mode::automatic: + should_do_colors_ = true; + break; + case color_mode::never: + should_do_colors_ = false; + break; + default: + should_do_colors_ = true; + } +} + // set color and return the orig console attributes (for resetting later) template WORD SPDLOG_INLINE wincolor_sink::set_console_attribs(WORD attribs) diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index 29319fd9..de1a02ed 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -35,7 +35,7 @@ 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); ~wincolor_sink() override; wincolor_sink(const wincolor_sink &other) = delete; @@ -47,11 +47,13 @@ public: void flush() final override; void set_pattern(const std::string &pattern) override final; void set_formatter(std::unique_ptr sink_formatter) override final; + void set_color_mode(color_mode mode); private: using mutex_t = typename ConsoleMutex::mutex_t; HANDLE out_handle_; mutex_t &mutex_; + bool should_do_colors_; std::unordered_map colors_; // set color and return the orig console attributes (for resetting later) @@ -71,4 +73,4 @@ using wincolor_stderr_sink_st = wincolor_sink spdlog::stdout_color_mt(const std::string &logger_name); -template std::shared_ptr spdlog::stdout_color_st(const std::string &logger_name); -template std::shared_ptr spdlog::stderr_color_mt(const std::string &logger_name); -template std::shared_ptr spdlog::stderr_color_st(const std::string &logger_name); +template std::shared_ptr spdlog::stdout_color_mt(const std::string &logger_name, color_mode mode); +template std::shared_ptr spdlog::stdout_color_st(const std::string &logger_name, color_mode mode); +template std::shared_ptr spdlog::stderr_color_mt(const std::string &logger_name, color_mode mode); +template std::shared_ptr spdlog::stderr_color_st(const std::string &logger_name, color_mode mode); -template std::shared_ptr spdlog::stdout_color_mt(const std::string &logger_name); -template std::shared_ptr spdlog::stdout_color_st(const std::string &logger_name); -template std::shared_ptr spdlog::stderr_color_mt(const std::string &logger_name); -template std::shared_ptr spdlog::stderr_color_st(const std::string &logger_name); +template std::shared_ptr spdlog::stdout_color_mt(const std::string &logger_name, color_mode mode); +template std::shared_ptr spdlog::stdout_color_st(const std::string &logger_name, color_mode mode); +template std::shared_ptr spdlog::stderr_color_mt(const std::string &logger_name, color_mode mode); +template std::shared_ptr spdlog::stderr_color_st(const std::string &logger_name, color_mode mode); // Slightly modified version of fmt lib's format.cc source file. // Copyright (c) 2012 - 2016, Victor Zverovich