mirror of
https://github.com/oxen-io/lokinet.git
synced 2024-11-11 07:10:36 +00:00
b81f7025c9
Replaces custom logging system with spdlog-based oxen logging. This commit mainly replaces the backend logging with the spdlog-based system, but doesn't (yet) convert all the existing LogWarn, etc. to use the new format-based logging. New logging statements will look like: llarp::log::warning(cat, "blah: {}", val); where `cat` should be set up in each .cpp or cluster of .cpp files, as described in the oxen-logging README. As part of spdlog we get fmt, which gives us nice format strings, where are applied generously in this commit. Making types printable now requires two steps: - add a ToString() method - add this specialization: template <> constexpr inline bool llarp::IsToStringFormattable<llarp::Whatever> = true; This will then allow the type to be printed as a "{}" value in a fmt::format string. This is applied to all our printable types here, and all of the `operator<<` are removed. This commit also: - replaces various uses of `operator<<` to ToString() - replaces various uses of std::stringstream with either fmt::format or plain std::string - Rename some to_string and toString() methods to ToString() for consistency (and to work with fmt) - Replace `stringify(...)` and `make_exception` usage with fmt::format (and remove stringify/make_exception from util/str.hpp).
47 lines
1.2 KiB
C++
47 lines
1.2 KiB
C++
#include <lokinet/lokinet_misc.h>
|
|
#include <spdlog/sinks/base_sink.h>
|
|
#include <spdlog/details/null_mutex.h>
|
|
#include <mutex>
|
|
|
|
namespace llarp::logging
|
|
{
|
|
// Logger that calls a C function with the formatted log message
|
|
template <typename Mutex>
|
|
class CallbackSink : public spdlog::sinks::base_sink<Mutex>
|
|
{
|
|
private:
|
|
lokinet_logger_func log_;
|
|
lokinet_logger_sync sync_;
|
|
void* ctx_;
|
|
|
|
public:
|
|
explicit CallbackSink(
|
|
lokinet_logger_func log, lokinet_logger_sync sync = nullptr, void* context = nullptr)
|
|
: log_{log}, sync_{sync}, ctx_{context}
|
|
{}
|
|
|
|
protected:
|
|
void
|
|
sink_it_(const spdlog::details::log_msg& msg) override
|
|
{
|
|
if (!log_)
|
|
return;
|
|
spdlog::memory_buf_t formatted;
|
|
spdlog::sinks::base_sink<Mutex>::formatter_->format(msg, formatted);
|
|
log_(fmt::to_string(formatted).c_str(), ctx_);
|
|
}
|
|
|
|
void
|
|
flush_() override
|
|
{
|
|
if (sync_)
|
|
sync_(ctx_);
|
|
}
|
|
};
|
|
|
|
// Convenience aliases with or without thread safety
|
|
using CallbackSink_mt = CallbackSink<std::mutex>;
|
|
using CallbackSink_st = CallbackSink<spdlog::details::null_mutex>;
|
|
|
|
} // namespace llarp::logging
|