-
Notifications
You must be signed in to change notification settings - Fork 30
/
loganalyzer.cpp
489 lines (417 loc) · 13.1 KB
/
loganalyzer.cpp
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#include "loganalyzer.h"
#include "mavlink_reader.h"
#include "heart.h"
#include <iostream> // for cout
#include "la-log.h"
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <regex>
#include "dataflash_reader.h"
#include "dataflash_textdump_reader.h"
#include "analyzing_dataflash_message_handler.h"
#include "analyzing_mavlink_message_handler.h"
// http://stackoverflow.com/questions/874134/find-if-string-ends-with-another-string-in-c
inline bool ends_with(std::string const & value, std::string const & ending)
{
if (ending.size() > value.size()) return false;
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}
void LogAnalyzer::parse_path(const char *path)
{
create_vehicle_from_commandline_arguments();
if (_vehicle == NULL) {
_vehicle = new AnalyzerVehicle::Base();
}
bool do_stdin = false;
log_format_t log_format = log_format_none;
if (!strcmp(path, "-")) {
do_stdin = true;
} else if (ends_with(path, ".BIN") ||
ends_with(path, ".bin")) {
log_format = log_format_df;
} else if (ends_with(path, ".log") ||
ends_with(path, ".LOG")) {
log_format = log_format_log;
} else if (ends_with(path, ".tlog") ||
ends_with(path, ".TLOG")) {
log_format = log_format_tlog;
}
if (force_format() != log_format_none) {
log_format = force_format();
}
if (log_format == log_format_none) {
if (do_stdin) {
::fprintf(stderr, "You asked to parse stdin but did not force a format type\n");
} else {
::fprintf(stderr, "Unable to determine log type from filename (%s); try -i?\n", path);
}
usage();
exit(1);
}
switch (log_format) {
case log_format_tlog:
prep_for_tlog();
break;
case log_format_df:
prep_for_df();
break;
case log_format_log:
prep_for_log();
break;
default:
abort();
}
int fd;
ssize_t fd_size = -1;
if (streq(path, "-")) {
// fd = fileno(stdin); // doesn't work on Cygwin
fd = 0;
} else {
fd = xopen(path, O_RDONLY);
struct stat buf;
if (fstat(fd, &buf) == -1) {
::fprintf(stderr, "fstat failed: %s\n", strerror(errno));
} else {
fd_size = buf.st_size;
}
}
if (output_style == Analyze::OUTPUT_BRIEF) {
printf("%25s: ", path);
}
parse_fd(reader, fd, fd_size);
if (!streq(path, "-")) {
close(fd);
}
if (output_style == Analyze::OUTPUT_BRIEF) {
printf("\n");
}
switch (log_format) {
case log_format_tlog:
cleanup_after_tlog();
break;
case log_format_df:
cleanup_after_df();
break;
case log_format_log:
cleanup_after_log();
break;
default:
abort();
}
delete _vehicle;
_vehicle = NULL;
}
int LogAnalyzer::xopen(const char *filepath, const uint8_t mode)
{
int fd = open(filepath, mode);
if (fd == -1) {
fprintf(stderr, "Failed to open (%s): %s\n", filepath, strerror(errno));
exit(1);
}
return fd;
}
void LogAnalyzer::prep_for_tlog()
{
reader = new MAVLink_Reader(config());
((MAVLink_Reader*)reader)->set_is_tlog(true);
Analyze *analyze = create_analyze();
Analyzing_MAVLink_Message_Handler *handler = new Analyzing_MAVLink_Message_Handler(analyze, _vehicle);
reader->add_message_handler(handler, "Analyze");
}
void LogAnalyzer::cleanup_after_tlog()
{
reader->clear_message_handlers();
}
void LogAnalyzer::cleanup_after_df()
{
reader->clear_message_handlers();
}
void LogAnalyzer::cleanup_after_log()
{
reader->clear_message_handlers();
}
void LogAnalyzer::do_idle_callbacks()
{
reader->do_idle_callbacks();
}
void LogAnalyzer::pack_select_fds(fd_set &fds_read, fd_set &fds_write, fd_set &fds_err, uint8_t &nfds)
{
_client->pack_select_fds(fds_read, fds_write, fds_err, nfds);
}
void LogAnalyzer::handle_select_fds(fd_set &fds_read, fd_set &fds_write, fd_set &fds_err, uint8_t &nfds)
{
_client->handle_select_fds(fds_read, fds_write, fds_err, nfds);
// FIXME: find a more interesting way of doing this...
reader->feed(_client->_recv_buf, _client->_recv_buflen_content);
_client->_recv_buflen_content = 0;
}
void LogAnalyzer::run_live_analysis()
{
reader = new MAVLink_Reader(config());
_client = new Telem_Forwarder_Client();
_client->configure(config());
writer = new MAVLink_Writer(config());
if (writer == NULL) {
la_log(LOG_ERR, "Failed to create writer from (%s)\n", config_filename);
exit(1);
}
Heart *heart= new Heart(writer);
if (heart != NULL) {
reader->add_message_handler(heart, "Heart");
} else {
la_log(LOG_INFO, "Failed to create heart");
}
create_vehicle_from_commandline_arguments();
if (_vehicle == NULL) {
_vehicle = new AnalyzerVehicle::Base();
}
Analyze *analyze = create_analyze();
Analyzing_MAVLink_Message_Handler *handler = new Analyzing_MAVLink_Message_Handler(analyze, _vehicle);
reader->add_message_handler(handler, "Analyze");
select_loop();
_vehicle = NULL; // leak this memory
}
Analyze *LogAnalyzer::create_analyze()
{
Analyze *analyze = new Analyze(_vehicle);
if (analyze == NULL) {
la_log(LOG_ERR, "Failed to create analyze");
abort();
}
analyze->set_output_style(output_style);
if (_analyzer_names_to_run.size()) {
analyze->set_analyzer_names_to_run(_analyzer_names_to_run);
}
analyze->set_pure_output(_pure_output);
analyze->instantiate_analyzers(config());
return analyze;
}
void LogAnalyzer::prep_for_df()
{
reader = new DataFlash_Reader(config());
Analyze *analyze = create_analyze();
Analyzing_DataFlash_Message_Handler *handler = new Analyzing_DataFlash_Message_Handler(analyze, _vehicle);
reader->add_message_handler(handler, "Analyze");
}
void LogAnalyzer::prep_for_log()
{
reader = new DataFlash_TextDump_Reader(config());
Analyze *analyze = create_analyze();
Analyzing_DataFlash_Message_Handler *handler = new Analyzing_DataFlash_Message_Handler(analyze, _vehicle);
reader->add_message_handler(handler, "Analyze");
}
void LogAnalyzer::show_version_information()
{
::printf("Version: " DRONEKIT_LA_VERSION "\n");
::printf("Git-Version: " GIT_VERSION "\n");
}
void LogAnalyzer::list_analyzers()
{
Analyze *analyze = new Analyze(_vehicle);
analyze->instantiate_analyzers(config());
std::vector<Analyzer *> analyzers = analyze->analyzers();
for (std::vector<Analyzer*>::iterator it = analyzers.begin();
it != analyzers.end();
++it) {
::printf("%s\n", (*it)->name().c_str());
}
}
// From: http://stackoverflow.com/questions/236129/split-a-string-in-c
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
// thanks stackoverflow!
void LogAnalyzer::expand_names_to_run()
{
std::vector<std::string> new_names;
for (std::vector<std::string>::const_iterator it = _analyzer_names_to_run.begin();
it != _analyzer_names_to_run.end();
++it) {
// insert lambda here if you dare.
std::vector<std::string> tmp = split((*it),',');
for (std::vector<std::string>::const_iterator iy = tmp.begin();
iy != tmp.end();
++iy) {
std::string value = std::regex_replace(std::string(*iy), std::regex("^ +| +$|( ) +"), std::string("$1"));
new_names.push_back(value);
}
}
_analyzer_names_to_run = new_names;
}
void LogAnalyzer::create_vehicle_from_commandline_arguments()
{
if (_model_string != NULL) {
if (strieq(_model_string,"copter")) {
_vehicle = new AnalyzerVehicle::Copter();
// _analyze->set_vehicle_copter();
if (_frame_string != NULL) {
((AnalyzerVehicle::Copter*)_vehicle)->set_frame(_frame_string);
}
} else if (strieq(_model_string,"plane")) {
_vehicle = new AnalyzerVehicle::Plane();
// } else if (streq(model_string,"rover")) {
// model = new AnalyzerVehicle::Rover();
} else {
la_log(LOG_ERR, "Unknown model type (%s)", _model_string);
exit(1);
}
_vehicle->set_vehicletype_is_forced(true);
} else if (_frame_string != NULL) {
la_log(LOG_ERR, "Can not specify frame type without specifying model type");
exit(1);
}
}
void LogAnalyzer::run()
{
// la_log(LOG_INFO, "loganalyzer starting: built " __DATE__ " " __TIME__);
// signal(SIGHUP, sighup_handler);
init_config();
if (_show_version_information) {
show_version_information();
exit(0);
}
if (_do_list_analyzers) {
list_analyzers();
exit(0);
}
expand_names_to_run();
if (_use_telem_forwarder) {
return run_live_analysis();
}
if (_paths == NULL) {
usage();
exit(1);
}
output_style = Analyze::OUTPUT_JSON;
if (output_style_string != NULL) {
output_style = Analyze::OUTPUT_JSON;
if (strieq(output_style_string, "json")) {
output_style = Analyze::OUTPUT_JSON;
} else if(strieq(output_style_string, "plain-text")) {
output_style = Analyze::OUTPUT_PLAINTEXT;
} else if(strieq(output_style_string, "brief")) {
output_style = Analyze::OUTPUT_BRIEF;
} else if(strieq(output_style_string, "html")) {
output_style = Analyze::OUTPUT_HTML;
} else {
usage();
exit(1);
}
}
if (forced_format_string != NULL) {
if (strieq(forced_format_string, "tlog")) {
_force_format = log_format_tlog;
} else if(strieq(forced_format_string, "df")) {
_force_format = log_format_df;
} else if(strieq(forced_format_string, "log")) {
_force_format = log_format_log;
} else {
usage();
exit(1);
}
}
for (uint8_t i=0; i<_pathcount; i++) {
parse_path(_paths[i]);
}
}
void LogAnalyzer::usage()
{
::printf("Usage:\n");
::printf("%s [OPTION] [FILE...]\n", program_name());
::printf(" -c FILEPATH use config file filepath\n");
// ::printf(" -t connect to telem forwarder to receive data\n");
::printf(" -m MODELTYPE override model; copter|plane|rover\n");
::printf(" -f FRAME set frame; QUAD|Y6\n");
::printf(" -s STYLE use output style (plain-text|json|brief)\n");
::printf(" -h display usage information\n");
::printf(" -l list analyzers\n");
::printf(" -a specify analyzers to run (comma-separated list)\n");
::printf(" -i FORMAT specify input format (tlog|df|log)\n");
::printf(" -p pure output - no deprecated fields\n");
::printf(" -V display version information\n");
::printf("\n");
::printf("Example: %s -s json 1.solo.tlog\n", program_name());
::printf("Example: %s -a \"Ever Flew, Battery\" 1.solo.tlog\n", program_name());
::printf("Example: %s -s brief 1.solo.tlog 2.solo.tlog logs/*.tlog\n", program_name());
::printf("Example: %s - (analyze stdin)\n", program_name());
::printf("Example: %s x.log (analyze text-dumped dataflash log)\n", program_name());
exit(0);
}
const char *LogAnalyzer::program_name()
{
if (_argv == NULL) {
return "[Unknown]";
}
return _argv[0];
}
void LogAnalyzer::parse_arguments(int argc, char *argv[])
{
int opt;
_argc = argc;
_argv = argv;
while ((opt = getopt(argc, argv, "hc:ts:m:pf:Vla:i:")) != -1) {
switch(opt) {
case 'h':
usage();
break;
case 'c':
config_filename = optarg;
break;
case 'i':
forced_format_string = optarg;
break;
case 't':
_use_telem_forwarder = true;
break;
case 's':
output_style_string = optarg;
break;
case 'm':
_model_string = optarg;
break;
case 'p':
_pure_output = true;
break;
case 'f':
_frame_string = optarg;
break;
case 'V':
_show_version_information = true;
break;
case 'l':
_do_list_analyzers = true;
break;
case 'a':
_analyzer_names_to_run.push_back(optarg);
break;
}
}
if (optind < argc) {
_paths = &argv[optind];
_pathcount = argc-optind;
}
}
/*
* main - entry point
*/
int main(int argc, char* argv[])
{
LogAnalyzer analyzer;
analyzer.parse_arguments(argc, argv);
analyzer.run();
exit(0);
}