From 5b03dc179658a63c8c7b409512f9d01c5e71ed44 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 5 Feb 2022 17:37:55 +0200 Subject: [PATCH] Throw if rotating_file_sink constructor receives max_size==0 as arg --- include/spdlog/sinks/rotating_file_sink-inl.h | 4 ++++ tests/test_file_logging.cpp | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/include/spdlog/sinks/rotating_file_sink-inl.h b/include/spdlog/sinks/rotating_file_sink-inl.h index faff7820..e32e20d4 100644 --- a/include/spdlog/sinks/rotating_file_sink-inl.h +++ b/include/spdlog/sinks/rotating_file_sink-inl.h @@ -31,6 +31,10 @@ SPDLOG_INLINE rotating_file_sink::rotating_file_sink( , max_files_(max_files) , file_helper_{event_handlers} { + if(max_size == 0) + { + throw_spdlog_ex("rotating sink constructor: max_size arg cannot be zero"); + } file_helper_.open(calc_filename(base_filename_, 0)); current_size_ = file_helper_.size(); // expensive. called only once if (rotate_on_open && current_size_ > 0) diff --git a/tests/test_file_logging.cpp b/tests/test_file_logging.cpp index c808916d..1c7a1853 100644 --- a/tests/test_file_logging.cpp +++ b/tests/test_file_logging.cpp @@ -98,3 +98,12 @@ TEST_CASE("rotating_file_logger2", "[rotating_logger]]") REQUIRE(get_filesize(ROTATING_LOG) <= max_size); REQUIRE(get_filesize(ROTATING_LOG ".1") <= max_size); } + +// test that passing max_size=0 throws +TEST_CASE("rotating_file_logger3", "[rotating_logger]]") +{ + prepare_logdir(); + size_t max_size = 0; + spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG); + REQUIRE_THROWS_AS(spdlog::rotating_logger_mt("logger", basename, max_size, 0), spdlog::spdlog_ex); +}