-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_analyzer.cpp
More file actions
156 lines (127 loc) · 5.07 KB
/
log_analyzer.cpp
File metadata and controls
156 lines (127 loc) · 5.07 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// Created by haris on 12/24/2025.
//
#include "log_analyzer.h"
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <cctype>
#include <chrono>
#include <thread>
#include <filesystem>
enum LogLevel { DEBUG, INFO, WARNING, ERROR, CRITICAL };
std::string toLower(std::string s) {
std::transform(s.begin(), s.end(), s.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
return s;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "Usage: log_analyzer <logfile>\n";
return 1;
}
const std::string path = argv[1];
std::ifstream logs(path, std::ios::in);
if (!logs.is_open()) {
std::cerr << "ERROR OPENING FILE: " << path << "\n";
return 1;
}
long long countDEBUG = 0;
long long countINFO = 0;
long long countWARNING = 0;
long long countERROR = 0;
long long countCRITICAL = 0;
long long countUNKNOWN = 0;
auto classifyAndCount = [&](const std::string& rawLine) {
std::string lower = toLower(rawLine);
if (lower.find("critical") != std::string::npos) countCRITICAL++;
else if (lower.find("error") != std::string::npos) countERROR++;
else if (lower.find("warning") != std::string::npos) countWARNING++;
else if (lower.find("info") != std::string::npos) countINFO++;
else if (lower.find("debug") != std::string::npos) countDEBUG++;
else countUNKNOWN++;
};
auto printSummary = [&]() {
std::cout << "\n===== Live Log Summary =====\n"
<< "DEBUG: " << countDEBUG << "\n"
<< "INFO: " << countINFO << "\n"
<< "WARNING: " << countWARNING << "\n"
<< "ERROR: " << countERROR << "\n"
<< "CRITICAL: " << countCRITICAL << "\n"
<< "UNKNOWN: " << countUNKNOWN << "\n"
<< "============================\n";
};
// 1) BASELINE: read the whole file first
std::string line;
while (std::getline(logs, line)) {
classifyAndCount(line);
}
// We are now at EOF; clear state so future reads work
logs.clear();
// Position where "new" content begins
logs.seekg(0, std::ios::end);
printSummary();
using clock = std::chrono::steady_clock;
auto lastPrint = clock::now();
auto lastActivity = clock::now(); // when we last saw a NEW line appended
constexpr auto PRINT_INTERVAL = std::chrono::seconds(2);
constexpr auto IDLE_TIMEOUT = std::chrono::minutes(1);
// 2) FOLLOW: keep reading new lines appended to the file
while (true) {
bool readAny = false;
while (std::getline(logs, line)) {
readAny = true;
lastActivity = clock::now(); // reset idle timer on new line
classifyAndCount(line);
}
// If we hit EOF, clear flags so we can read new data later
if (logs.eof()) {
logs.clear();
}
// Handle truncation/rotation: if file size shrank, reopen and re-baseline.
try {
auto pos = logs.tellg();
if (pos != std::streampos(-1)) {
auto fileSize = std::filesystem::file_size(path);
if (static_cast<std::uintmax_t>(pos) > fileSize) {
logs.close();
logs.open(path, std::ios::in);
if (!logs.is_open()) {
std::cerr << "Lost access to file; retrying...\n";
} else {
// Reset counts and re-read baseline from scratch
countDEBUG = countINFO = countWARNING = countERROR = countCRITICAL = countUNKNOWN = 0;
while (std::getline(logs, line)) {
classifyAndCount(line);
}
logs.clear();
logs.seekg(0, std::ios::end);
printSummary();
lastPrint = clock::now();
lastActivity = clock::now();
}
}
}
} catch (const std::filesystem::filesystem_error&) {
// File might be temporarily unavailable; ignore and keep trying
}
// Print summary every 2 seconds
auto now = clock::now();
if (now - lastPrint >= PRINT_INTERVAL) {
printSummary();
lastPrint = now;
}
// Exit if no new log lines have been appended for 1 minute
if (now - lastActivity >= IDLE_TIMEOUT) {
std::cout << "\nNo log activity for 60 seconds. Exiting...\n";
printSummary();
break;
}
if (!readAny) {
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
}
logs.close();
return 0;
}