blob: cbf48918913714f11885f9ad089891c7ca8dad0d [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>
8#include <errno.h>
9
Brian Silverman88471dc2014-02-15 22:35:42 -080010#include <algorithm>
11
Brian Silvermanfe9b7a22014-02-10 15:03:42 -080012#include "aos/linux_code/logging/binary_log_file.h"
Brian Silverman88471dc2014-02-15 22:35:42 -080013#include "aos/common/queue_types.h"
Brian Silvermana7234c62014-03-24 20:23:25 -070014#include "aos/common/logging/logging_impl.h"
15#include "aos/common/logging/logging_printf_formats.h"
Brian Silverman88471dc2014-02-15 22:35:42 -080016
17using ::aos::logging::linux_code::LogFileMessageHeader;
brians343bc112013-02-10 01:53:46 +000018
19namespace {
20
21const char *kArgsHelp = "[OPTION]... [FILE]\n"
22 "Display log file FILE (created by BinaryLogReader) to stdout.\n"
23 "FILE is \"aos_log-current\" by default.\n"
24 "\n"
25 " -n, --name NAME only display entries from processes named NAME\n"
26 " -l, --level LEVEL "
27 "only display log entries at least as important as LEVEL\n"
28 " // -p, --pid PID only display log entries from process PID\n"
29 " -f, --follow "
30 "wait when the end of the file is reached (implies --end)\n"
31 " -t, --terminate stop when the end of file is reached (default)\n"
32 " -b, --beginning start at the beginning of the file (default)\n"
33 " -e, --end start at the end of the file\n"
34 " -s, --skip NUMBER skip NUMBER matching logs\n"
35 " // -m, --max NUMBER only display up to NUMBER logs\n"
36 " // -o, --format FORMAT use FORMAT to display log entries\n"
Brian Silvermana52ba162013-02-28 15:01:12 -080037 " -h, --help display this help and exit\n"
brians343bc112013-02-10 01:53:46 +000038 "\n"
39 "LEVEL must be DEBUG, INFO, WARNING, ERROR, or FATAL.\n"
40 " It defaults to INFO.\n"
41 "\n"
42 "TODO(brians) implement the commented out ones and changing FILE\n";
43
44void PrintHelpAndExit() {
45 fprintf(stderr, "Usage: %s %s", program_invocation_name, kArgsHelp);
46
47 exit(EXIT_SUCCESS);
48}
49
50} // namespace
51
52int main(int argc, char **argv) {
53 const char *filter_name = NULL;
Brian Silvermanf7780312014-02-16 17:26:15 -080054 size_t filter_length = 0;
brians343bc112013-02-10 01:53:46 +000055 log_level filter_level = INFO;
Brian Silvermanf7780312014-02-16 17:26:15 -080056 bool follow = false;
57 // Whether we need to skip everything until we get to the end of the file.
58 bool skip_to_end = false;
brians343bc112013-02-10 01:53:46 +000059 const char *filename = "aos_log-current";
60
Brian Silvermanf7780312014-02-16 17:26:15 -080061 ::aos::logging::AddImplementation(
62 new ::aos::logging::StreamLogImplementation(stdout));
63
brians343bc112013-02-10 01:53:46 +000064 while (true) {
65 static struct option long_options[] = {
66 {"name", required_argument, NULL, 'n'},
67 {"level", required_argument, NULL, 'l'},
68 {"pid", required_argument, NULL, 'p'},
69
70 {"follow", no_argument, NULL, 'f'},
71 {"terminate", no_argument, NULL, 't'},
72 {"beginning", no_argument, NULL, 'b'},
73 {"end", no_argument, NULL, 'e'},
74 {"skip", required_argument, NULL, 's'},
75 {"max", required_argument, NULL, 'm'},
76
77 {"format", required_argument, NULL, 'o'},
78
79 {"help", no_argument, NULL, 'h'},
80 {0, 0, 0, 0}
81 };
82 int option_index = 0;
83
84 const int c = getopt_long(argc, argv, "n:l:p:fts:m:o:h",
85 long_options, &option_index);
86 if (c == -1) { // if we're at the end
87 break;
88 }
89 switch (c) {
90 case 0:
91 fprintf(stderr, "LogDisplayer: got a 0 option but didn't set up any\n");
92 abort();
93 case 'n':
94 filter_name = optarg;
Brian Silverman88471dc2014-02-15 22:35:42 -080095 filter_length = strlen(filter_name);
brians343bc112013-02-10 01:53:46 +000096 break;
97 case 'l':
Brian Silvermanab6615c2013-03-05 20:29:29 -080098 filter_level = ::aos::logging::str_log(optarg);
brians343bc112013-02-10 01:53:46 +000099 if (filter_level == LOG_UNKNOWN) {
100 fprintf(stderr, "LogDisplayer: unknown log level '%s'\n", optarg);
101 exit(EXIT_FAILURE);
102 }
103 break;
104 case 'p':
105 abort();
106 break;
107 case 'f':
108 follow = true;
Brian Silvermanf7780312014-02-16 17:26:15 -0800109 skip_to_end = true;
brians343bc112013-02-10 01:53:46 +0000110 break;
111 case 't':
112 follow = false;
113 break;
114 case 'b':
Brian Silvermanf7780312014-02-16 17:26:15 -0800115 skip_to_end = false;
brians343bc112013-02-10 01:53:46 +0000116 break;
117 case 'e':
Brian Silvermanf7780312014-02-16 17:26:15 -0800118 skip_to_end = true;
brians343bc112013-02-10 01:53:46 +0000119 break;
120 case 'm':
121 abort();
122 break;
123 case 'o':
124 abort();
125 break;
126 case 'h':
127 PrintHelpAndExit();
128 break;
129 case '?':
130 break;
131 default:
Brian Silvermanab6615c2013-03-05 20:29:29 -0800132 fprintf(stderr, "LogDisplayer: in a bad spot (%s: %d)\n",
133 __FILE__, __LINE__);
brians343bc112013-02-10 01:53:46 +0000134 abort();
135 }
136 }
137
138 fprintf(stderr, "displaying down to level %s from file '%s'\n",
Brian Silvermanab6615c2013-03-05 20:29:29 -0800139 ::aos::logging::log_str(filter_level), filename);
brians343bc112013-02-10 01:53:46 +0000140 if (optind < argc) {
141 fprintf(stderr, "non-option ARGV-elements: ");
142 while (optind < argc) {
143 fprintf(stderr, "%s\n", argv[optind++]);
144 }
145 }
146
147 int fd = open(filename, O_RDONLY);
148 if (fd == -1) {
149 fprintf(stderr, "error: couldn't open file '%s' for reading because of %s\n",
150 filename, strerror(errno));
151 exit(EXIT_FAILURE);
152 }
Brian Silverman003ba4b2014-02-10 16:56:18 -0800153 ::aos::logging::linux_code::LogFileAccessor accessor(fd, false);
Brian Silvermanf7780312014-02-16 17:26:15 -0800154
155 if (skip_to_end) {
156 fputs("skipping old logs...\n", stderr);
brians343bc112013-02-10 01:53:46 +0000157 }
Brian Silverman88471dc2014-02-15 22:35:42 -0800158
159 const LogFileMessageHeader *msg;
brians343bc112013-02-10 01:53:46 +0000160 do {
161 msg = accessor.ReadNextMessage(follow);
Brian Silverman003ba4b2014-02-10 16:56:18 -0800162 if (msg == NULL) {
163 fputs("reached end of file\n", stderr);
164 return 0;
165 }
Brian Silverman88471dc2014-02-15 22:35:42 -0800166
167 if (msg->type == LogFileMessageHeader::MessageType::kStructType) {
168 size_t bytes = msg->message_size;
169 ::aos::MessageType *type = ::aos::MessageType::Deserialize(
170 reinterpret_cast<const char *>(msg + 1), &bytes);
171 ::aos::type_cache::Add(*type);
brians343bc112013-02-10 01:53:46 +0000172 continue;
173 }
Brian Silvermanf665d692013-02-17 22:11:39 -0800174
Brian Silvermanf7780312014-02-16 17:26:15 -0800175 if (skip_to_end) {
176 if (accessor.IsLastPage()) {
177 fputs("done skipping old logs\n", stderr);
178 skip_to_end = false;
179 } else {
180 continue;
181 }
182 }
183
Brian Silverman88471dc2014-02-15 22:35:42 -0800184 if (::aos::logging::log_gt_important(filter_level, msg->level)) continue;
185 if (filter_name != NULL) {
186 if (filter_length != msg->name_size) continue;
187 if (memcmp(filter_name,
188 reinterpret_cast<const char *>(msg) + sizeof(*msg),
189 filter_length) !=
190 0) {
191 continue;
192 }
193 }
194
Brian Silvermana7234c62014-03-24 20:23:25 -0700195 const char *position =
196 reinterpret_cast<const char *>(msg + 1) + msg->name_size;
197#define BASE_ARGS \
198 AOS_LOGGING_BASE_ARGS( \
199 msg->name_size, reinterpret_cast<const char *>(msg + 1), msg->source, \
200 msg->sequence, msg->level, msg->time_sec, msg->time_nsec)
Brian Silverman88471dc2014-02-15 22:35:42 -0800201 switch (msg->type) {
Brian Silvermana7234c62014-03-24 20:23:25 -0700202 case LogFileMessageHeader::MessageType::kString:
203 fprintf(stdout, AOS_LOGGING_BASE_FORMAT "%.*s", BASE_ARGS,
204 static_cast<int>(msg->message_size), position);
Brian Silverman88471dc2014-02-15 22:35:42 -0800205 break;
Brian Silvermana7234c62014-03-24 20:23:25 -0700206 case LogFileMessageHeader::MessageType::kStruct: {
207 uint32_t type_id;
208 memcpy(&type_id, position, sizeof(type_id));
209 position += sizeof(type_id);
Brian Silverman664db1a2014-03-20 17:06:29 -0700210
Brian Silvermana7234c62014-03-24 20:23:25 -0700211 uint32_t string_length;
212 memcpy(&string_length, position, sizeof(string_length));
213 position += sizeof(string_length);
214
215 char buffer[2048];
216 size_t output_length = sizeof(buffer);
217 size_t input_length =
218 msg->message_size -
219 (sizeof(type_id) + sizeof(uint32_t) + string_length);
220 if (!PrintMessage(buffer, &output_length, position + string_length,
221 &input_length, ::aos::type_cache::Get(type_id))) {
222 LOG(FATAL, "printing message (%.*s) of type %s into %zu-byte buffer "
223 "failed\n",
224 static_cast<int>(string_length), position,
225 ::aos::type_cache::Get(type_id).name.c_str(), sizeof(buffer));
226 }
227 if (input_length > 0) {
228 LOG(WARNING, "%zu extra bytes on message of type %s\n",
229 input_length, ::aos::type_cache::Get(type_id).name.c_str());
230 }
231 fprintf(stdout, AOS_LOGGING_BASE_FORMAT "%.*s: %.*s\n", BASE_ARGS,
232 static_cast<int>(string_length), position,
233 static_cast<int>(sizeof(buffer) - output_length), buffer);
234 } break;
235 case LogFileMessageHeader::MessageType::kMatrix: {
236 uint32_t type;
237 memcpy(&type, position, sizeof(type));
238 position += sizeof(type);
239
240 uint32_t string_length;
241 memcpy(&string_length, position, sizeof(string_length));
242 position += sizeof(string_length);
Brian Silverman664db1a2014-03-20 17:06:29 -0700243
244 uint16_t rows;
245 memcpy(&rows, position, sizeof(rows));
Brian Silverman664db1a2014-03-20 17:06:29 -0700246 position += sizeof(rows);
247 uint16_t cols;
248 memcpy(&cols, position, sizeof(cols));
Brian Silverman664db1a2014-03-20 17:06:29 -0700249 position += sizeof(cols);
250
Brian Silvermana7234c62014-03-24 20:23:25 -0700251 const size_t matrix_bytes =
252 msg->message_size -
253 (sizeof(type) + sizeof(uint32_t) + sizeof(uint16_t) +
254 sizeof(uint16_t) + string_length);
255 CHECK_EQ(matrix_bytes, ::aos::MessageType::Sizeof(type) * rows * cols);
256 char buffer[2048];
257 size_t output_length = sizeof(buffer);
258 if (!::aos::PrintMatrix(buffer, &output_length,
259 position + string_length, type, rows, cols)) {
260 LOG(FATAL, "printing %dx%d matrix of type %" PRIu32 " failed\n", rows,
261 cols, type);
262 }
263 fprintf(stdout, AOS_LOGGING_BASE_FORMAT "%.*s: %.*s\n", BASE_ARGS,
264 static_cast<int>(string_length), position,
265 static_cast<int>(sizeof(buffer) - output_length), buffer);
266 } break;
Brian Silverman88471dc2014-02-15 22:35:42 -0800267 case LogFileMessageHeader::MessageType::kStructType:
268 LOG(FATAL, "shouldn't get here\n");
269 break;
Brian Silvermana7234c62014-03-24 20:23:25 -0700270 }
271#undef BASE_ARGS
brians343bc112013-02-10 01:53:46 +0000272 } while (msg != NULL);
273}