mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-14 17:52:05 +08:00
Fix hourly filename calc
This commit is contained in:
parent
a98cae60c2
commit
76474459d2
@ -22,16 +22,15 @@ namespace spdlog {
|
|||||||
namespace sinks {
|
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 {
|
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) {
|
static filename_t calc_filename(const filename_t &filename, const tm &now_tm) {
|
||||||
filename_t basename, ext;
|
filename_t basename, ext;
|
||||||
std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
|
std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
|
||||||
std::basic_ostringstream<filename_t::value_type> oss;
|
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 << '-'
|
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
|
<< std::setw(2) << now_tm.tm_mon + 1 << '-' << std::setw(2) << now_tm.tm_mday << '_' << std::setw(2) << now_tm.tm_hour
|
||||||
<< ext.native();
|
<< ext.native();
|
||||||
return oss.str();
|
return oss.str();
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#include "includes.h"
|
#include "includes.h"
|
||||||
#include "spdlog/sinks/daily_file_sink.h"
|
#include "spdlog/sinks/daily_file_sink.h"
|
||||||
#include "spdlog/sinks/rotating_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;
|
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;
|
std::match_results<spdlog::filename_t::string_type::const_iterator> match;
|
||||||
REQUIRE(std::regex_match(filename.native(), match, re));
|
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
|
#endif
|
||||||
|
|
||||||
TEST_CASE("daily_file_sink::daily_filename_format_calculator", "[daily_file_sink]") {
|
TEST_CASE("daily_file_sink::daily_filename_format_calculator", "[daily_file_sink]") {
|
||||||
|
Loading…
Reference in New Issue
Block a user