mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-21 08:10:43 +08:00
Added option for setting rotation time in daily file ctor
This commit is contained in:
parent
9e54057aaa
commit
c401e830d0
@ -642,11 +642,9 @@ FMT_FUNC void spdlog::details::fmt::BasicWriter<Char>::write_str(
|
||||
template <typename Char>
|
||||
inline Arg spdlog::details::fmt::BasicFormatter<Char>::parse_arg_index(const Char *&s) {
|
||||
const char *error = 0;
|
||||
Arg arg = *s < '0' || *s > '9' ?
|
||||
next_arg(error) : get_arg(parse_nonnegative_int(s), error);
|
||||
Arg arg = *s < '0' || *s > '9' ? next_arg(error) : get_arg(parse_nonnegative_int(s), error);
|
||||
if (error) {
|
||||
FMT_THROW(FormatError(
|
||||
*s != '}' && *s != ':' ? "invalid format string" : error));
|
||||
FMT_THROW(FormatError(*s != '}' && *s != ':' ? "invalid format string" : error));
|
||||
}
|
||||
return arg;
|
||||
}
|
||||
|
@ -54,13 +54,13 @@ inline std::shared_ptr<spdlog::logger> spdlog::rotating_logger_st(const std::str
|
||||
}
|
||||
|
||||
// Create file logger which creates new file at midnight):
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_mt(const std::string& logger_name, const std::string& filename, bool force_flush)
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_mt(const std::string& logger_name, const std::string& filename, int hour, int minute, bool force_flush)
|
||||
{
|
||||
return create<spdlog::sinks::daily_file_sink_mt>(logger_name, filename, "txt", force_flush);
|
||||
return create<spdlog::sinks::daily_file_sink_mt>(logger_name, filename, "txt", hour, minute, force_flush);
|
||||
}
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const std::string& logger_name, const std::string& filename, bool force_flush)
|
||||
inline std::shared_ptr<spdlog::logger> spdlog::daily_logger_st(const std::string& logger_name, const std::string& filename, int hour, int minute, bool force_flush)
|
||||
{
|
||||
return create<spdlog::sinks::daily_file_sink_st>(logger_name, filename, "txt", force_flush);
|
||||
return create<spdlog::sinks::daily_file_sink_st>(logger_name, filename, "txt", hour, minute, force_flush);
|
||||
}
|
||||
|
||||
|
||||
|
@ -155,14 +155,21 @@ template<class Mutex>
|
||||
class daily_file_sink:public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
explicit daily_file_sink(const std::string& base_filename,
|
||||
const std::string& extension,
|
||||
bool force_flush=false):
|
||||
_base_filename(base_filename),
|
||||
//create daily file sink which rotates on given time
|
||||
daily_file_sink(
|
||||
const std::string& base_filename,
|
||||
const std::string& extension,
|
||||
int rotation_hour,
|
||||
int rotation_minute,
|
||||
bool force_flush=false): _base_filename(base_filename),
|
||||
_extension(extension),
|
||||
_midnight_tp (_calc_midnight_tp() ),
|
||||
_rotation_h(rotation_hour),
|
||||
_rotation_m(rotation_minute),
|
||||
_file_helper(force_flush)
|
||||
{
|
||||
if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59)
|
||||
throw spdlog_ex("daily_file_sink: Invalid rotation time in ctor");
|
||||
_rotation_tp = _next_rotation_tp(),
|
||||
_file_helper.open(calc_filename(_base_filename, _extension));
|
||||
}
|
||||
|
||||
@ -170,26 +177,30 @@ public:
|
||||
protected:
|
||||
void _sink_it(const details::log_msg& msg) override
|
||||
{
|
||||
if (std::chrono::system_clock::now() >= _midnight_tp)
|
||||
if (std::chrono::system_clock::now() >= _rotation_tp)
|
||||
{
|
||||
_file_helper.close();
|
||||
_file_helper.open(calc_filename(_base_filename, _extension));
|
||||
_midnight_tp = _calc_midnight_tp();
|
||||
_rotation_tp = _next_rotation_tp();
|
||||
}
|
||||
_file_helper.write(msg);
|
||||
}
|
||||
|
||||
private:
|
||||
// Return next midnight's time_point
|
||||
static std::chrono::system_clock::time_point _calc_midnight_tp()
|
||||
std::chrono::system_clock::time_point _next_rotation_tp()
|
||||
{
|
||||
using namespace std::chrono;
|
||||
auto now = system_clock::now();
|
||||
time_t tnow = std::chrono::system_clock::to_time_t(now);
|
||||
tm date = spdlog::details::os::localtime(tnow);
|
||||
date.tm_hour = date.tm_min = date.tm_sec = 0;
|
||||
auto midnight = std::chrono::system_clock::from_time_t(std::mktime(&date));
|
||||
return system_clock::time_point(midnight + hours(24));
|
||||
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));
|
||||
if (rotation_time > now)
|
||||
return rotation_time;
|
||||
else
|
||||
return system_clock::time_point(rotation_time + hours(24));
|
||||
}
|
||||
|
||||
//Create filename for the form basename.YYYY-MM-DD.extension
|
||||
@ -197,15 +208,18 @@ private:
|
||||
{
|
||||
std::tm tm = spdlog::details::os::localtime();
|
||||
details::fmt::MemoryWriter w;
|
||||
w.write("{}.{:04d}-{:02d}-{:02d}.{}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, extension);
|
||||
w.write("{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}.{}", basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, extension);
|
||||
return w.str();
|
||||
}
|
||||
|
||||
std::string _base_filename;
|
||||
std::string _extension;
|
||||
std::chrono::system_clock::time_point _midnight_tp;
|
||||
int _rotation_h;
|
||||
int _rotation_m;
|
||||
std::chrono::system_clock::time_point _rotation_tp;
|
||||
details::file_helper _file_helper;
|
||||
|
||||
|
||||
};
|
||||
|
||||
typedef daily_file_sink<std::mutex> daily_file_sink_mt;
|
||||
|
@ -84,10 +84,10 @@ std::shared_ptr<logger> rotating_logger_mt(const std::string& logger_name, const
|
||||
std::shared_ptr<logger> rotating_logger_st(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, bool force_flush = false);
|
||||
|
||||
//
|
||||
// Create file logger which creates new file at midnight):
|
||||
// Create file logger which creates new file on the given time (default in midnight):
|
||||
//
|
||||
std::shared_ptr<logger> daily_logger_mt(const std::string& logger_name, const std::string& filename, bool force_flush = false);
|
||||
std::shared_ptr<logger> daily_logger_st(const std::string& logger_name, const std::string& filename, bool force_flush = false);
|
||||
std::shared_ptr<logger> daily_logger_mt(const std::string& logger_name, const std::string& filename, int hour=0, int minute=0, bool force_flush = false);
|
||||
std::shared_ptr<logger> daily_logger_st(const std::string& logger_name, const std::string& filename, int hour=0, int minute=0, bool force_flush = false);
|
||||
|
||||
|
||||
//
|
||||
|
Loading…
Reference in New Issue
Block a user