mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-15 16:35:45 +08:00
cache millis in full_formatter
This commit is contained in:
parent
374a22b4b9
commit
a776a774e1
@ -8,13 +8,16 @@
|
|||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
namespace fmt_helper {
|
namespace fmt_helper {
|
||||||
inline void append_str(const std::string &str, fmt::memory_buffer &dest)
|
|
||||||
|
template <size_t Buffer_Size>
|
||||||
|
inline void append_str(const std::string &str, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
|
||||||
{
|
{
|
||||||
auto *str_ptr = str.data();
|
auto *str_ptr = str.data();
|
||||||
dest.append(str_ptr, str_ptr + str.size());
|
dest.append(str_ptr, str_ptr + str.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void append_c_str(const char *c_str, fmt::memory_buffer &dest)
|
template <size_t Buffer_Size>
|
||||||
|
inline void append_c_str(const char *c_str, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
|
||||||
{
|
{
|
||||||
char ch;
|
char ch;
|
||||||
while ((ch = *c_str) != '\0')
|
while ((ch = *c_str) != '\0')
|
||||||
@ -24,21 +27,22 @@ inline void append_c_str(const char *c_str, fmt::memory_buffer &dest)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<size_t N1, size_t N2>
|
template<size_t Buffer_Size1, size_t Buffer_Size2>
|
||||||
inline void append_buf(const fmt::basic_memory_buffer<char, N1> &buf, fmt::basic_memory_buffer<char, N2> &dest)
|
inline void append_buf(const fmt::basic_memory_buffer<char, Buffer_Size1> &buf, fmt::basic_memory_buffer<char, Buffer_Size2> &dest)
|
||||||
{
|
{
|
||||||
auto *buf_ptr = buf.data();
|
auto *buf_ptr = buf.data();
|
||||||
dest.append(buf_ptr, buf_ptr + buf.size());
|
dest.append(buf_ptr, buf_ptr + buf.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T, size_t Buffer_Size>
|
||||||
inline void append_int(T n, fmt::memory_buffer &dest)
|
inline void append_int(T n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
|
||||||
{
|
{
|
||||||
fmt::format_int i(n);
|
fmt::format_int i(n);
|
||||||
dest.append(i.data(), i.data() + i.size());
|
dest.append(i.data(), i.data() + i.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void pad2(int n, fmt::memory_buffer &dest)
|
template <size_t Buffer_Size>
|
||||||
|
inline void pad2(int n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
|
||||||
{
|
{
|
||||||
if (n > 99)
|
if (n > 99)
|
||||||
{
|
{
|
||||||
@ -61,7 +65,8 @@ inline void pad2(int n, fmt::memory_buffer &dest)
|
|||||||
fmt::format_to(dest, "{:02}", n);
|
fmt::format_to(dest, "{:02}", n);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void pad3(int n, fmt::memory_buffer &dest)
|
template <size_t Buffer_Size>
|
||||||
|
inline void pad3(int n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
|
||||||
{
|
{
|
||||||
if (n > 99)
|
if (n > 99)
|
||||||
{
|
{
|
||||||
@ -86,7 +91,8 @@ inline void pad3(int n, fmt::memory_buffer &dest)
|
|||||||
fmt::format_to(dest, "{:03}", n);
|
fmt::format_to(dest, "{:03}", n);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void pad6(size_t n, fmt::memory_buffer &dest)
|
template <size_t Buffer_Size>
|
||||||
|
inline void pad6(size_t n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
|
||||||
{
|
{
|
||||||
// todo: maybe replace this implementation with
|
// todo: maybe replace this implementation with
|
||||||
// pad3(n / 1000, dest);
|
// pad3(n / 1000, dest);
|
||||||
|
@ -120,7 +120,16 @@ class c_formatter SPDLOG_FINAL : public flag_formatter
|
|||||||
{
|
{
|
||||||
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
|
void format(const details::log_msg &, const std::tm &tm_time, fmt::memory_buffer &dest) override
|
||||||
{
|
{
|
||||||
fmt::format_to(dest, "{} {} {} ", days[tm_time.tm_wday], months[tm_time.tm_mon], tm_time.tm_mday); //
|
//fmt::format_to(dest, "{} {} {} ", days[tm_time.tm_wday], months[tm_time.tm_mon], tm_time.tm_mday);
|
||||||
|
// date
|
||||||
|
fmt_helper::append_str(days[tm_time.tm_wday], dest);
|
||||||
|
dest.push_back(' ');
|
||||||
|
fmt_helper::append_str(months[tm_time.tm_mon], dest);
|
||||||
|
dest.push_back(' ');
|
||||||
|
fmt_helper::append_int(tm_time.tm_mday, dest);
|
||||||
|
dest.push_back(' ');
|
||||||
|
// time
|
||||||
|
|
||||||
fmt_helper::pad2(tm_time.tm_hour, dest);
|
fmt_helper::pad2(tm_time.tm_hour, dest);
|
||||||
dest.push_back(':');
|
dest.push_back(':');
|
||||||
fmt_helper::pad2(tm_time.tm_min, dest);
|
fmt_helper::pad2(tm_time.tm_min, dest);
|
||||||
@ -366,7 +375,6 @@ 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_helper::pad6(msg.thread_id, dest);
|
fmt_helper::pad6(msg.thread_id, dest);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -385,7 +393,6 @@ 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_helper::pad6(msg.msg_id, dest);
|
fmt_helper::pad6(msg.msg_id, dest);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -457,39 +464,48 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
|
|||||||
{
|
{
|
||||||
#ifndef SPDLOG_NO_DATETIME
|
#ifndef SPDLOG_NO_DATETIME
|
||||||
|
|
||||||
// each second cache the header
|
// cache the date/time part for the next second.
|
||||||
auto duration = msg.time.time_since_epoch();
|
auto duration = msg.time.time_since_epoch();
|
||||||
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration).count();
|
std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(duration);
|
||||||
if (cached_header_.size() == 0 || cached_seconds_ts_ != seconds)
|
|
||||||
|
if (cache_timestamp_ != seconds || cached_datetime_.size() == 0)
|
||||||
{
|
{
|
||||||
cached_header_.resize(0);
|
cached_datetime_.resize(0);
|
||||||
cached_header_.push_back('[');
|
cached_datetime_.push_back('[');
|
||||||
fmt_helper::append_int(tm_time.tm_year + 1900, cached_header_);
|
fmt_helper::append_int(tm_time.tm_year + 1900, cached_datetime_);
|
||||||
cached_header_.push_back('-');
|
cached_datetime_.push_back('-');
|
||||||
|
|
||||||
fmt_helper::pad2(tm_time.tm_mon + 1, cached_header_);
|
fmt_helper::pad2(tm_time.tm_mon + 1, cached_datetime_);
|
||||||
cached_header_.push_back('-');
|
cached_datetime_.push_back('-');
|
||||||
|
|
||||||
fmt_helper::pad2(tm_time.tm_mday, cached_header_);
|
fmt_helper::pad2(tm_time.tm_mday, cached_datetime_);
|
||||||
cached_header_.push_back(' ');
|
cached_datetime_.push_back(' ');
|
||||||
|
|
||||||
fmt_helper::pad2(tm_time.tm_hour, cached_header_);
|
fmt_helper::pad2(tm_time.tm_hour, cached_datetime_);
|
||||||
cached_header_.push_back(':');
|
cached_datetime_.push_back(':');
|
||||||
|
|
||||||
fmt_helper::pad2(tm_time.tm_min, cached_header_);
|
fmt_helper::pad2(tm_time.tm_min, cached_datetime_);
|
||||||
cached_header_.push_back(':');
|
cached_datetime_.push_back(':');
|
||||||
|
|
||||||
fmt_helper::pad2(tm_time.tm_sec, cached_header_);
|
fmt_helper::pad2(tm_time.tm_sec, cached_datetime_);
|
||||||
cached_header_.push_back('.');
|
cached_datetime_.push_back('.');
|
||||||
|
|
||||||
cached_seconds_ts_ = seconds;
|
cache_timestamp_ = seconds;
|
||||||
}
|
}
|
||||||
fmt_helper::append_buf(cached_header_, dest);
|
fmt_helper::append_buf(cached_datetime_, dest);
|
||||||
|
|
||||||
|
// cache the millis part for the next milli.
|
||||||
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
|
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
|
||||||
fmt_helper::pad3(static_cast<int>(millis), dest);
|
if(millis != millis_cache_timestamp_ || cached_millis_.size() == 0)
|
||||||
dest.push_back(']');
|
{
|
||||||
dest.push_back(' ');
|
cached_millis_.resize(0);
|
||||||
|
fmt_helper::pad3(millis, cached_millis_);
|
||||||
|
cached_millis_.push_back(']');
|
||||||
|
cached_millis_.push_back(' ');
|
||||||
|
millis_cache_timestamp_ = millis;
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt_helper::append_buf(cached_millis_, dest);
|
||||||
#else // no datetime needed
|
#else // no datetime needed
|
||||||
(void)tm_time;
|
(void)tm_time;
|
||||||
#endif
|
#endif
|
||||||
@ -512,8 +528,10 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::chrono::seconds::rep cached_seconds_ts_{0};
|
std::chrono::seconds cache_timestamp_ {0};
|
||||||
fmt::memory_buffer cached_header_;
|
std::chrono::milliseconds::rep millis_cache_timestamp_ {0};
|
||||||
|
fmt::basic_memory_buffer<char, 128> cached_datetime_;
|
||||||
|
fmt::basic_memory_buffer<char, 8> cached_millis_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace details
|
} // namespace details
|
||||||
|
Loading…
Reference in New Issue
Block a user