From 38929f856db663ec2abb938bb5cb88b24ed2c9b8 Mon Sep 17 00:00:00 2001 From: Jeeyong Um Date: Sat, 7 May 2022 20:39:10 +0800 Subject: [PATCH 1/3] Allow overriding need_localtime for custom formatter --- include/spdlog/pattern_formatter-inl.h | 5 ++++ include/spdlog/pattern_formatter.h | 1 + tests/test_pattern_formatter.cpp | 35 +++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h index bf9da079..0d36fc85 100644 --- a/include/spdlog/pattern_formatter-inl.h +++ b/include/spdlog/pattern_formatter-inl.h @@ -1081,6 +1081,11 @@ SPDLOG_INLINE void pattern_formatter::set_pattern(std::string pattern) compile_pattern_(pattern_); } +SPDLOG_INLINE void pattern_formatter::need_localtime(bool need) +{ + need_localtime_ = need; +} + SPDLOG_INLINE std::tm pattern_formatter::get_time_(const details::log_msg &msg) { if (pattern_time_type_ == pattern_time_type::local) diff --git a/include/spdlog/pattern_formatter.h b/include/spdlog/pattern_formatter.h index 8bcf8e2a..acf1c536 100644 --- a/include/spdlog/pattern_formatter.h +++ b/include/spdlog/pattern_formatter.h @@ -98,6 +98,7 @@ public: return *this; } void set_pattern(std::string pattern); + void need_localtime(bool need = true); private: std::string pattern_; diff --git a/tests/test_pattern_formatter.cpp b/tests/test_pattern_formatter.cpp index e4ea6cc3..4d8459b2 100644 --- a/tests/test_pattern_formatter.cpp +++ b/tests/test_pattern_formatter.cpp @@ -330,12 +330,18 @@ public: : some_txt{std::move(txt)} {} - void format(const spdlog::details::log_msg &, const std::tm &, spdlog::memory_buf_t &dest) override + void format(const spdlog::details::log_msg &, const std::tm &tm, spdlog::memory_buf_t &dest) override { if (some_txt == "throw_me") { throw spdlog::spdlog_ex("custom_flag_exception_test"); } + else if (some_txt == "time") + { + auto formatted = spdlog::fmt_lib::format("{:d}:{:02d}{:s}", tm.tm_hour % 12, tm.tm_min, tm.tm_hour / 12 ? "PM" : "AM"); + dest.append(formatted.data(), formatted.data() + formatted.size()); + return; + } some_txt = std::string(padinfo_.width_, ' ') + some_txt; dest.append(some_txt.data(), some_txt.data() + some_txt.size()); } @@ -466,3 +472,30 @@ TEST_CASE("custom flags-exception", "[pattern_formatter]") spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message"); CHECK_THROWS_AS(formatter->format(msg, formatted), spdlog::spdlog_ex); } + +TEST_CASE("override need_localtime", "[pattern_formatter]") +{ + auto formatter = std::make_shared(); + formatter->add_flag('t', "time").set_pattern("%t> %v"); + + { + memory_buf_t formatted; + spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message"); + formatter->format(msg, formatted); + REQUIRE(to_string_view(formatted) == "0:00AM> some message\n"); + } + + { + formatter->need_localtime(); + + auto now_tm = spdlog::details::os::localtime(); + std::stringstream oss; + oss << (now_tm.tm_hour % 12) << ":" << std::setfill('0') << std::setw(2) << now_tm.tm_min << (now_tm.tm_hour / 12 ? "PM" : "AM") + << "> some message\n"; + + memory_buf_t formatted; + spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message"); + formatter->format(msg, formatted); + REQUIRE(to_string_view(formatted) == oss.str()); + } +} From c03c925e295a7e67c64afedaf28dfb7f354cedb1 Mon Sep 17 00:00:00 2001 From: Jeeyong Um Date: Sun, 8 May 2022 01:16:31 +0800 Subject: [PATCH 2/3] Copy the value of need_localtime when cloning pattern_formatter --- include/spdlog/pattern_formatter-inl.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h index 0d36fc85..2559454b 100644 --- a/include/spdlog/pattern_formatter-inl.h +++ b/include/spdlog/pattern_formatter-inl.h @@ -1051,7 +1051,9 @@ SPDLOG_INLINE std::unique_ptr pattern_formatter::clone() const { cloned_custom_formatters[it.first] = it.second->clone(); } - return details::make_unique(pattern_, pattern_time_type_, eol_, std::move(cloned_custom_formatters)); + auto cloned = details::make_unique(pattern_, pattern_time_type_, eol_, std::move(cloned_custom_formatters)); + cloned->need_localtime(need_localtime_); + return cloned; } SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory_buf_t &dest) From aa7490d18784df307533c8486d9ab4dadb897dad Mon Sep 17 00:00:00 2001 From: Jeeyong Um Date: Sun, 8 May 2022 01:18:08 +0800 Subject: [PATCH 3/3] Set eol to the test for overriding need_localtime --- tests/test_pattern_formatter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_pattern_formatter.cpp b/tests/test_pattern_formatter.cpp index 4d8459b2..76cac855 100644 --- a/tests/test_pattern_formatter.cpp +++ b/tests/test_pattern_formatter.cpp @@ -475,7 +475,7 @@ TEST_CASE("custom flags-exception", "[pattern_formatter]") TEST_CASE("override need_localtime", "[pattern_formatter]") { - auto formatter = std::make_shared(); + auto formatter = std::make_shared(spdlog::pattern_time_type::local, "\n"); formatter->add_flag('t', "time").set_pattern("%t> %v"); {