Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
log_router.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
10#pragma once
11
14#include <kcenon/common/interfaces/logger_interface.h>
15#include <string>
16#include <vector>
17#include <unordered_map>
18#include <memory>
19#include <regex>
20
22
23// Type alias for log_level
24using log_level = common::interfaces::log_level;
25
33 std::vector<std::string> writer_names;
34 std::unique_ptr<log_filter_interface> filter;
35 bool stop_propagation = false;
36
37 route_config() = default;
40};
41
50private:
51 std::vector<route_config> routes_;
52 bool exclusive_routes_ = false;
53
54public:
58 void add_route(route_config config) {
59 routes_.push_back(std::move(config));
60 }
61
65 void set_exclusive_routes(bool exclusive) {
66 exclusive_routes_ = exclusive;
67 }
68
72 void clear_routes() {
73 routes_.clear();
74 }
75
79 std::vector<std::string> get_writers_for_log(const log_entry& entry) const {
80 std::vector<std::string> writers;
81
82 for (const auto& route : routes_) {
83 if (route.filter && route.filter->should_log(entry)) {
84 writers.insert(writers.end(), route.writer_names.begin(), route.writer_names.end());
85 if (route.stop_propagation) {
86 break;
87 }
88 }
89 }
90
91 return writers;
92 }
93
97 bool is_exclusive_routing() const {
98 return exclusive_routes_;
99 }
100};
101
109private:
112
113public:
114 explicit router_builder(log_router& router) : router_(router) {}
115
116 router_builder& when_level(log_level level) {
117 config_.filter = std::make_unique<class level_condition>(level);
118 return *this;
119 }
120
121 router_builder& when_matches(const std::string& pattern) {
122 config_.filter = std::make_unique<class regex_condition>(pattern);
123 return *this;
124 }
125
126 router_builder& route_to(const std::string& writer_name, bool stop_propagation = false) {
127 config_.writer_names.push_back(writer_name);
128 config_.stop_propagation = stop_propagation;
129 router_.add_route(std::move(config_));
130 return *this;
131 }
132
133 router_builder& route_to(const std::vector<std::string>& writer_names, bool stop_propagation = false) {
134 config_.writer_names = writer_names;
135 config_.stop_propagation = stop_propagation;
136 router_.add_route(std::move(config_));
137 return *this;
138 }
139
140private:
142 private:
143 log_level target_level_;
144 public:
145 explicit level_condition(log_level level) : target_level_(level) {}
146
147 bool should_log(const log_entry& entry) const override {
148 return entry.level == target_level_;
149 }
150
151 std::string get_name() const override {
152 return "level_condition";
153 }
154 };
155
157 private:
158 std::regex pattern_;
159 public:
160 explicit regex_condition(const std::string& pattern) : pattern_(pattern) {}
161
162 bool should_log(const log_entry& entry) const override {
163 // Use implicit conversion to string_view
164 std::string_view msg_view = entry.message;
165 return std::regex_search(msg_view.begin(), msg_view.end(), pattern_);
166 }
167
168 std::string get_name() const override {
169 return "regex_condition";
170 }
171 };
172};
173
174} // namespace kcenon::logger::routing
Log router for directing messages to specific writers.
Definition log_router.h:49
std::vector< route_config > routes_
Definition log_router.h:51
bool is_exclusive_routing() const
Check if exclusive routing is enabled.
Definition log_router.h:97
void set_exclusive_routes(bool exclusive)
Set exclusive routing mode.
Definition log_router.h:65
void add_route(route_config config)
Add a routing rule.
Definition log_router.h:58
std::vector< std::string > get_writers_for_log(const log_entry &entry) const
Get writer names for a log entry.
Definition log_router.h:79
void clear_routes()
Clear all routes.
Definition log_router.h:72
std::string get_name() const override
Get the name of this filter.
Definition log_router.h:151
bool should_log(const log_entry &entry) const override
Check if a log entry should be processed.
Definition log_router.h:147
bool should_log(const log_entry &entry) const override
Check if a log entry should be processed.
Definition log_router.h:162
std::string get_name() const override
Get the name of this filter.
Definition log_router.h:168
Builder for creating routing rules.
Definition log_router.h:108
router_builder & when_level(log_level level)
Definition log_router.h:116
router_builder & when_matches(const std::string &pattern)
Definition log_router.h:121
router_builder & route_to(const std::vector< std::string > &writer_names, bool stop_propagation=false)
Definition log_router.h:133
router_builder & route_to(const std::string &writer_name, bool stop_propagation=false)
Definition log_router.h:126
Data structures for representing log entries and source locations kcenon.
Interface for log filters used by filtered_logger.
Represents a single log entry with all associated metadata.
Definition log_entry.h:155
log_level level
Severity level of the log message.
Definition log_entry.h:162
small_string_256 message
The actual log message.
Definition log_entry.h:169
Route configuration for log messages.
Definition log_router.h:32
route_config & operator=(route_config &&)=default
std::vector< std::string > writer_names
Definition log_router.h:33
std::unique_ptr< log_filter_interface > filter
Definition log_router.h:34
route_config(route_config &&)=default