Upgrade to fmt 5.x

This commit is contained in:
gabime
2018-06-12 18:48:22 +03:00
parent 378c7789ba
commit cdbf2e361b
23 changed files with 138 additions and 7338 deletions

View File

@@ -37,8 +37,8 @@ struct log_msg
level::level_enum level;
log_clock::time_point time;
size_t thread_id;
fmt::MemoryWriter raw;
fmt::MemoryWriter formatted;
fmt::memory_buffer raw;
fmt::memory_buffer formatted;
size_t msg_id{0};
// info about wrapping the formatted text with color
size_t color_range_start{0};

View File

@@ -58,12 +58,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *fmt, const Ar
try
{
details::log_msg log_msg(&name_, lvl);
#if defined(SPDLOG_FMT_PRINTF)
fmt::printf(log_msg.raw, fmt, args...);
#else
log_msg.raw.write(fmt, args...);
#endif
fmt::format_to(log_msg.raw, fmt, args...);
sink_it_(log_msg);
}
SPDLOG_CATCH_AND_HANDLE
@@ -79,7 +74,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *msg)
try
{
details::log_msg log_msg(&name_, lvl);
log_msg.raw << msg;
fmt::format_to(log_msg.raw, "{}", msg);
sink_it_(log_msg);
}
SPDLOG_CATCH_AND_HANDLE
@@ -95,7 +90,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const T &msg)
try
{
details::log_msg log_msg(&name_, lvl);
log_msg.raw << msg;
fmt::format_to(log_msg.raw, "{}", msg);
sink_it_(log_msg);
}
SPDLOG_CATCH_AND_HANDLE

View File

