edit callback_sink, callback with formatted text deleted

the callbacks struct deleted and the callback function type is just a typedef
pull/2610/head
Mohammad Ali 3 years ago
parent 75b8fe9faf
commit 7446b49d4f

@ -240,15 +240,9 @@ void multi_sink_example()
// each time something is logged to the logger // each time something is logged to the logger
void callback_example() void callback_example()
{ {
spdlog::custom_log_callbacks callbacks; auto callback_sink = std::make_shared<spdlog::sinks::callback_sink_mt>([](const spdlog::details::log_msg &msg) {
callbacks.on_log_formatted = [](std::string msg){ // for example you can be notified by sending an email to yourself
// 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<spdlog::sinks::callback_sink_mt>(callbacks);
callback_sink->set_level(spdlog::level::err); callback_sink->set_level(spdlog::level::err);
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>(); auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();

@ -141,17 +141,10 @@ void daily_example()
#include "spdlog/sinks/callback_sink.h" #include "spdlog/sinks/callback_sink.h"
void callback_example() 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 // 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" #include "spdlog/cfg/env.h"

@ -12,17 +12,8 @@
namespace spdlog { namespace spdlog {
// callbacks struct // callbacks type
struct custom_log_callbacks typedef std::function<void(const details::log_msg &msg)> custom_log_callback;
{
custom_log_callbacks()
: on_log(nullptr)
, on_log_formatted(nullptr)
{}
std::function<void(const details::log_msg &msg)> on_log;
std::function<void(const std::string &mag_str)> on_log_formatted;
};
namespace sinks { namespace sinks {
/* /*
@ -32,28 +23,19 @@ template<typename Mutex>
class callback_sink final : public base_sink<Mutex> class callback_sink final : public base_sink<Mutex>
{ {
public: public:
explicit callback_sink(const custom_log_callbacks &callbacks) explicit callback_sink(const custom_log_callback &callback)
: callbacks_{callbacks} : callback_{callback}
{} {}
protected: protected:
void sink_it_(const details::log_msg &msg) override void sink_it_(const details::log_msg &msg) override
{ {
if (callbacks_.on_log) callback_(msg);
callbacks_.on_log(msg);
if (callbacks_.on_log_formatted)
{
memory_buf_t formatted;
base_sink<Mutex>::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);
}
} }
void flush_() override{}; void flush_() override{};
private: private:
custom_log_callbacks callbacks_; custom_log_callback callback_;
}; };
using callback_sink_mt = callback_sink<std::mutex>; using callback_sink_mt = callback_sink<std::mutex>;
@ -65,15 +47,15 @@ using callback_sink_st = callback_sink<details::null_mutex>;
// factory functions // factory functions
// //
template<typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> callback_logger_mt(const std::string &logger_name, const custom_log_callbacks &callbacks) inline std::shared_ptr<logger> callback_logger_mt(const std::string &logger_name, const custom_log_callback &callback)
{ {
return Factory::template create<sinks::callback_sink_mt>(logger_name, callbacks); return Factory::template create<sinks::callback_sink_mt>(logger_name, callback);
} }
template<typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> callback_logger_st(const std::string &logger_name, const custom_log_callbacks &callbacks) inline std::shared_ptr<logger> callback_logger_st(const std::string &logger_name, const custom_log_callback &callback)
{ {
return Factory::template create<sinks::callback_sink_st>(logger_name, callbacks); return Factory::template create<sinks::callback_sink_st>(logger_name, callback);
} }
} // namespace spdlog } // namespace spdlog

@ -5,15 +5,18 @@
#include "test_sink.h" #include "test_sink.h"
#include "spdlog/sinks/callback_sink.h" #include "spdlog/sinks/callback_sink.h"
#include "spdlog/async.h" #include "spdlog/async.h"
#include "spdlog/common.h"
TEST_CASE("custom_callback_logger", "[custom_callback_logger]]") TEST_CASE("custom_callback_logger", "[custom_callback_logger]]")
{ {
spdlog::custom_log_callbacks callbacks;
std::vector<std::string> lines; std::vector<std::string> lines;
callbacks.on_log_formatted = [&](std::string str) { lines.push_back(str); }; spdlog::pattern_formatter formatter;
auto callback_logger = std::make_shared<spdlog::sinks::callback_sink_st>([&](const spdlog::details::log_msg &msg) {
auto callback_logger = std::make_shared<spdlog::sinks::callback_sink_mt>(callbacks); 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<spdlog::sinks::test_sink_st> test_sink(new spdlog::sinks::test_sink_st); std::shared_ptr<spdlog::sinks::test_sink_st> test_sink(new spdlog::sinks::test_sink_st);
spdlog::logger logger("test-callback", {callback_logger, test_sink}); spdlog::logger logger("test-callback", {callback_logger, test_sink});

Loading…
Cancel
Save