diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h index bf9da079..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) @@ -1081,6 +1083,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..76cac855 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(spdlog::pattern_time_type::local, "\n"); + 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()); + } +}