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