From cbaf4880adbf78515a4347225472deb45c42c4c9 Mon Sep 17 00:00:00 2001 From: Gabi Melman Date: Mon, 15 Nov 2021 14:42:36 +0200 Subject: [PATCH] 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