From de84df8ad7714373c6ee9dba35e5c3984de7638f Mon Sep 17 00:00:00 2001 From: Asperger Date: Mon, 8 Oct 2018 11:28:38 +0800 Subject: [PATCH] Update rotate method Calculate the current amount of backups to reduce redundant file_exists check --- .../spdlog/sinks/daily_rotating_file_sink.h | 20 ++++++++++++++++++- include/spdlog/sinks/rotating_file_sink.h | 19 +++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/include/spdlog/sinks/daily_rotating_file_sink.h b/include/spdlog/sinks/daily_rotating_file_sink.h index a2044bbd..d1d44c04 100644 --- a/include/spdlog/sinks/daily_rotating_file_sink.h +++ b/include/spdlog/sinks/daily_rotating_file_sink.h @@ -54,6 +54,17 @@ public: auto now = log_clock::now(); today_filename_ = FileNameCalc::calc_filename(base_filename_, now_tm(now)); + + // calculate the amount of current backups + for (current_files_ = 0; ; ++current_files_) + { + filename_t src = FileNameCalc::calc_filename(today_filename_, current_files_); + if (!details::file_helper::file_exists(src)) + { + break; + } + } + file_helper_.open(today_filename_, truncate_); rotation_tp_ = next_rotation_tp_(); current_size_ = file_helper_.size(); // expensive. called only once @@ -118,7 +129,13 @@ private: { using details::os::filename_to_str; file_helper_.close(); - for (auto i = max_files_; i > 0; --i) + + // update amount of file + if (++current_files_ > max_files_) + { + current_files_ = max_files_; + } + for (auto i = current_files_; i > 0; --i) { filename_t src = FileNameCalc::calc_filename(today_filename_, i - 1); if (!details::file_helper::file_exists(src)) @@ -147,6 +164,7 @@ private: std::size_t max_size_; std::size_t max_files_; std::size_t current_size_; + std::size_t current_files_; filename_t today_filename_; filename_t base_filename_; std::chrono::hours rotation_h_; diff --git a/include/spdlog/sinks/rotating_file_sink.h b/include/spdlog/sinks/rotating_file_sink.h index cd5450eb..d221afa7 100644 --- a/include/spdlog/sinks/rotating_file_sink.h +++ b/include/spdlog/sinks/rotating_file_sink.h @@ -32,6 +32,16 @@ public: , max_size_(max_size) , max_files_(max_files) { + // calculate the amount of current backups + for (current_files_ = 0; ; ++current_files_) + { + filename_t src = FileNameCalc::calc_filename(base_filename_, current_files_); + if (!details::file_helper::file_exists(src)) + { + break; + } + } + file_helper_.open(FileNameCalc::calc_filename(base_filename_, 0)); current_size_ = file_helper_.size(); // expensive. called only once } @@ -65,7 +75,13 @@ private: { using details::os::filename_to_str; file_helper_.close(); - for (auto i = max_files_; i > 0; --i) + + // update amount of file + if (++current_files_ > max_files_) + { + current_files_ = max_files_; + } + for (auto i = current_files_; i > 0; --i) { filename_t src = FileNameCalc::calc_filename(base_filename_, i - 1); if (!details::file_helper::file_exists(src)) @@ -95,6 +111,7 @@ private: std::size_t max_size_; std::size_t max_files_; std::size_t current_size_; + std::size_t current_files_; details::file_helper file_helper_; };