From ba120e524b5a0e6e772c67c0aba45d493a22f49f Mon Sep 17 00:00:00 2001 From: Charless Milette Date: Mon, 15 Nov 2021 15:46:22 -0500 Subject: [PATCH] Add unit test for daily_filename_format_calculator --- include/spdlog/sinks/daily_file_sink.h | 2 +- tests/test_daily_logger.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index c1c87607..b3323780 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -75,7 +75,7 @@ struct daily_filename_format_calculator return buf; #else fmt::basic_memory_buffer tm_format; - tm_format.append(specs.begin(), specs.end()); + tm_format.append(filename.c_str(), filename.c_str() + filename.size()); // By appending an extra space we can distinguish an empty result that // indicates insufficient buffer size from a guaranteed non-empty result // https://github.com/fmtlib/fmt/issues/2238 diff --git a/tests/test_daily_logger.cpp b/tests/test_daily_logger.cpp index 93bd4662..66f01a46 100644 --- a/tests/test_daily_logger.cpp +++ b/tests/test_daily_logger.cpp @@ -142,6 +142,16 @@ TEST_CASE("daily_file_sink::daily_filename_calculator", "[daily_file_sink]]") } #endif +TEST_CASE("daily_file_sink::daily_filename_format_calculator", "[daily_file_sink]]") +{ + std::tm tm = spdlog::details::os::localtime(); + // 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)); +} + /* Test removal of old files */ static spdlog::details::log_msg create_msg(std::chrono::seconds offset) {