From b6ba0be5504d0686cd712ee3ca2944c53512cb2b Mon Sep 17 00:00:00 2001 From: fawdlstty Date: Thu, 25 Feb 2021 23:55:19 +0800 Subject: [PATCH 1/6] add daily_logger_format_mt and daily_logger_format_st sink --- include/spdlog/sinks/daily_file_sink.h | 128 +++++++++++++++++++++++-- 1 file changed, 122 insertions(+), 6 deletions(-) diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index ce140415..a94fdb59 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -18,6 +18,11 @@ #include #include +extern std::array spdlog::details::days; +extern std::array spdlog::details::full_days; +extern const std::array spdlog::details::months; +extern const std::array spdlog::details::full_months; + namespace spdlog { namespace sinks { @@ -34,6 +39,98 @@ struct daily_filename_calculator return fmt::format( SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}"), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday, ext); } + + // Create formatting filename for the form basename. + static filename_t calc_filename_fmt(const filename_t &filename, const tm &now_tm) + { + filename_t dest = ""; + int total_minutes; + for (size_t i = 0; i < filename.size (); ++i) + { + if (filename [i] == '%') { + if (i == filename.size () - 1) + throw_spdlog_ex("daily_file_sink: Invalid format"); + i += 1; + char ch = filename [i]; + switch (ch) + { + case 'a': + dest += spdlog::details::days[static_cast(now_tm.tm_wday)]; + break; + case 'A': + dest += spdlog::details::full_days[static_cast(now_tm.tm_wday)]; + break; + case 'b': + dest += spdlog::details::months[static_cast(now_tm.tm_mon)]; + break; + case 'B': + dest += spdlog::details::full_months[static_cast(now_tm.tm_mon)]; + break; + case 'c': + dest += calc_filename_fmt("%a %b %d %H:%M:%S %Y", now_tm); + break; + case 'C': + dest += fmt::format ("{}", (now_tm.tm_year + 1900) % 100); + break; + case 'Y': + dest += fmt::format ("{}", now_tm.tm_year + 1900); + break; + case 'D': + case 'x': + dest += calc_filename_fmt("%m %d %C", now_tm); + break; + case 'm': + dest += fmt::format ("{:02}", now_tm.tm_mon + 1); + break; + case 'd': + dest += fmt::format ("{:02}", now_tm.tm_mday); + break; + case 'H': + dest += fmt::format ("{:02}", now_tm.tm_hour); + break; + case 'I': + dest += fmt::format ("{:02}", (now_tm.tm_hour % 12) == 0 ? 12 : (now_tm.tm_hour % 12)); + break; + case 'M': + dest += fmt::format ("{:02}", now_tm.tm_min); + break; + case 'S': + dest += fmt::format ("{:02}", now_tm.tm_sec); + break; + case 'p': + dest += (now_tm.tm_hour >= 1 && now_tm.tm_hour <= 12 ? "AM" : "PM"); + break; + case 'r': + dest += calc_filename_fmt("%I:%M:%S %p", now_tm); + break; + case 'R': + dest += calc_filename_fmt("%H:%M", now_tm); + break; + case 'T': + case 'X': + dest += calc_filename_fmt("%H:%M:%S", now_tm); + break; + case 'z': + total_minutes = spdlog::details::os::utc_minutes_offset (); + if (total_minutes >= 0) { + dest += fmt::format ("+{:02}:{:02}", total_minutes / 60, total_minutes % 60); + } else { + total_minutes = -total_minutes; + dest += fmt::format ("-{:02}:{:02}", total_minutes / 60, total_minutes % 60); + } + break; + case '%': + dest += '%'; + break; + default: + throw_spdlog_ex("daily_file_sink: Invalid format"); + } + } else { + dest += filename [i]; + } + } + return dest; + } }; /* @@ -46,13 +143,14 @@ class daily_file_sink final : public base_sink { public: // create daily file sink which rotates on given time - daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute, bool truncate = false, uint16_t max_files = 0) + daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute, bool truncate = false, uint16_t max_files = 0, bool is_fmt = false) : base_filename_(std::move(base_filename)) , rotation_h_(rotation_hour) , rotation_m_(rotation_minute) , truncate_(truncate) , max_files_(max_files) , filenames_q_() + , is_fmt_(is_fmt) { if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59) { @@ -60,7 +158,8 @@ public: } auto now = log_clock::now(); - auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now)); + auto filename = is_fmt_ ? FileNameCalc::calc_filename_fmt(base_filename_, now_tm(now)) + : FileNameCalc::calc_filename(base_filename_, now_tm(now)); file_helper_.open(filename, truncate_); rotation_tp_ = next_rotation_tp_(); @@ -83,7 +182,8 @@ protected: bool should_rotate = time >= rotation_tp_; if (should_rotate) { - auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time)); + auto filename = is_fmt_ ? FileNameCalc::calc_filename_fmt(base_filename_, now_tm(time)) + : FileNameCalc::calc_filename(base_filename_, now_tm(time)); file_helper_.open(filename, truncate_); rotation_tp_ = next_rotation_tp_(); } @@ -113,7 +213,8 @@ private: auto now = log_clock::now(); while (filenames.size() < max_files_) { - auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now)); + auto filename = is_fmt_ ? FileNameCalc::calc_filename_fmt(base_filename_, now_tm(now)) + : FileNameCalc::calc_filename(base_filename_, now_tm(now)); if (!path_exists(filename)) { break; @@ -178,6 +279,7 @@ private: bool truncate_; uint16_t max_files_; details::circular_q filenames_q_; + bool is_fmt_; }; using daily_file_sink_mt = daily_file_sink; @@ -192,13 +294,27 @@ template inline std::shared_ptr daily_logger_mt( const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0) { - return Factory::template create(logger_name, filename, hour, minute, truncate, max_files); + return Factory::template create(logger_name, filename, hour, minute, truncate, max_files, false); +} + +template +inline std::shared_ptr daily_logger_format_mt( + const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0) +{ + return Factory::template create(logger_name, filename, hour, minute, truncate, max_files, true); } template inline std::shared_ptr daily_logger_st( const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0) { - return Factory::template create(logger_name, filename, hour, minute, truncate, max_files); + return Factory::template create(logger_name, filename, hour, minute, truncate, max_files, false); +} + +template +inline std::shared_ptr daily_logger_format_st( + const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0) +{ + return Factory::template create(logger_name, filename, hour, minute, truncate, max_files, true); } } // namespace spdlog From c1f4d7506a9af13e3f1191cd4ea8b029199ee3d6 Mon Sep 17 00:00:00 2001 From: fawdlstty Date: Thu, 25 Feb 2021 23:57:37 +0800 Subject: [PATCH 2/6] replace tab --- include/spdlog/sinks/daily_file_sink.h | 166 ++++++++++++------------- 1 file changed, 83 insertions(+), 83 deletions(-) diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index a94fdb59..55425267 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -43,93 +43,93 @@ struct daily_filename_calculator // Create formatting filename for the form basename. static filename_t calc_filename_fmt(const filename_t &filename, const tm &now_tm) { - filename_t dest = ""; + filename_t dest = ""; int total_minutes; - for (size_t i = 0; i < filename.size (); ++i) - { - if (filename [i] == '%') { - if (i == filename.size () - 1) - throw_spdlog_ex("daily_file_sink: Invalid format"); - i += 1; - char ch = filename [i]; - switch (ch) - { - case 'a': + for (size_t i = 0; i < filename.size (); ++i) + { + if (filename [i] == '%') { + if (i == filename.size () - 1) + throw_spdlog_ex("daily_file_sink: Invalid format"); + i += 1; + char ch = filename [i]; + switch (ch) + { + case 'a': dest += spdlog::details::days[static_cast(now_tm.tm_wday)]; - break; - case 'A': - dest += spdlog::details::full_days[static_cast(now_tm.tm_wday)]; - break; - case 'b': - dest += spdlog::details::months[static_cast(now_tm.tm_mon)]; - break; - case 'B': - dest += spdlog::details::full_months[static_cast(now_tm.tm_mon)]; - break; - case 'c': - dest += calc_filename_fmt("%a %b %d %H:%M:%S %Y", now_tm); - break; - case 'C': - dest += fmt::format ("{}", (now_tm.tm_year + 1900) % 100); - break; - case 'Y': - dest += fmt::format ("{}", now_tm.tm_year + 1900); - break; - case 'D': - case 'x': - dest += calc_filename_fmt("%m %d %C", now_tm); - break; - case 'm': - dest += fmt::format ("{:02}", now_tm.tm_mon + 1); - break; - case 'd': - dest += fmt::format ("{:02}", now_tm.tm_mday); - break; - case 'H': - dest += fmt::format ("{:02}", now_tm.tm_hour); - break; - case 'I': - dest += fmt::format ("{:02}", (now_tm.tm_hour % 12) == 0 ? 12 : (now_tm.tm_hour % 12)); - break; - case 'M': - dest += fmt::format ("{:02}", now_tm.tm_min); - break; - case 'S': - dest += fmt::format ("{:02}", now_tm.tm_sec); - break; - case 'p': - dest += (now_tm.tm_hour >= 1 && now_tm.tm_hour <= 12 ? "AM" : "PM"); - break; - case 'r': - dest += calc_filename_fmt("%I:%M:%S %p", now_tm); - break; - case 'R': - dest += calc_filename_fmt("%H:%M", now_tm); - break; - case 'T': - case 'X': - dest += calc_filename_fmt("%H:%M:%S", now_tm); - break; - case 'z': - total_minutes = spdlog::details::os::utc_minutes_offset (); - if (total_minutes >= 0) { - dest += fmt::format ("+{:02}:{:02}", total_minutes / 60, total_minutes % 60); - } else { - total_minutes = -total_minutes; - dest += fmt::format ("-{:02}:{:02}", total_minutes / 60, total_minutes % 60); - } - break; - case '%': - dest += '%'; - break; + break; + case 'A': + dest += spdlog::details::full_days[static_cast(now_tm.tm_wday)]; + break; + case 'b': + dest += spdlog::details::months[static_cast(now_tm.tm_mon)]; + break; + case 'B': + dest += spdlog::details::full_months[static_cast(now_tm.tm_mon)]; + break; + case 'c': + dest += calc_filename_fmt("%a %b %d %H:%M:%S %Y", now_tm); + break; + case 'C': + dest += fmt::format ("{}", (now_tm.tm_year + 1900) % 100); + break; + case 'Y': + dest += fmt::format ("{}", now_tm.tm_year + 1900); + break; + case 'D': + case 'x': + dest += calc_filename_fmt("%m %d %C", now_tm); + break; + case 'm': + dest += fmt::format ("{:02}", now_tm.tm_mon + 1); + break; + case 'd': + dest += fmt::format ("{:02}", now_tm.tm_mday); + break; + case 'H': + dest += fmt::format ("{:02}", now_tm.tm_hour); + break; + case 'I': + dest += fmt::format ("{:02}", (now_tm.tm_hour % 12) == 0 ? 12 : (now_tm.tm_hour % 12)); + break; + case 'M': + dest += fmt::format ("{:02}", now_tm.tm_min); + break; + case 'S': + dest += fmt::format ("{:02}", now_tm.tm_sec); + break; + case 'p': + dest += (now_tm.tm_hour >= 1 && now_tm.tm_hour <= 12 ? "AM" : "PM"); + break; + case 'r': + dest += calc_filename_fmt("%I:%M:%S %p", now_tm); + break; + case 'R': + dest += calc_filename_fmt("%H:%M", now_tm); + break; + case 'T': + case 'X': + dest += calc_filename_fmt("%H:%M:%S", now_tm); + break; + case 'z': + total_minutes = spdlog::details::os::utc_minutes_offset (); + if (total_minutes >= 0) { + dest += fmt::format ("+{:02}:{:02}", total_minutes / 60, total_minutes % 60); + } else { + total_minutes = -total_minutes; + dest += fmt::format ("-{:02}:{:02}", total_minutes / 60, total_minutes % 60); + } + break; + case '%': + dest += '%'; + break; default: throw_spdlog_ex("daily_file_sink: Invalid format"); - } - } else { - dest += filename [i]; - } - } - return dest; + } + } else { + dest += filename [i]; + } + } + return dest; } }; From 05a0b0d7b0a373df0a75f900c86b8d3c8baa936e Mon Sep 17 00:00:00 2001 From: fawdlstty Date: Fri, 26 Feb 2021 09:21:13 +0800 Subject: [PATCH 3/6] use fmt::chrono and remove is_fmt flags --- include/spdlog/sinks/daily_file_sink.h | 123 +++++-------------------- 1 file changed, 21 insertions(+), 102 deletions(-) diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 55425267..28cc77bd 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -39,97 +40,18 @@ struct daily_filename_calculator return fmt::format( SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}"), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday, ext); } +}; - // Create formatting filename for the form basename. - static filename_t calc_filename_fmt(const filename_t &filename, const tm &now_tm) +/* + * Generator of daily log file names by formatting + */ +struct daily_filename_format_calculator +{ + // Create filename by formatting %Y_%m_%d.log + static filename_t calc_filename (const filename_t &filename, const tm &now_tm) { - filename_t dest = ""; - int total_minutes; - for (size_t i = 0; i < filename.size (); ++i) - { - if (filename [i] == '%') { - if (i == filename.size () - 1) - throw_spdlog_ex("daily_file_sink: Invalid format"); - i += 1; - char ch = filename [i]; - switch (ch) - { - case 'a': - dest += spdlog::details::days[static_cast(now_tm.tm_wday)]; - break; - case 'A': - dest += spdlog::details::full_days[static_cast(now_tm.tm_wday)]; - break; - case 'b': - dest += spdlog::details::months[static_cast(now_tm.tm_mon)]; - break; - case 'B': - dest += spdlog::details::full_months[static_cast(now_tm.tm_mon)]; - break; - case 'c': - dest += calc_filename_fmt("%a %b %d %H:%M:%S %Y", now_tm); - break; - case 'C': - dest += fmt::format ("{}", (now_tm.tm_year + 1900) % 100); - break; - case 'Y': - dest += fmt::format ("{}", now_tm.tm_year + 1900); - break; - case 'D': - case 'x': - dest += calc_filename_fmt("%m %d %C", now_tm); - break; - case 'm': - dest += fmt::format ("{:02}", now_tm.tm_mon + 1); - break; - case 'd': - dest += fmt::format ("{:02}", now_tm.tm_mday); - break; - case 'H': - dest += fmt::format ("{:02}", now_tm.tm_hour); - break; - case 'I': - dest += fmt::format ("{:02}", (now_tm.tm_hour % 12) == 0 ? 12 : (now_tm.tm_hour % 12)); - break; - case 'M': - dest += fmt::format ("{:02}", now_tm.tm_min); - break; - case 'S': - dest += fmt::format ("{:02}", now_tm.tm_sec); - break; - case 'p': - dest += (now_tm.tm_hour >= 1 && now_tm.tm_hour <= 12 ? "AM" : "PM"); - break; - case 'r': - dest += calc_filename_fmt("%I:%M:%S %p", now_tm); - break; - case 'R': - dest += calc_filename_fmt("%H:%M", now_tm); - break; - case 'T': - case 'X': - dest += calc_filename_fmt("%H:%M:%S", now_tm); - break; - case 'z': - total_minutes = spdlog::details::os::utc_minutes_offset (); - if (total_minutes >= 0) { - dest += fmt::format ("+{:02}:{:02}", total_minutes / 60, total_minutes % 60); - } else { - total_minutes = -total_minutes; - dest += fmt::format ("-{:02}:{:02}", total_minutes / 60, total_minutes % 60); - } - break; - case '%': - dest += '%'; - break; - default: - throw_spdlog_ex("daily_file_sink: Invalid format"); - } - } else { - dest += filename [i]; - } - } - return dest; + std::string fmt_filename = fmt::format("{{:{}}}", filename); + return fmt::format(fmt_filename, now_tm); } }; @@ -143,14 +65,13 @@ class daily_file_sink final : public base_sink { public: // create daily file sink which rotates on given time - daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute, bool truncate = false, uint16_t max_files = 0, bool is_fmt = false) + daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute, bool truncate = false, uint16_t max_files = 0) : base_filename_(std::move(base_filename)) , rotation_h_(rotation_hour) , rotation_m_(rotation_minute) , truncate_(truncate) , max_files_(max_files) , filenames_q_() - , is_fmt_(is_fmt) { if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59) { @@ -158,8 +79,7 @@ public: } auto now = log_clock::now(); - auto filename = is_fmt_ ? FileNameCalc::calc_filename_fmt(base_filename_, now_tm(now)) - : FileNameCalc::calc_filename(base_filename_, now_tm(now)); + auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now)); file_helper_.open(filename, truncate_); rotation_tp_ = next_rotation_tp_(); @@ -182,8 +102,7 @@ protected: bool should_rotate = time >= rotation_tp_; if (should_rotate) { - auto filename = is_fmt_ ? FileNameCalc::calc_filename_fmt(base_filename_, now_tm(time)) - : FileNameCalc::calc_filename(base_filename_, now_tm(time)); + auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time)); file_helper_.open(filename, truncate_); rotation_tp_ = next_rotation_tp_(); } @@ -213,8 +132,7 @@ private: auto now = log_clock::now(); while (filenames.size() < max_files_) { - auto filename = is_fmt_ ? FileNameCalc::calc_filename_fmt(base_filename_, now_tm(now)) - : FileNameCalc::calc_filename(base_filename_, now_tm(now)); + auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now)); if (!path_exists(filename)) { break; @@ -279,11 +197,12 @@ private: bool truncate_; uint16_t max_files_; details::circular_q filenames_q_; - bool is_fmt_; }; using daily_file_sink_mt = daily_file_sink; using daily_file_sink_st = daily_file_sink; +using daily_file_format_sink_mt = daily_file_sink; +using daily_file_format_sink_st = daily_file_sink; } // namespace sinks @@ -294,27 +213,27 @@ template inline std::shared_ptr daily_logger_mt( const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0) { - return Factory::template create(logger_name, filename, hour, minute, truncate, max_files, false); + return Factory::template create(logger_name, filename, hour, minute, truncate, max_files); } template inline std::shared_ptr daily_logger_format_mt( const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0) { - return Factory::template create(logger_name, filename, hour, minute, truncate, max_files, true); + return Factory::template create(logger_name, filename, hour, minute, truncate, max_files); } template inline std::shared_ptr daily_logger_st( const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0) { - return Factory::template create(logger_name, filename, hour, minute, truncate, max_files, false); + return Factory::template create(logger_name, filename, hour, minute, truncate, max_files); } template inline std::shared_ptr daily_logger_format_st( const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0) { - return Factory::template create(logger_name, filename, hour, minute, truncate, max_files, true); + return Factory::template create(logger_name, filename, hour, minute, truncate, max_files); } } // namespace spdlog From f4b7210e7bbf7c90d0a3bbdd7ad5b970767b4adb Mon Sep 17 00:00:00 2001 From: fawdlstty Date: Fri, 26 Feb 2021 09:28:12 +0800 Subject: [PATCH 4/6] remove externs --- include/spdlog/sinks/daily_file_sink.h | 5 ----- 1 file changed, 5 deletions(-) diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 28cc77bd..34797469 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -19,11 +19,6 @@ #include #include -extern std::array spdlog::details::days; -extern std::array spdlog::details::full_days; -extern const std::array spdlog::details::months; -extern const std::array spdlog::details::full_months; - namespace spdlog { namespace sinks { From dd46579cb4a79fe61d5514562b0bad01430032be Mon Sep 17 00:00:00 2001 From: fawdlstty Date: Fri, 26 Feb 2021 10:17:43 +0800 Subject: [PATCH 5/6] fix name --- include/spdlog/sinks/daily_file_sink.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 34797469..d4f22487 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -45,7 +45,7 @@ struct daily_filename_format_calculator // Create filename by formatting %Y_%m_%d.log static filename_t calc_filename (const filename_t &filename, const tm &now_tm) { - std::string fmt_filename = fmt::format("{{:{}}}", filename); + filename_t fmt_filename = fmt::format("{{:{}}}", filename); return fmt::format(fmt_filename, now_tm); } }; From a709e2958626344529afae684676cac8d5390458 Mon Sep 17 00:00:00 2001 From: fawdlstty Date: Fri, 26 Feb 2021 10:18:33 +0800 Subject: [PATCH 6/6] fix unique mode compile --- include/spdlog/sinks/daily_file_sink.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index d4f22487..aedaf52f 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -45,7 +45,7 @@ struct daily_filename_format_calculator // Create filename by formatting %Y_%m_%d.log static filename_t calc_filename (const filename_t &filename, const tm &now_tm) { - filename_t fmt_filename = fmt::format("{{:{}}}", filename); + filename_t fmt_filename = fmt::format(SPDLOG_FILENAME_T ("{{:{}}}"), filename); return fmt::format(fmt_filename, now_tm); } };