|
|
|
@ -12,17 +12,8 @@
|
|
|
|
|
|
|
|
|
|
namespace spdlog {
|
|
|
|
|
|
|
|
|
|
// callbacks struct
|
|
|
|
|
struct custom_log_callbacks
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
// callbacks type
|
|
|
|
|
typedef std::function<void(const details::log_msg &msg)> custom_log_callback;
|
|
|
|
|
|
|
|
|
|
namespace sinks {
|
|
|
|
|
/*
|
|
|
|
@ -32,28 +23,19 @@ template<typename Mutex>
|
|
|
|
|
class callback_sink final : public base_sink<Mutex>
|
|
|
|
|
{
|
|
|
|
|
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<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);
|
|
|
|
|
}
|
|
|
|
|
callback_(msg);
|
|
|
|
|
}
|
|
|
|
|
void flush_() override{};
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
custom_log_callbacks callbacks_;
|
|
|
|
|
custom_log_callback callback_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using callback_sink_mt = callback_sink<std::mutex>;
|
|
|
|
@ -65,15 +47,15 @@ using callback_sink_st = callback_sink<details::null_mutex>;
|
|
|
|
|
// 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)
|
|
|
|
|
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>
|
|
|
|
|
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
|
|
|
|
|