Improved millis formatting

This commit is contained in:
gabime 2018-07-25 23:33:03 +03:00
parent d8053dd6a6
commit a12a21a18e
3 changed files with 24 additions and 56 deletions

View File

@ -68,11 +68,18 @@ inline void pad2(int n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
template<size_t Buffer_Size>
inline void pad3(int n, fmt::basic_memory_buffer<char, Buffer_Size> &dest)
{
if (n > 99)
if (n > 999)
{
append_int(n, dest);
return;
}
if (n > 99) // 100-999
{
append_int(n / 100, dest);
pad2(n % 100, dest);
return;
}
if (n > 9) // 10-99
{
dest.push_back('0');
@ -94,46 +101,13 @@ inline void pad3(int n, fmt::basic_memory_buffer<char, Buffer_Size> &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
// pad3(n / 1000, dest);
// pad3(n % 1000, 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 // 0-9
{
dest.push_back('0');
dest.push_back('0');
dest.push_back('0');
dest.push_back('0');
dest.push_back('0');
}
append_int(n, dest);
pad3(static_cast<int>(n / 1000), dest);
pad3(static_cast<int>(n % 1000), dest);
}
} // namespace fmt_helper

View File

@ -69,7 +69,7 @@ static const char *ampm(const tm &t)
return t.tm_hour >= 12 ? "PM" : "AM";
}
static int to12h(const tm &t)
static unsigned int to12h(const tm &t)
{
return t.tm_hour > 12 ? t.tm_hour - 12 : t.tm_hour;
}
@ -231,9 +231,11 @@ 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 millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
fmt_helper::pad3(static_cast<int>(millis), dest);
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);
}
};
@ -464,13 +466,14 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
{
void format(const details::log_msg &msg, const std::tm &tm_time, fmt::memory_buffer &dest) override
{
using namespace std::chrono;
#ifndef SPDLOG_NO_DATETIME
// cache the date/time part for the next second.
auto duration = msg.time.time_since_epoch();
std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(duration);
auto secs = duration_cast<seconds>(duration);
if (cache_timestamp_ != seconds || cached_datetime_.size() == 0)
if (cache_timestamp_ != secs || cached_datetime_.size() == 0)
{
cached_datetime_.resize(0);
cached_datetime_.push_back('[');
@ -492,22 +495,15 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
fmt_helper::pad2(tm_time.tm_sec, cached_datetime_);
cached_datetime_.push_back('.');
cache_timestamp_ = seconds;
cache_timestamp_ = secs;
}
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;
if (millis != millis_cache_timestamp_ || cached_millis_.size() == 0)
{
cached_millis_.resize(0);
fmt_helper::pad3(static_cast<int>(millis), cached_millis_);
cached_millis_.push_back(']');
cached_millis_.push_back(' ');
millis_cache_timestamp_ = millis;
}
auto millis = duration_cast<milliseconds>(duration) - duration_cast<milliseconds>(secs);
fmt_helper::pad3(static_cast<int>(millis.count()), dest);
dest.push_back(']');
dest.push_back(' ');
fmt_helper::append_buf(cached_millis_, dest);
#else // no datetime needed
(void)tm_time;
#endif
@ -531,9 +527,7 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
private:
std::chrono::seconds cache_timestamp_{0};
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

View File

@ -108,7 +108,7 @@ class thread_pool
{
public:
using item_type = async_msg;
using q_type = details::mpmc_blocking_queue<item_type>;
using q_type = details::mpmc_blocking_queue<item_type>;
thread_pool(size_t q_max_items, size_t threads_n)
: q_(q_max_items)