From bb4b719ccf31376f35152ca4d2e06d606a1e3f63 Mon Sep 17 00:00:00 2001 From: hjs-ast Date: Thu, 28 Nov 2024 17:37:29 +0200 Subject: [PATCH] Allow manual rotation of rotating_file_sink (#3269) * Allow manual rotation of rotating_file_sink * Rename rotation method * Attempted fix for tests on Windows * Apply review mark-ups --- include/spdlog/sinks/rotating_file_sink.h | 1 + tests/test_file_logging.cpp | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/include/spdlog/sinks/rotating_file_sink.h b/include/spdlog/sinks/rotating_file_sink.h index 71804ad5..91b49e0f 100644 --- a/include/spdlog/sinks/rotating_file_sink.h +++ b/include/spdlog/sinks/rotating_file_sink.h @@ -27,6 +27,7 @@ public: static filename_t calc_filename(const filename_t &filename, std::size_t index); filename_t filename(); + void rotate_now(); protected: void sink_it_(const details::log_msg &msg) override; diff --git a/tests/test_file_logging.cpp b/tests/test_file_logging.cpp index 5ba4b15b..50a6baec 100644 --- a/tests/test_file_logging.cpp +++ b/tests/test_file_logging.cpp @@ -100,3 +100,23 @@ TEST_CASE("rotating_file_logger3", "[rotating_logger]") { spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG); REQUIRE_THROWS_AS(spdlog::rotating_logger_mt("logger", basename, max_size, 0), spdlog::spdlog_ex); } + +// test on-demand rotation of logs +TEST_CASE("rotating_file_logger4", "[rotating_logger]") { + prepare_logdir(); + size_t max_size = 1024 * 10; + spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG); + auto sink = std::make_shared(basename, max_size, 2); + auto logger = std::make_shared("rotating_sink_logger", sink); + + logger->info("Test message - pre-rotation"); + logger->flush(); + + sink->rotate_now(); + + logger->info("Test message - post-rotation"); + logger->flush(); + + REQUIRE(get_filesize(ROTATING_LOG) > 0); + REQUIRE(get_filesize(ROTATING_LOG ".1") > 0); +}