From dd6830cf3c799db156a00e2fe0674d181314ffe5 Mon Sep 17 00:00:00 2001 From: seker <04070628@163.com> Date: Fri, 3 Sep 2021 19:49:32 +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..175cc304 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_(); } @@ -200,6 +204,7 @@ private: bool truncate_; uint16_t max_files_; details::circular_q filenames_q_; + rotate_file_callback rotate_cb; }; using daily_file_sink_mt = daily_file_sink; diff --git a/include/spdlog/sinks/hourly_file_sink.h b/include/spdlog/sinks/hourly_file_sink.h index 815781d9..5279e945 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_(); } @@ -168,6 +172,7 @@ private: bool truncate_; uint16_t max_files_; details::circular_q filenames_q_; + rotate_file_callback rotate_cb; }; using hourly_file_sink_mt = hourly_file_sink;