|
|
|
@ -6,7 +6,8 @@
|
|
|
|
|
#include "spdlog/sinks/daily_file_sink.h"
|
|
|
|
|
#include "spdlog/sinks/rotating_file_sink.h"
|
|
|
|
|
|
|
|
|
|
using filename_memory_buf_t = spdlog::memory_buf_t;
|
|
|
|
|
using file_t_sstream = std::basic_stringstream<spdlog::filename_t::value_type>;
|
|
|
|
|
using spdlog::details::os::filename_to_str;
|
|
|
|
|
|
|
|
|
|
TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]") {
|
|
|
|
|
using sink_type = spdlog::sinks::daily_file_sink<std::mutex, spdlog::sinks::daily_filename_calculator>;
|
|
|
|
@ -15,10 +16,12 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]") {
|
|
|
|
|
|
|
|
|
|
// calculate filename (time based)
|
|
|
|
|
spdlog::filename_t basename = SPDLOG_FILENAME_T("test_logs/daily_dateonly");
|
|
|
|
|
std::tm tm = spdlog::details::os::localtime();
|
|
|
|
|
filename_memory_buf_t w;
|
|
|
|
|
spdlog::fmt_lib::format_to(std::back_inserter(w), SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}"), basename, tm.tm_year + 1900,
|
|
|
|
|
tm.tm_mon + 1, tm.tm_mday);
|
|
|
|
|
std::tm now_tm = spdlog::details::os::localtime();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//spdlog::fmt_lib::format_to(std::back_inserter(w), SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}"), basename, tm.tm_year + 1900,
|
|
|
|
|
// tm.tm_mon + 1, tm.tm_mday);
|
|
|
|
|
|
|
|
|
|
auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
|
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
@ -26,39 +29,17 @@ TEST_CASE("daily_logger with dateonly calculator", "[daily_logger]") {
|
|
|
|
|
}
|
|
|
|
|
logger->flush();
|
|
|
|
|
|
|
|
|
|
require_message_count(SPDLOG_BUF_TO_STRING(w), 10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct custom_daily_file_name_calculator {
|
|
|
|
|
static spdlog::filename_t calc_filename(const spdlog::filename_t &basename, const tm &now_tm) {
|
|
|
|
|
filename_memory_buf_t w;
|
|
|
|
|
spdlog::fmt_lib::format_to(std::back_inserter(w), SPDLOG_FILENAME_T("{}{:04d}{:02d}{:02d}"), basename,
|
|
|
|
|
now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday);
|
|
|
|
|
// calcluate expected filename and check if it exists with the expected message count
|
|
|
|
|
file_t_sstream stream;
|
|
|
|
|
auto sep = SPDLOG_FILENAME_T('-');
|
|
|
|
|
stream << basename.native() << SPDLOG_FILENAME_T('_') << std::setfill(SPDLOG_FILENAME_T('0')) << std::setw(4)
|
|
|
|
|
<< now_tm.tm_year + 1900 << sep << std::setw(2) << now_tm.tm_mon + 1 << sep << std::setw(2) << now_tm.tm_mday;
|
|
|
|
|
|
|
|
|
|
return SPDLOG_BUF_TO_STRING(w);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TEST_CASE("daily_logger with custom calculator", "[daily_logger]") {
|
|
|
|
|
using sink_type = spdlog::sinks::daily_file_sink<std::mutex, custom_daily_file_name_calculator>;
|
|
|
|
|
|
|
|
|
|
prepare_logdir();
|
|
|
|
|
require_message_count(filename_to_str(stream.str()), 10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// calculate filename (time based)
|
|
|
|
|
spdlog::filename_t basename = SPDLOG_FILENAME_T("test_logs/daily_dateonly");
|
|
|
|
|
std::tm tm = spdlog::details::os::localtime();
|
|
|
|
|
filename_memory_buf_t w;
|
|
|
|
|
spdlog::fmt_lib::format_to(std::back_inserter(w), SPDLOG_FILENAME_T("{}{:04d}{:02d}{:02d}"), basename, tm.tm_year + 1900,
|
|
|
|
|
tm.tm_mon + 1, tm.tm_mday);
|
|
|
|
|
|
|
|
|
|
auto logger = spdlog::create<sink_type>("logger", basename, 0, 0);
|
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
|
logger->info("Test message {}", i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger->flush();
|
|
|
|
|
require_message_count(SPDLOG_BUF_TO_STRING(w), 10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* File name calculations
|
|
|
|
@ -86,13 +67,19 @@ TEST_CASE("rotating_file_sink::calc_filename3", "[rotating_file_sink]") {
|
|
|
|
|
|
|
|
|
|
TEST_CASE("daily_file_sink::daily_filename_calculator", "[daily_file_sink]") {
|
|
|
|
|
// daily_YYYY-MM-DD_hh-mm.txt
|
|
|
|
|
auto now = spdlog::details::os::localtime();
|
|
|
|
|
auto filename =
|
|
|
|
|
spdlog::sinks::daily_filename_calculator::calc_filename(SPDLOG_FILENAME_T("daily.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"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\.txt$)"));
|
|
|
|
|
std::match_results<spdlog::filename_t::const_iterator> match;
|
|
|
|
|
REQUIRE(std::regex_match(filename, match, re));
|
|
|
|
|
spdlog::sinks::daily_filename_calculator::calc_filename(SPDLOG_FILENAME_T("daily.txt"), now);
|
|
|
|
|
|
|
|
|
|
file_t_sstream stream;
|
|
|
|
|
stream << "daily_" << now.tm_year + 1900 << "-" << std::setw(2) << std::setfill(SPDLOG_FILENAME_T('0')) << now.tm_mon + 1 << "-"
|
|
|
|
|
<< std::setw(2) << std::setfill(SPDLOG_FILENAME_T('0')) << now.tm_mday << ".txt";
|
|
|
|
|
REQUIRE(filename == std::filesystem::path(stream.str()));
|
|
|
|
|
//// date regex based on https://www.regular-expressions.info/dates.html
|
|
|
|
|
//std::basic_regex<spdlog::filename_t::value_type> re(
|
|
|
|
|
// SPDLOG_FILENAME_T(R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\.txt$)"));
|
|
|
|
|
//std::match_results<spdlog::filename_t::const_iterator> match;
|
|
|
|
|
//REQUIRE(std::regex_match(filename, match, re));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
@ -101,8 +88,13 @@ TEST_CASE("daily_file_sink::daily_filename_format_calculator", "[daily_file_sink
|
|
|
|
|
// example-YYYY-MM-DD.log
|
|
|
|
|
auto filename = spdlog::sinks::daily_filename_format_calculator::calc_filename(SPDLOG_FILENAME_T("example-%Y-%m-%d.log"), tm);
|
|
|
|
|
|
|
|
|
|
REQUIRE(filename == spdlog::fmt_lib::format(SPDLOG_FILENAME_T("example-{:04d}-{:02d}-{:02d}.log"), tm.tm_year + 1900,
|
|
|
|
|
tm.tm_mon + 1, tm.tm_mday));
|
|
|
|
|
// calc excpected filename
|
|
|
|
|
file_t_sstream stream;
|
|
|
|
|
stream << "example-" << tm.tm_year + 1900 << "-" << std::setw(2) << std::setfill(SPDLOG_FILENAME_T('0')) << tm.tm_mon + 1
|
|
|
|
|
<< "-" << std::setw(2) << std::setfill(SPDLOG_FILENAME_T('0')) << tm.tm_mday << ".log";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
REQUIRE(filename == std::filesystem::path(stream.str()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Test removal of old files */
|
|
|
|
|