diff --git a/include/spdlog/details/fmt_helper.h b/include/spdlog/details/fmt_helper.h index 455cbc0a..d8abcf76 100644 --- a/include/spdlog/details/fmt_helper.h +++ b/include/spdlog/details/fmt_helper.h @@ -4,6 +4,9 @@ #pragma once +#include "chrono" +#include "spdlog/fmt/fmt.h" + // Some fmt helpers to efficiently format and pad ints and strings namespace spdlog { namespace details { @@ -110,6 +113,18 @@ inline void pad6(size_t n, fmt::basic_memory_buffer &dest) pad3(static_cast(n % 1000), dest); } +// return fraction of a second of the given time_point. +// e.g. +// fraction(tp) -> will return the millis part of the second +template +ToDuration time_fraction(const log_clock::time_point &tp) +{ + using namespace std::chrono; + auto duration = tp.time_since_epoch(); + auto secs = duration_cast(duration); + return duration_cast(duration) - duration_cast(secs); +} + } // namespace fmt_helper } // namespace details } // namespace spdlog \ No newline at end of file diff --git a/include/spdlog/details/pattern_formatter.h b/include/spdlog/details/pattern_formatter.h index 5c1804de..765f9305 100644 --- a/include/spdlog/details/pattern_formatter.h +++ b/include/spdlog/details/pattern_formatter.h @@ -231,10 +231,7 @@ class e_formatter SPDLOG_FINAL : public flag_formatter { void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override { - using namespace std::chrono; - auto duration = msg.time.time_since_epoch(); - auto secs = duration_cast(duration); - auto millis = duration_cast(duration) - duration_cast(secs); + auto millis = fmt_helper::time_fraction(msg.time); fmt_helper::pad3(static_cast(millis.count()), dest); } }; @@ -244,9 +241,8 @@ class f_formatter SPDLOG_FINAL : public flag_formatter { void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override { - auto duration = msg.time.time_since_epoch(); - auto micros = std::chrono::duration_cast(duration).count() % 1000000; - fmt_helper::pad6(static_cast(micros), dest); + auto micros = fmt_helper::time_fraction(msg.time); + fmt_helper::pad6(static_cast(micros.count()), dest); } }; @@ -255,12 +251,12 @@ class F_formatter SPDLOG_FINAL : public flag_formatter { void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override { - auto duration = msg.time.time_since_epoch(); - auto ns = std::chrono::duration_cast(duration).count() % 1000000000; - fmt::format_to(dest, "{:09}", ns); + auto ns = fmt_helper::time_fraction(msg.time); + fmt::format_to(dest, "{:09}", ns.count()); } }; +// seconds since epoch class E_formatter SPDLOG_FINAL : public flag_formatter { void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override @@ -499,7 +495,7 @@ class full_formatter SPDLOG_FINAL : public flag_formatter } fmt_helper::append_buf(cached_datetime_, dest); - auto millis = duration_cast(duration) - duration_cast(secs); + auto millis = fmt_helper::time_fraction(msg.time); fmt_helper::pad3(static_cast(millis.count()), dest); dest.push_back(']'); dest.push_back(' ');