From dd220b02737632cf66df110330f04fbc5e226c3b Mon Sep 17 00:00:00 2001 From: seker <04070628@163.com> Date: Thu, 14 Oct 2021 13:00:15 +0800 Subject: [PATCH] add callback while rotate the log file --- include/spdlog/common.h | 2 ++ include/spdlog/sinks/daily_file_sink.h | 7 ++++++- include/spdlog/sinks/hourly_file_sink.h | 7 ++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/include/spdlog/common.h b/include/spdlog/common.h index ed0ab46c..6b7f2b40 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -255,6 +255,8 @@ struct source_loc const char *funcname{nullptr}; }; +typedef void (*rotate_file_callback)(const filename_t& pre, const filename_t& next); + namespace details { // make_unique support for pre c++14 diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index d6a09f6b..ae3151cc 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -68,12 +68,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) + daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute, bool truncate = false, uint16_t max_files = 0, rotate_file_callback cb = nullptr) : base_filename_(std::move(base_filename)) , rotation_h_(rotation_hour) , rotation_m_(rotation_minute) , truncate_(truncate) , max_files_(max_files) + , rotate_cb(cb) , filenames_q_() { if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59) @@ -106,6 +107,9 @@ protected: if (should_rotate) { auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time)); + if (rotate_cb) { + rotate_cb(file_helper_.filename(), filename); + } file_helper_.open(filename, truncate_); rotation_tp_ = next_rotation_tp_(); } @@ -199,6 +203,7 @@ private: details::file_helper file_helper_; bool truncate_; uint16_t max_files_; + rotate_file_callback rotate_cb; details::circular_q filenames_q_; }; diff --git a/include/spdlog/sinks/hourly_file_sink.h b/include/spdlog/sinks/hourly_file_sink.h index 815781d9..39b4935b 100644 --- a/include/spdlog/sinks/hourly_file_sink.h +++ b/include/spdlog/sinks/hourly_file_sink.h @@ -46,10 +46,11 @@ class hourly_file_sink final : public base_sink { public: // create hourly file sink which rotates on given time - hourly_file_sink(filename_t base_filename, bool truncate = false, uint16_t max_files = 0) + hourly_file_sink(filename_t base_filename, bool truncate = false, uint16_t max_files = 0, rotate_file_callback cb = nullptr) : base_filename_(std::move(base_filename)) , truncate_(truncate) , max_files_(max_files) + , rotate_cb(cb) , filenames_q_() { auto now = log_clock::now(); @@ -77,6 +78,9 @@ protected: if (should_rotate) { auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time)); + if (rotate_cb) { + rotate_cb(file_helper_.filename(), filename); + } file_helper_.open(filename, truncate_); rotation_tp_ = next_rotation_tp_(); } @@ -167,6 +171,7 @@ private: details::file_helper file_helper_; bool truncate_; uint16_t max_files_; + rotate_file_callback rotate_cb; details::circular_q filenames_q_; };