Yarilo  v0.9
WPA2 Decrypter & Packet Analyzer
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 
14 namespace yarilo {
15 
16 namespace log {
17 
27 template <typename Mutex>
28 class ProtoSink : public spdlog::sinks::base_sink<Mutex> {
29 public:
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 
51 protected:
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());
62  entry->set_scope(
63  std::string(msg.logger_name.begin(), msg.logger_name.end()));
64  entry->set_payload(std::string(msg.payload.begin(), msg.payload.end()));
65 
66  switch (msg.level) {
67  case spdlog::level::level_enum::trace:
68  entry->set_level(proto::LogEntry::TRACE);
69  break;
70 
71  case spdlog::level::level_enum::debug:
72  entry->set_level(proto::LogEntry::DEBUG);
73  break;
74 
75  case spdlog::level::level_enum::info:
76  entry->set_level(proto::LogEntry::INFO);
77  break;
78 
79  case spdlog::level::level_enum::warn:
80  entry->set_level(proto::LogEntry::WARN);
81  break;
82 
83  case spdlog::level::level_enum::err:
84  entry->set_level(proto::LogEntry::ERR);
85  break;
86 
87  case spdlog::level::level_enum::critical:
88  entry->set_level(proto::LogEntry::CRITICAL);
89  break;
90  }
91 
92  queue.insert(entry.release());
93  }
94 
95  void flush_() override {}
96 
97 private:
98  LogQueue queue;
99  uint64_t max_entries;
100 };
101 
106 
111 
115 extern std::shared_ptr<ProtoSinkMt> global_proto_sink;
116 
120 extern spdlog::level::level_enum global_log_level;
121 
127 std::shared_ptr<spdlog::logger> get_logger(const std::string &name);
128 
129 } // namespace log
130 
131 } // namespace yarilo
132 
133 #endif // SNIFF_LOG_SINK
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