-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogReport.cpp
More file actions
123 lines (103 loc) · 4.23 KB
/
LogReport.cpp
File metadata and controls
123 lines (103 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "LogReport.h"
#include "Utils.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <vector>
void LogReport::printEntries(const std::vector<LogEntry>& entries, bool colorize) {
if (entries.empty()) {
std::cout << Utils::YELLOW << "No entries found matching your criteria.\n" << Utils::RESET;
return;
}
for (const auto& entry : entries) {
std::string color = colorize ? Utils::colorForLevel(entry.level) : "";
std::string reset = colorize ? Utils::RESET : "";
std::cout << color
<< "[" << std::setw(8) << std::left << entry.levelToString() << "] "
<< std::setw(22) << entry.timestamp << " "
<< Utils::DIM << "[" << std::setw(12) << std::left << entry.source << "] "
<< reset << color
<< entry.message
<< reset << "\n";
}
}
void LogReport::printSummary(const std::vector<LogEntry>& entries) {
std::map<LogLevel, int> counts;
for (const auto& e : entries) counts[e.level]++;
int total = (int)entries.size();
Utils::printSeparator('=', 60);
std::cout << Utils::BOLD << " LOG ANALYSIS REPORT\n" << Utils::RESET;
Utils::printSeparator('=', 60);
std::cout << " Total entries parsed : " << Utils::BOLD << total << Utils::RESET << "\n\n";
// Level breakdown
std::cout << " Level Breakdown:\n";
std::vector<std::pair<LogLevel, std::string>> levels = {
{LogLevel::CRITICAL, "CRITICAL"},
{LogLevel::ERROR, "ERROR "},
{LogLevel::WARNING, "WARNING "},
{LogLevel::INFO, "INFO "},
{LogLevel::DEBUG, "DEBUG "},
};
for (auto& [lvl, name] : levels) {
int count = counts.count(lvl) ? counts[lvl] : 0;
int barLen = total > 0 ? (count * 30 / total) : 0;
std::string bar(barLen, '█');
std::cout << " " << Utils::colorForLevel(lvl)
<< name << Utils::RESET
<< " : " << std::setw(5) << count
<< " " << Utils::colorForLevel(lvl) << bar << Utils::RESET << "\n";
}
// Top errors
std::cout << "\n Top Error Messages:\n";
auto topErrors = getTopErrors(entries, 5);
if (topErrors.empty()) {
std::cout << " " << Utils::GREEN << "No errors found!\n" << Utils::RESET;
} else {
int rank = 1;
for (auto it = topErrors.rbegin(); it != topErrors.rend(); ++it) {
std::string msg = it->first.length() > 45
? it->first.substr(0, 42) + "..." : it->first;
std::cout << " " << rank++ << ". [" << it->second << "x] "
<< Utils::RED << msg << Utils::RESET << "\n";
}
}
Utils::printSeparator('=', 60);
}
bool LogReport::exportToFile(const std::vector<LogEntry>& entries, const std::string& filepath) {
std::ofstream file(filepath);
if (!file.is_open()) {
std::cerr << Utils::RED << "Error: Cannot write to: " << filepath << Utils::RESET << "\n";
return false;
}
file << "# Log Export - " << entries.size() << " entries\n";
file << "# Format: [LEVEL] TIMESTAMP [SOURCE] MESSAGE\n\n";
for (const auto& e : entries) {
file << "[" << e.levelToString() << "] "
<< e.timestamp << " "
<< "[" << e.source << "] "
<< e.message << "\n";
}
std::cout << Utils::GREEN << "Exported " << entries.size()
<< " entries to: " << filepath << Utils::RESET << "\n";
return true;
}
std::map<std::string, int> LogReport::getTopErrors(
const std::vector<LogEntry>& entries, int n)
{
std::map<std::string, int> freq;
for (const auto& e : entries) {
if (e.level == LogLevel::ERROR || e.level == LogLevel::CRITICAL) {
freq[e.message]++;
}
}
// Return only top N by sorting
std::vector<std::pair<std::string, int>> sorted(freq.begin(), freq.end());
std::sort(sorted.begin(), sorted.end(),
[](const auto& a, const auto& b) { return a.second < b.second; });
if ((int)sorted.size() > n) {
sorted = std::vector<std::pair<std::string, int>>(
sorted.end() - n, sorted.end());
}
return std::map<std::string, int>(sorted.begin(), sorted.end());
}