Skip to content

Commit

Permalink
add clean_old_files flag to RotatingFileHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
odygrd committed May 21, 2022
1 parent 5ebbb65 commit 51293a3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -23,14 +24,22 @@
- [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.

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**

Expand Down
2 changes: 1 addition & 1 deletion quill/include/quill/Quill.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 **/
Expand Down
6 changes: 4 additions & 2 deletions quill/include/quill/handlers/RotatingFileHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions quill/src/handlers/RotatingFileHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 51293a3

Please sign in to comment.