From 46fcd2e8448cf3717475913c6ee6d749703db1f1 Mon Sep 17 00:00:00 2001 From: weiy Date: Sun, 19 Jan 2020 13:46:43 +0800 Subject: [PATCH 1/3] add max files for rotating days --- include/spdlog/sinks/daily_file_sink.h | 32 +++++++++++++++++++++----- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 40a37a71f..de673932a 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -66,8 +66,7 @@ class daily_file_sink final : public base_sink if (max_files_ > 0) { - filenames_q_ = details::circular_q(static_cast(max_files_)); - filenames_q_.push_back(std::move(filename)); + init_filenames_q(); } } @@ -104,6 +103,27 @@ class daily_file_sink final : public base_sink } private: + void init_filenames_q() + { + using details::os::file_exists; + filenames_q_ = details::circular_q(static_cast(max_files_)); + std::vector filenames; + auto now = log_clock::now(); + while (filenames.size() < max_files_) + { + auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now)); + if (!file_exists(filename)) + { + break; + } + filenames.emplace_back(filename); + now -= std::chrono::hours(24); + } + for (auto iter = filenames.rbegin(); iter != filenames.rend(); ++iter) { + filenames_q_.push_back(std::move(*iter)); + } + } + tm now_tm(log_clock::time_point tp) { time_t tnow = log_clock::to_time_t(tp); @@ -167,15 +187,15 @@ using daily_file_sink_st = daily_file_sink; // template 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) + const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0) { - return Factory::template create(logger_name, filename, hour, minute, truncate); + return Factory::template create(logger_name, filename, hour, minute, truncate, max_files); } template 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) + const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0) { - return Factory::template create(logger_name, filename, hour, minute, truncate); + return Factory::template create(logger_name, filename, hour, minute, truncate, max_files); } } // namespace spdlog From 34cc3419fa67d38f0a4918b40351c2d7e260c804 Mon Sep 17 00:00:00 2001 From: weiy Date: Sun, 19 Jan 2020 13:52:01 +0800 Subject: [PATCH 2/3] add max files for rotating days --- include/spdlog/sinks/daily_file_sink.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index de673932a..294c81acd 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -105,14 +105,15 @@ class daily_file_sink final : public base_sink private: void init_filenames_q() { - using details::os::file_exists; + using details::os::path_exists; + filenames_q_ = details::circular_q(static_cast(max_files_)); std::vector filenames; auto now = log_clock::now(); while (filenames.size() < max_files_) { auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now)); - if (!file_exists(filename)) + if (!path_exists(filename)) { break; } From 85ea4297b9a77131849e57572f7a4e0027b1e8b1 Mon Sep 17 00:00:00 2001 From: weiy Date: Sun, 19 Jan 2020 17:29:17 +0800 Subject: [PATCH 3/3] update method name --- include/spdlog/sinks/daily_file_sink.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 294c81acd..f796f831d 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -66,7 +66,7 @@ class daily_file_sink final : public base_sink if (max_files_ > 0) { - init_filenames_q(); + init_filenames_q_(); } } @@ -103,7 +103,7 @@ class daily_file_sink final : public base_sink } private: - void init_filenames_q() + void init_filenames_q_() { using details::os::path_exists;