diff --git a/include/spdlog/sinks/rotating_file_sink-inl.h b/include/spdlog/sinks/rotating_file_sink-inl.h index 785a5ec3..3c4091d3 100644 --- a/include/spdlog/sinks/rotating_file_sink-inl.h +++ b/include/spdlog/sinks/rotating_file_sink-inl.h @@ -30,11 +30,13 @@ SPDLOG_INLINE rotating_file_sink::rotating_file_sink( std::size_t max_size, std::size_t max_files, bool rotate_on_open, - const file_event_handlers &event_handlers) + const file_event_handlers &event_handlers, + std::function rotation_file_format) : base_filename_(std::move(base_filename)), max_size_(max_size), max_files_(max_files), - file_helper_{event_handlers} { + file_helper_{event_handlers}, + rotation_file_format_(std::move(rotation_file_format)) { if (max_size == 0) { throw_spdlog_ex("rotating sink constructor: max_size arg cannot be zero"); } @@ -50,12 +52,6 @@ SPDLOG_INLINE rotating_file_sink::rotating_file_sink( } } -template -SPDLOG_INLINE void rotating_file_sink::set_rotate_filename_format(std::function rotation_file_format) { - assert(!rotation_file_format_); - rotation_file_format_ = std::move(rotation_file_format); -} - template SPDLOG_INLINE filename_t rotating_file_sink::get_filename_for_rotation_(const filename_t &filename, std::size_t index) { if (rotation_file_format_) { diff --git a/include/spdlog/sinks/rotating_file_sink.h b/include/spdlog/sinks/rotating_file_sink.h index 75af0757..0b73f761 100644 --- a/include/spdlog/sinks/rotating_file_sink.h +++ b/include/spdlog/sinks/rotating_file_sink.h @@ -22,22 +22,19 @@ namespace sinks { template class rotating_file_sink final : public base_sink { public: + // @param rotation_file_format: the file format for rotation files. + // NOTE: If [index] is 0, [filename] is expected to return. rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open = false, - const file_event_handlers &event_handlers = {}); + const file_event_handlers &event_handlers = {}, + std::function rotation_file_format = {}); // Default function to get rotation filename by base filename and rotation file index. static filename_t calc_filename(const filename_t &filename, std::size_t index); filename_t filename(); void rotate_now(); - // Set the file format for rotation files. - // NOTE: - // 1. The format function is supposed to be called only once, otherwise check failure. - // 2. If [index] is 0, [filename] is expected to return. - void set_rotate_filename_format(std::function rotation_file_format); - protected: void sink_it_(const details::log_msg &msg) override; void flush_() override; diff --git a/tests/test_file_logging.cpp b/tests/test_file_logging.cpp index 01036cdd..45e76bec 100644 --- a/tests/test_file_logging.cpp +++ b/tests/test_file_logging.cpp @@ -147,14 +147,17 @@ TEST_CASE("rotating_file_logger5", "[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); - sink->set_rotate_filename_format([](const spdlog::filename_t &filename, std::size_t index) { + auto rotation_file_format = [](const spdlog::filename_t &filename, std::size_t index) -> spdlog::filename_t { if (index == 0u) { return filename; } const auto old_fname = spdlog::sinks::rotating_file_sink_st::calc_filename(filename, index); return spdlog::fmt_lib::format("{}.test_suffix", old_fname); - }); + }; + auto sink = std::make_shared( + basename, max_size, /*max_files=*/2, /*rotate_on_open=*/false, + /*event_handlers=*/spdlog::file_event_handlers{}, + /*rotation_file_format=*/std::move(rotation_file_format)); auto logger = std::make_shared("rotating_sink_logger", sink); logger->info("Test message - pre-rotation");