spdlog自定义根据时间和文件大小来同时作为条件的sink.
spdlog版本:1.11.0
my_file_sink.h
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#include <spdlog/common.h>
#include <spdlog/details/file_helper.h>
#include <spdlog/details/null_mutex.h>
#include <spdlog/fmt/fmt.h>
#include <spdlog/fmt/chrono.h>
#include <spdlog/sinks/base_sink.h>
#include <spdlog/details/os.h>
#include <spdlog/details/circular_q.h>
#include <spdlog/details/synchronous_factory.h>
#include <chrono>
#include <cstdio>
#include <ctime>
#include <mutex>
#include <string>
namespace spdlog {
namespace sinks {
/*
* Generator of my log file names in format basename.YYYY-MM-DD.ext
*/
struct my_filename_calculator
{
// Create filename for the form basename.YYYY-MM-DD
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);
return fmt_lib::format(SPDLOG_FMT_STRING(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}")), basename, now_tm.tm_year + 1900,
now_tm.tm_mon + 1, now_tm.tm_mday, ext);
}
};
/*
* Generator of my log file names with strftime format.
* Usages:
* auto sink = std::make_shared<spdlog::sinks::my_file_format_sink_mt>("myapp-%Y-%m-%d:%H:%M:%S.log", hour, minute);"
* auto logger = spdlog::my_logger_format_mt("loggername, "myapp-%Y-%m-%d:%X.log", hour, minute)"
*
*/
struct my_filename_format_calculator
{
static filename_t calc_filename(const filename_t &filename, const tm &now_tm)
{
#ifdef SPDLOG_USE_STD_FORMAT
// adapted from fmtlib: https://github.com/fmtlib/fmt/blob/8.0.1/include/fmt/chrono.h#L522-L546
filename_t tm_format;
tm_format.append(filename);
// By appending an extra space we can distinguish an empty result that
// indicates insufficient buffer size from a guaranteed non-empty result
// https://github.com/fmtlib/fmt/issues/2238
tm_format.push_back(' ');
const size_t MIN_SIZE = 10;
filename_t buf;
buf.resize(MIN_SIZE);
for (;;)
{
size_t count = strftime(buf.data(), buf.size(), tm_format.c_str(), &now_tm);
if (count != 0)
{
// Remove the extra space.
buf.resize(count - 1);
break;
}
buf.resize(buf.size() * 2);
}
return buf;
#else
// generate fmt datetime format string, e.g. {:%Y-%m-%d}.
filename_t fmt_filename = fmt::format(SPDLOG_FMT_STRING(SPDLOG_FILENAME_T("{{:{}}}")), filename);
// MSVC doesn't allow fmt::runtime(..) with wchar, with fmtlib versions < 9.1.x
# if defined(_MSC_VER) && defined(SPDLOG_WCHAR_FILENAMES) && FMT_VERSION < 90101
return fmt::format(fmt_filename, now_tm);
# else
return fmt::format(SPDLOG_FMT_RUNTIME(fmt_filename), now_tm);
# endif
#endif
}
private:
#if defined __GNUC__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wformat-nonliteral"
#endif
static size_t strftime(char *str, size_t count, const char *format, const std::tm *time)
{
return std::strftime(str, count, format, time);
}
static size_t strftime(wchar_t *str, size_t count, const wchar_t *format, const std::tm *time)
{
return std::wcsftime(str, count, format, time);
}
#if defined(__GNUC__)
# pragma GCC diagnostic pop
#endif
};
/*
* 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.
*/
template<typename Mutex, typename FileNameCalc = my_filename_calculator>
class my_file_sink final : public base_sink<Mutex>
{
public:
// create my file sink which rotates on given time
my_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files, int rotation_hour = 0, int rotation_minute = 0, bool truncate = false,
const file_event_handlers &event_handlers = {})
: base_filename_(std::move(base_filename))
, rotation_h_(rotation_hour)
, rotation_m_(rotation_minute)
, max_size_(max_size)
, max_files_(max_files)
, file_helper_{event_handlers}
, truncate_(truncate)
{
if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59)
{
throw_spdlog_ex("my_file_sink: Invalid rotation time in ctor");
}
if (max_size == 0)
{
throw_spdlog_ex("rotating sink constructor: max_size arg cannot be zero");
}
if (max_files > 200000)
{
throw_spdlog_ex("rotating sink constructor: max_files arg cannot exceed 200000");
}
auto now = log_clock::now();
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
date_filename_ = filename;
file_helper_.open(filename, truncate_);
rotation_tp_ = next_rotation_tp_();
current_size_ = file_helper_.size(); // expensive. called only once
}
filename_t filename()
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
return file_helper_.filename();
}
protected:
void sink_it_(const details::log_msg &msg) override
{
memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted);
auto time = msg.time;
bool should_rotate = time >= rotation_tp_; // 判断是否是新的一天
if (should_rotate)
{
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time)); // 计算日志文件名
date_filename_ = filename;
file_helper_.open(filename, truncate_); // 打开一个新的日志文件
rotation_tp_ = next_rotation_tp_(); // 获取下个需要新建文件的时间点
current_size_ = file_helper_.size(); // 获取新文件的文件大小
current_size_ += formatted.size();
}
else
{
auto new_size = current_size_ + formatted.size();
// rotate if the new estimated file size exceeds max size.
// rotate only if the real size > 0 to better deal with full disk (see issue #2261).
// we only check the real size when new_size > max_size_ because it is relatively expensive.
if (new_size > max_size_)
{
file_helper_.flush();
if (file_helper_.size() > 0)
{
rotate_();
new_size = formatted.size();
}
}
current_size_ = new_size;
}
file_helper_.write(formatted);
}
void flush_() override
{
file_helper_.flush();
}
private:
// Rotate files:
// log_xxx.txt -> log_xxx.1.txt
// log_xxx.1.txt -> log_xxx.2.txt
// log_xxx.2.txt -> log_xxx.3.txt
// log_xxx.3.txt -> delete
void rotate_()
{
using details::os::filename_to_str;
using details::os::path_exists;
file_helper_.close();
for (auto i = max_files_; i > 0; --i)
{
filename_t src = calc_filename_(date_filename_, i - 1);
if (!path_exists(src))
{
continue;
}
filename_t target = calc_filename_(date_filename_, i);
if (!rename_file_(src, target))
{
// if failed try again after a small delay.
// this is a workaround to a windows issue, where very high rotation
// rates can cause the rename to fail with permission denied (because of antivirus?).
details::os::sleep_for_millis(100);
if (!rename_file_(src, target))
{
file_helper_.reopen(true); // truncate the log file anyway to prevent it to grow beyond its limit!
current_size_ = 0;
throw_spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) + " to " + filename_to_str(target), errno);
}
}
}
file_helper_.reopen(true);
}
filename_t calc_filename_(const filename_t &filename, std::size_t index)
{
if (index == 0u)
{
return filename;
}
filename_t basename, ext;
std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
return fmt_lib::format(SPDLOG_FILENAME_T("{}.{}{}"), basename, index, ext);
}
// delete the target if exists, and rename the src file to target
// return true on success, false otherwise.
bool rename_file_(const filename_t &src_filename, const filename_t &target_filename)
{
// try to delete the target file in case it already exists.
(void)details::os::remove(target_filename);
return details::os::rename(src_filename, target_filename) == 0;
}
tm now_tm(log_clock::time_point tp)
{
time_t tnow = log_clock::to_time_t(tp);
return spdlog::details::os::localtime(tnow);
}
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));
if (rotation_time > now)
{
return rotation_time;
}
return {rotation_time + std::chrono::hours(24)};
}
filename_t base_filename_;
filename_t date_filename_;
int rotation_h_;
int rotation_m_;
std::size_t max_size_;
std::size_t max_files_;
std::size_t current_size_;
log_clock::time_point rotation_tp_;
details::file_helper file_helper_;
bool truncate_;
};
using my_file_sink_mt = my_file_sink<std::mutex>;
using my_file_sink_st = my_file_sink<details::null_mutex>;
using my_file_format_sink_mt = my_file_sink<std::mutex, my_filename_format_calculator>;
using my_file_format_sink_st = my_file_sink<details::null_mutex, my_filename_format_calculator>;
} // namespace sinks
//
// factory functions
//
template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> my_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::my_file_sink_mt>(logger_name, filename, hour, minute, truncate, max_files, event_handlers);
}
template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> my_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::my_file_format_sink_mt>(
logger_name, filename, hour, minute, truncate, max_files, event_handlers);
}
template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> my_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::my_file_sink_st>(logger_name, filename, hour, minute, truncate, max_files, event_handlers);
}
template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> my_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::my_file_format_sink_st>(
logger_name, filename, hour, minute, truncate, max_files, event_handlers);
}
} // namespace spdlog
使用示例:
#include "spdlog/spdlog.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/my_file_sink.h"
#include "spdlog/async.h"
int main()
{
spdlog::init_thread_pool(8192, 1);
auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt >();
auto my_sink = std::make_shared<spdlog::sinks::my_file_sink_mt>("./logs/mylog.txt", 1024*1024, 1, 23, 23);
std::vector<spdlog::sink_ptr> sinks {stdout_sink, my_sink};
auto logger = std::make_shared<spdlog::async_logger>("loggername", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
spdlog::register_logger(logger);
// 输出日志消息
for (int i = 0; i < 10000; ++i)
{
logger->info("Hello, world!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!{}", i);
}
sleep(60);
for (int i = 0; i < 10000; ++i)
{
logger->info("ok!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!{}", i);
}
return 0;
}