mirror of https://github.com/gabime/spdlog.git
Add a trivial callback sink
it will call a custom callback, each time something is logged. the code was initially cloned from `basic_file_sink.h` required test, example and readme sample, addedpull/2610/head
parent
654dbc5c32
commit
602510e27c
@ -0,0 +1,80 @@
|
||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <spdlog/details/null_mutex.h>
|
||||
#include <spdlog/sinks/base_sink.h>
|
||||
#include <spdlog/details/synchronous_factory.h>
|
||||
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
namespace spdlog {
|
||||
|
||||
// callbacks struct
|
||||
struct custom_log_callbacks
|
||||
{
|
||||
|
||||
custom_log_callbacks()
|
||||
: on_log(nullptr)
|
||||
, on_log_formatted(nullptr)
|
||||
{}
|
||||
|
||||
std::function<void(const std::string &mag_str)> on_log_formatted;
|
||||
std::function<void(const details::log_msg &msg)> on_log;
|
||||
};
|
||||
|
||||
namespace sinks {
|
||||
/*
|
||||
* Trivial callback sink, gets a callback function and calls it on each log
|
||||
*/
|
||||
template<typename Mutex>
|
||||
class callback_sink final : public base_sink<Mutex>
|
||||
{
|
||||
public:
|
||||
explicit callback_sink(const custom_log_callbacks &callbacks)
|
||||
: callbacks_{callbacks}
|
||||
{}
|
||||
|
||||
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<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{};
|
||||
|
||||
private:
|
||||
custom_log_callbacks callbacks_;
|
||||
};
|
||||
|
||||
using callback_sink_mt = callback_sink<std::mutex>;
|
||||
using callback_sink_st = callback_sink<details::null_mutex>;
|
||||
|
||||
} // namespace sinks
|
||||
|
||||
//
|
||||
// factory functions
|
||||
//
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> callback_logger_mt(const std::string &logger_name, const custom_log_callbacks &callbacks)
|
||||
{
|
||||
return Factory::template create<sinks::callback_sink_mt>(logger_name, callbacks);
|
||||
}
|
||||
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> callback_logger_st(const std::string &logger_name, const custom_log_callbacks &callbacks)
|
||||
{
|
||||
return Factory::template create<sinks::callback_sink_st>(logger_name, callbacks);
|
||||
}
|
||||
|
||||
} // namespace spdlog
|
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* This content is released under the MIT License as specified in https://raw.githubusercontent.com/gabime/spdlog/master/LICENSE
|
||||
*/
|
||||
#include "includes.h"
|
||||
#include "test_sink.h"
|
||||
#include "spdlog/sinks/callback_sink.h"
|
||||
#include "spdlog/async.h"
|
||||
|
||||
|
||||
TEST_CASE("custom_callback_logger", "[custom_callback_logger]]")
|
||||
{
|
||||
spdlog::custom_log_callbacks callbacks;
|
||||
std::vector<std::string> lines;
|
||||
callbacks.on_log_formatted = [&](std::string str) { lines.push_back(str); };
|
||||
|
||||
auto callback_logger = std::make_shared<spdlog::sinks::callback_sink_mt>(callbacks);
|
||||
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});
|
||||
|
||||
logger.info("test message 1");
|
||||
logger.info("test message 2");
|
||||
logger.info("test message 3");
|
||||
|
||||
std::vector<std::string> ref_lines = test_sink->lines();
|
||||
|
||||
REQUIRE(lines[0] == ref_lines[0]);
|
||||
REQUIRE(lines[1] == ref_lines[1]);
|
||||
REQUIRE(lines[2] == ref_lines[2]);
|
||||
spdlog::drop_all();
|
||||
}
|
Loading…
Reference in New Issue