11#include <spdlog/async.h>
12#include <spdlog/sinks/base_sink.h>
13#include <spdlog/sinks/rotating_file_sink.h>
14#include <spdlog/spdlog.h>
20LoggerBridge::QueueSink::QueueSink(
LoggerBridge *parent) : parent(parent) {}
22void LoggerBridge::QueueSink::sink_it_(
const spdlog::details::log_msg &msg) {
23 spdlog::memory_buf_t buf;
24 this->formatter_->format(msg, buf);
25 std::string text = fmt::to_string(buf);
28 case spdlog::level::trace:
31 case spdlog::level::debug:
34 case spdlog::level::info:
37 case spdlog::level::warn:
40 case spdlog::level::err:
43 case spdlog::level::critical:
50 parent->log_queue.push(std::move(entry));
53void LoggerBridge::QueueSink::flush_() {}
57 if (!std::filesystem::exists(logdir)) {
58 std::filesystem::create_directories(logdir);
59 }
else if (!std::filesystem::is_directory(logdir)) {
60 throw std::runtime_error(
"Log path is not a directory.");
64 spdlog::init_thread_pool(8192, 1);
66 auto queue_sink = std::make_shared<QueueSink>(
this);
67 queue_sink->set_pattern(
"[%Y-%m-%d %H:%M:%S.%e %t] [%s:%#] %v");
70 std::make_shared<spdlog::sinks::rotating_file_sink_mt>(logfile.string(), 1048576 * 5, 10);
71 file_sink->set_pattern(
"[%Y-%m-%d %H:%M:%S.%e %t] [%^%l%$] [%s:%#] %v");
73 std::vector<spdlog::sink_ptr> sinks{queue_sink, file_sink};
74 auto logger = std::make_shared<spdlog::async_logger>(
"logger", sinks.begin(), sinks.end(),
75 spdlog::thread_pool(),
76 spdlog::async_overflow_policy::block);
77 logger->set_level(spdlog::level::trace);
78 spdlog::set_default_logger(logger);
87 std::vector<LogEntry> entries;
89 while (log_queue.try_pop(entry)) {
90 entries.push_back(std::move(entry));
std::vector< LogEntry > get_pending_logs()
std::filesystem::path get_cache_directory()