Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix rotating file handler when append mode is used
- Loading branch information
Showing
2 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
} | ||
} | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
odygrd
Author
Owner
|
||
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); | ||
} | ||
|
||
|
This comment is for the code starting
if (mode == "w")
@odygrd Hmm isn't this quite radical to remove all previous logs when program is restarted? Previously it deleted the current/last file and not all.
I think Wiki example should use the 'a' mode by default?