diff --git a/CHANGELOG.md b/CHANGELOG.md index aac12d03..20dd81a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,9 @@ From version `v2.0` 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)) + **Improvements** - Reduced and simplified codebase. diff --git a/quill/src/handlers/RotatingFileHandler.cpp b/quill/src/handlers/RotatingFileHandler.cpp index 1322a53f..f9aa462a 100644 --- a/quill/src/handlers/RotatingFileHandler.cpp +++ b/quill/src/handlers/RotatingFileHandler.cpp @@ -18,7 +18,55 @@ RotatingFileHandler::RotatingFileHandler(std::filesystem::path const& base_filen _backup_count(backup_count), _overwrite_oldest_files(overwrite_oldest_files) { - _file = detail::open_file(_filename, mode); + // if we are starting in w mode, then we also should clean all previous log files of the previous run + if (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) + { + std::filesystem::remove(entry); + } + } + } + else if (mode == "a") + { + // Since we are appending, we need to find the current index + 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) + { + // stem will be something like `logfile.1` + size_t pos = entry.path().stem().string().find_last_of("."); + if (pos != std::string::npos) + { + std::string index = entry.path().stem().string().substr(pos + 1, entry.path().stem().string().length()); + + // Attempt to convert the index to a number + try + { + uint32_t index_num = std::stoul(index); + _current_index = (std::max)(_current_index, index_num); + if (_current_index > (_backup_count - 1)) + { + // don't increment past _backup_count + _current_index = (_backup_count - 1); + } + } + catch (...) + { + continue; + } + } + } + } + } + + _file = detail::open_file(_filename, mode); _current_size = detail::file_size(_filename); }