From 30fb78813beb00ca011ef56db8c2095451829f77 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 15 Nov 2021 14:32:34 +0200 Subject: [PATCH] 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"); }