From a984b1b073564e0d29b5ec540b127319ddb5752c Mon Sep 17 00:00:00 2001 From: Martin Green Date: Sun, 7 Apr 2019 17:12:41 +0300 Subject: [PATCH 1/7] Add a CMake option to use a header-only external fmt --- CMakeLists.txt | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f0951fc..3dd13060 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,12 +53,9 @@ option(SPDLOG_BUILD_EXAMPLES "Build examples" ${SPDLOG_MASTER_PROJECT}) option(SPDLOG_BUILD_BENCH "Build benchmarks (Requires https://github.com/google/benchmark.git to be installed)" OFF) option(SPDLOG_BUILD_TESTS "Build tests" ${SPDLOG_MASTER_PROJECT}) option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of bundled" OFF) +option(SPDLOG_FMT_HEADER_ONLY "Use header-only variant of external fmt library" OFF) option(SPDLOG_INSTALL "Generate the install target." ${SPDLOG_MASTER_PROJECT}) -if(SPDLOG_FMT_EXTERNAL AND NOT TARGET fmt::fmt) - find_package(fmt REQUIRED CONFIG) -endif() - target_include_directories( spdlog INTERFACE @@ -68,7 +65,16 @@ target_include_directories( if(SPDLOG_FMT_EXTERNAL) target_compile_definitions(spdlog INTERFACE SPDLOG_FMT_EXTERNAL) - target_link_libraries(spdlog INTERFACE fmt::fmt) + + if(NOT TARGET fmt::fmt) + find_package(fmt REQUIRED CONFIG) + endif() + + if(SPDLOG_FMT_HEADER_ONLY) + target_link_libraries(spdlog INTERFACE fmt::fmt-header-only) + else() + target_link_libraries(spdlog INTERFACE fmt::fmt) + endif() endif() set(HEADER_BASE "${CMAKE_CURRENT_SOURCE_DIR}/include") From 7e63d773ef85a3864094e505afdd505118c4f99d Mon Sep 17 00:00:00 2001 From: Martin Green Date: Wed, 10 Apr 2019 06:49:57 +0300 Subject: [PATCH 2/7] Make header-only external fmt the default --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3dd13060..12fe8bb5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,7 +53,7 @@ option(SPDLOG_BUILD_EXAMPLES "Build examples" ${SPDLOG_MASTER_PROJECT}) option(SPDLOG_BUILD_BENCH "Build benchmarks (Requires https://github.com/google/benchmark.git to be installed)" OFF) option(SPDLOG_BUILD_TESTS "Build tests" ${SPDLOG_MASTER_PROJECT}) option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of bundled" OFF) -option(SPDLOG_FMT_HEADER_ONLY "Use header-only variant of external fmt library" OFF) +option(SPDLOG_FMT_HEADER_ONLY "Use header-only variant of external fmt library" ON) option(SPDLOG_INSTALL "Generate the install target." ${SPDLOG_MASTER_PROJECT}) target_include_directories( From 79938b98da9cd70a77fab068fed0c1a2f53b2c58 Mon Sep 17 00:00:00 2001 From: Jan Niklas Hasse Date: Wed, 17 Apr 2019 15:18:16 +0200 Subject: [PATCH 3/7] Remove unnecessary semicolons (-Wextra-semi) --- include/spdlog/details/pattern_formatter.h | 42 +++++++++++----------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/include/spdlog/details/pattern_formatter.h b/include/spdlog/details/pattern_formatter.h index c0ad86e8..60ed72ad 100644 --- a/include/spdlog/details/pattern_formatter.h +++ b/include/spdlog/details/pattern_formatter.h @@ -352,7 +352,7 @@ class Y_formatter final : public flag_formatter { public: explicit Y_formatter(padding_info padinfo) - : flag_formatter(padinfo){}; + : flag_formatter(padinfo){} void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { @@ -418,7 +418,7 @@ class I_formatter final : public flag_formatter { public: explicit I_formatter(padding_info padinfo) - : flag_formatter(padinfo){}; + : flag_formatter(padinfo){} void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { @@ -433,7 +433,7 @@ class M_formatter final : public flag_formatter { public: explicit M_formatter(padding_info padinfo) - : flag_formatter(padinfo){}; + : flag_formatter(padinfo){} void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { @@ -448,7 +448,7 @@ class S_formatter final : public flag_formatter { public: explicit S_formatter(padding_info padinfo) - : flag_formatter(padinfo){}; + : flag_formatter(padinfo){} void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { @@ -463,7 +463,7 @@ class e_formatter final : public flag_formatter { public: explicit e_formatter(padding_info padinfo) - : flag_formatter(padinfo){}; + : flag_formatter(padinfo){} void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override { @@ -486,7 +486,7 @@ class f_formatter final : public flag_formatter { public: explicit f_formatter(padding_info padinfo) - : flag_formatter(padinfo){}; + : flag_formatter(padinfo){} void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override { @@ -509,7 +509,7 @@ class F_formatter final : public flag_formatter { public: explicit F_formatter(padding_info padinfo) - : flag_formatter(padinfo){}; + : flag_formatter(padinfo){} void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override { @@ -532,7 +532,7 @@ class E_formatter final : public flag_formatter { public: explicit E_formatter(padding_info padinfo) - : flag_formatter(padinfo){}; + : flag_formatter(padinfo){} void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override { @@ -549,7 +549,7 @@ class p_formatter final : public flag_formatter { public: explicit p_formatter(padding_info padinfo) - : flag_formatter(padinfo){}; + : flag_formatter(padinfo){} void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { @@ -564,7 +564,7 @@ class r_formatter final : public flag_formatter { public: explicit r_formatter(padding_info padinfo) - : flag_formatter(padinfo){}; + : flag_formatter(padinfo){} void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { @@ -586,7 +586,7 @@ class R_formatter final : public flag_formatter { public: explicit R_formatter(padding_info padinfo) - : flag_formatter(padinfo){}; + : flag_formatter(padinfo){} void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { @@ -604,7 +604,7 @@ class T_formatter final : public flag_formatter { public: explicit T_formatter(padding_info padinfo) - : flag_formatter(padinfo){}; + : flag_formatter(padinfo){} void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override { @@ -624,7 +624,7 @@ class z_formatter final : public flag_formatter { public: explicit z_formatter(padding_info padinfo) - : flag_formatter(padinfo){}; + : flag_formatter(padinfo){} const std::chrono::seconds cache_refresh = std::chrono::seconds(5); @@ -683,7 +683,7 @@ class t_formatter final : public flag_formatter { public: explicit t_formatter(padding_info padinfo) - : flag_formatter(padinfo){}; + : flag_formatter(padinfo){} void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override { @@ -705,7 +705,7 @@ class pid_formatter final : public flag_formatter { public: explicit pid_formatter(padding_info padinfo) - : flag_formatter(padinfo){}; + : flag_formatter(padinfo){} void format(const details::log_msg &, const std::tm &, fmt::memory_buffer &dest) override { @@ -728,7 +728,7 @@ class i_formatter final : public flag_formatter { public: explicit i_formatter(padding_info padinfo) - : flag_formatter(padinfo){}; + : flag_formatter(padinfo){} void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override { @@ -742,7 +742,7 @@ class v_formatter final : public flag_formatter { public: explicit v_formatter(padding_info padinfo) - : flag_formatter(padinfo){}; + : flag_formatter(padinfo){} void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override { @@ -829,7 +829,7 @@ class source_location_formatter final : public flag_formatter { public: explicit source_location_formatter(padding_info padinfo) - : flag_formatter(padinfo){}; + : flag_formatter(padinfo){} void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override { @@ -858,7 +858,7 @@ class source_filename_formatter final : public flag_formatter { public: explicit source_filename_formatter(padding_info padinfo) - : flag_formatter(padinfo){}; + : flag_formatter(padinfo){} void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override { @@ -875,7 +875,7 @@ class source_linenum_formatter final : public flag_formatter { public: explicit source_linenum_formatter(padding_info padinfo) - : flag_formatter(padinfo){}; + : flag_formatter(padinfo){} void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override { @@ -900,7 +900,7 @@ class source_funcname_formatter final : public flag_formatter { public: explicit source_funcname_formatter(padding_info padinfo) - : flag_formatter(padinfo){}; + : flag_formatter(padinfo){} void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override { From 5e856c6b4da77ca7d4c15dc494ffbfc489a50d48 Mon Sep 17 00:00:00 2001 From: Philip Salzmann Date: Thu, 9 May 2019 13:16:38 +0200 Subject: [PATCH 4/7] 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_; }; From 3ce9ac74a66cc19c652655c8c3aea526612820d2 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 10 May 2019 17:41:21 +0300 Subject: [PATCH 5/7] Mutex protect set_color_mode() --- include/spdlog/sinks/ansicolor_sink.h | 16 +++++++++++----- include/spdlog/sinks/wincolor_sink.h | 17 ++++++++++++----- 2 files changed, 23 insertions(+), 10 deletions(-) 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) { From e504aceeb5734568000f344540fc72b6be932c69 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Fri, 10 May 2019 17:59:48 +0300 Subject: [PATCH 6/7] Update wincolor_sink.h --- include/spdlog/sinks/wincolor_sink.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index 3c248192..7c51014b 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -123,10 +123,10 @@ private: case color_mode::always: case color_mode::automatic: should_do_colors_ = true; - break + break; case color_mode::never: should_do_colors_ = false; - break + break; } } From 74dbf4cf702b49c98642c9afe74d114a238a6a07 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 10 May 2019 18:46:13 +0300 Subject: [PATCH 7/7] mutex protect should_color() --- include/spdlog/sinks/ansicolor_sink.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index 905d270a..3fbbf27b 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -135,6 +135,7 @@ public: bool should_color() { + std::lock_guard lock(mutex_); return should_do_colors_; }