mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-25 10:01:33 +08:00
Some code refactoring in formatter
This commit is contained in:
parent
a12a21a18e
commit
de6ddf4e2a
@ -4,6 +4,9 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "chrono"
|
||||||
|
#include "spdlog/fmt/fmt.h"
|
||||||
|
|
||||||
// Some fmt helpers to efficiently format and pad ints and strings
|
// Some fmt helpers to efficiently format and pad ints and strings
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
@ -110,6 +113,18 @@ inline void pad6(size_t n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
|
|||||||
pad3(static_cast<int>(n % 1000), dest);
|
pad3(static_cast<int>(n % 1000), dest);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return fraction of a second of the given time_point.
|
||||||
|
// e.g.
|
||||||
|
// fraction<std::milliseconds>(tp) -> will return the millis part of the second
|
||||||
|
template<typename ToDuration>
|
||||||
|
ToDuration time_fraction(const log_clock::time_point &tp)
|
||||||
|
{
|
||||||
|
using namespace std::chrono;
|
||||||
|
auto duration = tp.time_since_epoch();
|
||||||
|
auto secs = duration_cast<seconds>(duration);
|
||||||
|
return duration_cast<ToDuration>(duration) - duration_cast<ToDuration>(secs);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace fmt_helper
|
} // namespace fmt_helper
|
||||||
} // namespace details
|
} // namespace details
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
@ -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
|
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
|
||||||
{
|
{
|
||||||
using namespace std::chrono;
|
auto millis = fmt_helper::time_fraction<std::chrono::milliseconds>(msg.time);
|
||||||
auto duration = msg.time.time_since_epoch();
|
|
||||||
auto secs = duration_cast<seconds>(duration);
|
|
||||||
auto millis = duration_cast<milliseconds>(duration) - duration_cast<milliseconds>(secs);
|
|
||||||
fmt_helper::pad3(static_cast<int>(millis.count()), dest);
|
fmt_helper::pad3(static_cast<int>(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
|
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
|
||||||
{
|
{
|
||||||
auto duration = msg.time.time_since_epoch();
|
auto micros = fmt_helper::time_fraction<std::chrono::microseconds>(msg.time);
|
||||||
auto micros = std::chrono::duration_cast<std::chrono::microseconds>(duration).count() % 1000000;
|
fmt_helper::pad6(static_cast<int>(micros.count()), dest);
|
||||||
fmt_helper::pad6(static_cast<int>(micros), 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
|
void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override
|
||||||
{
|
{
|
||||||
auto duration = msg.time.time_since_epoch();
|
auto ns = fmt_helper::time_fraction<std::chrono::nanoseconds>(msg.time);
|
||||||
auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count() % 1000000000;
|
fmt::format_to(dest, "{:09}", ns.count());
|
||||||
fmt::format_to(dest, "{:09}", ns);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// seconds since epoch
|
||||||
class E_formatter SPDLOG_FINAL : public flag_formatter
|
class E_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
|
||||||
@ -499,7 +495,7 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
|
|||||||
}
|
}
|
||||||
fmt_helper::append_buf(cached_datetime_, dest);
|
fmt_helper::append_buf(cached_datetime_, dest);
|
||||||
|
|
||||||
auto millis = duration_cast<milliseconds>(duration) - duration_cast<milliseconds>(secs);
|
auto millis = fmt_helper::time_fraction<milliseconds>(msg.time);
|
||||||
fmt_helper::pad3(static_cast<int>(millis.count()), dest);
|
fmt_helper::pad3(static_cast<int>(millis.count()), dest);
|
||||||
dest.push_back(']');
|
dest.push_back(']');
|
||||||
dest.push_back(' ');
|
dest.push_back(' ');
|
||||||
|
Loading…
Reference in New Issue
Block a user