pad6 thread id and micros in formatter

This commit is contained in:
gabime 2018-07-04 01:41:05 +03:00
parent 94a7152afc
commit 1f801828a5
2 changed files with 50 additions and 4 deletions

View File

@ -86,6 +86,51 @@ inline void pad3(int n, fmt::memory_buffer &dest)
fmt::format_to(dest, "{:03}", n); 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 fmt_helper
} // namespace details } // namespace details
} // namespace spdlog } // namespace spdlog

View File

@ -235,7 +235,7 @@ class f_formatter SPDLOG_FINAL : public flag_formatter
{ {
auto duration = msg.time.time_since_epoch(); auto duration = msg.time.time_since_epoch();
auto micros = std::chrono::duration_cast<std::chrono::microseconds>(duration).count() % 1000000; auto micros = std::chrono::duration_cast<std::chrono::microseconds>(duration).count() % 1000000;
fmt::format_to(dest, "{:06}", static_cast<int>(micros)); fmt_helper::pad6(static_cast<int>(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 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 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);
} }
}; };