|
|
|
@ -19,37 +19,19 @@
|
|
|
|
|
namespace spdlog {
|
|
|
|
|
namespace sinks {
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Default generator of daily log file names.
|
|
|
|
|
*/
|
|
|
|
|
struct default_daily_file_name_calculator
|
|
|
|
|
{
|
|
|
|
|
// Create filename for the form filename.YYYY-MM-DD_hh-mm.ext
|
|
|
|
|
static filename_t calc_filename(const filename_t &filename)
|
|
|
|
|
{
|
|
|
|
|
std::tm tm = spdlog::details::os::localtime();
|
|
|
|
|
filename_t basename, ext;
|
|
|
|
|
std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename);
|
|
|
|
|
std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::memory_buffer, fmt::wmemory_buffer>::type w;
|
|
|
|
|
fmt::format_to(w, SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}{}"), basename, tm.tm_year + 1900, tm.tm_mon + 1,
|
|
|
|
|
tm.tm_mday, tm.tm_hour, tm.tm_min, ext);
|
|
|
|
|
return fmt::to_string(w);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Generator of daily log file names in format basename.YYYY-MM-DD.ext
|
|
|
|
|
*/
|
|
|
|
|
struct dateonly_daily_file_name_calculator
|
|
|
|
|
struct daily_filename_calculator
|
|
|
|
|
{
|
|
|
|
|
// Create filename for the form basename.YYYY-MM-DD
|
|
|
|
|
static filename_t calc_filename(const filename_t &filename)
|
|
|
|
|
static filename_t calc_filename(const filename_t &filename, const tm &now_tm)
|
|
|
|
|
{
|
|
|
|
|
std::tm tm = spdlog::details::os::localtime();
|
|
|
|
|
filename_t basename, ext;
|
|
|
|
|
std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename);
|
|
|
|
|
std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::memory_buffer, fmt::wmemory_buffer>::type w;
|
|
|
|
|
fmt::format_to(w, SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, ext);
|
|
|
|
|
fmt::format_to(
|
|
|
|
|
w, SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}"), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday, ext);
|
|
|
|
|
return fmt::to_string(w);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
@ -57,7 +39,7 @@ struct dateonly_daily_file_name_calculator
|
|
|
|
|
/*
|
|
|
|
|
* Rotating file sink based on date. rotates at midnight
|
|
|
|
|
*/
|
|
|
|
|
template<class Mutex, class FileNameCalc = default_daily_file_name_calculator>
|
|
|
|
|
template<class Mutex, class FileNameCalc = daily_filename_calculator>
|
|
|
|
|
class daily_file_sink SPDLOG_FINAL : public base_sink<Mutex>
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
@ -71,16 +53,17 @@ public:
|
|
|
|
|
{
|
|
|
|
|
throw spdlog_ex("daily_file_sink: Invalid rotation time in ctor");
|
|
|
|
|
}
|
|
|
|
|
auto now = log_clock::now();
|
|
|
|
|
file_helper_.open(FileNameCalc::calc_filename(base_filename_, now_tm(now)));
|
|
|
|
|
rotation_tp_ = next_rotation_tp_();
|
|
|
|
|
file_helper_.open(FileNameCalc::calc_filename(base_filename_));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void sink_it_(const details::log_msg &, const fmt::memory_buffer &formatted) override
|
|
|
|
|
void sink_it_(const details::log_msg &msg, const fmt::memory_buffer &formatted) override
|
|
|
|
|
{
|
|
|
|
|
if (std::chrono::system_clock::now() >= rotation_tp_)
|
|
|
|
|
if (msg.time >= rotation_tp_)
|
|
|
|
|
{
|
|
|
|
|
file_helper_.open(FileNameCalc::calc_filename(base_filename_));
|
|
|
|
|
file_helper_.open(FileNameCalc::calc_filename(base_filename_, now_tm(msg.time)));
|
|
|
|
|
rotation_tp_ = next_rotation_tp_();
|
|
|
|
|
}
|
|
|
|
|
file_helper_.write(formatted);
|
|
|
|
@ -92,15 +75,20 @@ protected:
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::chrono::system_clock::time_point next_rotation_tp_()
|
|
|
|
|
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 = std::chrono::system_clock::now();
|
|
|
|
|
time_t tnow = std::chrono::system_clock::to_time_t(now);
|
|
|
|
|
tm date = spdlog::details::os::localtime(tnow);
|
|
|
|
|
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 = std::chrono::system_clock::from_time_t(std::mktime(&date));
|
|
|
|
|
auto rotation_time = log_clock::from_time_t(std::mktime(&date));
|
|
|
|
|
if (rotation_time > now)
|
|
|
|
|
{
|
|
|
|
|
return rotation_time;
|
|
|
|
@ -111,7 +99,7 @@ private:
|
|
|
|
|
filename_t base_filename_;
|
|
|
|
|
int rotation_h_;
|
|
|
|
|
int rotation_m_;
|
|
|
|
|
std::chrono::system_clock::time_point rotation_tp_;
|
|
|
|
|
log_clock::time_point rotation_tp_;
|
|
|
|
|
details::file_helper file_helper_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|