File indexing completed on 2025-04-02 09:08:02
0001
0002
0003
0004 #pragma once
0005
0006 #include <spdlog/details/null_mutex.h>
0007 #include <spdlog/details/synchronous_factory.h>
0008 #include <spdlog/sinks/base_sink.h>
0009
0010 #include <mutex>
0011 #include <string>
0012
0013 namespace spdlog {
0014
0015
0016 typedef std::function<void(const details::log_msg &msg)> custom_log_callback;
0017
0018 namespace sinks {
0019
0020
0021
0022 template <typename Mutex>
0023 class callback_sink final : public base_sink<Mutex> {
0024 public:
0025 explicit callback_sink(const custom_log_callback &callback)
0026 : callback_{callback} {}
0027
0028 protected:
0029 void sink_it_(const details::log_msg &msg) override { callback_(msg); }
0030 void flush_() override{};
0031
0032 private:
0033 custom_log_callback callback_;
0034 };
0035
0036 using callback_sink_mt = callback_sink<std::mutex>;
0037 using callback_sink_st = callback_sink<details::null_mutex>;
0038
0039 }
0040
0041
0042
0043
0044 template <typename Factory = spdlog::synchronous_factory>
0045 inline std::shared_ptr<logger> callback_logger_mt(const std::string &logger_name,
0046 const custom_log_callback &callback) {
0047 return Factory::template create<sinks::callback_sink_mt>(logger_name, callback);
0048 }
0049
0050 template <typename Factory = spdlog::synchronous_factory>
0051 inline std::shared_ptr<logger> callback_logger_st(const std::string &logger_name,
0052 const custom_log_callback &callback) {
0053 return Factory::template create<sinks::callback_sink_st>(logger_name, callback);
0054 }
0055
0056 }