30import <unordered_map>;
44 common::interfaces::log_level
level;
46 std::chrono::system_clock::time_point
timestamp;
59 std::unordered_map<common::interfaces::log_level, size_t>
level_counts;
77 std::vector<analyzed_log_entry>
entries_;
95 void add_entries(
const std::vector<analyzed_log_entry>& entries) {
126 std::vector<analyzed_log_entry>
filter_by_level(common::interfaces::log_level level)
const {
127 std::vector<analyzed_log_entry> filtered;
128 for (
const auto& entry :
entries_) {
129 if (entry.level == level) {
130 filtered.push_back(entry);
143 const std::chrono::system_clock::time_point& start,
144 const std::chrono::system_clock::time_point& end)
const {
146 std::vector<analyzed_log_entry> filtered;
147 for (
const auto& entry :
entries_) {
148 if (entry.timestamp >= start && entry.timestamp <= end) {
149 filtered.push_back(entry);
160 std::vector<analyzed_log_entry>
search_messages(
const std::string& search_text)
const {
161 std::vector<analyzed_log_entry> results;
162 for (
const auto& entry :
entries_) {
163 if (entry.message.find(search_text) != std::string::npos) {
164 results.push_back(entry);
175 double get_error_rate(
const std::chrono::minutes& window = std::chrono::minutes(60))
const {
176 auto now = std::chrono::system_clock::now();
177 auto start_time = now - window;
179 size_t total_in_window = 0;
180 size_t errors_in_window = 0;
182 for (
const auto& entry :
entries_) {
183 if (entry.timestamp >= start_time) {
185 if (entry.level == common::interfaces::log_level::error ||
186 entry.level == common::interfaces::log_level::critical) {
192 return total_in_window > 0 ?
193 static_cast<double>(errors_in_window) /
static_cast<double>(total_in_window) : 0.0;
203 std::string report =
"=== Log Analysis Summary ===\n";
204 report +=
"Total Entries: " + std::to_string(stats.total_entries) +
"\n";
205 report +=
"Level Distribution:\n";
207 for (
const auto& [level, count] : stats.level_counts) {
208 report +=
" " +
level_to_string(level) +
": " + std::to_string(count) +
"\n";
211 if (stats.total_entries > 0) {
212 auto duration = std::chrono::duration_cast<std::chrono::minutes>(
213 stats.latest_timestamp - stats.earliest_timestamp);
214 report +=
"Time Range: " + std::to_string(duration.count()) +
" minutes\n";
250 for (
const auto& entry :
entries_) {
265 const int level_value =
static_cast<int>(level);
266 switch (level_value) {
267 case 0:
return "TRACE";
268 case 1:
return "DEBUG";
269 case 2:
return "INFO";
270 case 3:
return "WARN";
271 case 4:
return "ERROR";
272 case 5:
return "FATAL";
273 case 6:
return "OFF";
274 default:
return "UNKNOWN";
289 return std::make_unique<log_analyzer>();
Factory for creating log analyzers.
static std::unique_ptr< log_analyzer > create_basic()
Create a basic log analyzer.
Log analyzer for processing and analyzing log data.
analysis_stats cached_stats_
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.
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.
size_t size() const
Get the total number of entries.
std::string level_to_string(log_level level) const
bool empty() const
Check if the analyzer has no entries.
std::string level_to_string(common::interfaces::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_
void clear()
Clear all entries.
std::vector< analyzed_log_entry > filter_by_level(common::interfaces::log_level level) const
Filter entries by log level.
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.
std::unordered_map< log_level, size_t > level_counts
std::unordered_map< common::interfaces::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
std::string function_name
common::interfaces::log_level level
std::chrono::system_clock::time_point timestamp