-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
104 lines (88 loc) · 4.29 KB
/
main.cpp
File metadata and controls
104 lines (88 loc) · 4.29 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
#include <iostream>
#include <string>
#include <vector>
#include "LogParser.h"
#include "LogFilter.h"
#include "LogReport.h"
#include "Utils.h"
void printHelp(const std::string& programName) {
std::cout << Utils::BOLD << "\nLogParser - C++ Log Analysis Tool v1.0.0\n" << Utils::RESET;
Utils::printSeparator('-', 60);
std::cout << "Usage:\n";
std::cout << " " << programName << " <logfile> [options]\n\n";
std::cout << "Options:\n";
std::cout << " --level <LEVEL> Filter by level: DEBUG, INFO, WARNING, ERROR, CRITICAL\n";
std::cout << " --keyword <TEXT> Filter by keyword in message\n";
std::cout << " --source <SOURCE> Filter by source/component\n";
std::cout << " --after <TIME> Show entries after timestamp (YYYY-MM-DD HH:MM:SS)\n";
std::cout << " --before <TIME> Show entries before timestamp\n";
std::cout << " --export <FILE> Export results to a file\n";
std::cout << " --summary Show summary report only\n";
std::cout << " --no-color Disable colorized output\n";
std::cout << " --help Show this help message\n\n";
std::cout << "Examples:\n";
std::cout << " " << programName << " app.log\n";
std::cout << " " << programName << " app.log --level ERROR\n";
std::cout << " " << programName << " app.log --keyword \"database\" --level ERROR\n";
std::cout << " " << programName << " app.log --summary\n";
std::cout << " " << programName << " app.log --level ERROR --export errors.log\n\n";
}
std::string getArg(const std::vector<std::string>& args,
const std::string& flag, const std::string& def = "") {
for (size_t i = 0; i < args.size() - 1; i++) {
if (args[i] == flag) return args[i + 1];
}
return def;
}
bool hasFlag(const std::vector<std::string>& args, const std::string& flag) {
for (const auto& a : args) if (a == flag) return true;
return false;
}
int main(int argc, char* argv[]) {
std::vector<std::string> args(argv, argv + argc);
if (argc < 2 || hasFlag(args, "--help")) {
printHelp(args[0]);
return 0;
}
std::string filepath = args[1];
bool showSummary = hasFlag(args, "--summary");
bool noColor = hasFlag(args, "--no-color");
std::string exportFile = getArg(args, "--export");
FilterOptions opts;
opts.level = getArg(args, "--level");
opts.keyword = getArg(args, "--keyword");
opts.source = getArg(args, "--source");
opts.afterTime = getArg(args, "--after");
opts.beforeTime = getArg(args, "--before");
// ── Parse ─────────────────────────────────────────────────
std::cout << Utils::CYAN << "Parsing: " << filepath << Utils::RESET << "\n";
LogParser parser;
auto entries = parser.parseFile(filepath);
if (entries.empty()) {
std::cerr << Utils::RED << "No valid log entries found.\n" << Utils::RESET;
return 1;
}
std::cout << Utils::DIM << "Parsed " << entries.size()
<< " entries (" << parser.getSkippedLines() << " lines skipped)\n"
<< Utils::RESET;
// ── Filter ────────────────────────────────────────────────
auto filtered = LogFilter::apply(entries, opts);
if (!opts.level.empty() || !opts.keyword.empty() ||
!opts.source.empty() || !opts.afterTime.empty() || !opts.beforeTime.empty()) {
std::cout << Utils::DIM << "Filtered to " << filtered.size() << " entries\n" << Utils::RESET;
}
Utils::printSeparator();
// ── Output ────────────────────────────────────────────────
if (showSummary) {
LogReport::printSummary(filtered);
} else {
LogReport::printEntries(filtered, !noColor);
std::cout << "\n";
LogReport::printSummary(filtered);
}
// ── Export ────────────────────────────────────────────────
if (!exportFile.empty()) {
LogReport::exportToFile(filtered, exportFile);
}
return 0;
}