Yarilo v0.9.9
WPA2 Decrypter & Packet Analyzer
Loading...
Searching...
No Matches
log_sink.h
Go to the documentation of this file.
1#ifndef SNIFF_LOG_SINK
2#define SNIFF_LOG_SINK
3
4#include "log_queue.h"
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>
13
14namespace yarilo {
15
16namespace log {
17
27template <typename Mutex>
28class ProtoSink : public spdlog::sinks::base_sink<Mutex> {
29public:
34 ProtoSink(uint64_t max_entries = 50)
35 : max_entries(max_entries), queue(max_entries) {}
36
37 ~ProtoSink() override = default;
38
44 std::vector<proto::LogEntry *> get_entries() {
45 std::lock_guard<Mutex> lock(spdlog::sinks::base_sink<Mutex>::mutex_);
46 std::vector<proto::LogEntry *> result;
47 queue.fetch_all(result);
48 return result;
49 }
50
54 void stop() { queue.stop(); }
55
60 bool is_stopped() { return queue.is_stopped(); }
61
62protected:
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());
73 entry->set_scope(
74 std::string(msg.logger_name.begin(), msg.logger_name.end()));
75 entry->set_payload(std::string(msg.payload.begin(), msg.payload.end()));
76
77 switch (msg.level) {
78 case spdlog::level::level_enum::trace:
79 entry->set_level(proto::LogEntry::TRACE);
80 break;
81
82 case spdlog::level::level_enum::debug:
83 entry->set_level(proto::LogEntry::DEBUG);
84 break;
85
86 case spdlog::level::level_enum::info:
87 entry->set_level(proto::LogEntry::INFO);
88 break;
89
90 case spdlog::level::level_enum::warn:
91 entry->set_level(proto::LogEntry::WARN);
92 break;
93
94 case spdlog::level::level_enum::err:
95 entry->set_level(proto::LogEntry::ERR);
96 break;
97
98 case spdlog::level::level_enum::critical:
99 entry->set_level(proto::LogEntry::CRITICAL);
100 break;
101 }
102
103 queue.insert(entry.release());
104 }
105
106 void flush_() override {}
107
108private:
109 LogQueue queue;
110 uint64_t max_entries;
111};
112
117
122
126extern std::shared_ptr<ProtoSinkMt> global_proto_sink;
127
131extern spdlog::level::level_enum global_log_level;
132
138std::shared_ptr<spdlog::logger> get_logger(const std::string &name);
139
140} // namespace log
141
142} // namespace yarilo
143
144#endif // SNIFF_LOG_SINK
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