diff --git a/include/spdlog/details/fmt_helper.h b/include/spdlog/details/fmt_helper.h index 5206eb9f..9f356a7d 100644 --- a/include/spdlog/details/fmt_helper.h +++ b/include/spdlog/details/fmt_helper.h @@ -86,6 +86,51 @@ inline void pad3(int n, fmt::memory_buffer &dest) fmt::format_to(dest, "{:03}", n); } +inline void pad6(int n, fmt::memory_buffer &dest) +{ + if (n > 99999) + { + append_int(n, dest); + return; + } + if (n > 9999) + { + dest.push_back('0'); + } + else if (n > 999) + { + dest.push_back('0'); + dest.push_back('0'); + } + else if (n > 99) + { + dest.push_back('0'); + dest.push_back('0'); + dest.push_back('0'); + } + else if (n > 9) + { + dest.push_back('0'); + dest.push_back('0'); + dest.push_back('0'); + dest.push_back('0'); + } + else if (n >= 0) + { + dest.push_back('0'); + dest.push_back('0'); + dest.push_back('0'); + dest.push_back('0'); + dest.push_back('0'); + } + else // negatives (unlikely, but just in case let fmt deal with it) + { + fmt::format_to(dest, "{:06}", n); + return; + } + append_int(n, dest); +} + } // namespace fmt_helper } // namespace details } // namespace spdlog \ No newline at end of file diff --git a/include/spdlog/details/pattern_formatter_impl.h b/include/spdlog/details/pattern_formatter_impl.h index 541ca944..942421eb 100644 --- a/include/spdlog/details/pattern_formatter_impl.h +++ b/include/spdlog/details/pattern_formatter_impl.h @@ -235,7 +235,7 @@ class f_formatter SPDLOG_FINAL : public flag_formatter { auto duration = msg.time.time_since_epoch(); auto micros = std::chrono::duration_cast(duration).count() % 1000000; - fmt::format_to(dest, "{:06}", static_cast(micros)); + fmt_helper::pad6(static_cast(micros), dest); } }; @@ -371,8 +371,8 @@ class t_formatter SPDLOG_FINAL : public flag_formatter { void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override { - // fmt::format_to(dest, "{}", msg.thread_id); - fmt_helper::append_int(msg.thread_id, dest); + + fmt_helper::pad6(msg.thread_id, dest); } }; @@ -390,7 +390,8 @@ class i_formatter SPDLOG_FINAL : public flag_formatter { void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override { - fmt::format_to(dest, "{:06}", msg.msg_id); + // fmt::format_to(dest, "{:06}", msg.msg_id); + fmt_helper::pad6(msg.msg_id, dest); } };