add callback while rotate the log file

pull/2087/head
seker 4 years ago
parent a1d9f501e3
commit fe8999ad90

@ -255,6 +255,8 @@ struct source_loc
const char *funcname{nullptr}; const char *funcname{nullptr};
}; };
typedef void (*rotate_file_callback)(const filename_t& pre, const filename_t& next);
namespace details { namespace details {
// make_unique support for pre c++14 // make_unique support for pre c++14

@ -68,12 +68,13 @@ class daily_file_sink final : public base_sink<Mutex>
{ {
public: public:
// create daily file sink which rotates on given time // 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)) : base_filename_(std::move(base_filename))
, rotation_h_(rotation_hour) , rotation_h_(rotation_hour)
, rotation_m_(rotation_minute) , rotation_m_(rotation_minute)
, truncate_(truncate) , truncate_(truncate)
, max_files_(max_files) , max_files_(max_files)
, rotate_cb(cb)
, filenames_q_() , filenames_q_()
{ {
if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59) if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59)
@ -106,6 +107,9 @@ protected:
if (should_rotate) if (should_rotate)
{ {
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time)); auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time));
if (rotate_cb) {
rotate_cb(file_helper_.filename(), filename);
}
file_helper_.open(filename, truncate_); file_helper_.open(filename, truncate_);
rotation_tp_ = next_rotation_tp_(); rotation_tp_ = next_rotation_tp_();
} }
@ -193,6 +197,7 @@ private:
} }
filename_t base_filename_; filename_t base_filename_;
rotate_file_callback rotate_cb;
int rotation_h_; int rotation_h_;
int rotation_m_; int rotation_m_;
log_clock::time_point rotation_tp_; log_clock::time_point rotation_tp_;

@ -46,10 +46,11 @@ class hourly_file_sink final : public base_sink<Mutex>
{ {
public: public:
// create hourly file sink which rotates on given time // 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)) : base_filename_(std::move(base_filename))
, truncate_(truncate) , truncate_(truncate)
, max_files_(max_files) , max_files_(max_files)
, rotate_cb(cb)
, filenames_q_() , filenames_q_()
{ {
auto now = log_clock::now(); auto now = log_clock::now();
@ -77,6 +78,9 @@ protected:
if (should_rotate) if (should_rotate)
{ {
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time)); auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time));
if (rotate_cb) {
rotate_cb(file_helper_.filename(), filename);
}
file_helper_.open(filename, truncate_); file_helper_.open(filename, truncate_);
rotation_tp_ = next_rotation_tp_(); rotation_tp_ = next_rotation_tp_();
} }
@ -163,6 +167,7 @@ private:
} }
filename_t base_filename_; filename_t base_filename_;
rotate_file_callback rotate_cb;
log_clock::time_point rotation_tp_; log_clock::time_point rotation_tp_;
details::file_helper file_helper_; details::file_helper file_helper_;
bool truncate_; bool truncate_;

Loading…
Cancel
Save