2019-06-03 17:09:16 -04:00
|
|
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
2018-04-28 18:23:18 -04:00
|
|
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
|
|
|
|
|
|
#pragma once
|
2018-10-11 20:04:55 -04:00
|
|
|
|
2018-04-28 18:23:18 -04:00
|
|
|
#include "spdlog/details/file_helper.h"
|
|
|
|
#include "spdlog/details/null_mutex.h"
|
|
|
|
#include "spdlog/fmt/fmt.h"
|
|
|
|
#include "spdlog/sinks/base_sink.h"
|
2019-05-12 16:08:14 -04:00
|
|
|
#include "spdlog/details/os.h"
|
2019-06-03 16:56:18 -04:00
|
|
|
#include "spdlog/details/synchronous_factory.h"
|
2018-04-28 18:23:18 -04:00
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <ctime>
|
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace spdlog {
|
|
|
|
namespace sinks {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generator of daily log file names in format basename.YYYY-MM-DD.ext
|
|
|
|
*/
|
2018-07-12 18:44:29 -04:00
|
|
|
struct daily_filename_calculator
|
2018-04-28 18:23:18 -04:00
|
|
|
{
|
|
|
|
// Create filename for the form basename.YYYY-MM-DD
|
2018-07-12 18:44:29 -04:00
|
|
|
static filename_t calc_filename(const filename_t &filename, const tm &now_tm)
|
2018-04-28 18:23:18 -04:00
|
|
|
{
|
|
|
|
filename_t basename, ext;
|
2019-01-08 13:09:11 -05:00
|
|
|
std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
|
2018-06-12 11:48:22 -04:00
|
|
|
std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::memory_buffer, fmt::wmemory_buffer>::type w;
|
2018-07-12 18:44:29 -04:00
|
|
|
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);
|
2018-06-12 11:48:22 -04:00
|
|
|
return fmt::to_string(w);
|
2018-04-28 18:23:18 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Rotating file sink based on date. rotates at midnight
|
|
|
|
*/
|
2018-07-14 09:21:53 -04:00
|
|
|
template<typename Mutex, typename FileNameCalc = daily_filename_calculator>
|
2018-09-26 07:33:37 -04:00
|
|
|
class daily_file_sink final : public base_sink<Mutex>
|
2018-04-28 18:23:18 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// create daily file sink which rotates on given time
|
2018-08-01 23:58:15 -04:00
|
|
|
daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute, bool truncate = false)
|
2018-06-10 15:59:17 -04:00
|
|
|
: base_filename_(std::move(base_filename))
|
|
|
|
, rotation_h_(rotation_hour)
|
|
|
|
, rotation_m_(rotation_minute)
|
2018-08-01 23:58:15 -04:00
|
|
|
, truncate_(truncate)
|
2018-04-28 18:23:18 -04:00
|
|
|
{
|
|
|
|
if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59)
|
|
|
|
{
|
2019-08-19 04:31:33 -04:00
|
|
|
SPDLOG_THROW(spdlog_ex("daily_file_sink: Invalid rotation time in ctor"));
|
2018-04-28 18:23:18 -04:00
|
|
|
}
|
2018-07-12 18:44:29 -04:00
|
|
|
auto now = log_clock::now();
|
2018-08-02 08:27:49 -04:00
|
|
|
file_helper_.open(FileNameCalc::calc_filename(base_filename_, now_tm(now)), truncate_);
|
2018-06-10 15:59:17 -04:00
|
|
|
rotation_tp_ = next_rotation_tp_();
|
2018-04-28 18:23:18 -04:00
|
|
|
}
|
|
|
|
|
2019-01-23 13:35:55 -05:00
|
|
|
const filename_t &filename() const
|
|
|
|
{
|
|
|
|
return file_helper_.filename();
|
|
|
|
}
|
|
|
|
|
2018-04-28 18:23:18 -04:00
|
|
|
protected:
|
2018-07-14 09:21:53 -04:00
|
|
|
void sink_it_(const details::log_msg &msg) override
|
2018-04-28 18:23:18 -04:00
|
|
|
{
|
2019-07-01 02:57:48 -04:00
|
|
|
#ifdef SPDLOG_NO_DATETIME
|
2019-07-01 06:00:30 -04:00
|
|
|
auto time = log_clock::now();
|
2019-07-01 02:57:48 -04:00
|
|
|
#else
|
2019-07-01 06:00:30 -04:00
|
|
|
auto time = msg.time;
|
2019-07-01 02:57:48 -04:00
|
|
|
#endif
|
2019-07-01 06:00:30 -04:00
|
|
|
if (time >= rotation_tp_)
|
2018-04-28 18:23:18 -04:00
|
|
|
{
|
2019-07-01 06:00:30 -04:00
|
|
|
file_helper_.open(FileNameCalc::calc_filename(base_filename_, now_tm(time)), truncate_);
|
2018-06-10 15:59:17 -04:00
|
|
|
rotation_tp_ = next_rotation_tp_();
|
2018-04-28 18:23:18 -04:00
|
|
|
}
|
2018-07-14 09:21:53 -04:00
|
|
|
fmt::memory_buffer formatted;
|
2019-06-27 16:56:37 -04:00
|
|
|
base_sink<Mutex>::formatter_->format(msg, formatted);
|
2018-06-23 18:32:39 -04:00
|
|
|
file_helper_.write(formatted);
|
2018-04-28 18:23:18 -04:00
|
|
|
}
|
|
|
|
|
2018-06-10 15:59:17 -04:00
|
|
|
void flush_() override
|
2018-04-28 18:23:18 -04:00
|
|
|
{
|
2018-06-10 15:59:17 -04:00
|
|
|
file_helper_.flush();
|
2018-04-28 18:23:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-07-12 18:44:29 -04:00
|
|
|
tm now_tm(log_clock::time_point tp)
|
|
|
|
{
|
|
|
|
time_t tnow = log_clock::to_time_t(tp);
|
|
|
|
return spdlog::details::os::localtime(tnow);
|
|
|
|
}
|
|
|
|
|
2018-07-21 16:48:07 -04:00
|
|
|
log_clock::time_point next_rotation_tp_()
|
2018-04-28 18:23:18 -04:00
|
|
|
{
|
2018-07-12 18:44:29 -04:00
|
|
|
auto now = log_clock::now();
|
|
|
|
tm date = now_tm(now);
|
2018-06-10 15:59:17 -04:00
|
|
|
date.tm_hour = rotation_h_;
|
|
|
|
date.tm_min = rotation_m_;
|
2018-04-28 18:23:18 -04:00
|
|
|
date.tm_sec = 0;
|
2018-07-12 18:44:29 -04:00
|
|
|
auto rotation_time = log_clock::from_time_t(std::mktime(&date));
|
2018-04-28 18:23:18 -04:00
|
|
|
if (rotation_time > now)
|
|
|
|
{
|
|
|
|
return rotation_time;
|
|
|
|
}
|
|
|
|
return {rotation_time + std::chrono::hours(24)};
|
|
|
|
}
|
|
|
|
|
2018-06-10 15:59:17 -04:00
|
|
|
filename_t base_filename_;
|
|
|
|
int rotation_h_;
|
|
|
|
int rotation_m_;
|
2018-07-12 18:44:29 -04:00
|
|
|
log_clock::time_point rotation_tp_;
|
2018-06-10 15:59:17 -04:00
|
|
|
details::file_helper file_helper_;
|
2018-08-01 23:58:15 -04:00
|
|
|
bool truncate_;
|
2018-04-28 18:23:18 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
using daily_file_sink_mt = daily_file_sink<std::mutex>;
|
|
|
|
using daily_file_sink_st = daily_file_sink<details::null_mutex>;
|
|
|
|
|
|
|
|
} // namespace sinks
|
|
|
|
|
|
|
|
//
|
2018-04-28 18:36:45 -04:00
|
|
|
// factory functions
|
2018-04-28 18:23:18 -04:00
|
|
|
//
|
2019-06-03 16:56:18 -04:00
|
|
|
template<typename Factory = spdlog::synchronous_factory>
|
2018-08-04 17:35:32 -04:00
|
|
|
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)
|
2018-04-28 18:23:18 -04:00
|
|
|
{
|
2018-08-01 23:58:15 -04:00
|
|
|
return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute, truncate);
|
2018-04-28 18:23:18 -04:00
|
|
|
}
|
|
|
|
|
2019-06-03 16:56:18 -04:00
|
|
|
template<typename Factory = spdlog::synchronous_factory>
|
2018-08-04 17:35:32 -04:00
|
|
|
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)
|
2018-04-28 18:23:18 -04:00
|
|
|
{
|
2018-08-01 23:58:15 -04:00
|
|
|
return Factory::template create<sinks::daily_file_sink_st>(logger_name, filename, hour, minute, truncate);
|
2018-04-28 18:23:18 -04:00
|
|
|
}
|
|
|
|
} // namespace spdlog
|