From 7446b49d4f4071ff2125596808178a9cccbdf032 Mon Sep 17 00:00:00 2001 From: Mohammad Ali Date: Wed, 18 Jan 2023 17:13:11 +0330 Subject: [PATCH] edit callback_sink, callback with formatted text deleted the callbacks struct deleted and the callback function type is just a typedef --- README.md | 12 +++------ example/example.cpp | 13 +++------- include/spdlog/sinks/callback_sink.h | 38 ++++++++-------------------- tests/test_custom_callbacks.cpp | 13 ++++++---- 4 files changed, 24 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index dbc5a81b..d650b3ed 100644 --- a/README.md +++ b/README.md @@ -240,15 +240,9 @@ void multi_sink_example() // each time something is logged to the logger void callback_example() { - spdlog::custom_log_callbacks callbacks; - callbacks.on_log_formatted = [](std::string msg){ - // for example you can be notified by sending an email to yourself - }; - // or you can catch the log_msg object - // callbacks.on_log = [](spdlog::details::log_msg msg){ - // }; - - auto callback_sink = std::make_shared(callbacks); + auto callback_sink = std::make_shared([](const spdlog::details::log_msg &msg) { + // for example you can be notified by sending an email to yourself + }); callback_sink->set_level(spdlog::level::err); auto console_sink = std::make_shared(); diff --git a/example/example.cpp b/example/example.cpp index b273c451..3853a139 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -141,17 +141,10 @@ void daily_example() #include "spdlog/sinks/callback_sink.h" void callback_example() { - // Define the callbacks - spdlog::custom_log_callbacks callbacks; - callbacks.on_log_formatted = [](std::string /*str*/){ - // do what you need to do with str - }; - callbacks.on_log = [](spdlog::details::log_msg /*msg*/){ - // do what you need to do with msg - }; - // Create the logger - auto logger = spdlog::callback_logger_mt("custom_callback_logger", callbacks); + auto logger = spdlog::callback_logger_mt("custom_callback_logger", [](const spdlog::details::log_msg &msg) { + // do what you need to do with msg + }); } #include "spdlog/cfg/env.h" diff --git a/include/spdlog/sinks/callback_sink.h b/include/spdlog/sinks/callback_sink.h index 68c82864..bcd31383 100644 --- a/include/spdlog/sinks/callback_sink.h +++ b/include/spdlog/sinks/callback_sink.h @@ -12,17 +12,8 @@ namespace spdlog { -// callbacks struct -struct custom_log_callbacks -{ - custom_log_callbacks() - : on_log(nullptr) - , on_log_formatted(nullptr) - {} - - std::function on_log; - std::function on_log_formatted; -}; +// callbacks type +typedef std::function custom_log_callback; namespace sinks { /* @@ -32,28 +23,19 @@ template class callback_sink final : public base_sink { public: - explicit callback_sink(const custom_log_callbacks &callbacks) - : callbacks_{callbacks} + explicit callback_sink(const custom_log_callback &callback) + : callback_{callback} {} protected: void sink_it_(const details::log_msg &msg) override { - if (callbacks_.on_log) - callbacks_.on_log(msg); - if (callbacks_.on_log_formatted) - { - memory_buf_t formatted; - base_sink::formatter_->format(msg, formatted); - auto eol_len = strlen(details::os::default_eol); - std::string str(formatted.data(), formatted.size() - eol_len); - callbacks_.on_log_formatted(str); - } + callback_(msg); } void flush_() override{}; private: - custom_log_callbacks callbacks_; + custom_log_callback callback_; }; using callback_sink_mt = callback_sink; @@ -65,15 +47,15 @@ using callback_sink_st = callback_sink; // factory functions // template -inline std::shared_ptr callback_logger_mt(const std::string &logger_name, const custom_log_callbacks &callbacks) +inline std::shared_ptr callback_logger_mt(const std::string &logger_name, const custom_log_callback &callback) { - return Factory::template create(logger_name, callbacks); + return Factory::template create(logger_name, callback); } template -inline std::shared_ptr callback_logger_st(const std::string &logger_name, const custom_log_callbacks &callbacks) +inline std::shared_ptr callback_logger_st(const std::string &logger_name, const custom_log_callback &callback) { - return Factory::template create(logger_name, callbacks); + return Factory::template create(logger_name, callback); } } // namespace spdlog diff --git a/tests/test_custom_callbacks.cpp b/tests/test_custom_callbacks.cpp index dd0f54f5..877e1608 100644 --- a/tests/test_custom_callbacks.cpp +++ b/tests/test_custom_callbacks.cpp @@ -5,15 +5,18 @@ #include "test_sink.h" #include "spdlog/sinks/callback_sink.h" #include "spdlog/async.h" - +#include "spdlog/common.h" TEST_CASE("custom_callback_logger", "[custom_callback_logger]]") { - spdlog::custom_log_callbacks callbacks; std::vector lines; - callbacks.on_log_formatted = [&](std::string str) { lines.push_back(str); }; - - auto callback_logger = std::make_shared(callbacks); + spdlog::pattern_formatter formatter; + auto callback_logger = std::make_shared([&](const spdlog::details::log_msg &msg) { + spdlog::memory_buf_t formatted; + formatter.format(msg, formatted); + auto eol_len = strlen(spdlog::details::os::default_eol); + lines.emplace_back(formatted.begin(), formatted.end() - eol_len); + }); std::shared_ptr test_sink(new spdlog::sinks::test_sink_st); spdlog::logger logger("test-callback", {callback_logger, test_sink});