Skip to content

Commit

Permalink
Minor improvements to TimeRotatingFileHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
odygrd committed May 28, 2023
1 parent f08073d commit b547466
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
5 changes: 3 additions & 2 deletions quill/include/quill/detail/misc/FileUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ QUILL_NODISCARD QUILL_ATTRIBUTE_COLD std::pair<std::string, std::string> extract
* @return a filepath with the date appended
*/
QUILL_NODISCARD QUILL_ATTRIBUTE_COLD fs::path append_date_to_filename(
fs::path const& filename, std::chrono::system_clock::time_point timestamp = {}, bool append_time = false,
Timezone timezone = Timezone::LocalTime, bool zero_out_seconds = false) noexcept;
fs::path const& filename, std::chrono::system_clock::time_point timestamp = {},
bool append_time = false, Timezone timezone = Timezone::LocalTime, bool zero_out_minutes = false,
bool zero_out_seconds = false) noexcept;

/**
* Append an index to the given filename
Expand Down
10 changes: 8 additions & 2 deletions quill/src/detail/misc/FileUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ std::pair<std::string, std::string> extract_stem_and_extension(fs::path const& f

/***/
fs::path append_date_to_filename(fs::path const& filename, std::chrono::system_clock::time_point timestamp, /* = {} */
bool append_time, /* = false */
Timezone timezone, /* = Timezone::LocalTime */
bool append_time, /* = false */
Timezone timezone, /* = Timezone::LocalTime */
bool zero_out_minutes, /* = false */
bool zero_out_seconds /* = false */) noexcept
{
// Get the time now as tm from user or default to now
Expand All @@ -106,6 +107,11 @@ fs::path append_date_to_filename(fs::path const& filename, std::chrono::system_c
detail::localtime_rs(&time_now, &now_tm);
}

if (zero_out_minutes)
{
now_tm.tm_min = 0;
}

if (zero_out_seconds)
{
now_tm.tm_sec = 0;
Expand Down
4 changes: 3 additions & 1 deletion quill/src/handlers/TimeRotatingFileHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,14 @@ void TimeRotatingFileHandler::write(fmt_buffer_t const& formatted_log_message, q

if (_append_to_filename == FilenameAppend::None)
{
bool const zero_out_minutes = (_when == "H") || (_when == "daily");

// when the log files don't include the date and time information as part of their name
// we add that as part of the rotation
fs::path const previous_file = _filename;
bool const append_time_to_filename = true;
fs::path const new_file = detail::append_date_to_filename(
_filename, _file_creation_time, append_time_to_filename, _using_timezone, true);
_filename, _file_creation_time, append_time_to_filename, _using_timezone, zero_out_minutes, true);

detail::rename_file(previous_file, new_file);

Expand Down

0 comments on commit b547466

Please sign in to comment.