diff --git a/include/spdlog/details/spdlog_impl.h b/include/spdlog/details/spdlog_impl.h index 8337ab47..a0cf8033 100644 --- a/include/spdlog/details/spdlog_impl.h +++ b/include/spdlog/details/spdlog_impl.h @@ -57,6 +57,17 @@ inline std::shared_ptr spdlog::daily_logger_st(const std::string return create(logger_name, filename, SPDLOG_FILENAME_T("txt"), hour, minute, force_flush); } +// Create file logger which creates new file every hour): +inline std::shared_ptr spdlog::hourly_logger_mt(const std::string& logger_name, const filename_t& filename, bool force_flush) +{ + return create(logger_name, filename, SPDLOG_FILENAME_T("txt"), force_flush); +} + +inline std::shared_ptr spdlog::hourly_logger_st(const std::string& logger_name, const filename_t& filename, bool force_flush) +{ + return create(logger_name, filename, SPDLOG_FILENAME_T("txt"), force_flush); +} + // Create stdout/stderr loggers (with optinal color support) inline std::shared_ptr create_console_logger(const std::string& logger_name, spdlog::sink_ptr sink, bool color) { diff --git a/include/spdlog/sinks/file_sinks.h b/include/spdlog/sinks/file_sinks.h index 14b3cbff..2bbc0c3c 100644 --- a/include/spdlog/sinks/file_sinks.h +++ b/include/spdlog/sinks/file_sinks.h @@ -238,5 +238,62 @@ private: typedef daily_file_sink daily_file_sink_mt; typedef daily_file_sink daily_file_sink_st; + +/* +* Rotating file sink based on hour. rotates every hour +*/ +template +class hourly_file_sink :public base_sink < Mutex > +{ +public: + //create hourly file sink which rotates on given time + hourly_file_sink( + const filename_t& base_filename, + const filename_t& extension, + bool force_flush = false) : _base_filename(base_filename), + _extension(extension), + _file_helper(force_flush) + { + _rotation_tp = _next_rotation_tp(); + _file_helper.open(FileNameCalc::calc_filename(_base_filename, _extension)); + } + + void flush() override + { + _file_helper.flush(); + } + +protected: + void _sink_it(const details::log_msg& msg) override + { + if (std::chrono::system_clock::now() >= _rotation_tp) + { + _file_helper.open(FileNameCalc::calc_filename(_base_filename, _extension)); + _rotation_tp = _next_rotation_tp(); + } + _file_helper.write(msg); + } + +private: + std::chrono::system_clock::time_point _next_rotation_tp() + { + using namespace std::chrono; + auto now = system_clock::now(); + now += hours(1); + time_t tnow = std::chrono::system_clock::to_time_t(now); + tm date = spdlog::details::os::localtime(tnow); + date.tm_sec = 0; + auto rotation_time = std::chrono::system_clock::from_time_t(std::mktime(&date)); + return rotation_time; + } + + filename_t _base_filename; + filename_t _extension; + std::chrono::system_clock::time_point _rotation_tp; + details::file_helper _file_helper; +}; + +typedef hourly_file_sink hourly_file_sink_mt; +typedef hourly_file_sink hourly_file_sink_st; } } diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 043e8141..0f707f1f 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -74,6 +74,12 @@ std::shared_ptr rotating_logger_st(const std::string& logger_name, const std::shared_ptr daily_logger_mt(const std::string& logger_name, const filename_t& filename, int hour=0, int minute=0, bool force_flush = false); std::shared_ptr daily_logger_st(const std::string& logger_name, const filename_t& filename, int hour=0, int minute=0, bool force_flush = false); +// +// Create file logger which creates new file on the given hour: +// +std::shared_ptr hourly_logger_mt(const std::string& logger_name, const filename_t& filename, bool force_flush = false); +std::shared_ptr hourly_logger_st(const std::string& logger_name, const filename_t& filename, bool force_flush = false); + // // Create and register stdout/stderr loggers //