-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtlogcli.cpp
More file actions
251 lines (219 loc) · 8.06 KB
/
tlogcli.cpp
File metadata and controls
251 lines (219 loc) · 8.06 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <thread>
#include <vector>
using namespace std;
const string R = "\033[0m";
const string RED = "\033[31m";
const string GRN = "\033[32m";
const string YEL = "\033[33m";
const string CYN = "\033[36m";
const string DIM = "\033[90m";
struct Entry {
uint64_t ts;
string tid, sid, tags, msg;
int lvl;
string raw_line;
};
uint64_t parse_hex(const string& s) {
try { return stoull(s, nullptr, 16); } catch(...) { return 0; }
}
string format_time(uint64_t ns) {
time_t s = ns / 1000000000;
tm* t = localtime(&s);
char buf[32];
strftime(buf, sizeof(buf), "%H:%M:%S", t);
return string(buf);
}
Entry parse_line(const string& line) {
Entry e;
e.raw_line = line;
if (line.length() < 30) return e;
size_t s1 = line.find(' ');
size_t s2 = line.find(' ', s1 + 1);
size_t s3 = line.find(' ', s2 + 1);
size_t s4 = line.find(' ', s3 + 1);
if (s1 == string::npos || s4 == string::npos) return e;
e.ts = parse_hex(line.substr(0, s1));
e.tid = line.substr(s1 + 1, s2 - s1 - 1);
e.sid = line.substr(s2 + 1, s3 - s2 - 1);
try { e.lvl = stoi(line.substr(s3 + 1, s4 - s3 - 1)); } catch(...) { e.lvl = 0; }
size_t msg_start = s4 + 1;
if (msg_start < line.size() && line[msg_start] == '[') {
size_t bracket_end = line.find(']', msg_start);
if (bracket_end != string::npos) {
e.tags = line.substr(msg_start + 1, bracket_end - msg_start - 1);
msg_start = bracket_end + 2;
}
}
if (msg_start < line.size()) e.msg = line.substr(msg_start);
return e;
}
void run_scan(const string& path, const string& filter) {
ifstream file(path);
string line;
while (getline(file, line)) {
Entry e = parse_line(line);
if (e.tid.empty()) continue;
if (!filter.empty() && line.find(filter) == string::npos && e.tags.find(filter) == string::npos) continue;
string c = R, l = "INFO";
if (e.lvl == 1) { c = YEL; l = "WARN"; }
else if (e.lvl == 2) { c = RED; l = "FAIL"; }
else if (e.lvl == 3) { c = DIM; l = "DBUG"; }
cout << DIM << format_time(e.ts) << R << " " << CYN << e.tid.substr(0, 8) << R << " " << c << l << R << " "
<< (e.tags.empty() ? "" : DIM + "[" + e.tags + "] " + R) << e.msg << "\n";
}
}
void run_stats(const string& path) {
ifstream file(path);
string line;
map<int, int> lvls;
size_t count = 0, errors = 0;
map<string, uint64_t> starts, ends;
while (getline(file, line)) {
Entry e = parse_line(line);
if (e.tid.empty()) continue;
count++;
lvls[e.lvl]++;
if (e.lvl == 2) errors++;
if (starts.find(e.tid) == starts.end()) starts[e.tid] = e.ts;
ends[e.tid] = e.ts;
}
vector<double> durs;
for (auto const& [tid, start] : starts) durs.push_back((double)(ends[tid] - start) / 1e6);
sort(durs.begin(), durs.end());
cout << "\n" << string(40, '-') << "\n";
cout << "Total Events: " << count << "\n";
cout << "Unique Traces: " << starts.size() << "\n";
cout << "Error Traces: " << (errors > 0 ? RED : GRN) << errors << R << "\n";
cout << "Latency p50: " << (durs.empty() ? 0 : durs[durs.size()*0.5]) << "ms\n";
cout << "Latency p95: " << YEL << (durs.empty() ? 0 : durs[durs.size()*0.95]) << R << "ms\n";
cout << "Latency p99: " << RED << (durs.empty() ? 0 : durs[durs.size()*0.99]) << R << "ms\n";
cout << string(40, '-') << "\n";
}
void run_trace(const string& path, const string& query) {
ifstream file(path);
string line, target_tid = query;
bool found = false;
if (query.length() < 16) {
while (getline(file, line)) {
if (line.find(query) != string::npos) {
target_tid = parse_line(line).tid;
found = true;
break;
}
}
if (!found) return;
file.clear();
file.seekg(0);
}
uint64_t start = 0;
int indent = 0;
while (getline(file, line)) {
Entry e = parse_line(line);
if (e.tid != target_tid) continue;
if (start == 0) start = e.ts;
bool is_s = (e.msg.find("> ") == 0), is_e = (e.msg.find("< ") == 0);
if (is_e && indent > 0) indent--;
cout << fixed << setprecision(2) << setw(8) << (double)(e.ts - start)/1e6 << "ms | ";
for (int i = 0; i < indent; i++) cout << DIM << "│ " << R;
if (is_s) cout << GRN << "┌ " << e.msg.substr(2) << R;
else if (is_e) cout << DIM << "└ " << e.msg.substr(2) << R;
else if (e.lvl == 2) cout << RED << "× " << e.msg << R;
else cout << e.msg;
if (!e.tags.empty()) cout << DIM << " [" << e.tags << "]" << R;
cout << "\n";
if (is_s) indent++;
}
}
void run_tail(const string& path) {
ifstream file(path);
file.seekg(0, ios::end);
string line;
while (true) {
while (getline(file, line)) {
Entry e = parse_line(line);
if (!e.tid.empty()) cout << (e.lvl == 2 ? RED : GRN) << e.tid.substr(0, 8) << R << " " << e.msg << "\n";
}
file.clear();
this_thread::sleep_for(chrono::milliseconds(100));
}
}
void run_json(const string& path) {
ifstream file(path);
string line;
cout << "[\n";
bool first = true;
while (getline(file, line)) {
Entry e = parse_line(line);
if (e.tid.empty()) continue;
if (!first) cout << ",\n";
cout << " {\"ts\":" << e.ts << ",\"tid\":\"" << e.tid << "\",\"lvl\":" << e.lvl << ",\"msg\":\"" << e.msg << "\",\"tags\":\"" << e.tags << "\"}";
first = false;
}
cout << "\n]\n";
}
void run_diff(const string& path, const string& id1, const string& id2) {
ifstream file(path);
string line;
vector<Entry> t1, t2;
while (getline(file, line)) {
Entry e = parse_line(line);
if (e.tid == id1) t1.push_back(e);
if (e.tid == id2) t2.push_back(e);
}
for(size_t i = 0; i < max(t1.size(), t2.size()); ++i) {
string l = (i < t1.size()) ? t1[i].msg : "", r = (i < t2.size()) ? t2[i].msg : "";
cout << (l != r ? RED : GRN) << setw(45) << l.substr(0, 43) << "| " << r.substr(0, 43) << R << "\n";
}
}
void show_help() {
cout << "tlog CLI explorer\n\n";
cout << "USAGE:\n";
cout << " tlog <command> <logfile> [args]\n\n";
cout << "COMMANDS:\n";
cout << " scan <file> [filter] List all events, optionally filtered\n";
cout << " trace <file> <id|prefix> Show indented tree for a specific trace\n";
cout << " stats <file> Calculate latency percentiles and error rates\n";
cout << " tail <file> Real-time stream of incoming events\n";
cout << " json <file> Export entire log as JSON\n";
cout << " diff <file> <id1> <id2> Compare two traces side-by-side\n";
cout << " help Show this menu\n";
}
int main(int argc, char** argv) {
if (argc < 2 || string(argv[1]) == "help" || string(argv[1]) == "--help") {
show_help();
return 0;
}
if (argc < 3) {
cerr << RED << "Error: Log file path required." << R << "\n";
return 1;
}
string cmd = argv[1], path = argv[2];
if (cmd == "scan") run_scan(path, (argc > 3 ? argv[3] : ""));
else if (cmd == "trace") {
if (argc < 4) { cerr << RED << "Error: Trace ID required for 'trace' command." << R << "\n"; return 1; }
run_trace(path, argv[3]);
}
else if (cmd == "stats") run_stats(path);
else if (cmd == "tail") run_tail(path);
else if (cmd == "json") run_json(path);
else if (cmd == "diff") {
if (argc < 5) { cerr << RED << "Error: Two IDs required for 'diff' command." << R << "\n"; return 1; }
run_diff(path, argv[3], argv[4]);
} else {
cerr << RED << "Unknown command: " << cmd << R << "\n";
show_help();
return 1;
}
return 0;
}