Debian Codegraph @

src/rollingfilesink_p.h

raw
1 // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4
5 #pragma once
6
7 #include <spdlog/spdlog.h>
8 #include <spdlog/details/file_helper.h>
9 #include <spdlog/details/null_mutex.h>
10 #include <spdlog/fmt/fmt.h>
11 #include <spdlog/sinks/base_sink.h>
12 #include <spdlog/details/os.h>
13
14 #include <QDir>
15 #include <chrono>
16 #include <cstdio>
17 #include <ctime>
18 #include <mutex>
19 #include <string>
20
21 #include <dlog_global.h>
22
23 #ifndef SPDLOG_VERSION_CHECK
24 #define SPDLOG_VERSION_CHECK(major, minor, patch) (major * 10000 + minor * 100 + patch)
25 #endif
26 DLOG_CORE_BEGIN_NAMESPACE
27
28 using namespace spdlog;
29
30 /*
31 * Generator of Hourly log file names in format basename.ext.YYYY-MM-DD-HH-MM-SS
32 */
33 struct rolling_filename_calculator
34 {
35 // Create filename for the form basename.ext.YYYY-MM-DD-HH-MM-SS
36 static filename_t calc_filename(const filename_t &filename, const tm &now_tm)
37 {
38 #if SPDLOG_VERSION < SPDLOG_VERSION_CHECK(1, 10, 0)
39 #if SPDLOG_VERSION < SPDLOG_VERSION_CHECK(1, 4, 0)
40 std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::basic_memory_buffer<char, 250>, fmt::basic_string_view<wchar_t>>::type w;
41 #else
42 std::conditional<std::is_same<filename_t::value_type, char>::value, spdlog::memory_buf_t, spdlog::wmemory_buf_t>::type w;
43 #endif
44 fmt::format_to(w, SPDLOG_FILENAME_T("{}.{:04d}-{:02d}-{:02d}-{:02d}-{:02d}-{:02d}"), filename,
45 now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday,
46 now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec);
47 return fmt::to_string(w);
48 #else
49 return fmt_lib::format(SPDLOG_FILENAME_T("{}.{:04d}-{:02d}-{:02d}-{:02d}-{:02d}-{:02d}"), filename,
50 now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday,
51 now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec);
52 #endif
53 }
54 };
55
56 enum RollingInterval
57 {
58 RI_Minutely = 0,
59 RI_Hourly,
60 RI_HalfDaily,
61 RI_Daily,
62 RI_Weekly,
63 RI_Monthly
64 };
65
66 /*
67 * Rolling file sink based on time and file size.
68 * If max_files > 0, retain only the last max_files and delete previous.
69 */
70 template <typename Mutex, typename FileNameCalc = rolling_filename_calculator>
71 class rolling_file_sink final : public sinks::base_sink<Mutex>
72 {
73 public:
74 // create rolling file sink which rollings on given interval
75 rolling_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files,
76 bool rolling_on_open = false, RollingInterval interval = RI_Daily)
77 : base_filename_(std::move(base_filename)), rolling_on_open_(rolling_on_open), interval_(interval), filenames_q_()
78 {
79 set_max_size(max_size);
80 set_max_files(max_files);
81
82 auto filename = base_filename_;
83 file_helper_.open(filename, false);
84 current_size_ = file_helper_.size(); // expensive. called only once
85 rotation_tp_ = next_rotation_tp_();
86 if (rolling_on_open && current_size_ > 0)
87 {
88 rolling_();
89 current_size_ = 0;
90 }
91 }
92
93 filename_t filename()
94 {
95 std::lock_guard<Mutex> lock(sinks::base_sink<Mutex>::mutex_);
96 return file_helper_.filename();
97 }
98 size_t filesize()
99 {
100 std::lock_guard<Mutex> lock(sinks::base_sink<Mutex>::mutex_);
101 return file_helper_.size();
102 }
103 void set_max_files(std::size_t max_files)
104 {
105 std::lock_guard<Mutex> lock(sinks::base_sink<Mutex>::mutex_);
106 if (max_files > 200000)
107 {
108 throw spdlog_ex("rolling sink constructor: max_files arg cannot exceed 200000");
109 }
110 max_files_ = max_files;
111 if (max_files > 0)
112 init_filenames_q_();
113 }
114 void set_max_size(std::size_t max_size)
115 {
116 std::lock_guard<Mutex> lock(sinks::base_sink<Mutex>::mutex_);
117 if (max_size == 0)
118 {
119 throw spdlog_ex("rolling sink constructor: max_size arg cannot be zero");
120 }
121 max_size_ = max_size;
122 }
123 void set_interval(RollingInterval interval)
124 {
125 std::lock_guard<Mutex> lock(sinks::base_sink<Mutex>::mutex_);
126 interval_ = interval;
127 rotation_tp_ = next_rotation_tp_();
128 }
129
130 protected:
131 void sink_it_(const details::log_msg &msg) override
132 {
133 #if SPDLOG_VERSION < SPDLOG_VERSION_CHECK(1, 4, 0)
134 fmt::memory_buffer formatted;
135 #else
136 memory_buf_t formatted;
137 #endif
138 sinks::base_sink<Mutex>::formatter_->format(msg, formatted);
139 auto new_size = current_size_ + formatted.size();
140
141 auto time = msg.time;
142 bool should_rolling = time >= rotation_tp_ || new_size > max_size_;
143 if (should_rolling)
144 {
145 file_helper_.flush();
146 if (file_helper_.size() > 0)
147 {
148 rolling_();
149 new_size = formatted.size();
150 }
151 }
152
153 file_helper_.write(formatted);
154 current_size_ = new_size;
155
156 // Do the cleaning only at the end because it might throw on failure.
157 if (should_rolling && max_files_ > 0)
158 {
159 delete_old_();
160 }
161 }
162
163 void flush_() override
164 {
165 file_helper_.flush();
166 }
167
168 private:
169 void init_filenames_q_()
170 {
171 filenames_q_.clear();
172 QDir dir(QString::fromStdString(base_filename_));
173 dir.cdUp();
174
175 auto namefilter = QFileInfo(base_filename_.c_str()).fileName().append("*");
176
177 auto fileInfos = dir.entryInfoList({namefilter}, QDir::NoDotAndDotDot | QDir::Files, QDir::Name);
178 for (const auto &fi : fileInfos)
179 {
180 if (fi.filePath().compare(base_filename_.c_str()))
181 filenames_q_.push_back(std::move(fi.filePath().toStdString()));
182 }
183 }
184
185 tm now_tm(log_clock::time_point tp)
186 {
187 time_t tnow = log_clock::to_time_t(tp);
188 return spdlog::details::os::localtime(tnow);
189 }
190
191 log_clock::time_point next_rotation_tp_()
192 {
193 auto now = log_clock::now();
194 tm date = now_tm(now);
195 auto rotation_time = log_clock::from_time_t(std::mktime(&date));
196
197 switch (interval_)
198 {
199 case RI_Minutely:
200 date.tm_min += 1;
201 break;
202 case RI_Hourly:
203 date.tm_hour += 1;
204 break;
205 case RI_HalfDaily:
206 date.tm_hour += 12;
207 break;
208 case RI_Daily:
209 date.tm_mday += 1;
210 break;
211 case RI_Weekly:
212 date.tm_mday += 7;
213 break;
214 case RI_Monthly:
215 date.tm_mon += 1;
216 break;
217 }
218
219 rotation_time = log_clock::from_time_t(std::mktime(&date));
220 return rotation_time;
221 }
222
223 // Delete the file N rotations ago.
224 // Throw spdlog_ex on failure to delete the old file.
225 void delete_old_()
226 {
227 using details::os::filename_to_str;
228 using details::os::remove;
229
230 // base_filename_ not in filenames_q_
231 while (filenames_q_.size() > max_files_ - 1)
232 {
233 auto old_filename = std::move(filenames_q_.front());
234 filenames_q_.pop_front();
235 bool ok = remove(old_filename) == 0;
236 if (!ok)
237 {
238 filenames_q_.push_back(std::move(old_filename));
239 throw(spdlog_ex("Failed removing file " + filename_to_str(old_filename), errno));
240 }
241 }
242 }
243
244 void rolling_()
245 {
246 using details::os::filename_to_str;
247
248 file_helper_.close();
249 // xxx.log == > xxx.log.YYYY-MM-DD-HH-MM-SS
250 auto backupName = FileNameCalc::calc_filename(base_filename_, now_tm(log_clock::now()));
251 if (details::os::rename(base_filename_, backupName))
252 {
253 file_helper_.reopen(true); // truncate the log file anyway to prevent it to grow beyond its limit!
254 current_size_ = 0;
255 throw spdlog_ex("rolling_file_sink: failed renaming " + filename_to_str(base_filename_) + " to " + filename_to_str(backupName), errno);
256 }
257
258 filenames_q_.push_back(std::move(backupName));
259 rotation_tp_ = next_rotation_tp_();
260
261 file_helper_.reopen(true);
262 }
263
264 filename_t base_filename_;
265 log_clock::time_point rotation_tp_;
266 details::file_helper file_helper_;
267 bool rolling_on_open_;
268 std::size_t max_size_;
269 std::size_t max_files_;
270 std::size_t current_size_;
271 RollingInterval interval_;
272 std::list<filename_t> filenames_q_;
273 };
274
275 using rolling_file_sink_mt = rolling_file_sink<std::mutex>;
276 using rolling_file_sink_st = rolling_file_sink<details::null_mutex>;
277
278 //
279 // factory functions
280 //
281 template <typename Factory = spdlog::synchronous_factory>
282 inline std::shared_ptr<logger> rolling_logger_mt(const std::string &logger_name, const filename_t &filename, std::size_t max_file_size,
283 std::size_t max_files, bool rolling_on_open = false, RollingInterval interval = RI_Daily)
284 {
285 return Factory::template create<rolling_file_sink_mt>(logger_name, filename, max_file_size, max_files, rolling_on_open, interval);
286 }
287
288 template <typename Factory = spdlog::synchronous_factory>
289 inline std::shared_ptr<logger> rolling_logger_st(const std::string &logger_name, const filename_t &filename, std::size_t max_file_size,
290 std::size_t max_files, bool rolling_on_open = false, RollingInterval interval = RI_Daily)
291 {
292 return Factory::template create<rolling_file_sink_st>(logger_name, filename, max_file_size, max_files, rolling_on_open, interval);
293 }
294
295 template <typename Sink>
296 Sink *get_sink(const std::string &logger_name)
297 {
298 Sink *sink = nullptr;
299
300 auto fl = spdlog::get(logger_name);
301 if (!fl)
302 return sink;
303 spdlog::sink_ptr s_ptr = fl->sinks()[0];
304 sink = dynamic_cast<Sink *>(s_ptr.get());
305
306 return sink;
307 }
308
309 DLOG_CORE_END_NAMESPACE