blob: bc00ddcfddd079133b43b9ac8636e8562e2c2178 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include <stdio.h>
2#include <stdlib.h>
3#include <getopt.h>
4#include <sys/types.h>
5#include <sys/stat.h>
6#include <fcntl.h>
7#include <inttypes.h>
brians343bc112013-02-10 01:53:46 +00008
Brian Silverman88471dc2014-02-15 22:35:42 -08009#include <algorithm>
Daniel Pettib6c885b2014-09-12 10:04:28 -070010#include <memory>
11#include <string>
Brian Silverman88471dc2014-02-15 22:35:42 -080012
Brian Silvermanfe9b7a22014-02-10 15:03:42 -080013#include "aos/linux_code/logging/binary_log_file.h"
Brian Silverman88471dc2014-02-15 22:35:42 -080014#include "aos/common/queue_types.h"
Brian Silvermana7234c62014-03-24 20:23:25 -070015#include "aos/common/logging/logging_impl.h"
16#include "aos/common/logging/logging_printf_formats.h"
Daniel Pettib6c885b2014-09-12 10:04:28 -070017#include "aos/common/util/string_to_num.h"
Brian Silverman88471dc2014-02-15 22:35:42 -080018
19using ::aos::logging::linux_code::LogFileMessageHeader;
brians343bc112013-02-10 01:53:46 +000020
21namespace {
22
23const char *kArgsHelp = "[OPTION]... [FILE]\n"
24 "Display log file FILE (created by BinaryLogReader) to stdout.\n"
25 "FILE is \"aos_log-current\" by default.\n"
26 "\n"
27 " -n, --name NAME only display entries from processes named NAME\n"
28 " -l, --level LEVEL "
29 "only display log entries at least as important as LEVEL\n"
Daniel Pettib6c885b2014-09-12 10:04:28 -070030 " -p, --pid PID only display log entries from process PID\n"
brians343bc112013-02-10 01:53:46 +000031 " -f, --follow "
32 "wait when the end of the file is reached (implies --end)\n"
33 " -t, --terminate stop when the end of file is reached (default)\n"
34 " -b, --beginning start at the beginning of the file (default)\n"
35 " -e, --end start at the end of the file\n"
36 " -s, --skip NUMBER skip NUMBER matching logs\n"
Daniel Pettib6c885b2014-09-12 10:04:28 -070037 " -m, --max NUMBER only display up to NUMBER logs\n"
brians343bc112013-02-10 01:53:46 +000038 " // -o, --format FORMAT use FORMAT to display log entries\n"
Brian Silvermana52ba162013-02-28 15:01:12 -080039 " -h, --help display this help and exit\n"
brians343bc112013-02-10 01:53:46 +000040 "\n"
41 "LEVEL must be DEBUG, INFO, WARNING, ERROR, or FATAL.\n"
42 " It defaults to INFO.\n"
43 "\n"
Daniel Pettie6f33e22014-08-21 20:35:55 -070044 "TODO(brians) implement the commented out ones.\n";
brians343bc112013-02-10 01:53:46 +000045
Daniel Petti88c81f42014-09-12 10:05:05 -070046const char *kExampleUsages = "To view logs from the shooter:\n"
47 "\t`log_displayer -n shooter`\n"
48 "To view debug logs from the shooter:\n"
49 "\t`log_displayer -n shooter -l DEBUG`\n"
50 "To view what the shooter is logging in realtime:\n"
51 "\t`log_displayer -f -n shooter`\n"
52 "To view shooter logs from an old log file:\n"
53 "\t`log_displayer aos_log-<number> -n shooter`\n"
54 "To view the statuses of the shooter hall effects in realtime:\n"
55 "\t`log_displayer -f -n shooter -l DEBUG | grep .Position`\n";
56
brians343bc112013-02-10 01:53:46 +000057void PrintHelpAndExit() {
58 fprintf(stderr, "Usage: %s %s", program_invocation_name, kArgsHelp);
Daniel Petti88c81f42014-09-12 10:05:05 -070059 fprintf(stderr, "\nExample usages:\n\n%s", kExampleUsages);
60
61 // Get the possible executables from start_list.txt.
62 FILE *start_list = fopen("start_list.txt", "r");
63 if (start_list == NULL) {
64 PLOG(FATAL, "Unable to open start_list.txt");
65 }
66
67 // Get file size.
68 if (fseek(start_list, 0, SEEK_END)) {
69 PLOG(FATAL, "fseek() failed while reading start_list.txt");
70 }
71 int size = ftell(start_list);
72 if (size < 0) {
73 PLOG(FATAL, "ftell() failed while reading start_list.txt");
74 }
75 rewind(start_list);
76
77 ::std::unique_ptr<char[]> contents(new char[size + 1]);
78 if (contents == NULL) {
79 LOG(FATAL, "malloc() failed while reading start_list.txt.\n");
80 }
81 size_t bytes_read = fread(contents.get(), 1, size, start_list);
82 if (bytes_read < static_cast<size_t>(size)) {
83 LOG(FATAL, "Read %zu bytes from start_list.txt, expected %d.\n",
84 bytes_read, size);
85 }
86
87 // printf doesn't like strings without the \0.
88 contents[size] = '\0';
89 fprintf(stderr, "\nPossible arguments for the -n option:\n%s", contents.get());
90
91 if (fclose(start_list)) {
92 LOG(FATAL, "fclose() failed.\n");
93 }
brians343bc112013-02-10 01:53:46 +000094
95 exit(EXIT_SUCCESS);
96}
97
98} // namespace
99
100int main(int argc, char **argv) {
101 const char *filter_name = NULL;
Brian Silvermanf7780312014-02-16 17:26:15 -0800102 size_t filter_length = 0;
brians343bc112013-02-10 01:53:46 +0000103 log_level filter_level = INFO;
Brian Silvermanf7780312014-02-16 17:26:15 -0800104 bool follow = false;
105 // Whether we need to skip everything until we get to the end of the file.
106 bool skip_to_end = false;
brians343bc112013-02-10 01:53:46 +0000107 const char *filename = "aos_log-current";
Daniel Pettib6c885b2014-09-12 10:04:28 -0700108 int display_max = 0;
109 int32_t source_pid = -1;
brians343bc112013-02-10 01:53:46 +0000110
Brian Silvermanff485782014-06-18 19:59:09 -0700111 ::aos::logging::Init();
Brian Silvermanf7780312014-02-16 17:26:15 -0800112 ::aos::logging::AddImplementation(
113 new ::aos::logging::StreamLogImplementation(stdout));
114
brians343bc112013-02-10 01:53:46 +0000115 while (true) {
116 static struct option long_options[] = {
117 {"name", required_argument, NULL, 'n'},
118 {"level", required_argument, NULL, 'l'},
119 {"pid", required_argument, NULL, 'p'},
120
121 {"follow", no_argument, NULL, 'f'},
122 {"terminate", no_argument, NULL, 't'},
123 {"beginning", no_argument, NULL, 'b'},
124 {"end", no_argument, NULL, 'e'},
125 {"skip", required_argument, NULL, 's'},
126 {"max", required_argument, NULL, 'm'},
127
128 {"format", required_argument, NULL, 'o'},
129
130 {"help", no_argument, NULL, 'h'},
131 {0, 0, 0, 0}
132 };
133 int option_index = 0;
134
135 const int c = getopt_long(argc, argv, "n:l:p:fts:m:o:h",
136 long_options, &option_index);
137 if (c == -1) { // if we're at the end
138 break;
139 }
140 switch (c) {
141 case 0:
Daniel Petti88c81f42014-09-12 10:05:05 -0700142 fputs("LogDisplayer: got a 0 option but didn't set up any\n", stderr);
brians343bc112013-02-10 01:53:46 +0000143 abort();
144 case 'n':
145 filter_name = optarg;
Brian Silverman88471dc2014-02-15 22:35:42 -0800146 filter_length = strlen(filter_name);
brians343bc112013-02-10 01:53:46 +0000147 break;
148 case 'l':
Brian Silvermanab6615c2013-03-05 20:29:29 -0800149 filter_level = ::aos::logging::str_log(optarg);
brians343bc112013-02-10 01:53:46 +0000150 if (filter_level == LOG_UNKNOWN) {
151 fprintf(stderr, "LogDisplayer: unknown log level '%s'\n", optarg);
152 exit(EXIT_FAILURE);
153 }
154 break;
155 case 'p':
Daniel Pettib6c885b2014-09-12 10:04:28 -0700156 if (!::aos::util::StringToInteger(::std::string(optarg), &source_pid)) {
157 fprintf(stderr, "ERROR: -p expects a number, not '%s'.\n", optarg);
158 exit(EXIT_FAILURE);
159 }
160 if (source_pid < 0) {
161 fprintf(stderr, "LogDisplayer: invalid pid '%s'\n", optarg);
162 exit(EXIT_FAILURE);
163 }
brians343bc112013-02-10 01:53:46 +0000164 break;
165 case 'f':
166 follow = true;
Brian Silvermanf7780312014-02-16 17:26:15 -0800167 skip_to_end = true;
brians343bc112013-02-10 01:53:46 +0000168 break;
169 case 't':
170 follow = false;
171 break;
172 case 'b':
Brian Silvermanf7780312014-02-16 17:26:15 -0800173 skip_to_end = false;
brians343bc112013-02-10 01:53:46 +0000174 break;
175 case 'e':
Brian Silvermanf7780312014-02-16 17:26:15 -0800176 skip_to_end = true;
brians343bc112013-02-10 01:53:46 +0000177 break;
178 case 'm':
Daniel Pettib6c885b2014-09-12 10:04:28 -0700179 if (!::aos::util::StringToInteger(::std::string(optarg), &display_max)) {
180 fprintf(stderr, "ERROR: -m expects a number, not '%s'.\n", optarg);
181 exit(EXIT_FAILURE);
182 }
183 if (display_max <= 0) {
184 fprintf(stderr, "LogDisplayer: invalid max log number '%s'\n",
185 optarg);
186 exit(EXIT_FAILURE);
187 }
brians343bc112013-02-10 01:53:46 +0000188 break;
189 case 'o':
190 abort();
191 break;
192 case 'h':
193 PrintHelpAndExit();
194 break;
195 case '?':
196 break;
197 default:
Brian Silvermanab6615c2013-03-05 20:29:29 -0800198 fprintf(stderr, "LogDisplayer: in a bad spot (%s: %d)\n",
199 __FILE__, __LINE__);
brians343bc112013-02-10 01:53:46 +0000200 abort();
201 }
202 }
203
Daniel Pettie6f33e22014-08-21 20:35:55 -0700204 if (optind < argc) {
205 // We got a filename.
206 filename = argv[optind++];
207 }
brians343bc112013-02-10 01:53:46 +0000208 if (optind < argc) {
Daniel Petti88c81f42014-09-12 10:05:05 -0700209 fputs("non-option ARGV-elements: ", stderr);
brians343bc112013-02-10 01:53:46 +0000210 while (optind < argc) {
211 fprintf(stderr, "%s\n", argv[optind++]);
212 }
213 }
214
Daniel Pettie6f33e22014-08-21 20:35:55 -0700215 fprintf(stderr, "displaying down to level %s from file '%s'\n",
216 ::aos::logging::log_str(filter_level), filename);
217
brians343bc112013-02-10 01:53:46 +0000218 int fd = open(filename, O_RDONLY);
Daniel Pettie6f33e22014-08-21 20:35:55 -0700219
brians343bc112013-02-10 01:53:46 +0000220 if (fd == -1) {
Brian Silverman01be0002014-05-10 15:44:38 -0700221 PLOG(FATAL, "couldn't open file '%s' for reading", filename);
brians343bc112013-02-10 01:53:46 +0000222 }
Brian Silvermanab5ba472014-04-18 15:26:14 -0700223 ::aos::logging::linux_code::LogFileReader reader(fd);
Brian Silvermanf7780312014-02-16 17:26:15 -0800224
225 if (skip_to_end) {
226 fputs("skipping old logs...\n", stderr);
brians343bc112013-02-10 01:53:46 +0000227 }
Brian Silverman88471dc2014-02-15 22:35:42 -0800228
229 const LogFileMessageHeader *msg;
Daniel Pettib6c885b2014-09-12 10:04:28 -0700230 int displayed = 0;
brians343bc112013-02-10 01:53:46 +0000231 do {
Brian Silvermanab5ba472014-04-18 15:26:14 -0700232 msg = reader.ReadNextMessage(follow);
Brian Silverman003ba4b2014-02-10 16:56:18 -0800233 if (msg == NULL) {
234 fputs("reached end of file\n", stderr);
235 return 0;
236 }
Brian Silverman88471dc2014-02-15 22:35:42 -0800237
Daniel Pettib6c885b2014-09-12 10:04:28 -0700238 if (source_pid >= 0 && msg->source != source_pid) {
239 // Message is from the wrong process.
240 continue;
241 }
242
Brian Silverman88471dc2014-02-15 22:35:42 -0800243 if (msg->type == LogFileMessageHeader::MessageType::kStructType) {
244 size_t bytes = msg->message_size;
245 ::aos::MessageType *type = ::aos::MessageType::Deserialize(
246 reinterpret_cast<const char *>(msg + 1), &bytes);
247 ::aos::type_cache::Add(*type);
brians343bc112013-02-10 01:53:46 +0000248 continue;
249 }
Brian Silvermanf665d692013-02-17 22:11:39 -0800250
Brian Silvermanf7780312014-02-16 17:26:15 -0800251 if (skip_to_end) {
Brian Silvermanab5ba472014-04-18 15:26:14 -0700252 if (reader.IsLastPage()) {
Brian Silvermanf7780312014-02-16 17:26:15 -0800253 fputs("done skipping old logs\n", stderr);
254 skip_to_end = false;
255 } else {
256 continue;
257 }
258 }
259
Brian Silverman88471dc2014-02-15 22:35:42 -0800260 if (::aos::logging::log_gt_important(filter_level, msg->level)) continue;
261 if (filter_name != NULL) {
262 if (filter_length != msg->name_size) continue;
263 if (memcmp(filter_name,
264 reinterpret_cast<const char *>(msg) + sizeof(*msg),
265 filter_length) !=
266 0) {
267 continue;
268 }
269 }
270
Daniel Pettib6c885b2014-09-12 10:04:28 -0700271 if (display_max && displayed++ >= display_max) {
272 fputs("Not displaying the rest of the messages.\n", stderr);
273 return 0;
274 }
275
Brian Silvermana7234c62014-03-24 20:23:25 -0700276 const char *position =
277 reinterpret_cast<const char *>(msg + 1) + msg->name_size;
278#define BASE_ARGS \
279 AOS_LOGGING_BASE_ARGS( \
280 msg->name_size, reinterpret_cast<const char *>(msg + 1), msg->source, \
281 msg->sequence, msg->level, msg->time_sec, msg->time_nsec)
Brian Silverman88471dc2014-02-15 22:35:42 -0800282 switch (msg->type) {
Brian Silvermana7234c62014-03-24 20:23:25 -0700283 case LogFileMessageHeader::MessageType::kString:
284 fprintf(stdout, AOS_LOGGING_BASE_FORMAT "%.*s", BASE_ARGS,
285 static_cast<int>(msg->message_size), position);
Brian Silverman88471dc2014-02-15 22:35:42 -0800286 break;
Brian Silvermana7234c62014-03-24 20:23:25 -0700287 case LogFileMessageHeader::MessageType::kStruct: {
288 uint32_t type_id;
289 memcpy(&type_id, position, sizeof(type_id));
290 position += sizeof(type_id);
Brian Silverman664db1a2014-03-20 17:06:29 -0700291
Brian Silvermana7234c62014-03-24 20:23:25 -0700292 uint32_t string_length;
293 memcpy(&string_length, position, sizeof(string_length));
294 position += sizeof(string_length);
295
296 char buffer[2048];
297 size_t output_length = sizeof(buffer);
298 size_t input_length =
299 msg->message_size -
300 (sizeof(type_id) + sizeof(uint32_t) + string_length);
301 if (!PrintMessage(buffer, &output_length, position + string_length,
302 &input_length, ::aos::type_cache::Get(type_id))) {
303 LOG(FATAL, "printing message (%.*s) of type %s into %zu-byte buffer "
304 "failed\n",
305 static_cast<int>(string_length), position,
306 ::aos::type_cache::Get(type_id).name.c_str(), sizeof(buffer));
307 }
308 if (input_length > 0) {
309 LOG(WARNING, "%zu extra bytes on message of type %s\n",
310 input_length, ::aos::type_cache::Get(type_id).name.c_str());
311 }
312 fprintf(stdout, AOS_LOGGING_BASE_FORMAT "%.*s: %.*s\n", BASE_ARGS,
313 static_cast<int>(string_length), position,
314 static_cast<int>(sizeof(buffer) - output_length), buffer);
315 } break;
316 case LogFileMessageHeader::MessageType::kMatrix: {
317 uint32_t type;
318 memcpy(&type, position, sizeof(type));
319 position += sizeof(type);
320
321 uint32_t string_length;
322 memcpy(&string_length, position, sizeof(string_length));
323 position += sizeof(string_length);
Brian Silverman664db1a2014-03-20 17:06:29 -0700324
325 uint16_t rows;
326 memcpy(&rows, position, sizeof(rows));
Brian Silverman664db1a2014-03-20 17:06:29 -0700327 position += sizeof(rows);
328 uint16_t cols;
329 memcpy(&cols, position, sizeof(cols));
Brian Silverman664db1a2014-03-20 17:06:29 -0700330 position += sizeof(cols);
331
Brian Silvermana7234c62014-03-24 20:23:25 -0700332 const size_t matrix_bytes =
333 msg->message_size -
334 (sizeof(type) + sizeof(uint32_t) + sizeof(uint16_t) +
335 sizeof(uint16_t) + string_length);
336 CHECK_EQ(matrix_bytes, ::aos::MessageType::Sizeof(type) * rows * cols);
337 char buffer[2048];
338 size_t output_length = sizeof(buffer);
339 if (!::aos::PrintMatrix(buffer, &output_length,
340 position + string_length, type, rows, cols)) {
341 LOG(FATAL, "printing %dx%d matrix of type %" PRIu32 " failed\n", rows,
342 cols, type);
343 }
344 fprintf(stdout, AOS_LOGGING_BASE_FORMAT "%.*s: %.*s\n", BASE_ARGS,
345 static_cast<int>(string_length), position,
346 static_cast<int>(sizeof(buffer) - output_length), buffer);
347 } break;
Brian Silverman88471dc2014-02-15 22:35:42 -0800348 case LogFileMessageHeader::MessageType::kStructType:
349 LOG(FATAL, "shouldn't get here\n");
350 break;
Brian Silvermana7234c62014-03-24 20:23:25 -0700351 }
352#undef BASE_ARGS
brians343bc112013-02-10 01:53:46 +0000353 } while (msg != NULL);
354}