5#include "proto/service.pb.h"
6#include <google/protobuf/util/time_util.h>
7#include <spdlog/details/null_mutex.h>
8#include <spdlog/logger.h>
9#include <spdlog/sinks/base_sink.h>
10#include <spdlog/sinks/sink.h>
11#include <spdlog/sinks/stdout_color_sinks.h>
12#include <spdlog/spdlog.h>
27template <
typename Mutex>
28class ProtoSink :
public spdlog::sinks::base_sink<Mutex> {
35 : max_entries(max_entries), queue(max_entries) {}
45 std::lock_guard<Mutex> lock(spdlog::sinks::base_sink<Mutex>::mutex_);
46 std::vector<proto::LogEntry *> result;
67 void sink_it_(
const spdlog::details::log_msg &msg)
override {
68 auto entry = std::make_unique<proto::LogEntry>();
69 auto timestamp = std::make_unique<google::protobuf::Timestamp>(
70 google::protobuf::util::TimeUtil::NanosecondsToTimestamp(
71 msg.time.time_since_epoch().count()));
72 entry->set_allocated_timestamp(timestamp.release());
74 std::string(msg.logger_name.begin(), msg.logger_name.end()));
75 entry->set_payload(std::string(msg.payload.begin(), msg.payload.end()));
78 case spdlog::level::level_enum::trace:
79 entry->set_level(proto::LogEntry::TRACE);
82 case spdlog::level::level_enum::debug:
83 entry->set_level(proto::LogEntry::DEBUG);
86 case spdlog::level::level_enum::info:
87 entry->set_level(proto::LogEntry::INFO);
90 case spdlog::level::level_enum::warn:
91 entry->set_level(proto::LogEntry::WARN);
94 case spdlog::level::level_enum::err:
95 entry->set_level(proto::LogEntry::ERR);
98 case spdlog::level::level_enum::critical:
99 entry->set_level(proto::LogEntry::CRITICAL);
103 queue.
insert(entry.release());
110 uint64_t max_entries;
138std::shared_ptr<spdlog::logger>
get_logger(
const std::string &name);
A queue for storing log entries with thread-safe operations.
Definition log_queue.h:17
void stop()
Stops the queue. This will unblock all waiting threads and prevent further operations.
Definition log_queue.h:74
bool fetch_all(std::vector< proto::LogEntry * > &refFetchedItems)
Fetches all log entries from the queue.
Definition log_queue.h:56
bool is_stopped()
Checks if the queue has been stopped.
Definition log_queue.h:87
bool insert(proto::LogEntry *item)
Inserts a log entry into the queue. If the queue is full, it replaces the last item in the queue....
Definition log_queue.h:35
A sink for logging messages in a protobuf format.
Definition log_sink.h:28
void stop()
Stops the sink and all waiting clients.
Definition log_sink.h:54
ProtoSink(uint64_t max_entries=50)
Constructs a ProtoSink with a maximum number of entries.
Definition log_sink.h:34
void flush_() override
Definition log_sink.h:106
bool is_stopped()
Checks if the sink is stopped.
Definition log_sink.h:60
~ProtoSink() override=default
void sink_it_(const spdlog::details::log_msg &msg) override
Processes and stores a log message.
Definition log_sink.h:67
std::vector< proto::LogEntry * > get_entries()
Retrieves all log entries from the sink. This method locks the sink, fetches all entries,...
Definition log_sink.h:44
std::shared_ptr< ProtoSinkMt > global_proto_sink
Sink storing messages in a proto format.
Definition log_sink.cpp:8
spdlog::level::level_enum global_log_level
Yarilo log level.
Definition log_sink.cpp:11
std::shared_ptr< spdlog::logger > get_logger(const std::string &name)
Definition log_sink.cpp:13
Definition access_point.cpp:22