Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
log_analyzer.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
12#include <kcenon/common/interfaces/logger_interface.h>
13#include <string>
14#include <vector>
15#include <unordered_map>
16#include <chrono>
17#include <memory>
18
20
21// Type alias for log_level
22using log_level = common::interfaces::log_level;
23
27struct analyzed_log_entry {
28 log_level level;
29 std::string message;
30 std::chrono::system_clock::time_point timestamp;
31 std::string source_file;
33 std::string function_name;
34};
35
39struct analysis_stats {
40 size_t total_entries = 0;
41 std::unordered_map<log_level, size_t> level_counts;
42 std::chrono::system_clock::time_point earliest_timestamp;
43 std::chrono::system_clock::time_point latest_timestamp;
44 std::vector<std::string> most_frequent_messages;
45 std::unordered_map<std::string, size_t> error_patterns;
46};
47
51class log_analyzer {
52private:
53 std::vector<analyzed_log_entry> entries_;
55 bool stats_dirty_ = true;
56
57public:
61 void add_entry(const analyzed_log_entry& entry) {
62 entries_.push_back(entry);
63 stats_dirty_ = true;
64 }
65
69 void add_entries(const std::vector<analyzed_log_entry>& entries) {
70 entries_.insert(entries_.end(), entries.begin(), entries.end());
71 stats_dirty_ = true;
72 }
73
77 void clear() {
78 entries_.clear();
80 stats_dirty_ = true;
81 }
82
87 if (stats_dirty_) {
89 stats_dirty_ = false;
90 }
91 return cached_stats_;
92 }
93
97 std::vector<analyzed_log_entry> filter_by_level(log_level level) const {
98 std::vector<analyzed_log_entry> filtered;
99 for (const auto& entry : entries_) {
100 if (entry.level == level) {
101 filtered.push_back(entry);
102 }
103 }
104 return filtered;
105 }
106
110 std::vector<analyzed_log_entry> filter_by_time_range(
111 const std::chrono::system_clock::time_point& start,
112 const std::chrono::system_clock::time_point& end) const {
113
114 std::vector<analyzed_log_entry> filtered;
115 for (const auto& entry : entries_) {
116 if (entry.timestamp >= start && entry.timestamp <= end) {
117 filtered.push_back(entry);
118 }
119 }
120 return filtered;
121 }
122
126 std::vector<analyzed_log_entry> search_messages(const std::string& search_text) const {
127 std::vector<analyzed_log_entry> results;
128 for (const auto& entry : entries_) {
129 if (entry.message.find(search_text) != std::string::npos) {
130 results.push_back(entry);
131 }
132 }
133 return results;
134 }
135
139 double get_error_rate(const std::chrono::minutes& window = std::chrono::minutes(60)) const {
140 auto now = std::chrono::system_clock::now();
141 auto start_time = now - window;
142
143 size_t total_in_window = 0;
144 size_t errors_in_window = 0;
145
146 for (const auto& entry : entries_) {
147 if (entry.timestamp >= start_time) {
148 total_in_window++;
149 if (entry.level == log_level::error ||
150 entry.level == log_level::fatal) {
151 errors_in_window++;
152 }
153 }
154 }
155
156 return total_in_window > 0 ?
157 static_cast<double>(errors_in_window) / total_in_window : 0.0;
158 }
159
164 const auto& stats = get_stats();
165
166 std::string report = "=== Log Analysis Summary ===\n";
167 report += "Total Entries: " + std::to_string(stats.total_entries) + "\n";
168 report += "Level Distribution:\n";
169
170 for (const auto& [level, count] : stats.level_counts) {
171 report += " " + level_to_string(level) + ": " + std::to_string(count) + "\n";
172 }
173
174 if (stats.total_entries > 0) {
175 auto duration = std::chrono::duration_cast<std::chrono::minutes>(
176 stats.latest_timestamp - stats.earliest_timestamp);
177 report += "Time Range: " + std::to_string(duration.count()) + " minutes\n";
178 }
179
180 return report;
181 }
182
183private:
187
188 if (entries_.empty()) {
189 return;
190 }
191
192 // Initialize timestamps
195
196 // Count levels and update timestamps
197 for (const auto& entry : entries_) {
198 cached_stats_.level_counts[entry.level]++;
199
200 if (entry.timestamp < cached_stats_.earliest_timestamp) {
201 cached_stats_.earliest_timestamp = entry.timestamp;
202 }
203 if (entry.timestamp > cached_stats_.latest_timestamp) {
204 cached_stats_.latest_timestamp = entry.timestamp;
205 }
206 }
207 }
208
209 std::string level_to_string(log_level level) const {
210 switch (level) {
211 case log_level::trace: return "TRACE";
212 case log_level::debug: return "DEBUG";
213 case log_level::info: return "INFO";
214 case log_level::warn: return "WARN";
215 case log_level::error: return "ERROR";
216 case log_level::fatal: return "FATAL";
217 case log_level::off: return "OFF";
218 default: return "UNKNOWN";
219 }
220 }
221};
222
226class analyzer_factory {
227public:
231 static std::unique_ptr<log_analyzer> create_basic() {
232 return std::make_unique<log_analyzer>();
233 }
234};
235
236} // namespace kcenon::logger::analysis
static std::unique_ptr< log_analyzer > create_basic()
Create a basic log analyzer.
Log analyzer for processing and analyzing log data.
Definition analysis.cppm:75
const analysis_stats & get_stats()
Get analysis statistics.
void add_entries(const std::vector< analyzed_log_entry > &entries)
Add multiple log entries.
std::string generate_summary_report()
Generate summary report.
std::vector< analyzed_log_entry > filter_by_level(log_level level) const
Filter entries by log level.
void add_entry(const analyzed_log_entry &entry)
Add a log entry for analysis.
std::vector< analyzed_log_entry > search_messages(const std::string &search_text) const
Find entries containing specific text.
std::string level_to_string(log_level level) const
double get_error_rate(const std::chrono::minutes &window=std::chrono::minutes(60)) const
Get error rate for a time window.
std::vector< analyzed_log_entry > entries_
std::vector< analyzed_log_entry > filter_by_time_range(const std::chrono::system_clock::time_point &start, const std::chrono::system_clock::time_point &end) const
Filter entries by time range.
Analysis result statistics.
Definition analysis.cppm:57
std::unordered_map< log_level, size_t > level_counts
std::chrono::system_clock::time_point latest_timestamp
std::unordered_map< std::string, size_t > error_patterns
std::vector< std::string > most_frequent_messages
std::chrono::system_clock::time_point earliest_timestamp
Log entry for analysis.
Definition analysis.cppm:43
log_level level
std::string message
std::string source_file
std::string function_name
int source_line
std::chrono::system_clock::time_point timestamp