@@ -20,8 +20,23 @@
#include <utility>
#include <vector>
namespace {
inline void append_str(const std::string &str, fmt::memory_buffer &dest)
{
const char *str_ptr = str.data();
dest.append(str_ptr, str_ptr + str.size());
}
inline void append_buf(const fmt::memory_buffer &buf, fmt::memory_buffer &dest)
{
const char *buf_ptr = buf.data();
dest.append(buf_ptr, buf_ptr + buf.size());
}
} // namespace
namespace spdlog {
namespace details {
class flag_formatter
{
public:
@@ -36,7 +51,8 @@ class name_formatter : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &) override
{
msg.formatted << *msg.logger_name;
// fmt::format_to(msg.formatted, *msg.logger_name);
append_str(*msg.logger_name, msg.formatted);
}
};
@@ -45,7 +61,9 @@ class level_formatter : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &) override
{
msg.formatted << level::to_str(msg.level);
// msg.formatted << level::to_str(msg.level);
auto level_str = level::to_str(msg.level);
fmt::format_to(msg.formatted, "{}", level_str);
}
};
@@ -54,7 +72,9 @@ class short_level_formatter : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &) override
{
msg.formatted << level::to_short_str(msg.level);
// msg.formatted << level::to_short_str(msg.level);
auto level_str = level::to_short_str(msg.level);
fmt::format_to(msg.formatted, "{}", level_str);
}
};
@@ -78,7 +98,8 @@ class a_formatter : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << days[tm_time.tm_wday];
// msg.formatted << days[tm_time.tm_wday];
fmt::format_to(msg.formatted, "{}", days[tm_time.tm_wday]);
}
};
@@ -88,7 +109,8 @@ class A_formatter : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << full_days[tm_time.tm_wday];
// msg.formatted << full_days[tm_time.tm_wday];
fmt::format_to(msg.formatted, "{}", full_days[tm_time.tm_wday]);
}
};
@@ -98,7 +120,8 @@ class b_formatter : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << months[tm_time.tm_mon];
// msg.formatted << months[tm_time.tm_mon];
append_str(months[tm_time.tm_mon], msg.formatted);
}
};
@@ -109,31 +132,21 @@ class B_formatter : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << full_months[tm_time.tm_mon];
// msg.formatted << full_months[tm_time.tm_mon];
append_str(full_months[tm_time.tm_mon], msg.formatted);
}
};
// write 2 ints separated by sep with padding of 2
static fmt::MemoryWriter &pad_n_join(fmt::MemoryWriter &w, int v1, int v2, char sep)
{
w << fmt::pad(v1, 2, '0') << sep << fmt::pad(v2, 2, '0');
return w;
}
// write 3 ints separated by sep with padding of 2
static fmt::MemoryWriter &pad_n_join(fmt::MemoryWriter &w, int v1, int v2, int v3, char sep)
{
w << fmt::pad(v1, 2, '0') << sep << fmt::pad(v2, 2, '0') << sep << fmt::pad(v3, 2, '0');
return w;
}
// Date and time representation (Thu Aug 23 15:35:46 2014)
class c_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << days[tm_time.tm_wday] << ' ' << months[tm_time.tm_mon] << ' ' << tm_time.tm_mday << ' ';
pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, ':') << ' ' << tm_time.tm_year + 1900;
// msg.formatted << days[tm_time.tm_wday] << ' ' << months[tm_time.tm_mon] << ' ' << tm_time.tm_mday << ' ';
// pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, ':') << ' ' << tm_time.tm_year + 1900;
fmt::format_to(msg.formatted, "{} {} {} {:02}:{:02}:{:02} {}", days[tm_time.tm_wday], months[tm_time.tm_mon], tm_time.tm_mday,
tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, tm_time.tm_year + 1900);
}
};
@@ -142,7 +155,8 @@ class C_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << fmt::pad(tm_time.tm_year % 100, 2, '0');
// msg.formatted << fmt::pad(tm_time.tm_year % 100, 2, '0');
fmt::format_to(msg.formatted, "{:02}", tm_time.tm_year % 100);
}
};
@@ -151,7 +165,8 @@ class D_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &tm_time) override
{
pad_n_join(msg.formatted, tm_time.tm_mon + 1, tm_time.tm_mday, tm_time.tm_year % 100, '/');
// pad_n_join(msg.formatted, tm_time.tm_mon + 1, tm_time.tm_mday, tm_time.tm_year % 100, '/');
fmt::format_to(msg.formatted, "{:02}/{:02}/{:02}", tm_time.tm_mon + 1, tm_time.tm_mday, tm_time.tm_year % 100);
}
};
@@ -160,7 +175,8 @@ class Y_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << tm_time.tm_year + 1900;
// msg.formatted << tm_time.tm_year + 1900;
fmt::format_to(msg.formatted, "{}", tm_time.tm_year + 1900);
}
};
@@ -169,7 +185,8 @@ class m_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << fmt::pad(tm_time.tm_mon + 1, 2, '0');
// msg.formatted << fmt::pad(tm_time.tm_mon + 1, 2, '0');
fmt::format_to(msg.formatted, "{:02}", tm_time.tm_mon + 1);
}
};
@@ -178,7 +195,8 @@ class d_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << fmt::pad(tm_time.tm_mday, 2, '0');
// msg.formatted << fmt::pad(tm_time.tm_mday, 2, '0');
fmt::format_to(msg.formatted, "{:02}", tm_time.tm_mday);
}
};
@@ -187,7 +205,8 @@ class H_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << fmt::pad(tm_time.tm_hour, 2, '0');
// msg.formatted << fmt::pad(tm_time.tm_hour, 2, '0');
fmt::format_to(msg.formatted, "{:02}", tm_time.tm_hour);
}
};
@@ -196,7 +215,8 @@ class I_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << fmt::pad(to12h(tm_time), 2, '0');
// msg.formatted << fmt::pad(to12h(tm_time), 2, '0');
fmt::format_to(msg.formatted, "{:02}", to12h(tm_time));
}
};
@@ -205,7 +225,8 @@ class M_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << fmt::pad(tm_time.tm_min, 2, '0');
// msg.formatted << fmt::pad(tm_time.tm_min, 2, '0');
fmt::format_to(msg.formatted, "{:02}", tm_time.tm_min);
}
};
@@ -214,7 +235,8 @@ class S_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << fmt::pad(tm_time.tm_sec, 2, '0');
// msg.formatted << fmt::pad(tm_time.tm_sec, 2, '0');
fmt::format_to(msg.formatted, "{:02}", tm_time.tm_sec);
}
};
@@ -225,7 +247,8 @@ class e_formatter SPDLOG_FINAL : public flag_formatter
{
auto duration = msg.time.time_since_epoch();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
msg.formatted << fmt::pad(static_cast<int>(millis), 3, '0');
// msg.formatted << fmt::pad(static_cast<int>(millis), 3, '0');
fmt::format_to(msg.formatted, "{:03}", static_cast<int>(millis));
}
};
@@ -236,7 +259,8 @@ class f_formatter SPDLOG_FINAL : public flag_formatter
{
auto duration = msg.time.time_since_epoch();
auto micros = std::chrono::duration_cast<std::chrono::microseconds>(duration).count() % 1000000;
msg.formatted << fmt::pad(static_cast<int>(micros), 6, '0');
// msg.formatted << fmt::pad(static_cast<int>(micros), 6, '0');
fmt::format_to(msg.formatted, "{:06}", static_cast<int>(micros));
}
};
@@ -247,7 +271,8 @@ class F_formatter SPDLOG_FINAL : public flag_formatter
{
auto duration = msg.time.time_since_epoch();
auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count() % 1000000000;
msg.formatted << fmt::pad(static_cast<int>(ns), 9, '0');
// msg.formatted << fmt::pad(static_cast<int>(ns), 9, '0');
fmt::format_to(msg.formatted, "{:06}", static_cast<int>(ns));
}
};
@@ -257,7 +282,8 @@ class E_formatter SPDLOG_FINAL : public flag_formatter
{
auto duration = msg.time.time_since_epoch();
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration).count();
msg.formatted << seconds;
// msg.formatted << seconds;
fmt::format_to(msg.formatted, "{}", seconds);
}
};
@@ -266,7 +292,8 @@ class p_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &tm_time) override
{
msg.formatted << ampm(tm_time);
// msg.formatted << ampm(tm_time);
fmt::format_to(msg.formatted, "{}", ampm(tm_time));
}
};
@@ -275,7 +302,8 @@ class r_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &tm_time) override
{
pad_n_join(msg.formatted, to12h(tm_time), tm_time.tm_min, tm_time.tm_sec, ':') << ' ' << ampm(tm_time);
// pad_n_join(msg.formatted, to12h(tm_time), tm_time.tm_min, tm_time.tm_sec, ':') << ' ' << ampm(tm_time);
fmt::format_to(msg.formatted, "{:02}:{:02}:{02} {}", tm_time.tm_min, tm_time.tm_sec, ampm(tm_time));
}
};
@@ -284,7 +312,8 @@ class R_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &tm_time) override
{
pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, ':');
// pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, ':');
fmt::format_to(msg.formatted, "{:02}:{:02}", tm_time.tm_hour, tm_time.tm_min);
}
};
@@ -293,7 +322,8 @@ class T_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &tm_time) override
{
pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, ':');
// pad_n_join(msg.formatted, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, ':');
fmt::format_to(msg.formatted, "{:02}:{:02}", tm_time.tm_min, tm_time.tm_sec);
}
};
@@ -330,8 +360,9 @@ public:
int h = total_minutes / 60;
int m = total_minutes % 60;
msg.formatted << sign;
pad_n_join(msg.formatted, h, m, ':');
// msg.formatted << sign;
// pad_n_join(msg.formatted, h, m, ':');
fmt::format_to(msg.formatted, "{}{:02}:{:02}", sign, h, m);
}
private:
@@ -356,7 +387,8 @@ class t_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &) override
{
msg.formatted << msg.thread_id;
// msg.formatted << msg.thread_id;
fmt::format_to(msg.formatted, "{}", msg.thread_id);
}
};
@@ -365,7 +397,8 @@ class pid_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &) override
{
msg.formatted << details::os::pid();
// msg.formatted << details::os::pid();
fmt::format_to(msg.formatted, "{}", details::os::pid());
}
};
@@ -374,7 +407,8 @@ class i_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &) override
{
msg.formatted << fmt::pad(msg.msg_id, 6, '0');
// msg.formatted << fmt::pad(msg.msg_id, 6, '0');
fmt::format_to(msg.formatted, "{:06}", msg.msg_id);
}
};
@@ -382,7 +416,8 @@ class v_formatter SPDLOG_FINAL : public flag_formatter
{
void format(details::log_msg &msg, const std::tm &) override
{
msg.formatted << fmt::StringRef(msg.raw.data(), msg.raw.size());
// msg.formatted << fmt::StringRef(msg.raw.data(), msg.raw.size());
append_buf(msg.raw, msg.formatted);
}
};
@@ -395,7 +430,8 @@ public:
}
void format(details::log_msg &msg, const std::tm &) override
{
msg.formatted << ch_;
// msg.formatted << ch_;
msg.formatted.push_back(ch_);
}
private:
@@ -414,7 +450,8 @@ public:
}
void format(details::log_msg &msg, const std::tm &) override
{
msg.formatted << str_;
// msg.formatted << str_;
append_str(str_, msg.formatted);
}
private:
@@ -447,20 +484,7 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
auto duration = msg.time.time_since_epoch();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
/* Slower version(while still very fast - about 3.2 million lines/sec under 10 threads),
msg.formatted.write("[{:d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}.{:03d}] [{}] [{}] {} ",
tm_time.tm_year + 1900,
tm_time.tm_mon + 1,
tm_time.tm_mday,
tm_time.tm_hour,
tm_time.tm_min,
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 under 10 threads)
/*
msg.formatted << '[' << static_cast<unsigned int>(tm_time.tm_year + 1900) << '-'
<< fmt::pad(static_cast<unsigned int>(tm_time.tm_mon + 1), 2, '0') << '-'
<< fmt::pad(static_cast<unsigned int>(tm_time.tm_mday), 2, '0') << ' '
@@ -468,6 +492,11 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
<< fmt::pad(static_cast<unsigned int>(tm_time.tm_min), 2, '0') << ':'
<< fmt::pad(static_cast<unsigned int>(tm_time.tm_sec), 2, '0') << '.'
<< fmt::pad(static_cast<unsigned int>(millis), 3, '0') << "] ";
*/
// fmt::format_to(msg.formatted, "[{:d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}.{:03d}] [{}] [{}] ",
fmt::format_to(msg.formatted, "[{}-{:02}-{:02} {:02}:{:02}:{:02}.{:03}] ", tm_time.tm_year + 1900, tm_time.tm_mon + 1,
tm_time.tm_mday, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, static_cast<int>(millis));
// no datetime needed
#else
@@ -475,15 +504,24 @@ class full_formatter SPDLOG_FINAL : public flag_formatter
#endif
#ifndef SPDLOG_NO_NAME
msg.formatted << '[' << *msg.logger_name << "] ";
// msg.formatted << '[' << *msg.logger_name << "] ";
fmt::format_to(msg.formatted, "[{}] ", *msg.logger_name);
#endif
msg.formatted << '[';
// msg.formatted << '[';
msg.formatted.push_back('[');
// wrap the level name with color
msg.color_range_start = msg.formatted.size();
msg.formatted << level::to_str(msg.level);
// msg.formatted << level::to_str(msg.level);
append_str(level::to_str(msg.level), msg.formatted);
msg.color_range_end = msg.formatted.size();
msg.formatted << "] " << fmt::StringRef(msg.raw.data(), msg.raw.size());
// msg.formatted << "] " << fmt::StringRef(msg.raw.data(), msg.raw.size());
msg.formatted.push_back(']');
msg.formatted.push_back(' ');
append_buf(msg.raw, msg.formatted);
}
};
@@ -703,5 +741,7 @@ inline void spdlog::pattern_formatter::format(details::log_msg &msg)
f->format(msg, tm_time);
}
// write eol
msg.formatted << eol_;
// msg.formatted << eol_;
append_str(eol_, msg.formatted);
}

View File

@@ -65,15 +65,13 @@ struct async_msg
}
// copy into log_msg
void to_log_msg(log_msg &&msg)
void to_log_msg(log_msg &msg)
{
msg.logger_name = &worker_ptr->name();
msg.level = level;
msg.time = time;
msg.thread_id = thread_id;
msg.raw.clear();
msg.raw << txt;
msg.formatted.clear();
msg.raw.append(txt.data(), txt.data() + txt.size());
msg.msg_id = msg_id;
msg.color_range_start = 0;
msg.color_range_end = 0;
@@ -182,7 +180,7 @@ private:
default:
{
log_msg msg;
incoming_async_msg.to_log_msg(std::move(msg));
incoming_async_msg.to_log_msg(msg);
incoming_async_msg.worker_ptr->backend_log_(msg);
return true;
}