diff --git a/include/spdlog/sinks/hourly_file_sink.h b/include/spdlog/sinks/hourly_file_sink.h index 43c6a638..3de176b0 100644 --- a/include/spdlog/sinks/hourly_file_sink.h +++ b/include/spdlog/sinks/hourly_file_sink.h @@ -22,16 +22,15 @@ namespace spdlog { namespace sinks { /* - * Generator of Hourly log file names in format basename.YYYY-MM-DD-HH.ext + * Generator of Hourly log file names in format basename_YYYY-MM-DD_HH.ext */ struct hourly_filename_calculator { - // Create filename for the form basename.YYYY-MM-DD-H static filename_t calc_filename(const filename_t &filename, const tm &now_tm) { filename_t basename, ext; std::tie(basename, ext) = details::file_helper::split_by_extension(filename); std::basic_ostringstream oss; - oss << basename.native() << '-' << std::setfill(SPDLOG_FILENAME_T('0')) << std::setw(4) << now_tm.tm_year + 1900 << '-' - << std::setw(2) << now_tm.tm_mon + 1 << '-' << std::setw(2) << now_tm.tm_mday << '-' << std::setw(2) << now_tm.tm_hour + oss << basename.native() << '_' << std::setfill(SPDLOG_FILENAME_T('0')) << std::setw(4) << now_tm.tm_year + 1900 << '-' + << std::setw(2) << now_tm.tm_mon + 1 << '-' << std::setw(2) << now_tm.tm_mday << '_' << std::setw(2) << now_tm.tm_hour << ext.native(); return oss.str(); } diff --git a/tests/test_daily_and_rotation_loggers.cpp b/tests/test_daily_and_rotation_loggers.cpp index 64154127..757404b5 100644 --- a/tests/test_daily_and_rotation_loggers.cpp +++ b/tests/test_daily_and_rotation_loggers.cpp @@ -5,6 +5,7 @@ #include "includes.h" #include "spdlog/sinks/daily_file_sink.h" #include "spdlog/sinks/rotating_file_sink.h" +#include "spdlog/sinks/hourly_file_sink.h" using filename_memory_buf_t = spdlog::memory_buf_t; @@ -95,6 +96,19 @@ TEST_CASE("daily_file_sink::daily_filename_calculator", "[daily_file_sink]") { std::match_results match; REQUIRE(std::regex_match(filename.native(), match, re)); } + + +TEST_CASE("hourly_file_sink::hourly_filename_calculator", "[hrouly_file_sink]") { + // daily_YYYY-MM-DD_hh-mm.txt + auto filename = + spdlog::sinks::hourly_filename_calculator::calc_filename(SPDLOG_FILENAME_T("hourly.txt"), spdlog::details::os::localtime()); + // date regex based on https://www.regular-expressions.info/dates.html + std::basic_regex re( + SPDLOG_FILENAME_T(R"(^hourly_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])_\d\d\.txt$)")); + + std::match_results match; + REQUIRE(std::regex_match(filename.native(), match, re)); +} #endif TEST_CASE("daily_file_sink::daily_filename_format_calculator", "[daily_file_sink]") {