From 64521005ab64b50c49d301a35e205c7204535ea5 Mon Sep 17 00:00:00 2001 From: slapenko Date: Wed, 1 Aug 2018 22:58:15 -0500 Subject: [PATCH 1/2] We can control should daily_file_sink truncate an underlying file or not --- include/spdlog/sinks/daily_file_sink.h | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 3cfe8dbc..eb9ee694 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -44,17 +44,18 @@ class daily_file_sink SPDLOG_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) + daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute, bool truncate = false) : base_filename_(std::move(base_filename)) , rotation_h_(rotation_hour) , rotation_m_(rotation_minute) + , truncate_(truncate) { if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59) { throw spdlog_ex("daily_file_sink: Invalid rotation time in ctor"); } auto now = log_clock::now(); - file_helper_.open(FileNameCalc::calc_filename(base_filename_, now_tm(now))); + open_file(now); rotation_tp_ = next_rotation_tp_(); } @@ -64,7 +65,7 @@ protected: if (msg.time >= rotation_tp_) { - file_helper_.open(FileNameCalc::calc_filename(base_filename_, now_tm(msg.time))); + open_file(msg.time); rotation_tp_ = next_rotation_tp_(); } fmt::memory_buffer formatted; @@ -78,6 +79,11 @@ protected: } private: + void open_file(log_clock::time_point tp) + { + file_helper_.open(FileNameCalc::calc_filename(base_filename_, now_tm(tp)), truncate_); + } + tm now_tm(log_clock::time_point tp) { time_t tnow = log_clock::to_time_t(tp); @@ -104,6 +110,7 @@ private: int rotation_m_; log_clock::time_point rotation_tp_; details::file_helper file_helper_; + bool truncate_; }; using daily_file_sink_mt = daily_file_sink; @@ -115,14 +122,14 @@ using daily_file_sink_st = daily_file_sink; // factory functions // template -inline std::shared_ptr daily_logger_mt(const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0) +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) { - return Factory::template create(logger_name, filename, hour, minute); + return Factory::template create(logger_name, filename, hour, minute, truncate); } template -inline std::shared_ptr daily_logger_st(const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0) +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) { - return Factory::template create(logger_name, filename, hour, minute); + return Factory::template create(logger_name, filename, hour, minute, truncate); } } // namespace spdlog From 34ada56f5d31d00ac33f0c574cbb2f12b2f294e8 Mon Sep 17 00:00:00 2001 From: slapenko Date: Thu, 2 Aug 2018 07:27:49 -0500 Subject: [PATCH 2/2] Refactoring. Rid of open_file for clarity --- include/spdlog/sinks/daily_file_sink.h | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index eb9ee694..7ced6e00 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -55,7 +55,7 @@ public: throw spdlog_ex("daily_file_sink: Invalid rotation time in ctor"); } auto now = log_clock::now(); - open_file(now); + file_helper_.open(FileNameCalc::calc_filename(base_filename_, now_tm(now)), truncate_); rotation_tp_ = next_rotation_tp_(); } @@ -65,7 +65,7 @@ protected: if (msg.time >= rotation_tp_) { - open_file(msg.time); + file_helper_.open(FileNameCalc::calc_filename(base_filename_, now_tm(msg.time)), truncate_); rotation_tp_ = next_rotation_tp_(); } fmt::memory_buffer formatted; @@ -79,11 +79,6 @@ protected: } private: - void open_file(log_clock::time_point tp) - { - file_helper_.open(FileNameCalc::calc_filename(base_filename_, now_tm(tp)), truncate_); - } - tm now_tm(log_clock::time_point tp) { time_t tnow = log_clock::to_time_t(tp);