brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
Daniel Petti | aa5bdb1 | 2014-12-20 16:33:05 -0800 | [diff] [blame] | 3 | #include <string.h> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 4 | #include <getopt.h> |
| 5 | #include <sys/types.h> |
| 6 | #include <sys/stat.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <inttypes.h> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 9 | |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 10 | #include <algorithm> |
Daniel Petti | b6c885b | 2014-09-12 10:04:28 -0700 | [diff] [blame] | 11 | #include <memory> |
| 12 | #include <string> |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 13 | |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame^] | 14 | #include "aos/configuration.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 15 | #include "aos/logging/binary_log_file.h" |
| 16 | #include "aos/queue_types.h" |
| 17 | #include "aos/logging/logging.h" |
| 18 | #include "aos/logging/implementations.h" |
| 19 | #include "aos/logging/printf_formats.h" |
| 20 | #include "aos/util/string_to_num.h" |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 21 | |
| 22 | using ::aos::logging::linux_code::LogFileMessageHeader; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 23 | |
| 24 | namespace { |
| 25 | |
| 26 | const char *kArgsHelp = "[OPTION]... [FILE]\n" |
| 27 | "Display log file FILE (created by BinaryLogReader) to stdout.\n" |
| 28 | "FILE is \"aos_log-current\" by default.\n" |
Brian Silverman | 65e569d | 2014-10-24 15:43:20 -0400 | [diff] [blame] | 29 | "FILE can also be \"-\" to read from standard input.\n" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 30 | "\n" |
Brian Silverman | 8c2b00e | 2015-02-17 20:30:50 -0500 | [diff] [blame] | 31 | " -n, --name-prefix NAME " |
| 32 | "only display entries from processes which share NAME as a prefix\n" |
| 33 | " -N, --name NAME only display entries from processes named NAME\n" |
| 34 | " -l, --level LEVEL " |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 35 | "only display log entries at least as important as LEVEL\n" |
Brian Silverman | 8c2b00e | 2015-02-17 20:30:50 -0500 | [diff] [blame] | 36 | " -p, --pid PID only display log entries from process PID\n" |
| 37 | " -f, --follow " |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 38 | "wait when the end of the file is reached (implies --end)\n" |
Brian Silverman | 8c2b00e | 2015-02-17 20:30:50 -0500 | [diff] [blame] | 39 | " -t, --terminate stop when the end of file is reached (default)\n" |
| 40 | " -b, --beginning start at the beginning of the file (default)\n" |
| 41 | " -e, --end start at the end of the file\n" |
| 42 | " -s, --skip NUMBER skip NUMBER matching logs\n" |
| 43 | " -m, --max NUMBER only display up to NUMBER logs\n" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 44 | " // -o, --format FORMAT use FORMAT to display log entries\n" |
Brian Silverman | 8c2b00e | 2015-02-17 20:30:50 -0500 | [diff] [blame] | 45 | " -h, --help display this help and exit\n" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 46 | "\n" |
| 47 | "LEVEL must be DEBUG, INFO, WARNING, ERROR, or FATAL.\n" |
| 48 | " It defaults to INFO.\n" |
| 49 | "\n" |
Daniel Petti | e6f33e2 | 2014-08-21 20:35:55 -0700 | [diff] [blame] | 50 | "TODO(brians) implement the commented out ones.\n"; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 51 | |
Daniel Petti | 88c81f4 | 2014-09-12 10:05:05 -0700 | [diff] [blame] | 52 | const char *kExampleUsages = "To view logs from the shooter:\n" |
| 53 | "\t`log_displayer -n shooter`\n" |
| 54 | "To view debug logs from the shooter:\n" |
| 55 | "\t`log_displayer -n shooter -l DEBUG`\n" |
| 56 | "To view what the shooter is logging in realtime:\n" |
| 57 | "\t`log_displayer -f -n shooter`\n" |
| 58 | "To view shooter logs from an old log file:\n" |
| 59 | "\t`log_displayer aos_log-<number> -n shooter`\n" |
| 60 | "To view the statuses of the shooter hall effects in realtime:\n" |
| 61 | "\t`log_displayer -f -n shooter -l DEBUG | grep .Position`\n"; |
| 62 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 63 | void PrintHelpAndExit() { |
| 64 | fprintf(stderr, "Usage: %s %s", program_invocation_name, kArgsHelp); |
Daniel Petti | 88c81f4 | 2014-09-12 10:05:05 -0700 | [diff] [blame] | 65 | fprintf(stderr, "\nExample usages:\n\n%s", kExampleUsages); |
| 66 | |
| 67 | // Get the possible executables from start_list.txt. |
| 68 | FILE *start_list = fopen("start_list.txt", "r"); |
Daniel Petti | aa5bdb1 | 2014-12-20 16:33:05 -0800 | [diff] [blame] | 69 | if (!start_list) { |
| 70 | ::std::string path(::aos::configuration::GetRootDirectory()); |
| 71 | path += "/start_list.txt"; |
| 72 | start_list = fopen(path.c_str(), "r"); |
| 73 | if (!start_list) { |
| 74 | printf("\nCannot open start_list.txt. This means that the\n" |
| 75 | "possible arguments for the -n option cannot be shown. log_displayer\n" |
| 76 | "looks for start_list.txt in the current working directory and in\n" |
| 77 | "%s.\n\n", ::aos::configuration::GetRootDirectory()); |
| 78 | PLOG(FATAL, "Unable to open start_list.txt"); |
| 79 | } |
Daniel Petti | 88c81f4 | 2014-09-12 10:05:05 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // Get file size. |
| 83 | if (fseek(start_list, 0, SEEK_END)) { |
| 84 | PLOG(FATAL, "fseek() failed while reading start_list.txt"); |
| 85 | } |
| 86 | int size = ftell(start_list); |
| 87 | if (size < 0) { |
| 88 | PLOG(FATAL, "ftell() failed while reading start_list.txt"); |
| 89 | } |
| 90 | rewind(start_list); |
| 91 | |
| 92 | ::std::unique_ptr<char[]> contents(new char[size + 1]); |
| 93 | if (contents == NULL) { |
| 94 | LOG(FATAL, "malloc() failed while reading start_list.txt.\n"); |
| 95 | } |
| 96 | size_t bytes_read = fread(contents.get(), 1, size, start_list); |
| 97 | if (bytes_read < static_cast<size_t>(size)) { |
| 98 | LOG(FATAL, "Read %zu bytes from start_list.txt, expected %d.\n", |
| 99 | bytes_read, size); |
| 100 | } |
| 101 | |
| 102 | // printf doesn't like strings without the \0. |
| 103 | contents[size] = '\0'; |
| 104 | fprintf(stderr, "\nPossible arguments for the -n option:\n%s", contents.get()); |
| 105 | |
| 106 | if (fclose(start_list)) { |
| 107 | LOG(FATAL, "fclose() failed.\n"); |
| 108 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 109 | |
| 110 | exit(EXIT_SUCCESS); |
| 111 | } |
| 112 | |
| 113 | } // namespace |
| 114 | |
| 115 | int main(int argc, char **argv) { |
Brian Silverman | 8c2b00e | 2015-02-17 20:30:50 -0500 | [diff] [blame] | 116 | const char *filter_name = nullptr, *filter_exact_name = nullptr; |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 117 | size_t filter_length = 0; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 118 | log_level filter_level = INFO; |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 119 | bool follow = false; |
| 120 | // Whether we need to skip everything until we get to the end of the file. |
| 121 | bool skip_to_end = false; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 122 | const char *filename = "aos_log-current"; |
Daniel Petti | b6c885b | 2014-09-12 10:04:28 -0700 | [diff] [blame] | 123 | int display_max = 0; |
| 124 | int32_t source_pid = -1; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 125 | |
Brian Silverman | ff48578 | 2014-06-18 19:59:09 -0700 | [diff] [blame] | 126 | ::aos::logging::Init(); |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 127 | ::aos::logging::AddImplementation( |
| 128 | new ::aos::logging::StreamLogImplementation(stdout)); |
| 129 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 130 | while (true) { |
| 131 | static struct option long_options[] = { |
Brian Silverman | 8c2b00e | 2015-02-17 20:30:50 -0500 | [diff] [blame] | 132 | {"name-prefix", required_argument, NULL, 'n'}, |
| 133 | {"name", required_argument, NULL, 'N'}, |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 134 | {"level", required_argument, NULL, 'l'}, |
| 135 | {"pid", required_argument, NULL, 'p'}, |
| 136 | |
| 137 | {"follow", no_argument, NULL, 'f'}, |
| 138 | {"terminate", no_argument, NULL, 't'}, |
| 139 | {"beginning", no_argument, NULL, 'b'}, |
| 140 | {"end", no_argument, NULL, 'e'}, |
| 141 | {"skip", required_argument, NULL, 's'}, |
| 142 | {"max", required_argument, NULL, 'm'}, |
| 143 | |
| 144 | {"format", required_argument, NULL, 'o'}, |
| 145 | |
| 146 | {"help", no_argument, NULL, 'h'}, |
| 147 | {0, 0, 0, 0} |
| 148 | }; |
| 149 | int option_index = 0; |
| 150 | |
Brian Silverman | dbcc87d | 2015-03-15 20:41:34 -0700 | [diff] [blame] | 151 | const int c = getopt_long(argc, argv, "N:n:l:p:fts:m:o:h", |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 152 | long_options, &option_index); |
| 153 | if (c == -1) { // if we're at the end |
| 154 | break; |
| 155 | } |
| 156 | switch (c) { |
| 157 | case 0: |
Daniel Petti | 88c81f4 | 2014-09-12 10:05:05 -0700 | [diff] [blame] | 158 | fputs("LogDisplayer: got a 0 option but didn't set up any\n", stderr); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 159 | abort(); |
| 160 | case 'n': |
| 161 | filter_name = optarg; |
Brian Silverman | 8c2b00e | 2015-02-17 20:30:50 -0500 | [diff] [blame] | 162 | filter_exact_name = nullptr; |
| 163 | filter_length = strlen(filter_name); |
| 164 | break; |
| 165 | case 'N': |
| 166 | filter_exact_name = optarg; |
| 167 | filter_name = nullptr; |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 168 | filter_length = strlen(filter_name); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 169 | break; |
| 170 | case 'l': |
Brian Silverman | ab6615c | 2013-03-05 20:29:29 -0800 | [diff] [blame] | 171 | filter_level = ::aos::logging::str_log(optarg); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 172 | if (filter_level == LOG_UNKNOWN) { |
| 173 | fprintf(stderr, "LogDisplayer: unknown log level '%s'\n", optarg); |
| 174 | exit(EXIT_FAILURE); |
| 175 | } |
| 176 | break; |
| 177 | case 'p': |
Daniel Petti | 5aa2979 | 2014-12-27 17:48:07 -0500 | [diff] [blame] | 178 | if (!::aos::util::StringToNumber(::std::string(optarg), &source_pid)) { |
Daniel Petti | b6c885b | 2014-09-12 10:04:28 -0700 | [diff] [blame] | 179 | fprintf(stderr, "ERROR: -p expects a number, not '%s'.\n", optarg); |
| 180 | exit(EXIT_FAILURE); |
| 181 | } |
| 182 | if (source_pid < 0) { |
| 183 | fprintf(stderr, "LogDisplayer: invalid pid '%s'\n", optarg); |
| 184 | exit(EXIT_FAILURE); |
| 185 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 186 | break; |
| 187 | case 'f': |
| 188 | follow = true; |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 189 | skip_to_end = true; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 190 | break; |
| 191 | case 't': |
| 192 | follow = false; |
| 193 | break; |
| 194 | case 'b': |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 195 | skip_to_end = false; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 196 | break; |
| 197 | case 'e': |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 198 | skip_to_end = true; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 199 | break; |
| 200 | case 'm': |
Daniel Petti | 5aa2979 | 2014-12-27 17:48:07 -0500 | [diff] [blame] | 201 | if (!::aos::util::StringToNumber(::std::string(optarg), &display_max)) { |
Daniel Petti | b6c885b | 2014-09-12 10:04:28 -0700 | [diff] [blame] | 202 | fprintf(stderr, "ERROR: -m expects a number, not '%s'.\n", optarg); |
| 203 | exit(EXIT_FAILURE); |
| 204 | } |
| 205 | if (display_max <= 0) { |
| 206 | fprintf(stderr, "LogDisplayer: invalid max log number '%s'\n", |
| 207 | optarg); |
| 208 | exit(EXIT_FAILURE); |
| 209 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 210 | break; |
| 211 | case 'o': |
| 212 | abort(); |
| 213 | break; |
| 214 | case 'h': |
| 215 | PrintHelpAndExit(); |
| 216 | break; |
| 217 | case '?': |
| 218 | break; |
| 219 | default: |
Brian Silverman | ab6615c | 2013-03-05 20:29:29 -0800 | [diff] [blame] | 220 | fprintf(stderr, "LogDisplayer: in a bad spot (%s: %d)\n", |
| 221 | __FILE__, __LINE__); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 222 | abort(); |
| 223 | } |
| 224 | } |
| 225 | |
Daniel Petti | e6f33e2 | 2014-08-21 20:35:55 -0700 | [diff] [blame] | 226 | if (optind < argc) { |
| 227 | // We got a filename. |
| 228 | filename = argv[optind++]; |
| 229 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 230 | if (optind < argc) { |
Daniel Petti | 88c81f4 | 2014-09-12 10:05:05 -0700 | [diff] [blame] | 231 | fputs("non-option ARGV-elements: ", stderr); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 232 | while (optind < argc) { |
| 233 | fprintf(stderr, "%s\n", argv[optind++]); |
| 234 | } |
| 235 | } |
| 236 | |
Brian Silverman | 65e569d | 2014-10-24 15:43:20 -0400 | [diff] [blame] | 237 | int fd; |
| 238 | if (strcmp(filename, "-") == 0) { |
Brian Silverman | f5ca4d0 | 2015-03-01 16:52:24 -0500 | [diff] [blame] | 239 | if (skip_to_end) { |
| 240 | fputs("Can't skip to end of stdin!\n", stderr); |
| 241 | return EXIT_FAILURE; |
| 242 | } |
Brian Silverman | 65e569d | 2014-10-24 15:43:20 -0400 | [diff] [blame] | 243 | fd = STDIN_FILENO; |
| 244 | } else { |
| 245 | fd = open(filename, O_RDONLY); |
| 246 | } |
Daniel Petti | e6f33e2 | 2014-08-21 20:35:55 -0700 | [diff] [blame] | 247 | |
Brian Silverman | f5ca4d0 | 2015-03-01 16:52:24 -0500 | [diff] [blame] | 248 | fprintf(stderr, "displaying down to level %s from file '%s'\n", |
| 249 | ::aos::logging::log_str(filter_level), filename); |
| 250 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 251 | if (fd == -1) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 252 | PLOG(FATAL, "couldn't open file '%s' for reading", filename); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 253 | } |
Brian Silverman | ab5ba47 | 2014-04-18 15:26:14 -0700 | [diff] [blame] | 254 | ::aos::logging::linux_code::LogFileReader reader(fd); |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 255 | |
| 256 | if (skip_to_end) { |
| 257 | fputs("skipping old logs...\n", stderr); |
Brian Silverman | f5ca4d0 | 2015-03-01 16:52:24 -0500 | [diff] [blame] | 258 | reader.SkipToLastSeekablePage(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 259 | } |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 260 | |
| 261 | const LogFileMessageHeader *msg; |
Daniel Petti | b6c885b | 2014-09-12 10:04:28 -0700 | [diff] [blame] | 262 | int displayed = 0; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 263 | do { |
Brian Silverman | ab5ba47 | 2014-04-18 15:26:14 -0700 | [diff] [blame] | 264 | msg = reader.ReadNextMessage(follow); |
Brian Silverman | 003ba4b | 2014-02-10 16:56:18 -0800 | [diff] [blame] | 265 | if (msg == NULL) { |
| 266 | fputs("reached end of file\n", stderr); |
| 267 | return 0; |
| 268 | } |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 269 | |
| 270 | if (msg->type == LogFileMessageHeader::MessageType::kStructType) { |
| 271 | size_t bytes = msg->message_size; |
| 272 | ::aos::MessageType *type = ::aos::MessageType::Deserialize( |
| 273 | reinterpret_cast<const char *>(msg + 1), &bytes); |
Brian Silverman | f4452d7 | 2014-10-25 17:23:11 -0400 | [diff] [blame] | 274 | if (type == nullptr) { |
Austin Schuh | 7e95839 | 2014-10-21 22:16:23 -0700 | [diff] [blame] | 275 | LOG(INFO, "Trying old version of type decoding.\n"); |
| 276 | bytes = msg->message_size; |
| 277 | type = ::aos::MessageType::Deserialize( |
| 278 | reinterpret_cast<const char *>(msg + 1), &bytes, false); |
| 279 | } |
| 280 | |
| 281 | if (type == nullptr) { |
Brian Silverman | f4452d7 | 2014-10-25 17:23:11 -0400 | [diff] [blame] | 282 | LOG(WARNING, "Error deserializing MessageType of size %" PRIx32 |
| 283 | " starting at %zx.\n", |
| 284 | msg->message_size, reader.file_offset(msg + 1)); |
| 285 | } else { |
| 286 | ::aos::type_cache::Add(*type); |
| 287 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 288 | continue; |
| 289 | } |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 290 | |
Brian Silverman | 64833d4 | 2015-02-08 21:11:36 -0500 | [diff] [blame] | 291 | if (source_pid >= 0 && msg->source != source_pid) { |
| 292 | // Message is from the wrong process. |
| 293 | continue; |
| 294 | } |
| 295 | |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 296 | if (skip_to_end) { |
Brian Silverman | ab5ba47 | 2014-04-18 15:26:14 -0700 | [diff] [blame] | 297 | if (reader.IsLastPage()) { |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 298 | fputs("done skipping old logs\n", stderr); |
| 299 | skip_to_end = false; |
| 300 | } else { |
| 301 | continue; |
| 302 | } |
| 303 | } |
| 304 | |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 305 | if (::aos::logging::log_gt_important(filter_level, msg->level)) continue; |
Brian Silverman | 8c2b00e | 2015-02-17 20:30:50 -0500 | [diff] [blame] | 306 | |
Brian Silverman | 58657aa | 2015-02-21 20:06:40 -0500 | [diff] [blame] | 307 | const char *position = |
| 308 | reinterpret_cast<const char *>(msg + 1); |
| 309 | |
Brian Silverman | 8c2b00e | 2015-02-17 20:30:50 -0500 | [diff] [blame] | 310 | if (filter_name != nullptr) { |
| 311 | const size_t compare_length = |
| 312 | ::std::min<size_t>(filter_length, msg->name_size); |
Brian Silverman | 58657aa | 2015-02-21 20:06:40 -0500 | [diff] [blame] | 313 | if (memcmp(filter_name, position, compare_length) != 0) { |
Brian Silverman | 8c2b00e | 2015-02-17 20:30:50 -0500 | [diff] [blame] | 314 | continue; |
| 315 | } |
Brian Silverman | 58657aa | 2015-02-21 20:06:40 -0500 | [diff] [blame] | 316 | if (compare_length < msg->name_size) { |
| 317 | if (position[compare_length] != '.') continue; |
| 318 | } |
Brian Silverman | 8c2b00e | 2015-02-17 20:30:50 -0500 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | if (filter_exact_name != nullptr) { |
| 322 | if (filter_length != msg->name_size) continue; |
Brian Silverman | 58657aa | 2015-02-21 20:06:40 -0500 | [diff] [blame] | 323 | if (memcmp(filter_exact_name, position, filter_length) != 0) { |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 324 | continue; |
| 325 | } |
| 326 | } |
| 327 | |
Daniel Petti | b6c885b | 2014-09-12 10:04:28 -0700 | [diff] [blame] | 328 | if (display_max && displayed++ >= display_max) { |
| 329 | fputs("Not displaying the rest of the messages.\n", stderr); |
| 330 | return 0; |
| 331 | } |
| 332 | |
Brian Silverman | 58657aa | 2015-02-21 20:06:40 -0500 | [diff] [blame] | 333 | position += msg->name_size; |
| 334 | |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 335 | #define BASE_ARGS \ |
| 336 | AOS_LOGGING_BASE_ARGS( \ |
| 337 | msg->name_size, reinterpret_cast<const char *>(msg + 1), msg->source, \ |
| 338 | msg->sequence, msg->level, msg->time_sec, msg->time_nsec) |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 339 | switch (msg->type) { |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 340 | case LogFileMessageHeader::MessageType::kString: |
| 341 | fprintf(stdout, AOS_LOGGING_BASE_FORMAT "%.*s", BASE_ARGS, |
| 342 | static_cast<int>(msg->message_size), position); |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 343 | break; |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 344 | case LogFileMessageHeader::MessageType::kStruct: { |
| 345 | uint32_t type_id; |
| 346 | memcpy(&type_id, position, sizeof(type_id)); |
| 347 | position += sizeof(type_id); |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 348 | |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 349 | uint32_t string_length; |
| 350 | memcpy(&string_length, position, sizeof(string_length)); |
| 351 | position += sizeof(string_length); |
| 352 | |
Austin Schuh | eee8c96 | 2017-04-12 22:32:44 -0700 | [diff] [blame] | 353 | char buffer[4096]; |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 354 | size_t output_length = sizeof(buffer); |
| 355 | size_t input_length = |
| 356 | msg->message_size - |
| 357 | (sizeof(type_id) + sizeof(uint32_t) + string_length); |
| 358 | if (!PrintMessage(buffer, &output_length, position + string_length, |
| 359 | &input_length, ::aos::type_cache::Get(type_id))) { |
| 360 | LOG(FATAL, "printing message (%.*s) of type %s into %zu-byte buffer " |
| 361 | "failed\n", |
| 362 | static_cast<int>(string_length), position, |
| 363 | ::aos::type_cache::Get(type_id).name.c_str(), sizeof(buffer)); |
| 364 | } |
| 365 | if (input_length > 0) { |
| 366 | LOG(WARNING, "%zu extra bytes on message of type %s\n", |
| 367 | input_length, ::aos::type_cache::Get(type_id).name.c_str()); |
| 368 | } |
| 369 | fprintf(stdout, AOS_LOGGING_BASE_FORMAT "%.*s: %.*s\n", BASE_ARGS, |
| 370 | static_cast<int>(string_length), position, |
| 371 | static_cast<int>(sizeof(buffer) - output_length), buffer); |
| 372 | } break; |
| 373 | case LogFileMessageHeader::MessageType::kMatrix: { |
| 374 | uint32_t type; |
| 375 | memcpy(&type, position, sizeof(type)); |
| 376 | position += sizeof(type); |
| 377 | |
| 378 | uint32_t string_length; |
| 379 | memcpy(&string_length, position, sizeof(string_length)); |
| 380 | position += sizeof(string_length); |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 381 | |
| 382 | uint16_t rows; |
| 383 | memcpy(&rows, position, sizeof(rows)); |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 384 | position += sizeof(rows); |
| 385 | uint16_t cols; |
| 386 | memcpy(&cols, position, sizeof(cols)); |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 387 | position += sizeof(cols); |
| 388 | |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 389 | const size_t matrix_bytes = |
| 390 | msg->message_size - |
| 391 | (sizeof(type) + sizeof(uint32_t) + sizeof(uint16_t) + |
| 392 | sizeof(uint16_t) + string_length); |
| 393 | CHECK_EQ(matrix_bytes, ::aos::MessageType::Sizeof(type) * rows * cols); |
Austin Schuh | eee8c96 | 2017-04-12 22:32:44 -0700 | [diff] [blame] | 394 | char buffer[4096]; |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 395 | size_t output_length = sizeof(buffer); |
| 396 | if (!::aos::PrintMatrix(buffer, &output_length, |
| 397 | position + string_length, type, rows, cols)) { |
| 398 | LOG(FATAL, "printing %dx%d matrix of type %" PRIu32 " failed\n", rows, |
| 399 | cols, type); |
| 400 | } |
| 401 | fprintf(stdout, AOS_LOGGING_BASE_FORMAT "%.*s: %.*s\n", BASE_ARGS, |
| 402 | static_cast<int>(string_length), position, |
| 403 | static_cast<int>(sizeof(buffer) - output_length), buffer); |
| 404 | } break; |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 405 | case LogFileMessageHeader::MessageType::kStructType: |
| 406 | LOG(FATAL, "shouldn't get here\n"); |
| 407 | break; |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 408 | } |
| 409 | #undef BASE_ARGS |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 410 | } while (msg != NULL); |
| 411 | } |