Fix hourly filename calc

This commit is contained in:
Gabi Melman 2024-12-04 12:06:17 +02:00
parent a98cae60c2
commit 76474459d2
2 changed files with 17 additions and 4 deletions

View File

@ -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<filename_t::value_type> 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();
}

View File

@ -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<spdlog::filename_t::string_type::const_iterator> 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<spdlog::filename_t::value_type> 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<spdlog::filename_t::string_type::const_iterator> match;
REQUIRE(std::regex_match(filename.native(), match, re));
}
#endif
TEST_CASE("daily_file_sink::daily_filename_format_calculator", "[daily_file_sink]") {