Use fmt::pad for faster formatting of the default format pattern

This commit is contained in:
gabi 2014-12-01 18:25:42 +02:00
parent 45628c8ec3
commit c5afdbddcf

View File

@ -371,18 +371,32 @@ class full_formatter :public flag_formatter
{ {
auto duration = msg.time.time_since_epoch(); auto duration = msg.time.time_since_epoch();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000; auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
msg.formatted.write("[{:d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}.{:03d}] [{}] [{}] ",
msg.tm_time.tm_year + 1900,
msg.tm_time.tm_mon + 1,
msg.tm_time.tm_mday,
msg.tm_time.tm_hour,
msg.tm_time.tm_min,
msg.tm_time.tm_sec,
static_cast<int>(millis),
msg.logger_name,
level::to_str(msg.level));
msg.formatted.write(msg.raw.str());
/* Slower version(while still very fast - about 3.2 million lines/sec),
msg.formatted.write("[{:d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}.{:03d}] [{}] [{}] {} ",
msg.tm_time.tm_year + 1900,
msg.tm_time.tm_mon + 1,
msg.tm_time.tm_mday,
msg.tm_time.tm_hour,
msg.tm_time.tm_min,
msg.tm_time.tm_sec,
static_cast<int>(millis),
msg.logger_name,
level::to_str(msg.level),
msg.raw.str());*/
// Faster (albeit uglier) way to format the line (5.6 million lines/sec)
msg.formatted << '[' << msg.tm_time.tm_year + 1900 << '-'
<< fmt::pad(msg.tm_time.tm_mon + 1, 2, '0') << '-'
<< fmt::pad(msg.tm_time.tm_mday, 2, '0') << ' '
<< fmt::pad(msg.tm_time.tm_hour, 2, '0') << ':'
<< fmt::pad(msg.tm_time.tm_min, 2, '0') << ':'
<< fmt::pad(msg.tm_time.tm_sec, 2, '0') << '.'
<< fmt::pad(static_cast<int>(millis), 3, '0') << "] ";
msg.formatted << '[' << msg.logger_name << "] [" << level::to_str(msg.level) << "] ";
msg.formatted.write(msg.raw.data(), msg.raw.size());
} }
}; };
@ -429,7 +443,7 @@ inline void spdlog::pattern_formatter::handle_flag(char flag)
{ {
switch (flag) switch (flag)
{ {
// logger name // logger name
case 'n': case 'n':
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::name_formatter())); _formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::name_formatter()));
break; break;