From 975cdf6b940532d5fc50ca3c5c56088a958eecb9 Mon Sep 17 00:00:00 2001 From: Huw Smithson Date: Wed, 27 Nov 2024 11:20:58 +0000 Subject: [PATCH 1/4] Allow manual rotation of rotating_file_sink --- include/spdlog/sinks/rotating_file_sink-inl.h | 6 ++++++ include/spdlog/sinks/rotating_file_sink.h | 1 + tests/test_file_logging.cpp | 19 +++++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/include/spdlog/sinks/rotating_file_sink-inl.h b/include/spdlog/sinks/rotating_file_sink-inl.h index bf9351eb02..8338e172dc 100644 --- a/include/spdlog/sinks/rotating_file_sink-inl.h +++ b/include/spdlog/sinks/rotating_file_sink-inl.h @@ -69,6 +69,12 @@ SPDLOG_INLINE filename_t rotating_file_sink::filename() { return file_helper_.filename(); } +template +SPDLOG_INLINE void rotating_file_sink::force_rotation() { + SPDLOG_TRY { rotate_(); } + SPDLOG_CATCH_STD +} + template SPDLOG_INLINE void rotating_file_sink::sink_it_(const details::log_msg &msg) { memory_buf_t formatted; diff --git a/include/spdlog/sinks/rotating_file_sink.h b/include/spdlog/sinks/rotating_file_sink.h index cd43d349d0..6e71e1e7f3 100644 --- a/include/spdlog/sinks/rotating_file_sink.h +++ b/include/spdlog/sinks/rotating_file_sink.h @@ -28,6 +28,7 @@ class rotating_file_sink final : public base_sink { const file_event_handlers &event_handlers = {}); static filename_t calc_filename(const filename_t &filename, std::size_t index); filename_t filename(); + void force_rotation(); 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 ac378b5cc4..559a3bd8da 100644 --- a/tests/test_file_logging.cpp +++ b/tests/test_file_logging.cpp @@ -101,3 +101,22 @@ TEST_CASE("rotating_file_logger3", "[rotating_logger]") { REQUIRE_THROWS_AS(spdlog::rotating_logger_mt("logger", basename, max_size, 0), spdlog::spdlog_ex); } + +// test forced rotation of logs +TEST_CASE("rotating_file_logger4", "[rotating_logger]") { + prepare_logdir(); + size_t max_size = 1024 * 10; + auto sink = std::make_shared(ROTATING_LOG, max_size, 2); + auto logger = std::make_shared("rotating_sink_logger", sink); + + logger->info("Test message - pre-rotation"); + logger->flush(); + + sink->force_rotation(); + + logger->info("Test message - post-rotation"); + logger->flush(); + + REQUIRE(get_filesize(ROTATING_LOG) > 0); + REQUIRE(get_filesize(ROTATING_LOG ".1") > 0); +} From e0b3ae2ba3c40fdfbb6ca07f69566106030e0674 Mon Sep 17 00:00:00 2001 From: Huw Smithson Date: Wed, 27 Nov 2024 16:12:29 +0000 Subject: [PATCH 2/4] Rename rotation method --- include/spdlog/sinks/rotating_file_sink-inl.h | 2 +- include/spdlog/sinks/rotating_file_sink.h | 2 +- tests/test_file_logging.cpp | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/spdlog/sinks/rotating_file_sink-inl.h b/include/spdlog/sinks/rotating_file_sink-inl.h index 8338e172dc..bc759066ea 100644 --- a/include/spdlog/sinks/rotating_file_sink-inl.h +++ b/include/spdlog/sinks/rotating_file_sink-inl.h @@ -70,7 +70,7 @@ SPDLOG_INLINE filename_t rotating_file_sink::filename() { } template -SPDLOG_INLINE void rotating_file_sink::force_rotation() { +SPDLOG_INLINE void rotating_file_sink::rotate_now() { SPDLOG_TRY { rotate_(); } SPDLOG_CATCH_STD } diff --git a/include/spdlog/sinks/rotating_file_sink.h b/include/spdlog/sinks/rotating_file_sink.h index 6e71e1e7f3..42bd3760c6 100644 --- a/include/spdlog/sinks/rotating_file_sink.h +++ b/include/spdlog/sinks/rotating_file_sink.h @@ -28,7 +28,7 @@ class rotating_file_sink final : public base_sink { const file_event_handlers &event_handlers = {}); static filename_t calc_filename(const filename_t &filename, std::size_t index); filename_t filename(); - void force_rotation(); + 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 559a3bd8da..477d10e20c 100644 --- a/tests/test_file_logging.cpp +++ b/tests/test_file_logging.cpp @@ -102,7 +102,7 @@ TEST_CASE("rotating_file_logger3", "[rotating_logger]") { spdlog::spdlog_ex); } -// test forced rotation of logs +// test on-demand rotation of logs TEST_CASE("rotating_file_logger4", "[rotating_logger]") { prepare_logdir(); size_t max_size = 1024 * 10; @@ -112,7 +112,7 @@ TEST_CASE("rotating_file_logger4", "[rotating_logger]") { logger->info("Test message - pre-rotation"); logger->flush(); - sink->force_rotation(); + sink->rotate_now(); logger->info("Test message - post-rotation"); logger->flush(); From ad6bdbb67aee0f2fb117c0e55cfd9e5c18e2c1f6 Mon Sep 17 00:00:00 2001 From: Huw Smithson Date: Wed, 27 Nov 2024 16:25:36 +0000 Subject: [PATCH 3/4] Attempted fix for tests on Windows --- tests/test_file_logging.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_file_logging.cpp b/tests/test_file_logging.cpp index 477d10e20c..0e4902be4c 100644 --- a/tests/test_file_logging.cpp +++ b/tests/test_file_logging.cpp @@ -106,7 +106,8 @@ TEST_CASE("rotating_file_logger3", "[rotating_logger]") { TEST_CASE("rotating_file_logger4", "[rotating_logger]") { prepare_logdir(); size_t max_size = 1024 * 10; - auto sink = std::make_shared(ROTATING_LOG, max_size, 2); + 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"); From e0ac126175c1a49bb75612d7addc990f5944855c Mon Sep 17 00:00:00 2001 From: Huw Smithson Date: Thu, 28 Nov 2024 11:10:39 +0000 Subject: [PATCH 4/4] Apply review mark-ups --- include/spdlog/sinks/rotating_file_sink-inl.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/spdlog/sinks/rotating_file_sink-inl.h b/include/spdlog/sinks/rotating_file_sink-inl.h index bc759066ea..9f9aa47a7c 100644 --- a/include/spdlog/sinks/rotating_file_sink-inl.h +++ b/include/spdlog/sinks/rotating_file_sink-inl.h @@ -71,8 +71,7 @@ SPDLOG_INLINE filename_t rotating_file_sink::filename() { template SPDLOG_INLINE void rotating_file_sink::rotate_now() { - SPDLOG_TRY { rotate_(); } - SPDLOG_CATCH_STD + rotate_(); } template