mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-12 17:00:25 +08:00
Allow overriding need_localtime for custom formatter
This commit is contained in:
parent
a9347017db
commit
38929f856d
@ -1081,6 +1081,11 @@ SPDLOG_INLINE void pattern_formatter::set_pattern(std::string pattern)
|
|||||||
compile_pattern_(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)
|
SPDLOG_INLINE std::tm pattern_formatter::get_time_(const details::log_msg &msg)
|
||||||
{
|
{
|
||||||
if (pattern_time_type_ == pattern_time_type::local)
|
if (pattern_time_type_ == pattern_time_type::local)
|
||||||
|
@ -98,6 +98,7 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
void set_pattern(std::string pattern);
|
void set_pattern(std::string pattern);
|
||||||
|
void need_localtime(bool need = true);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string pattern_;
|
std::string pattern_;
|
||||||
|
@ -330,12 +330,18 @@ public:
|
|||||||
: some_txt{std::move(txt)}
|
: 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")
|
if (some_txt == "throw_me")
|
||||||
{
|
{
|
||||||
throw spdlog::spdlog_ex("custom_flag_exception_test");
|
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;
|
some_txt = std::string(padinfo_.width_, ' ') + some_txt;
|
||||||
dest.append(some_txt.data(), some_txt.data() + some_txt.size());
|
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");
|
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);
|
CHECK_THROWS_AS(formatter->format(msg, formatted), spdlog::spdlog_ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("override need_localtime", "[pattern_formatter]")
|
||||||
|
{
|
||||||
|
auto formatter = std::make_shared<spdlog::pattern_formatter>();
|
||||||
|
formatter->add_flag<custom_test_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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user