1
0
mirror of https://github.com/gabime/spdlog.git synced 2025-04-01 02:42:41 +08:00
spdlog/include/spdlog/sinks/daily_file_sink.h

245 lines
9.8 KiB
C
Raw Normal View History

2019-06-04 05:09:16 +08:00
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
2018-10-12 08:04:55 +08:00
#include <spdlog/common.h>
2023-09-25 07:35:55 +08:00
#include <spdlog/details/circular_q.h>
#include <spdlog/details/file_helper.h>
#include <spdlog/details/null_mutex.h>
#include <spdlog/details/os.h>
#include <spdlog/details/synchronous_factory.h>
2023-09-25 07:35:55 +08:00
#include <spdlog/fmt/chrono.h>
#include <spdlog/fmt/fmt.h>
#include <spdlog/sinks/base_sink.h>
#include <chrono>
#include <cstdio>
2023-09-25 07:35:55 +08:00
#include <iomanip>
#include <mutex>
2023-09-25 07:35:55 +08:00
#include <sstream>
#include <string>
namespace spdlog {
namespace sinks {
/*
* Generator of daily log file names in format basename.YYYY-MM-DD.ext
*/
2023-09-25 07:35:55 +08:00
struct daily_filename_calculator {
// Create filename for the form basename.YYYY-MM-DD
2023-09-25 07:35:55 +08:00
static filename_t calc_filename(const filename_t &filename, const tm &now_tm) {
filename_t basename, ext;
std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
2023-09-25 07:35:55 +08:00
return fmt_lib::format(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}"), basename, now_tm.tm_year + 1900,
now_tm.tm_mon + 1, now_tm.tm_mday, ext);
}
};
/*
2021-02-26 18:38:49 +08:00
* Generator of daily log file names with strftime format.
2021-06-26 22:59:08 +08:00
* Usages:
2023-09-25 07:35:55 +08:00
* auto sink = std::make_shared<spdlog::sinks::daily_file_format_sink_mt>("myapp-%Y-%m-%d:%H:%M:%S.log", hour,
* minute);" auto logger = spdlog::daily_logger_format_mt("loggername, "myapp-%Y-%m-%d:%X.log", hour, minute)"
2021-06-26 22:59:08 +08:00
*
*/
2023-09-25 07:35:55 +08:00
struct daily_filename_format_calculator {
static filename_t calc_filename(const filename_t &file_path, const tm &now_tm) {
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
2023-06-07 18:23:44 +08:00
std::wstringstream stream;
2021-06-27 00:43:37 +08:00
#else
2023-06-07 18:23:44 +08:00
std::stringstream stream;
2021-06-27 00:43:37 +08:00
#endif
2023-06-07 18:23:44 +08:00
stream << std::put_time(&now_tm, file_path.c_str());
return stream.str();
}
};
/*
2019-09-15 23:34:29 +08:00
* Rotating file sink based on date.
* If truncate != false , the created file will be truncated.
* If max_files > 0, retain only the last max_files and delete previous.
*/
2023-09-25 07:35:55 +08:00
template <typename Mutex, typename FileNameCalc = daily_filename_calculator>
class daily_file_sink final : public base_sink<Mutex> {
public:
// create daily file sink which rotates on given time
2023-09-25 07:35:55 +08:00
daily_file_sink(filename_t base_filename,
int rotation_hour,
int rotation_minute,
bool truncate = false,
uint16_t max_files = 0,
const file_event_handlers &event_handlers = {})
: base_filename_(std::move(base_filename)),
rotation_h_(rotation_hour),
rotation_m_(rotation_minute),
file_helper_{event_handlers},
truncate_(truncate),
max_files_(max_files),
filenames_q_() {
if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59) {
throw_spdlog_ex("daily_file_sink: Invalid rotation time in ctor");
}
2019-09-15 23:34:29 +08:00
auto now = log_clock::now();
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
2019-09-15 23:34:29 +08:00
file_helper_.open(filename, truncate_);
rotation_tp_ = next_rotation_tp_();
2019-09-15 23:34:29 +08:00
2023-09-25 07:35:55 +08:00
if (max_files_ > 0) {
2020-01-19 17:29:17 +08:00
init_filenames_q_();
2019-09-15 23:34:29 +08:00
}
}
2023-09-25 07:35:55 +08:00
filename_t filename() {
2020-05-27 04:59:57 +08:00
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
return file_helper_.filename();
}
protected:
2023-09-25 07:35:55 +08:00
void sink_it_(const details::log_msg &msg) override {
2019-07-01 18:00:30 +08:00
auto time = msg.time;
2019-09-15 23:34:29 +08:00
bool should_rotate = time >= rotation_tp_;
2023-09-25 07:35:55 +08:00
if (should_rotate) {
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time));
2019-09-15 23:34:29 +08:00
file_helper_.open(filename, truncate_);
rotation_tp_ = next_rotation_tp_();
}
memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted);
2018-06-24 06:32:39 +08:00
file_helper_.write(formatted);
2019-09-15 23:34:29 +08:00
// Do the cleaning only at the end because it might throw on failure.
2023-09-25 07:35:55 +08:00
if (should_rotate && max_files_ > 0) {
2019-09-15 23:34:29 +08:00
delete_old_();
}
}
2023-09-25 07:35:55 +08:00
void flush_() override { file_helper_.flush(); }
private:
2023-09-25 07:35:55 +08:00
void init_filenames_q_() {
2020-01-19 13:52:01 +08:00
using details::os::path_exists;
2020-01-19 13:46:43 +08:00
filenames_q_ = details::circular_q<filename_t>(static_cast<size_t>(max_files_));
std::vector<filename_t> filenames;
auto now = log_clock::now();
2023-09-25 07:35:55 +08:00
while (filenames.size() < max_files_) {
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
2023-09-25 07:35:55 +08:00
if (!path_exists(filename)) {
2020-01-19 13:46:43 +08:00
break;
}
filenames.emplace_back(filename);
now -= std::chrono::hours(24);
}
2023-09-25 07:35:55 +08:00
for (auto iter = filenames.rbegin(); iter != filenames.rend(); ++iter) {
2020-01-19 13:46:43 +08:00
filenames_q_.push_back(std::move(*iter));
}
}
2023-09-25 07:35:55 +08:00
tm now_tm(log_clock::time_point tp) {
time_t tnow = log_clock::to_time_t(tp);
return spdlog::details::os::localtime(tnow);
}
2023-09-25 07:35:55 +08:00
log_clock::time_point next_rotation_tp_() {
auto now = log_clock::now();
tm date = now_tm(now);
date.tm_hour = rotation_h_;
date.tm_min = rotation_m_;
date.tm_sec = 0;
auto rotation_time = log_clock::from_time_t(std::mktime(&date));
2023-09-25 07:35:55 +08:00
if (rotation_time > now) {
return rotation_time;
}
return {rotation_time + std::chrono::hours(24)};
}
2019-09-15 23:34:29 +08:00
// Delete the file N rotations ago.
// Throw spdlog_ex on failure to delete the old file.
2023-09-25 07:35:55 +08:00
void delete_old_() {
2019-09-15 23:34:29 +08:00
using details::os::filename_to_str;
using details::os::remove_if_exists;
2020-05-27 04:35:04 +08:00
filename_t current_file = file_helper_.filename();
2023-09-25 07:35:55 +08:00
if (filenames_q_.full()) {
auto old_filename = std::move(filenames_q_.front());
filenames_q_.pop_front();
2019-09-15 23:34:29 +08:00
bool ok = remove_if_exists(old_filename) == 0;
2023-09-25 07:35:55 +08:00
if (!ok) {
2019-09-15 23:34:29 +08:00
filenames_q_.push_back(std::move(current_file));
throw_spdlog_ex("Failed removing daily file " + filename_to_str(old_filename), errno);
2019-09-15 23:34:29 +08:00
}
}
filenames_q_.push_back(std::move(current_file));
}
filename_t base_filename_;
int rotation_h_;
int rotation_m_;
log_clock::time_point rotation_tp_;
details::file_helper file_helper_;
bool truncate_;
2019-09-15 23:44:45 +08:00
uint16_t max_files_;
2019-09-15 23:34:29 +08:00
details::circular_q<filename_t> filenames_q_;
};
using daily_file_sink_mt = daily_file_sink<std::mutex>;
using daily_file_sink_st = daily_file_sink<details::null_mutex>;
using daily_file_format_sink_mt = daily_file_sink<std::mutex, daily_filename_format_calculator>;
using daily_file_format_sink_st = daily_file_sink<details::null_mutex, daily_filename_format_calculator>;
} // namespace sinks
//
2018-04-29 06:36:45 +08:00
// factory functions
//
2023-09-25 07:35:55 +08:00
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> daily_logger_mt(const std::string &logger_name,
const filename_t &filename,
int hour = 0,
int minute = 0,
bool truncate = false,
uint16_t max_files = 0,
const file_event_handlers &event_handlers = {}) {
return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute, truncate, max_files,
event_handlers);
}
2023-09-25 07:35:55 +08:00
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> daily_logger_format_mt(const std::string &logger_name,
const filename_t &filename,
int hour = 0,
int minute = 0,
bool truncate = false,
uint16_t max_files = 0,
const file_event_handlers &event_handlers = {}) {
return Factory::template create<sinks::daily_file_format_sink_mt>(logger_name, filename, hour, minute, truncate,
max_files, event_handlers);
}
2023-09-25 07:35:55 +08:00
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> daily_logger_st(const std::string &logger_name,
const filename_t &filename,
int hour = 0,
int minute = 0,
bool truncate = false,
uint16_t max_files = 0,
const file_event_handlers &event_handlers = {}) {
return Factory::template create<sinks::daily_file_sink_st>(logger_name, filename, hour, minute, truncate, max_files,
event_handlers);
}
2023-09-25 07:35:55 +08:00
template <typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> daily_logger_format_st(const std::string &logger_name,
const filename_t &filename,
int hour = 0,
int minute = 0,
bool truncate = false,
uint16_t max_files = 0,
const file_event_handlers &event_handlers = {}) {
return Factory::template create<sinks::daily_file_format_sink_st>(logger_name, filename, hour, minute, truncate,
max_files, event_handlers);
}
} // namespace spdlog