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>
27 template <
typename Mutex>
28 class 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;
56 void sink_it_(
const spdlog::details::log_msg &msg)
override {
57 auto entry = std::make_unique<proto::LogEntry>();
58 auto timestamp = std::make_unique<google::protobuf::Timestamp>(
59 google::protobuf::util::TimeUtil::NanosecondsToTimestamp(
60 msg.time.time_since_epoch().count()));
61 entry->set_allocated_timestamp(timestamp.release());
63 std::string(msg.logger_name.begin(), msg.logger_name.end()));
64 entry->set_payload(std::string(msg.payload.begin(), msg.payload.end()));
67 case spdlog::level::level_enum::trace:
68 entry->set_level(proto::LogEntry::TRACE);
71 case spdlog::level::level_enum::debug:
72 entry->set_level(proto::LogEntry::DEBUG);
75 case spdlog::level::level_enum::info:
76 entry->set_level(proto::LogEntry::INFO);
79 case spdlog::level::level_enum::warn:
80 entry->set_level(proto::LogEntry::WARN);
83 case spdlog::level::level_enum::err:
84 entry->set_level(proto::LogEntry::ERR);
87 case spdlog::level::level_enum::critical:
88 entry->set_level(proto::LogEntry::CRITICAL);
92 queue.
insert(entry.release());
127 std::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
bool fetch_all(std::vector< proto::LogEntry * > &refFetchedItems)
Fetches all log entries from the queue. This method blocks until there are items available to fetch.
Definition: log_queue.h:54
bool insert(proto::LogEntry *item)
Inserts a log entry into the queue. This method blocks until space is available in the queue.
Definition: log_queue.h:33
A sink for logging messages in a protobuf format.
Definition: log_sink.h:28
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:95
~ProtoSink() override=default
void sink_it_(const spdlog::details::log_msg &msg) override
Processes and stores a log message.
Definition: log_sink.h:56
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