Motion Master 6.0.0-alpha.50
Next-generation motion control software
Loading...
Searching...
No Matches
ring_log_sink.h
Go to the documentation of this file.
1#pragma once
2
3#include <spdlog/sinks/base_sink.h>
4
5#include <cstddef>
6#include <deque>
7#include <mutex>
8#include <string>
9#include <utility>
10#include <vector>
11
12namespace mm {
13
25template <typename Mutex>
26class RingLogSink : public spdlog::sinks::base_sink<Mutex> {
27 public:
30 explicit RingLogSink(std::size_t capacity = 100000) : capacity_(capacity) {}
31
39 std::vector<std::string> entries() {
40 std::lock_guard<Mutex> lock(this->mutex_);
41 return {buffer_.begin(), buffer_.end()};
42 }
43
44 protected:
45 void sink_it_(const spdlog::details::log_msg& msg) override {
46 spdlog::memory_buf_t formatted;
47 this->formatter_->format(msg, formatted);
48 std::string entry{formatted.data(), formatted.size()};
49 // Strip the trailing end-of-line. spdlog uses "\n" on POSIX and "\r\n" on
50 // Windows, so drop the newline and any preceding carriage return.
51 if (!entry.empty() && entry.back() == '\n') {
52 entry.pop_back();
53 }
54 if (!entry.empty() && entry.back() == '\r') {
55 entry.pop_back();
56 }
57 if (buffer_.size() >= capacity_) {
58 buffer_.pop_front();
59 }
60 buffer_.push_back(std::move(entry));
61 }
62
63 void flush_() override {}
64
65 private:
66 std::size_t capacity_;
67 std::deque<std::string> buffer_;
68};
69
72
73} // namespace mm
Thread-safe spdlog sink that retains the most recent log entries in memory.
Definition ring_log_sink.h:26
std::vector< std::string > entries()
Returns a snapshot of all currently buffered log lines.
Definition ring_log_sink.h:39
void flush_() override
Definition ring_log_sink.h:63
void sink_it_(const spdlog::details::log_msg &msg) override
Definition ring_log_sink.h:45
RingLogSink(std::size_t capacity=100000)
Constructs the sink with the given ring-buffer capacity.
Definition ring_log_sink.h:30
Definition cert_info.cc:13