-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
166 lines (147 loc) · 4.07 KB
/
Copy pathmain.cpp
File metadata and controls
166 lines (147 loc) · 4.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
157
158
159
160
161
162
163
164
165
166
#include "app/AppRequest.h"
#include "app/AppResult.h"
#include "app/Application.h"
#include "core/Timestamp.h"
#include "io/FileWriter.h"
#include "report/TextReportRenderer.h"
#include <cstring>
#include <ctime>
#include <iostream>
#include <sstream>
using namespace loganalyzer;
struct CliArgs {
std::string inputPath;
std::string reportPath;
std::optional<Timestamp> from;
std::optional<Timestamp> to;
std::optional<std::string> keyword;
};
bool parseArgs(int argc, char *argv[], CliArgs &args) {
for (int i = 1; i < argc; i++) {
if (std::strcmp(argv[i], "--input") == 0) {
if (i + 1 < argc) {
args.inputPath = argv[++i];
} else {
return false;
}
} else if (std::strcmp(argv[i], "--report") == 0) {
if (i + 1 < argc) {
args.reportPath = argv[++i];
} else {
return false;
}
} else if (std::strcmp(argv[i], "--from") == 0) {
if (i + 1 < argc) {
Timestamp ts;
if (Timestamp::parse(argv[++i], ts)) {
args.from = ts;
} else {
std::cerr << "Invalid --from timestamp format\n";
return false;
}
} else {
return false;
}
} else if (std::strcmp(argv[i], "--to") == 0) {
if (i + 1 < argc) {
Timestamp ts;
if (Timestamp::parse(argv[++i], ts)) {
args.to = ts;
} else {
std::cerr << "Invalid --to timestamp format\n";
return false;
}
} else {
return false;
}
} else if (std::strcmp(argv[i], "--keyword") == 0) {
if (i + 1 < argc) {
args.keyword = argv[++i];
} else {
return false;
}
}
}
return !args.inputPath.empty() && !args.reportPath.empty();
}
std::string getCurrentTimestamp() {
std::time_t now = std::time(nullptr);
std::tm *tm = std::localtime(&now);
char buffer[64];
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", tm);
return std::string(buffer);
}
std::string buildFiltersDescription(const CliArgs &args) {
std::ostringstream oss;
bool first = true;
if (args.from.has_value()) {
oss << "from=" << args.from.value().toString();
first = false;
}
if (args.to.has_value()) {
if (!first)
oss << ", ";
oss << "to=" << args.to.value().toString();
first = false;
}
if (args.keyword.has_value()) {
if (!first)
oss << ", ";
oss << "keyword=\"" << args.keyword.value() << "\"";
}
return oss.str();
}
int main(int argc, char *argv[]) {
// Parse CLI arguments
CliArgs cliArgs;
if (!parseArgs(argc, argv, cliArgs)) {
std::cerr << "Usage: " << argv[0] << " --input <path> --report <path> "
<< "[--from <YYYY-MM-DD HH:MM:SS>] [--to <YYYY-MM-DD HH:MM:SS>] "
<< "[--keyword <text>]\n";
return 2; // INVALID_ARGS
}
// Build AppRequest
AppRequest request;
request.inputPath = cliArgs.inputPath;
request.fromTimestamp = cliArgs.from;
request.toTimestamp = cliArgs.to;
request.keyword = cliArgs.keyword;
// Run application
Application app;
AppResult result = app.run(request);
// Handle errors
if (result.status != AppStatus::OK) {
std::cerr << "Error: " << result.message << "\n";
switch (result.status) {
case AppStatus::INVALID_ARGS:
return 2;
case AppStatus::INPUT_IO_ERROR:
return 3;
case AppStatus::OUTPUT_IO_ERROR:
return 4;
case AppStatus::PIPELINE_ERROR:
case AppStatus::PARSER_ERROR:
case AppStatus::IO_ERROR:
return 4;
default:
return 4;
}
}
// Render report
std::string reportText = TextReportRenderer::render(
result.analysisResult, cliArgs.inputPath, getCurrentTimestamp(),
buildFiltersDescription(cliArgs));
// Write report
FileWriter writer(cliArgs.reportPath);
if (!writer.isOpen()) {
std::cerr << "Error: " << writer.getError() << "\n";
return 4; // OUTPUT_IO_ERROR
}
if (!writer.write(reportText)) {
std::cerr << "Error: Failed to write report\n";
return 4;
}
std::cout << "Analysis complete. Report written to: " << cliArgs.reportPath
<< "\n";
return 0;
}