Asink sink (#3309)

Replace async logger with async sink
This commit is contained in:
Gabi Melman
2025-01-05 02:17:31 +02:00
committed by GitHub
parent 166843ff3a
commit 83c9ede9e6
82 changed files with 986 additions and 1789 deletions

View File

@@ -8,10 +8,10 @@
#include <chrono>
#include <mutex>
#include <thread>
#include <exception>
#include "spdlog/details/null_mutex.h"
#include "spdlog/details/os.h"
#include "spdlog/fmt/fmt.h"
#include "spdlog/sinks/base_sink.h"
namespace spdlog {
@@ -37,6 +37,14 @@ public:
delay_ = delay;
}
void set_exception(const std::runtime_error& ex) {
exception_ptr_ = std::make_exception_ptr(ex);
}
void clear_exception() {
exception_ptr_ = nullptr;
}
// return last output without the eol
std::vector<std::string> lines() {
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
@@ -45,6 +53,9 @@ public:
protected:
void sink_it_(const details::log_msg &msg) override {
if (exception_ptr_) {
std::rethrow_exception(exception_ptr_);
}
memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted);
// save the line without the eol
@@ -56,12 +67,18 @@ protected:
std::this_thread::sleep_for(delay_);
}
void flush_() override { flush_counter_++; }
void flush_() override {
if (exception_ptr_) {
std::rethrow_exception(exception_ptr_);
}
flush_counter_++;
}
size_t msg_counter_{0};
size_t flush_counter_{0};
std::chrono::milliseconds delay_{std::chrono::milliseconds::zero()};
std::vector<std::string> lines_;
std::exception_ptr exception_ptr_; // will be thrown on next log or flush if not null
};
using test_sink_mt = test_sink<std::mutex>;