use constructor instead of setter

pull/3296/head
hjiang 8 months ago
parent 9975c01204
commit b7f7c3f167

@ -30,11 +30,13 @@ SPDLOG_INLINE rotating_file_sink<Mutex>::rotating_file_sink(
std::size_t max_size, std::size_t max_size,
std::size_t max_files, std::size_t max_files,
bool rotate_on_open, bool rotate_on_open,
const file_event_handlers &event_handlers) const file_event_handlers &event_handlers,
std::function<filename_t(const filename_t &filename, std::size_t index)> rotation_file_format)
: base_filename_(std::move(base_filename)), : base_filename_(std::move(base_filename)),
max_size_(max_size), max_size_(max_size),
max_files_(max_files), max_files_(max_files),
file_helper_{event_handlers} { file_helper_{event_handlers},
rotation_file_format_(std::move(rotation_file_format)) {
if (max_size == 0) { if (max_size == 0) {
throw_spdlog_ex("rotating sink constructor: max_size arg cannot be zero"); throw_spdlog_ex("rotating sink constructor: max_size arg cannot be zero");
} }
@ -50,12 +52,6 @@ SPDLOG_INLINE rotating_file_sink<Mutex>::rotating_file_sink(
} }
} }
template <typename Mutex>
SPDLOG_INLINE void rotating_file_sink<Mutex>::set_rotate_filename_format(std::function<filename_t(const filename_t &filename, std::size_t index)> rotation_file_format) {
assert(!rotation_file_format_);
rotation_file_format_ = std::move(rotation_file_format);
}
template <typename Mutex> template <typename Mutex>
SPDLOG_INLINE filename_t rotating_file_sink<Mutex>::get_filename_for_rotation_(const filename_t &filename, std::size_t index) { SPDLOG_INLINE filename_t rotating_file_sink<Mutex>::get_filename_for_rotation_(const filename_t &filename, std::size_t index) {
if (rotation_file_format_) { if (rotation_file_format_) {

@ -22,22 +22,19 @@ namespace sinks {
template <typename Mutex> template <typename Mutex>
class rotating_file_sink final : public base_sink<Mutex> { class rotating_file_sink final : public base_sink<Mutex> {
public: 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, rotating_file_sink(filename_t base_filename,
std::size_t max_size, std::size_t max_size,
std::size_t max_files, std::size_t max_files,
bool rotate_on_open = false, bool rotate_on_open = false,
const file_event_handlers &event_handlers = {}); const file_event_handlers &event_handlers = {},
std::function<filename_t(const filename_t &filename, std::size_t index)> rotation_file_format = {});
// Default function to get rotation filename by base filename and rotation file index. // 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); static filename_t calc_filename(const filename_t &filename, std::size_t index);
filename_t filename(); filename_t filename();
void rotate_now(); 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<filename_t(const filename_t &filename, std::size_t index)> rotation_file_format);
protected: protected:
void sink_it_(const details::log_msg &msg) override; void sink_it_(const details::log_msg &msg) override;
void flush_() override; void flush_() override;

@ -147,14 +147,17 @@ TEST_CASE("rotating_file_logger5", "[rotating_logger]") {
prepare_logdir(); prepare_logdir();
size_t max_size = 1024 * 10; size_t max_size = 1024 * 10;
spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG); spdlog::filename_t basename = SPDLOG_FILENAME_T(ROTATING_LOG);
auto sink = std::make_shared<spdlog::sinks::rotating_file_sink_st>(basename, max_size, 2); auto rotation_file_format = [](const spdlog::filename_t &filename, std::size_t index) -> spdlog::filename_t {
sink->set_rotate_filename_format([](const spdlog::filename_t &filename, std::size_t index) {
if (index == 0u) { if (index == 0u) {
return filename; return filename;
} }
const auto old_fname = spdlog::sinks::rotating_file_sink_st::calc_filename(filename, index); const auto old_fname = spdlog::sinks::rotating_file_sink_st::calc_filename(filename, index);
return spdlog::fmt_lib::format("{}.test_suffix", old_fname); return spdlog::fmt_lib::format("{}.test_suffix", old_fname);
}); };
auto sink = std::make_shared<spdlog::sinks::rotating_file_sink_st>(
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<spdlog::logger>("rotating_sink_logger", sink); auto logger = std::make_shared<spdlog::logger>("rotating_sink_logger", sink);
logger->info("Test message - pre-rotation"); logger->info("Test message - pre-rotation");

Loading…
Cancel
Save