From 24a551c14e96e16d1338b072976610b62d322e84 Mon Sep 17 00:00:00 2001 From: seker <04070628@163.com> Date: Sat, 13 Nov 2021 17:19:23 +0800 Subject: [PATCH 01/10] file_event_handlers add before_open function --- example/example.cpp | 6 +++++- include/spdlog/common.h | 6 +++++- include/spdlog/details/file_helper-inl.h | 4 ++++ include/spdlog/details/file_helper.h | 2 +- include/spdlog/sinks/basic_file_sink.h | 6 +++--- include/spdlog/sinks/daily_file_sink.h | 10 +++++----- include/spdlog/sinks/hourly_file_sink.h | 6 +++--- include/spdlog/sinks/rotating_file_sink.h | 6 +++--- 8 files changed, 29 insertions(+), 17 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index 56e28825..1e145312 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -121,7 +121,11 @@ void rotating_example() { // Create a file rotating logger with 5mb size max and 3 rotated files. spdlog::file_event_handlers_t file_event_handlers; - file_event_handlers.after_open = [](spdlog::filename_t filename, std::FILE* fstream) + file_event_handlers.before_open = [](spdlog::filename_t filename) + { + spdlog::info("basic_example() : file_event_handlers.before_open : {}", filename.c_str()); + }; + file_event_handlers.after_open = [](spdlog::filename_t filename, std::FILE* fstream) { fputs("OPEN!\r\n", fstream); spdlog::info("basic_example() : file_event_handlers.after_open : {}", filename.c_str()); diff --git a/include/spdlog/common.h b/include/spdlog/common.h index ddaa34ce..70d4daed 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -256,11 +256,15 @@ struct source_loc const char *funcname{nullptr}; }; -typedef struct +typedef struct file_event_handlers { + std::function before_open; std::function after_open; std::function before_close; std::function after_close; + + file_event_handlers(): before_open(nullptr), after_open(nullptr), before_close(nullptr), after_close(nullptr) + {} } file_event_handlers_t; namespace details { diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index df6cd4c1..ab36f52f 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -37,6 +37,10 @@ SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) auto *mode = SPDLOG_FILENAME_T("ab"); auto *trunc_mode = SPDLOG_FILENAME_T("wb"); + if (event_handlers_.before_open) + { + event_handlers_.before_open(filename_); + } for (int tries = 0; tries < open_tries_; ++tries) { // create containing folder if not exists already. diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index 60c47788..6b63f3a1 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -51,7 +51,7 @@ private: const unsigned int open_interval_ = 10; std::FILE *fd_{nullptr}; filename_t filename_; - file_event_handlers_t event_handlers_{nullptr, nullptr, nullptr}; + file_event_handlers_t event_handlers_; }; } // namespace details } // namespace spdlog diff --git a/include/spdlog/sinks/basic_file_sink.h b/include/spdlog/sinks/basic_file_sink.h index 51e26e38..f2e35f6e 100644 --- a/include/spdlog/sinks/basic_file_sink.h +++ b/include/spdlog/sinks/basic_file_sink.h @@ -20,7 +20,7 @@ template class basic_file_sink final : public base_sink { public: - explicit basic_file_sink(const filename_t &filename, bool truncate = false, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr}); + explicit basic_file_sink(const filename_t &filename, bool truncate = false, const file_event_handlers_t& event_handlers = {}); const filename_t &filename() const; protected: @@ -40,13 +40,13 @@ using basic_file_sink_st = basic_file_sink; // factory functions // template -inline std::shared_ptr basic_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate = false, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr}) +inline std::shared_ptr basic_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate = false, const file_event_handlers_t& event_handlers = {}) { return Factory::template create(logger_name, filename, truncate, event_handlers); } template -inline std::shared_ptr basic_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate = false, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr}) +inline std::shared_ptr basic_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate = false, const file_event_handlers_t& event_handlers = {}) { return Factory::template create(logger_name, filename, truncate, event_handlers); } diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index fc552398..2958a001 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -68,7 +68,7 @@ class daily_file_sink final : public base_sink { public: // create daily file sink which rotates on given time - daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr}) + daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {}) : base_filename_(std::move(base_filename)) , rotation_h_(rotation_hour) , rotation_m_(rotation_minute) @@ -215,28 +215,28 @@ using daily_file_format_sink_st = daily_file_sink inline std::shared_ptr daily_logger_mt( - const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr}) + const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {}) { return Factory::template create(logger_name, filename, hour, minute, truncate, max_files, event_handlers); } template inline std::shared_ptr daily_logger_format_mt( - const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr}) + const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {}) { return Factory::template create(logger_name, filename, hour, minute, truncate, max_files, event_handlers); } template inline std::shared_ptr daily_logger_st( - const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr}) + const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {}) { return Factory::template create(logger_name, filename, hour, minute, truncate, max_files, event_handlers); } template inline std::shared_ptr daily_logger_format_st( - const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr}) + const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {}) { return Factory::template create(logger_name, filename, hour, minute, truncate, max_files, event_handlers); } diff --git a/include/spdlog/sinks/hourly_file_sink.h b/include/spdlog/sinks/hourly_file_sink.h index 653faceb..8914d883 100644 --- a/include/spdlog/sinks/hourly_file_sink.h +++ b/include/spdlog/sinks/hourly_file_sink.h @@ -46,7 +46,7 @@ class hourly_file_sink final : public base_sink { public: // create hourly file sink which rotates on given time - hourly_file_sink(filename_t base_filename, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr}) + hourly_file_sink(filename_t base_filename, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {}) : base_filename_(std::move(base_filename)) , file_helper_{event_handlers} , truncate_(truncate) @@ -181,14 +181,14 @@ using hourly_file_sink_st = hourly_file_sink; // template inline std::shared_ptr hourly_logger_mt( - const std::string &logger_name, const filename_t &filename, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr}) + const std::string &logger_name, const filename_t &filename, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {}) { return Factory::template create(logger_name, filename, truncate, max_files, event_handlers); } template inline std::shared_ptr hourly_logger_st( - const std::string &logger_name, const filename_t &filename, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr}) + const std::string &logger_name, const filename_t &filename, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {}) { return Factory::template create(logger_name, filename, truncate, max_files, event_handlers); } diff --git a/include/spdlog/sinks/rotating_file_sink.h b/include/spdlog/sinks/rotating_file_sink.h index 8455227f..304e9e20 100644 --- a/include/spdlog/sinks/rotating_file_sink.h +++ b/include/spdlog/sinks/rotating_file_sink.h @@ -22,7 +22,7 @@ template class rotating_file_sink final : public base_sink { public: - rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open = false, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr}); + rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open = false, const file_event_handlers_t& event_handlers = {}); static filename_t calc_filename(const filename_t &filename, std::size_t index); filename_t filename(); @@ -60,14 +60,14 @@ using rotating_file_sink_st = rotating_file_sink; template inline std::shared_ptr rotating_logger_mt( - const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr}) + const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false, const file_event_handlers_t& event_handlers = {}) { return Factory::template create(logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers); } template inline std::shared_ptr rotating_logger_st( - const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false, const file_event_handlers_t& event_handlers = {nullptr, nullptr, nullptr}) + const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false, const file_event_handlers_t& event_handlers = {}) { return Factory::template create(logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers); } From 30fb78813beb00ca011ef56db8c2095451829f77 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 15 Nov 2021 14:32:34 +0200 Subject: [PATCH 02/10] Updated file events example --- example/example.cpp | 54 +++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index 1e145312..a3a6ff7a 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -22,6 +22,7 @@ void err_handler_example(); void syslog_example(); void udp_example(); void custom_flags_example(); +void file_events_example(); #include "spdlog/spdlog.h" #include "spdlog/cfg/env.h" // support for loading levels from the environment variable @@ -73,11 +74,13 @@ int main(int, char *[]) binary_example(); multi_sink_example(); user_defined_example(); - err_handler_example(); + err_handler_example(); trace_example(); stopwatch_example(); udp_example(); custom_flags_example(); + file_events_example(); + // Flush all *registered* loggers using a worker thread every 3 seconds. // note: registered loggers *must* be thread safe for this to work correctly! @@ -120,26 +123,7 @@ void basic_example() void rotating_example() { // Create a file rotating logger with 5mb size max and 3 rotated files. - spdlog::file_event_handlers_t file_event_handlers; - file_event_handlers.before_open = [](spdlog::filename_t filename) - { - spdlog::info("basic_example() : file_event_handlers.before_open : {}", filename.c_str()); - }; - file_event_handlers.after_open = [](spdlog::filename_t filename, std::FILE* fstream) - { - fputs("OPEN!\r\n", fstream); - spdlog::info("basic_example() : file_event_handlers.after_open : {}", filename.c_str()); - }; - file_event_handlers.before_close = [](spdlog::filename_t filename, std::FILE* fstream) - { - fputs("CLOSE!\r\n", fstream); - spdlog::info("basic_example() : file_event_handlers.before_close : {}", filename.c_str()); - }; - file_event_handlers.after_close = [](spdlog::filename_t filename) - { - spdlog::info("basic_example() : file_event_handlers.after_close : {}", filename.c_str()); - }; - auto rotating_logger = spdlog::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3, false, file_event_handlers); + auto rotating_logger = spdlog::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3); } #include "spdlog/sinks/daily_file_sink.h" @@ -321,5 +305,31 @@ void custom_flags_example() using spdlog::details::make_unique; // for pre c++14 auto formatter = make_unique(); formatter->add_flag('*').set_pattern("[%n] [%*] [%^%l%$] %v"); - spdlog::set_formatter(std::move(formatter)); + // set the new formatter using spdlog::set_formatter(formatter) or logger->set_formatter(formatter) + //spdlog::set_formatter(std::move(formatter)); +} + +void file_events_example(){ + + // pass the spdlog::file_event_handlers_t to file sinks for open/close log file notifications + spdlog::file_event_handlers_t handlers; + handlers.before_open = [](spdlog::filename_t filename) { + spdlog::info("Before opening {}", filename); + }; + handlers.after_open = [](spdlog::filename_t filename, std::FILE *fstream) { + spdlog::info("After opening {}", filename); + fputs("After opening\n", fstream); + }; + handlers.before_close = [](spdlog::filename_t filename, std::FILE *fstream) { + spdlog::info("Before closing {}", filename); + fputs("Before closing\n", fstream); + }; + handlers.after_close = [](spdlog::filename_t filename) { + spdlog::info("After closing {}", filename); + }; + auto file_sink = std::make_shared("logs/events-sample.txt", true, handlers); + spdlog::logger my_logger("some_logger", file_sink); + // or + // auto my_logger = spdlog::basic_logger_st("some_logger", "logs/events-sample.txt", true, handlers); + my_logger.info("Some log line"); } From b813bb863d0b2616300547d864331cc10023ddc4 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 15 Nov 2021 14:35:00 +0200 Subject: [PATCH 03/10] Updated file_events example --- example/example.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index a3a6ff7a..a4324ffa 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -316,12 +316,10 @@ void file_events_example(){ handlers.before_open = [](spdlog::filename_t filename) { spdlog::info("Before opening {}", filename); }; - handlers.after_open = [](spdlog::filename_t filename, std::FILE *fstream) { - spdlog::info("After opening {}", filename); + handlers.after_open = [](spdlog::filename_t filename, std::FILE *fstream) { fputs("After opening\n", fstream); }; - handlers.before_close = [](spdlog::filename_t filename, std::FILE *fstream) { - spdlog::info("Before closing {}", filename); + handlers.before_close = [](spdlog::filename_t filename, std::FILE *fstream) { fputs("Before closing\n", fstream); }; handlers.after_close = [](spdlog::filename_t filename) { From cbaf4880adbf78515a4347225472deb45c42c4c9 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Mon, 15 Nov 2021 14:42:36 +0200 Subject: [PATCH 04/10] Update README.md --- README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.md b/README.md index 00e0ac29..6c3c4c28 100644 --- a/README.md +++ b/README.md @@ -373,6 +373,34 @@ $ export SPDLOG_LEVEL=info,mylogger=trace $ ./example ``` + +--- +#### Log file open/close event handlers +```c++ +// You can get subscribe to get callbacks from spdlog before/after log file has been opened or closed. +// This is useful for cleanup procedures or for adding someting the start/end of the log files. +void file_events_example(){ + spdlog::file_event_handlers_t handlers; + handlers.before_open = [](spdlog::filename_t filename) { + spdlog::info("Before opening {}", filename); + }; + handlers.after_open = [](spdlog::filename_t filename, std::FILE *fstream) { + fputs("After opening\n", fstream); + }; + handlers.before_close = [](spdlog::filename_t filename, std::FILE *fstream) { + fputs("Before closing\n", fstream); + }; + handlers.after_close = [](spdlog::filename_t filename) { + spdlog::info("After closing {}", filename); + }; + auto file_sink = std::make_shared("logs/events-sample.txt", true, handlers); + spdlog::logger my_logger("some_logger", file_sink); + // or + // auto my_logger = spdlog::basic_logger_st("some_logger", "logs/events-sample.txt", true, handlers); + my_logger.info("Some log line"); +} +``` + --- ## Benchmarks From a5fa6eb356a0b408211458b1da0d2f7aadd5ad1c Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Mon, 15 Nov 2021 14:45:07 +0200 Subject: [PATCH 05/10] Update README.md --- README.md | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 6c3c4c28..f964e067 100644 --- a/README.md +++ b/README.md @@ -379,23 +379,17 @@ $ ./example ```c++ // You can get subscribe to get callbacks from spdlog before/after log file has been opened or closed. // This is useful for cleanup procedures or for adding someting the start/end of the log files. -void file_events_example(){ +void file_events_example() +{ + // pass the spdlog::file_event_handlers_t to file sinks for open/close log file notifications spdlog::file_event_handlers_t handlers; - handlers.before_open = [](spdlog::filename_t filename) { - spdlog::info("Before opening {}", filename); - }; - handlers.after_open = [](spdlog::filename_t filename, std::FILE *fstream) { - fputs("After opening\n", fstream); - }; - handlers.before_close = [](spdlog::filename_t filename, std::FILE *fstream) { - fputs("Before closing\n", fstream); - }; - handlers.after_close = [](spdlog::filename_t filename) { - spdlog::info("After closing {}", filename); - }; + handlers.before_open = [](spdlog::filename_t filename) { spdlog::info("Before opening {}", filename); }; + handlers.after_open = [](spdlog::filename_t filename, std::FILE *fstream) { fputs("After opening\n", fstream); }; + handlers.before_close = [](spdlog::filename_t filename, std::FILE *fstream) { fputs("Before closing\n", fstream); }; + handlers.after_close = [](spdlog::filename_t filename) { spdlog::info("After closing {}", filename); }; auto file_sink = std::make_shared("logs/events-sample.txt", true, handlers); - spdlog::logger my_logger("some_logger", file_sink); - // or + spdlog::logger my_logger("some_logger", file_sink); + // or // auto my_logger = spdlog::basic_logger_st("some_logger", "logs/events-sample.txt", true, handlers); my_logger.info("Some log line"); } From ea92864a4d01655befa2f08463085dbd812f865d Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Mon, 15 Nov 2021 14:46:23 +0200 Subject: [PATCH 06/10] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f964e067..b7d6d034 100644 --- a/README.md +++ b/README.md @@ -388,6 +388,7 @@ void file_events_example() handlers.before_close = [](spdlog::filename_t filename, std::FILE *fstream) { fputs("Before closing\n", fstream); }; handlers.after_close = [](spdlog::filename_t filename) { spdlog::info("After closing {}", filename); }; auto file_sink = std::make_shared("logs/events-sample.txt", true, handlers); + spdlog::logger my_logger("some_logger", file_sink); // or // auto my_logger = spdlog::basic_logger_st("some_logger", "logs/events-sample.txt", true, handlers); From da621e4402f6af5a16068bf3b5ce487e1fec5fcd Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Mon, 15 Nov 2021 14:48:20 +0200 Subject: [PATCH 07/10] Update README.md --- README.md | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/README.md b/README.md index b7d6d034..ed673345 100644 --- a/README.md +++ b/README.md @@ -387,12 +387,7 @@ void file_events_example() handlers.after_open = [](spdlog::filename_t filename, std::FILE *fstream) { fputs("After opening\n", fstream); }; handlers.before_close = [](spdlog::filename_t filename, std::FILE *fstream) { fputs("Before closing\n", fstream); }; handlers.after_close = [](spdlog::filename_t filename) { spdlog::info("After closing {}", filename); }; - auto file_sink = std::make_shared("logs/events-sample.txt", true, handlers); - - spdlog::logger my_logger("some_logger", file_sink); - // or - // auto my_logger = spdlog::basic_logger_st("some_logger", "logs/events-sample.txt", true, handlers); - my_logger.info("Some log line"); + auto my_logger = spdlog::basic_logger_st("some_logger", "logs/events-sample.txt", true, handlers); } ``` From 698516f3f5e88c7fa31434ef0abb64b5d63e39be Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 15 Nov 2021 14:54:51 +0200 Subject: [PATCH 08/10] Updated example --- example/example.cpp | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index a4324ffa..119fff55 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -74,13 +74,12 @@ int main(int, char *[]) binary_example(); multi_sink_example(); user_defined_example(); - err_handler_example(); + err_handler_example(); trace_example(); stopwatch_example(); udp_example(); custom_flags_example(); file_events_example(); - // Flush all *registered* loggers using a worker thread every 3 seconds. // note: registered loggers *must* be thread safe for this to work correctly! @@ -306,28 +305,24 @@ void custom_flags_example() auto formatter = make_unique(); formatter->add_flag('*').set_pattern("[%n] [%*] [%^%l%$] %v"); // set the new formatter using spdlog::set_formatter(formatter) or logger->set_formatter(formatter) - //spdlog::set_formatter(std::move(formatter)); + // spdlog::set_formatter(std::move(formatter)); } -void file_events_example(){ - - // pass the spdlog::file_event_handlers_t to file sinks for open/close log file notifications +void file_events_example() +{ + // pass the spdlog::file_event_handlers_t to file sinks for open/close log file notifications spdlog::file_event_handlers_t handlers; - handlers.before_open = [](spdlog::filename_t filename) { - spdlog::info("Before opening {}", filename); + handlers.before_open = [](spdlog::filename_t filename) { spdlog::info("Before opening {}", filename); }; + handlers.after_open = [](spdlog::filename_t filename, std::FILE *fstream) { + spdlog::info("After opening {}", filename); + fputs("After opening\n", fstream); }; - handlers.after_open = [](spdlog::filename_t filename, std::FILE *fstream) { - fputs("After opening\n", fstream); + handlers.before_close = [](spdlog::filename_t filename, std::FILE *fstream) { + spdlog::info("Before closing {}", filename); + fputs("Before closing\n", fstream); }; - handlers.before_close = [](spdlog::filename_t filename, std::FILE *fstream) { - fputs("Before closing\n", fstream); - }; - handlers.after_close = [](spdlog::filename_t filename) { - spdlog::info("After closing {}", filename); - }; + handlers.after_close = [](spdlog::filename_t filename) { spdlog::info("After closing {}", filename); }; auto file_sink = std::make_shared("logs/events-sample.txt", true, handlers); - spdlog::logger my_logger("some_logger", file_sink); - // or - // auto my_logger = spdlog::basic_logger_st("some_logger", "logs/events-sample.txt", true, handlers); + spdlog::logger my_logger("some_logger", file_sink); my_logger.info("Some log line"); } From 5b7dfefc7e94b74c7f84db874abffe716df49257 Mon Sep 17 00:00:00 2001 From: gabime Date: Tue, 16 Nov 2021 16:41:04 +0200 Subject: [PATCH 09/10] rename file_event_handlers_t to file_event_handlers --- README.md | 4 ++-- example/example.cpp | 4 ++-- include/spdlog/common.h | 4 ++-- include/spdlog/details/file_helper-inl.h | 2 +- include/spdlog/details/file_helper.h | 4 ++-- include/spdlog/sinks/basic_file_sink-inl.h | 2 +- include/spdlog/sinks/basic_file_sink.h | 6 +++--- include/spdlog/sinks/daily_file_sink.h | 10 +++++----- include/spdlog/sinks/hourly_file_sink.h | 6 +++--- include/spdlog/sinks/rotating_file_sink-inl.h | 2 +- include/spdlog/sinks/rotating_file_sink.h | 6 +++--- 11 files changed, 25 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index ed673345..d1f228c2 100644 --- a/README.md +++ b/README.md @@ -381,8 +381,8 @@ $ ./example // This is useful for cleanup procedures or for adding someting the start/end of the log files. void file_events_example() { - // pass the spdlog::file_event_handlers_t to file sinks for open/close log file notifications - spdlog::file_event_handlers_t handlers; + // pass the spdlog::file_event_handlers to file sinks for open/close log file notifications + spdlog::file_event_handlers handlers; handlers.before_open = [](spdlog::filename_t filename) { spdlog::info("Before opening {}", filename); }; handlers.after_open = [](spdlog::filename_t filename, std::FILE *fstream) { fputs("After opening\n", fstream); }; handlers.before_close = [](spdlog::filename_t filename, std::FILE *fstream) { fputs("Before closing\n", fstream); }; diff --git a/example/example.cpp b/example/example.cpp index 119fff55..e379ca34 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -310,8 +310,8 @@ void custom_flags_example() void file_events_example() { - // pass the spdlog::file_event_handlers_t to file sinks for open/close log file notifications - spdlog::file_event_handlers_t handlers; + // pass the spdlog::file_event_handlers to file sinks for open/close log file notifications + spdlog::file_event_handlers handlers; handlers.before_open = [](spdlog::filename_t filename) { spdlog::info("Before opening {}", filename); }; handlers.after_open = [](spdlog::filename_t filename, std::FILE *fstream) { spdlog::info("After opening {}", filename); diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 0dac1726..95877beb 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -256,7 +256,7 @@ struct source_loc const char *funcname{nullptr}; }; -typedef struct file_event_handlers +struct file_event_handlers { std::function before_open; std::function after_open; @@ -265,7 +265,7 @@ typedef struct file_event_handlers file_event_handlers(): before_open(nullptr), after_open(nullptr), before_close(nullptr), after_close(nullptr) {} -} file_event_handlers_t; +}; namespace details { // make_unique support for pre c++14 diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h index ab36f52f..63d511c6 100644 --- a/include/spdlog/details/file_helper-inl.h +++ b/include/spdlog/details/file_helper-inl.h @@ -20,7 +20,7 @@ namespace spdlog { namespace details { -SPDLOG_INLINE file_helper::file_helper(const file_event_handlers_t& event_handlers) +SPDLOG_INLINE file_helper::file_helper(const file_event_handlers& event_handlers) : event_handlers_(event_handlers) {} diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index 6b63f3a1..69c5afdc 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -17,7 +17,7 @@ class SPDLOG_API file_helper { public: file_helper() = default; - explicit file_helper(const file_event_handlers_t& event_handlers); + explicit file_helper(const file_event_handlers& event_handlers); file_helper(const file_helper &) = delete; file_helper &operator=(const file_helper &) = delete; @@ -51,7 +51,7 @@ private: const unsigned int open_interval_ = 10; std::FILE *fd_{nullptr}; filename_t filename_; - file_event_handlers_t event_handlers_; + file_event_handlers event_handlers_; }; } // namespace details } // namespace spdlog diff --git a/include/spdlog/sinks/basic_file_sink-inl.h b/include/spdlog/sinks/basic_file_sink-inl.h index 463e075f..af383214 100644 --- a/include/spdlog/sinks/basic_file_sink-inl.h +++ b/include/spdlog/sinks/basic_file_sink-inl.h @@ -14,7 +14,7 @@ namespace spdlog { namespace sinks { template -SPDLOG_INLINE basic_file_sink::basic_file_sink(const filename_t &filename, bool truncate, const file_event_handlers_t& event_handlers) +SPDLOG_INLINE basic_file_sink::basic_file_sink(const filename_t &filename, bool truncate, const file_event_handlers& event_handlers) : file_helper_{event_handlers} { file_helper_.open(filename, truncate); diff --git a/include/spdlog/sinks/basic_file_sink.h b/include/spdlog/sinks/basic_file_sink.h index f2e35f6e..12cd95ef 100644 --- a/include/spdlog/sinks/basic_file_sink.h +++ b/include/spdlog/sinks/basic_file_sink.h @@ -20,7 +20,7 @@ template class basic_file_sink final : public base_sink { public: - explicit basic_file_sink(const filename_t &filename, bool truncate = false, const file_event_handlers_t& event_handlers = {}); + explicit basic_file_sink(const filename_t &filename, bool truncate = false, const file_event_handlers& event_handlers = {}); const filename_t &filename() const; protected: @@ -40,13 +40,13 @@ using basic_file_sink_st = basic_file_sink; // factory functions // template -inline std::shared_ptr basic_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate = false, const file_event_handlers_t& event_handlers = {}) +inline std::shared_ptr basic_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate = false, const file_event_handlers& event_handlers = {}) { return Factory::template create(logger_name, filename, truncate, event_handlers); } template -inline std::shared_ptr basic_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate = false, const file_event_handlers_t& event_handlers = {}) +inline std::shared_ptr basic_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate = false, const file_event_handlers& event_handlers = {}) { return Factory::template create(logger_name, filename, truncate, event_handlers); } diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 000becb4..a4469d64 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -68,7 +68,7 @@ class daily_file_sink final : public base_sink { public: // create daily file sink which rotates on given time - daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {}) + daily_file_sink(filename_t base_filename, int rotation_hour, int rotation_minute, bool truncate = false, uint16_t max_files = 0, const file_event_handlers& event_handlers = {}) : base_filename_(std::move(base_filename)) , rotation_h_(rotation_hour) , rotation_m_(rotation_minute) @@ -215,28 +215,28 @@ using daily_file_format_sink_st = daily_file_sink inline std::shared_ptr daily_logger_mt( - const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {}) + const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers& event_handlers = {}) { return Factory::template create(logger_name, filename, hour, minute, truncate, max_files, event_handlers); } template inline std::shared_ptr daily_logger_format_mt( - const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {}) + const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers& event_handlers = {}) { return Factory::template create(logger_name, filename, hour, minute, truncate, max_files, event_handlers); } template inline std::shared_ptr daily_logger_st( - const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {}) + const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers& event_handlers = {}) { return Factory::template create(logger_name, filename, hour, minute, truncate, max_files, event_handlers); } template inline std::shared_ptr daily_logger_format_st( - const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {}) + const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0, const file_event_handlers& event_handlers = {}) { return Factory::template create(logger_name, filename, hour, minute, truncate, max_files, event_handlers); } diff --git a/include/spdlog/sinks/hourly_file_sink.h b/include/spdlog/sinks/hourly_file_sink.h index 8914d883..3656cb0f 100644 --- a/include/spdlog/sinks/hourly_file_sink.h +++ b/include/spdlog/sinks/hourly_file_sink.h @@ -46,7 +46,7 @@ class hourly_file_sink final : public base_sink { public: // create hourly file sink which rotates on given time - hourly_file_sink(filename_t base_filename, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {}) + hourly_file_sink(filename_t base_filename, bool truncate = false, uint16_t max_files = 0, const file_event_handlers& event_handlers = {}) : base_filename_(std::move(base_filename)) , file_helper_{event_handlers} , truncate_(truncate) @@ -181,14 +181,14 @@ using hourly_file_sink_st = hourly_file_sink; // template inline std::shared_ptr hourly_logger_mt( - const std::string &logger_name, const filename_t &filename, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {}) + const std::string &logger_name, const filename_t &filename, bool truncate = false, uint16_t max_files = 0, const file_event_handlers& event_handlers = {}) { return Factory::template create(logger_name, filename, truncate, max_files, event_handlers); } template inline std::shared_ptr hourly_logger_st( - const std::string &logger_name, const filename_t &filename, bool truncate = false, uint16_t max_files = 0, const file_event_handlers_t& event_handlers = {}) + const std::string &logger_name, const filename_t &filename, bool truncate = false, uint16_t max_files = 0, const file_event_handlers& event_handlers = {}) { return Factory::template create(logger_name, filename, truncate, max_files, event_handlers); } diff --git a/include/spdlog/sinks/rotating_file_sink-inl.h b/include/spdlog/sinks/rotating_file_sink-inl.h index 2e3087af..ae715d28 100644 --- a/include/spdlog/sinks/rotating_file_sink-inl.h +++ b/include/spdlog/sinks/rotating_file_sink-inl.h @@ -25,7 +25,7 @@ namespace sinks { template SPDLOG_INLINE rotating_file_sink::rotating_file_sink( - filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open, const file_event_handlers_t& event_handlers) + filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open, const file_event_handlers& event_handlers) : base_filename_(std::move(base_filename)) , max_size_(max_size) , max_files_(max_files) diff --git a/include/spdlog/sinks/rotating_file_sink.h b/include/spdlog/sinks/rotating_file_sink.h index 304e9e20..d34ec02e 100644 --- a/include/spdlog/sinks/rotating_file_sink.h +++ b/include/spdlog/sinks/rotating_file_sink.h @@ -22,7 +22,7 @@ template class rotating_file_sink final : public base_sink { public: - rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open = false, const file_event_handlers_t& event_handlers = {}); + rotating_file_sink(filename_t base_filename, std::size_t max_size, std::size_t max_files, bool rotate_on_open = false, const file_event_handlers& event_handlers = {}); static filename_t calc_filename(const filename_t &filename, std::size_t index); filename_t filename(); @@ -60,14 +60,14 @@ using rotating_file_sink_st = rotating_file_sink; template inline std::shared_ptr rotating_logger_mt( - const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false, const file_event_handlers_t& event_handlers = {}) + const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false, const file_event_handlers& event_handlers = {}) { return Factory::template create(logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers); } template inline std::shared_ptr rotating_logger_st( - const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false, const file_event_handlers_t& event_handlers = {}) + const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open = false, const file_event_handlers& event_handlers = {}) { return Factory::template create(logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers); } From 518bf36aa97f77d9f83c5fde1a4bc48f8a667654 Mon Sep 17 00:00:00 2001 From: gabime Date: Tue, 16 Nov 2021 16:44:47 +0200 Subject: [PATCH 10/10] removed redundant intialization --- include/spdlog/common.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 95877beb..ce62b9b9 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -262,9 +262,6 @@ struct file_event_handlers std::function after_open; std::function before_close; std::function after_close; - - file_event_handlers(): before_open(nullptr), after_open(nullptr), before_close(nullptr), after_close(nullptr) - {} }; namespace details {