6#ifndef SPDLOG_HEADER_ONLY
28 template <
typename Mutex>
31 : base_filename_(
std::move(base_filename)), max_size_(max_size), max_files_(max_files), file_helper_{event_handlers}
35 throw_spdlog_ex(
"rotating sink constructor: max_size arg cannot be zero");
38 if (max_files > 200000)
40 throw_spdlog_ex(
"rotating sink constructor: max_files arg cannot exceed 200000");
53 template <
typename Mutex>
66 template <
typename Mutex>
70 return file_helper_.filename();
73 template <
typename Mutex>
78 auto new_size = current_size_ + formatted.size();
83 if (new_size > max_size_)
86 if (file_helper_.size() > 0)
89 new_size = formatted.size();
92 file_helper_.write(formatted);
93 current_size_ = new_size;
96 template <
typename Mutex>
107 template <
typename Mutex>
113 file_helper_.close();
114 for (
auto i = max_files_;
i > 0; --
i)
116 filename_t src = calc_filename(base_filename_,
i - 1);
117 if (!path_exists(src))
121 filename_t target = calc_filename(base_filename_,
i);
123 if (!rename_file_(src, target))
129 if (!rename_file_(src, target))
131 file_helper_.reopen(
true);
133 throw_spdlog_ex(
"rotating_file_sink: failed renaming " + filename_to_str(src) +
" to " + filename_to_str(target), errno);
137 file_helper_.reopen(
true);
142 template <
typename Mutex>
size_t size() const
Definition file_helper-inl.h:133
static std::tuple< filename_t, filename_t > split_by_extension(const filename_t &fname)
Definition file_helper-inl.h:160
void open(const filename_t &fname, bool truncate=false)
Definition file_helper-inl.h:35
Definition base_sink.h:22
void flush() final
Definition base_sink-inl.h:35
bool rename_file_(const filename_t &src_filename, const filename_t &target_filename)
Definition rotating_file_sink-inl.h:143
filename_t base_filename_
Definition rotating_file_sink.h:47
void flush_() override
Definition rotating_file_sink-inl.h:97
std::size_t current_size_
Definition rotating_file_sink.h:50
filename_t filename()
Definition rotating_file_sink-inl.h:67
static filename_t calc_filename(const filename_t &filename, std::size_t index)
Definition rotating_file_sink-inl.h:54
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={})
Definition rotating_file_sink-inl.h:29
void sink_it_(const details::log_msg &msg) override
Definition rotating_file_sink-inl.h:74
details::file_helper file_helper_
Definition rotating_file_sink.h:51
void rotate_()
Definition rotating_file_sink-inl.h:108
#define SPDLOG_FILENAME_T(s)
Definition common.h:132
#define SPDLOG_INLINE
Definition common.h:47
SPDLOG_INLINE std::string filename_to_str(const filename_t &filename)
Definition os-inl.h:399
SPDLOG_INLINE int remove(const filename_t &filename) SPDLOG_NOEXCEPT
Definition os-inl.h:168
SPDLOG_INLINE int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT
Definition os-inl.h:182
SPDLOG_INLINE bool path_exists(const filename_t &filename) SPDLOG_NOEXCEPT
Definition os-inl.h:192
SPDLOG_INLINE void sleep_for_millis(unsigned int milliseconds) SPDLOG_NOEXCEPT
Definition os-inl.h:381
SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno)
Definition common-inl.h:75
std::string filename_t
Definition common.h:131
fmt::basic_memory_buffer< char, 250 > memory_buf_t
Definition common.h:173
u
Definition tag_strings.h:62
i
Definition tag_strings.h:60