-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLogsCommand.cpp
More file actions
273 lines (230 loc) · 6.17 KB
/
Copy pathLogsCommand.cpp
File metadata and controls
273 lines (230 loc) · 6.17 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
*
* @file LogsCommand.cpp
* @author Gaspard Kirira
*
* Copyright 2026, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*/
#include <vix/cli/commands/LogsCommand.hpp>
#include <vix/cli/commands/logs/LogsConfig.hpp>
#include <vix/cli/commands/logs/LogsOutput.hpp>
#include <vix/cli/commands/logs/LogsRunner.hpp>
#include <vix/cli/commands/logs/LogsTypes.hpp>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
namespace vix::commands
{
namespace
{
#ifdef __linux__
bool consume_flag(
std::vector<std::string> &args,
const std::string &flag)
{
for (auto it = args.begin(); it != args.end(); ++it)
{
if (*it == flag)
{
args.erase(it);
return true;
}
}
return false;
}
bool consume_value(
std::vector<std::string> &args,
const std::string &flag,
std::string &out)
{
for (auto it = args.begin(); it != args.end(); ++it)
{
if (*it != flag)
continue;
auto valueIt = it + 1;
if (valueIt == args.end())
return false;
out = *valueIt;
args.erase(it, valueIt + 1);
return true;
}
return true;
}
bool consume_int_value(
std::vector<std::string> &args,
const std::string &flag,
int &out)
{
std::string value;
if (!consume_value(args, flag, value))
return false;
if (value.empty())
return true;
try
{
const int parsed = std::stoi(value);
if (parsed <= 0)
return false;
out = parsed;
return true;
}
catch (...)
{
return false;
}
}
bool parse_target(
std::vector<std::string> &args,
logs::LogsOptions &options)
{
if (args.empty())
return true;
const std::string target = args[0];
if (target == "app")
{
options.target = logs::LogsTarget::App;
args.erase(args.begin());
return true;
}
if (target == "proxy")
{
options.target = logs::LogsTarget::Proxy;
args.erase(args.begin());
return true;
}
if (target == "errors")
{
options.target = logs::LogsTarget::Errors;
options.errorsOnly = true;
args.erase(args.begin());
return true;
}
return true;
}
logs::LogsOptions parse_options(
std::vector<std::string> &args,
bool &ok,
std::string &errorMessage)
{
logs::LogsOptions options;
ok = true;
if (!parse_target(args, options))
{
ok = false;
errorMessage = "invalid logs target";
return options;
}
options.follow = consume_flag(args, "--follow") ||
consume_flag(args, "-f");
options.errorsOnly = options.errorsOnly ||
consume_flag(args, "--errors");
options.repeated = consume_flag(args, "--repeated");
options.json = consume_flag(args, "--json");
if (!consume_value(args, "--since", options.since))
{
ok = false;
errorMessage = "missing value for --since";
return options;
}
if (!consume_int_value(args, "--lines", options.lines))
{
ok = false;
errorMessage = "invalid value for --lines";
return options;
}
if (!consume_int_value(args, "-n", options.lines))
{
ok = false;
errorMessage = "invalid value for -n";
return options;
}
return options;
}
#endif
}
int LogsCommand::run(const std::vector<std::string> &argsIn)
{
#ifndef __linux__
(void)argsIn;
logs::output::error(
std::cerr,
"vix logs is currently supported on Linux only.");
return 1;
#else
std::vector<std::string> args = argsIn;
if (!args.empty() &&
(args[0] == "-h" || args[0] == "--help"))
{
return help();
}
bool ok = true;
std::string errorMessage;
logs::LogsOptions options =
parse_options(args, ok, errorMessage);
if (!ok)
{
logs::output::error(std::cerr, errorMessage);
logs::output::fix(std::cerr, "vix logs --help");
return 1;
}
if (!args.empty())
{
logs::output::error(
std::cerr,
"unknown logs argument: " + args[0]);
logs::output::fix(
std::cerr,
"vix logs --help");
return 1;
}
logs::LogsConfig cfg =
logs::apply_logs_options(
logs::load_logs_config(),
options);
return logs::runner::run(cfg, options);
#endif
}
int LogsCommand::help()
{
std::cout
<< "Usage:\n"
<< " vix logs [target] [options]\n\n"
<< "Targets:\n"
<< " app Show systemd app logs\n"
<< " proxy Show Nginx access and error logs\n"
<< " errors Show app errors and Nginx error logs\n\n"
<< "Options:\n"
<< " --follow Follow logs live\n"
<< " -f Alias for --follow\n"
<< " --errors Filter logs by common error keywords\n"
<< " --repeated Detect repeated errors\n"
<< " --json Print supported output as JSON\n"
<< " --since Filter app logs by systemd time expression\n"
<< " --lines Show last N lines\n"
<< " -n Alias for --lines\n"
<< " -h, --help Show this help message\n\n"
<< "Examples:\n"
<< " vix logs\n"
<< " vix logs app\n"
<< " vix logs proxy\n"
<< " vix logs errors\n"
<< " vix logs --follow\n"
<< " vix logs --errors\n"
<< " vix logs errors --repeated\n"
<< " vix logs errors --repeated --json\n"
<< " vix logs --since \"1 hour ago\"\n"
<< " vix logs -n 200\n\n"
<< "Config:\n"
<< " production.logs.service\n"
<< " production.logs.nginx_access\n"
<< " production.logs.nginx_error\n"
<< " production.logs.lines\n";
return 0;
}
}