From c401e830d043e33fee0a823d98810874cc385d85 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 15 Feb 2015 23:28:13 +0200 Subject: [PATCH] Added option for setting rotation time in daily file ctor --- include/spdlog/details/format.cc | 6 ++-- include/spdlog/details/spdlog_impl.h | 8 +++--- include/spdlog/sinks/file_sinks.h | 42 ++++++++++++++++++---------- include/spdlog/spdlog.h | 6 ++-- 4 files changed, 37 insertions(+), 25 deletions(-) diff --git a/include/spdlog/details/format.cc b/include/spdlog/details/format.cc index 5ca49bbc..08af20fb 100644 --- a/include/spdlog/details/format.cc +++ b/include/spdlog/details/format.cc @@ -642,11 +642,9 @@ FMT_FUNC void spdlog::details::fmt::BasicWriter::write_str( template inline Arg spdlog::details::fmt::BasicFormatter::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; } diff --git a/include/spdlog/details/spdlog_impl.h b/include/spdlog/details/spdlog_impl.h index 69fb85d8..34805597 100644 --- a/include/spdlog/details/spdlog_impl.h +++ b/include/spdlog/details/spdlog_impl.h @@ -54,13 +54,13 @@ inline std::shared_ptr spdlog::rotating_logger_st(const std::str } // Create file logger which creates new file at midnight): -inline std::shared_ptr spdlog::daily_logger_mt(const std::string& logger_name, const std::string& filename, bool force_flush) +inline std::shared_ptr spdlog::daily_logger_mt(const std::string& logger_name, const std::string& filename, int hour, int minute, bool force_flush) { - return create(logger_name, filename, "txt", force_flush); + return create(logger_name, filename, "txt", hour, minute, force_flush); } -inline std::shared_ptr spdlog::daily_logger_st(const std::string& logger_name, const std::string& filename, bool force_flush) +inline std::shared_ptr spdlog::daily_logger_st(const std::string& logger_name, const std::string& filename, int hour, int minute, bool force_flush) { - return create(logger_name, filename, "txt", force_flush); + return create(logger_name, filename, "txt", hour, minute, force_flush); } diff --git a/include/spdlog/sinks/file_sinks.h b/include/spdlog/sinks/file_sinks.h index 0f73f5c5..dd463d54 100644 --- a/include/spdlog/sinks/file_sinks.h +++ b/include/spdlog/sinks/file_sinks.h @@ -155,14 +155,21 @@ template class daily_file_sink:public base_sink { 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 daily_file_sink_mt; diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 3af3ad57..cd80a90c 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -84,10 +84,10 @@ std::shared_ptr rotating_logger_mt(const std::string& logger_name, const std::shared_ptr 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 daily_logger_mt(const std::string& logger_name, const std::string& filename, bool force_flush = false); -std::shared_ptr daily_logger_st(const std::string& logger_name, const std::string& filename, bool force_flush = false); +std::shared_ptr 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 daily_logger_st(const std::string& logger_name, const std::string& filename, int hour=0, int minute=0, bool force_flush = false); //