File indexing completed on 2025-12-17 10:29:02
0001
0002
0003
0004 #pragma once
0005
0006 #include <spdlog/common.h>
0007 #include <spdlog/details/circular_q.h>
0008 #include <spdlog/details/file_helper.h>
0009 #include <spdlog/details/null_mutex.h>
0010 #include <spdlog/details/os.h>
0011 #include <spdlog/details/synchronous_factory.h>
0012 #include <spdlog/fmt/chrono.h>
0013 #include <spdlog/fmt/fmt.h>
0014 #include <spdlog/sinks/base_sink.h>
0015
0016 #include <chrono>
0017 #include <cstdio>
0018 #include <iomanip>
0019 #include <mutex>
0020 #include <sstream>
0021 #include <string>
0022
0023 namespace spdlog {
0024 namespace sinks {
0025
0026
0027
0028
0029 struct daily_filename_calculator {
0030
0031 static filename_t calc_filename(const filename_t &filename, const tm &now_tm) {
0032 filename_t basename, ext;
0033 std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
0034 return fmt_lib::format(SPDLOG_FMT_STRING(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}")),
0035 basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday,
0036 ext);
0037 }
0038 };
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049 struct daily_filename_format_calculator {
0050 static filename_t calc_filename(const filename_t &file_path, const tm &now_tm) {
0051 #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
0052 std::wstringstream stream;
0053 #else
0054 std::stringstream stream;
0055 #endif
0056 stream << std::put_time(&now_tm, file_path.c_str());
0057 return stream.str();
0058 }
0059 };
0060
0061
0062
0063
0064
0065
0066 template <typename Mutex, typename FileNameCalc = daily_filename_calculator>
0067 class daily_file_sink final : public base_sink<Mutex> {
0068 public:
0069
0070 daily_file_sink(filename_t base_filename,
0071 int rotation_hour,
0072 int rotation_minute,
0073 bool truncate = false,
0074 uint16_t max_files = 0,
0075 const file_event_handlers &event_handlers = {})
0076 : base_filename_(std::move(base_filename)),
0077 rotation_h_(rotation_hour),
0078 rotation_m_(rotation_minute),
0079 file_helper_{event_handlers},
0080 truncate_(truncate),
0081 max_files_(max_files),
0082 filenames_q_() {
0083 if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 ||
0084 rotation_minute > 59) {
0085 throw_spdlog_ex("daily_file_sink: Invalid rotation time in ctor");
0086 }
0087
0088 auto now = log_clock::now();
0089 auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
0090 file_helper_.open(filename, truncate_);
0091 rotation_tp_ = next_rotation_tp_();
0092
0093 if (max_files_ > 0) {
0094 init_filenames_q_();
0095 }
0096 }
0097
0098 filename_t filename() {
0099 std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
0100 return file_helper_.filename();
0101 }
0102
0103 protected:
0104 void sink_it_(const details::log_msg &msg) override {
0105 auto time = msg.time;
0106 bool should_rotate = time >= rotation_tp_;
0107 if (should_rotate) {
0108 auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time));
0109 file_helper_.open(filename, truncate_);
0110 rotation_tp_ = next_rotation_tp_();
0111 }
0112 memory_buf_t formatted;
0113 base_sink<Mutex>::formatter_->format(msg, formatted);
0114 file_helper_.write(formatted);
0115
0116
0117 if (should_rotate && max_files_ > 0) {
0118 delete_old_();
0119 }
0120 }
0121
0122 void flush_() override { file_helper_.flush(); }
0123
0124 private:
0125 void init_filenames_q_() {
0126 using details::os::path_exists;
0127
0128 filenames_q_ = details::circular_q<filename_t>(static_cast<size_t>(max_files_));
0129 std::vector<filename_t> filenames;
0130 auto now = log_clock::now();
0131 while (filenames.size() < max_files_) {
0132 auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
0133 if (!path_exists(filename)) {
0134 break;
0135 }
0136 filenames.emplace_back(filename);
0137 now -= std::chrono::hours(24);
0138 }
0139 for (auto iter = filenames.rbegin(); iter != filenames.rend(); ++iter) {
0140 filenames_q_.push_back(std::move(*iter));
0141 }
0142 }
0143
0144 tm now_tm(log_clock::time_point tp) {
0145 time_t tnow = log_clock::to_time_t(tp);
0146 return spdlog::details::os::localtime(tnow);
0147 }
0148
0149 log_clock::time_point next_rotation_tp_() {
0150 auto now = log_clock::now();
0151 tm date = now_tm(now);
0152 date.tm_hour = rotation_h_;
0153 date.tm_min = rotation_m_;
0154 date.tm_sec = 0;
0155 auto rotation_time = log_clock::from_time_t(std::mktime(&date));
0156 if (rotation_time > now) {
0157 return rotation_time;
0158 }
0159 return {rotation_time + std::chrono::hours(24)};
0160 }
0161
0162
0163
0164 void delete_old_() {
0165 using details::os::filename_to_str;
0166 using details::os::remove_if_exists;
0167
0168 filename_t current_file = file_helper_.filename();
0169 if (filenames_q_.full()) {
0170 auto old_filename = std::move(filenames_q_.front());
0171 filenames_q_.pop_front();
0172 bool ok = remove_if_exists(old_filename) == 0;
0173 if (!ok) {
0174 filenames_q_.push_back(std::move(current_file));
0175 throw_spdlog_ex("Failed removing daily file " + filename_to_str(old_filename),
0176 errno);
0177 }
0178 }
0179 filenames_q_.push_back(std::move(current_file));
0180 }
0181
0182 filename_t base_filename_;
0183 int rotation_h_;
0184 int rotation_m_;
0185 log_clock::time_point rotation_tp_;
0186 details::file_helper file_helper_;
0187 bool truncate_;
0188 uint16_t max_files_;
0189 details::circular_q<filename_t> filenames_q_;
0190 };
0191
0192 using daily_file_sink_mt = daily_file_sink<std::mutex>;
0193 using daily_file_sink_st = daily_file_sink<details::null_mutex>;
0194 using daily_file_format_sink_mt = daily_file_sink<std::mutex, daily_filename_format_calculator>;
0195 using daily_file_format_sink_st =
0196 daily_file_sink<details::null_mutex, daily_filename_format_calculator>;
0197
0198 }
0199
0200
0201
0202
0203 template <typename Factory = spdlog::synchronous_factory>
0204 inline std::shared_ptr<logger> daily_logger_mt(const std::string &logger_name,
0205 const filename_t &filename,
0206 int hour = 0,
0207 int minute = 0,
0208 bool truncate = false,
0209 uint16_t max_files = 0,
0210 const file_event_handlers &event_handlers = {}) {
0211 return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute,
0212 truncate, max_files, event_handlers);
0213 }
0214
0215 template <typename Factory = spdlog::synchronous_factory>
0216 inline std::shared_ptr<logger> daily_logger_format_mt(
0217 const std::string &logger_name,
0218 const filename_t &filename,
0219 int hour = 0,
0220 int minute = 0,
0221 bool truncate = false,
0222 uint16_t max_files = 0,
0223 const file_event_handlers &event_handlers = {}) {
0224 return Factory::template create<sinks::daily_file_format_sink_mt>(
0225 logger_name, filename, hour, minute, truncate, max_files, event_handlers);
0226 }
0227
0228 template <typename Factory = spdlog::synchronous_factory>
0229 inline std::shared_ptr<logger> daily_logger_st(const std::string &logger_name,
0230 const filename_t &filename,
0231 int hour = 0,
0232 int minute = 0,
0233 bool truncate = false,
0234 uint16_t max_files = 0,
0235 const file_event_handlers &event_handlers = {}) {
0236 return Factory::template create<sinks::daily_file_sink_st>(logger_name, filename, hour, minute,
0237 truncate, max_files, event_handlers);
0238 }
0239
0240 template <typename Factory = spdlog::synchronous_factory>
0241 inline std::shared_ptr<logger> daily_logger_format_st(
0242 const std::string &logger_name,
0243 const filename_t &filename,
0244 int hour = 0,
0245 int minute = 0,
0246 bool truncate = false,
0247 uint16_t max_files = 0,
0248 const file_event_handlers &event_handlers = {}) {
0249 return Factory::template create<sinks::daily_file_format_sink_st>(
0250 logger_name, filename, hour, minute, truncate, max_files, event_handlers);
0251 }
0252 }