From 51293a3a0dc730f36bb0740108248552419889ba Mon Sep 17 00:00:00 2001 From: Odysseas Gergoudis Date: Sat, 21 May 2022 17:12:38 +0100 Subject: [PATCH] add clean_old_files flag to RotatingFileHandler --- CHANGELOG.md | 11 ++++++++++- quill/include/quill/Quill.h | 2 +- quill/include/quill/handlers/RotatingFileHandler.h | 6 ++++-- quill/src/handlers/RotatingFileHandler.cpp | 9 +++++---- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 06da5051..32638cec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ +- [v2.0.1](#v2.0.1) - [v2.0.0](#v2.0.0) - [v1.7.3](#v1.7.3) - [v1.7.2](#v1.7.2) @@ -23,6 +24,12 @@ - [v1.1.0](#v1.1.0) - [v1.0.0](#v1.0.0) +## v2.0.1 + +**Improvements** + +- Add a flag to RotatingFileHandler to disable removing the old files when `w` mode is used. + ## v2.0.0 From version `v2` and onwards only c++17 is supported. @@ -30,7 +37,9 @@ From version `v2` and onwards only c++17 is supported. This version is a major refactor. **Fixes** -- RotatingFileHandler will now correctly rotate the files when append mode is used ([#123](https://github.com/odygrd/quill/issues/123)) + +- RotatingFileHandler will now correctly rotate the files when append mode is + used ([#123](https://github.com/odygrd/quill/issues/123)) **Improvements** diff --git a/quill/include/quill/Quill.h b/quill/include/quill/Quill.h index 682a753e..87ba4965 100644 --- a/quill/include/quill/Quill.h +++ b/quill/include/quill/Quill.h @@ -27,7 +27,7 @@ namespace quill /** Version Info **/ constexpr uint32_t VersionMajor{2}; constexpr uint32_t VersionMinor{0}; -constexpr uint32_t VersionPatch{0}; +constexpr uint32_t VersionPatch{1}; constexpr uint32_t Version{VersionMajor * 10000 + VersionMinor * 100 + VersionPatch}; /** forward declarations **/ diff --git a/quill/include/quill/handlers/RotatingFileHandler.h b/quill/include/quill/handlers/RotatingFileHandler.h index f35cb48f..b2d43c45 100644 --- a/quill/include/quill/handlers/RotatingFileHandler.h +++ b/quill/include/quill/handlers/RotatingFileHandler.h @@ -29,10 +29,12 @@ class RotatingFileHandler final : public FileHandler * @param replace_oldest_files if set to true, oldest logs will get overwritten when backup_count * is reached. if set to false then when backup_count is reached the rotation will stop and * the base log file will grow indefinitely + * @param clean_old_files Setting this to true will also clean any previous rotated log files when + * mode="w" is used * @throws on invalid rotation values */ - RotatingFileHandler(std::filesystem::path const& base_filename, std::string const& mode, - size_t max_bytes, uint32_t backup_count, bool overwrite_oldest_files); + RotatingFileHandler(std::filesystem::path const& base_filename, std::string const& mode, size_t max_bytes, + uint32_t backup_count, bool overwrite_oldest_files, bool clean_old_files = false); /** * Destructor diff --git a/quill/src/handlers/RotatingFileHandler.cpp b/quill/src/handlers/RotatingFileHandler.cpp index d15d07ee..2e5c9eff 100644 --- a/quill/src/handlers/RotatingFileHandler.cpp +++ b/quill/src/handlers/RotatingFileHandler.cpp @@ -11,21 +11,22 @@ namespace quill { /***/ -RotatingFileHandler::RotatingFileHandler(std::filesystem::path const& base_filename, std::string const& mode, - size_t max_bytes, uint32_t backup_count, bool overwrite_oldest_files) +RotatingFileHandler::RotatingFileHandler(std::filesystem::path const& base_filename, + std::string const& mode, size_t max_bytes, uint32_t backup_count, + bool overwrite_oldest_files, bool clean_old_files /*=false*/) : FileHandler(base_filename), _max_bytes(max_bytes), _backup_count(backup_count), _overwrite_oldest_files(overwrite_oldest_files) { // if we are starting in w mode, then we also should clean all previous log files of the previous run - if (mode == "w") + if (clean_old_files && (mode == "w")) { for (const auto& entry : std::filesystem::directory_iterator(std::filesystem::current_path() / base_filename.parent_path())) { std::size_t found = entry.path().string().find(base_filename.stem().string() + "."); - if (found != std::string::npos) + if (found != std::string::npos) { std::filesystem::remove(entry); }