brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #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> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 8 | |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
Brian Silverman | fe9b7a2 | 2014-02-10 15:03:42 -0800 | [diff] [blame] | 11 | #include "aos/linux_code/logging/binary_log_file.h" |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 12 | #include "aos/common/queue_types.h" |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 13 | #include "aos/common/logging/logging_impl.h" |
| 14 | #include "aos/common/logging/logging_printf_formats.h" |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 15 | |
| 16 | using ::aos::logging::linux_code::LogFileMessageHeader; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 17 | |
| 18 | namespace { |
| 19 | |
| 20 | const char *kArgsHelp = "[OPTION]... [FILE]\n" |
| 21 | "Display log file FILE (created by BinaryLogReader) to stdout.\n" |
| 22 | "FILE is \"aos_log-current\" by default.\n" |
| 23 | "\n" |
| 24 | " -n, --name NAME only display entries from processes named NAME\n" |
| 25 | " -l, --level LEVEL " |
| 26 | "only display log entries at least as important as LEVEL\n" |
| 27 | " // -p, --pid PID only display log entries from process PID\n" |
| 28 | " -f, --follow " |
| 29 | "wait when the end of the file is reached (implies --end)\n" |
| 30 | " -t, --terminate stop when the end of file is reached (default)\n" |
| 31 | " -b, --beginning start at the beginning of the file (default)\n" |
| 32 | " -e, --end start at the end of the file\n" |
| 33 | " -s, --skip NUMBER skip NUMBER matching logs\n" |
| 34 | " // -m, --max NUMBER only display up to NUMBER logs\n" |
| 35 | " // -o, --format FORMAT use FORMAT to display log entries\n" |
Brian Silverman | a52ba16 | 2013-02-28 15:01:12 -0800 | [diff] [blame] | 36 | " -h, --help display this help and exit\n" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 37 | "\n" |
| 38 | "LEVEL must be DEBUG, INFO, WARNING, ERROR, or FATAL.\n" |
| 39 | " It defaults to INFO.\n" |
| 40 | "\n" |
| 41 | "TODO(brians) implement the commented out ones and changing FILE\n"; |
| 42 | |
| 43 | void PrintHelpAndExit() { |
| 44 | fprintf(stderr, "Usage: %s %s", program_invocation_name, kArgsHelp); |
| 45 | |
| 46 | exit(EXIT_SUCCESS); |
| 47 | } |
| 48 | |
| 49 | } // namespace |
| 50 | |
| 51 | int main(int argc, char **argv) { |
| 52 | const char *filter_name = NULL; |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 53 | size_t filter_length = 0; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 54 | log_level filter_level = INFO; |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 55 | bool follow = false; |
| 56 | // Whether we need to skip everything until we get to the end of the file. |
| 57 | bool skip_to_end = false; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 58 | const char *filename = "aos_log-current"; |
| 59 | |
Brian Silverman | ff48578 | 2014-06-18 19:59:09 -0700 | [diff] [blame^] | 60 | ::aos::logging::Init(); |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 61 | ::aos::logging::AddImplementation( |
| 62 | new ::aos::logging::StreamLogImplementation(stdout)); |
| 63 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 64 | 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 Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 95 | filter_length = strlen(filter_name); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 96 | break; |
| 97 | case 'l': |
Brian Silverman | ab6615c | 2013-03-05 20:29:29 -0800 | [diff] [blame] | 98 | filter_level = ::aos::logging::str_log(optarg); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 99 | 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 Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 109 | skip_to_end = true; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 110 | break; |
| 111 | case 't': |
| 112 | follow = false; |
| 113 | break; |
| 114 | case 'b': |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 115 | skip_to_end = false; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 116 | break; |
| 117 | case 'e': |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 118 | skip_to_end = true; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 119 | 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 Silverman | ab6615c | 2013-03-05 20:29:29 -0800 | [diff] [blame] | 132 | fprintf(stderr, "LogDisplayer: in a bad spot (%s: %d)\n", |
| 133 | __FILE__, __LINE__); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 134 | abort(); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | fprintf(stderr, "displaying down to level %s from file '%s'\n", |
Brian Silverman | ab6615c | 2013-03-05 20:29:29 -0800 | [diff] [blame] | 139 | ::aos::logging::log_str(filter_level), filename); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 140 | 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) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 149 | PLOG(FATAL, "couldn't open file '%s' for reading", filename); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 150 | } |
Brian Silverman | ab5ba47 | 2014-04-18 15:26:14 -0700 | [diff] [blame] | 151 | ::aos::logging::linux_code::LogFileReader reader(fd); |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 152 | |
| 153 | if (skip_to_end) { |
| 154 | fputs("skipping old logs...\n", stderr); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 155 | } |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 156 | |
| 157 | const LogFileMessageHeader *msg; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 158 | do { |
Brian Silverman | ab5ba47 | 2014-04-18 15:26:14 -0700 | [diff] [blame] | 159 | msg = reader.ReadNextMessage(follow); |
Brian Silverman | 003ba4b | 2014-02-10 16:56:18 -0800 | [diff] [blame] | 160 | if (msg == NULL) { |
| 161 | fputs("reached end of file\n", stderr); |
| 162 | return 0; |
| 163 | } |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 164 | |
| 165 | if (msg->type == LogFileMessageHeader::MessageType::kStructType) { |
| 166 | size_t bytes = msg->message_size; |
| 167 | ::aos::MessageType *type = ::aos::MessageType::Deserialize( |
| 168 | reinterpret_cast<const char *>(msg + 1), &bytes); |
| 169 | ::aos::type_cache::Add(*type); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 170 | continue; |
| 171 | } |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 172 | |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 173 | if (skip_to_end) { |
Brian Silverman | ab5ba47 | 2014-04-18 15:26:14 -0700 | [diff] [blame] | 174 | if (reader.IsLastPage()) { |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 175 | fputs("done skipping old logs\n", stderr); |
| 176 | skip_to_end = false; |
| 177 | } else { |
| 178 | continue; |
| 179 | } |
| 180 | } |
| 181 | |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 182 | if (::aos::logging::log_gt_important(filter_level, msg->level)) continue; |
| 183 | if (filter_name != NULL) { |
| 184 | if (filter_length != msg->name_size) continue; |
| 185 | if (memcmp(filter_name, |
| 186 | reinterpret_cast<const char *>(msg) + sizeof(*msg), |
| 187 | filter_length) != |
| 188 | 0) { |
| 189 | continue; |
| 190 | } |
| 191 | } |
| 192 | |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 193 | const char *position = |
| 194 | reinterpret_cast<const char *>(msg + 1) + msg->name_size; |
| 195 | #define BASE_ARGS \ |
| 196 | AOS_LOGGING_BASE_ARGS( \ |
| 197 | msg->name_size, reinterpret_cast<const char *>(msg + 1), msg->source, \ |
| 198 | msg->sequence, msg->level, msg->time_sec, msg->time_nsec) |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 199 | switch (msg->type) { |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 200 | case LogFileMessageHeader::MessageType::kString: |
| 201 | fprintf(stdout, AOS_LOGGING_BASE_FORMAT "%.*s", BASE_ARGS, |
| 202 | static_cast<int>(msg->message_size), position); |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 203 | break; |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 204 | case LogFileMessageHeader::MessageType::kStruct: { |
| 205 | uint32_t type_id; |
| 206 | memcpy(&type_id, position, sizeof(type_id)); |
| 207 | position += sizeof(type_id); |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 208 | |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 209 | uint32_t string_length; |
| 210 | memcpy(&string_length, position, sizeof(string_length)); |
| 211 | position += sizeof(string_length); |
| 212 | |
| 213 | char buffer[2048]; |
| 214 | size_t output_length = sizeof(buffer); |
| 215 | size_t input_length = |
| 216 | msg->message_size - |
| 217 | (sizeof(type_id) + sizeof(uint32_t) + string_length); |
| 218 | if (!PrintMessage(buffer, &output_length, position + string_length, |
| 219 | &input_length, ::aos::type_cache::Get(type_id))) { |
| 220 | LOG(FATAL, "printing message (%.*s) of type %s into %zu-byte buffer " |
| 221 | "failed\n", |
| 222 | static_cast<int>(string_length), position, |
| 223 | ::aos::type_cache::Get(type_id).name.c_str(), sizeof(buffer)); |
| 224 | } |
| 225 | if (input_length > 0) { |
| 226 | LOG(WARNING, "%zu extra bytes on message of type %s\n", |
| 227 | input_length, ::aos::type_cache::Get(type_id).name.c_str()); |
| 228 | } |
| 229 | fprintf(stdout, AOS_LOGGING_BASE_FORMAT "%.*s: %.*s\n", BASE_ARGS, |
| 230 | static_cast<int>(string_length), position, |
| 231 | static_cast<int>(sizeof(buffer) - output_length), buffer); |
| 232 | } break; |
| 233 | case LogFileMessageHeader::MessageType::kMatrix: { |
| 234 | uint32_t type; |
| 235 | memcpy(&type, position, sizeof(type)); |
| 236 | position += sizeof(type); |
| 237 | |
| 238 | uint32_t string_length; |
| 239 | memcpy(&string_length, position, sizeof(string_length)); |
| 240 | position += sizeof(string_length); |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 241 | |
| 242 | uint16_t rows; |
| 243 | memcpy(&rows, position, sizeof(rows)); |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 244 | position += sizeof(rows); |
| 245 | uint16_t cols; |
| 246 | memcpy(&cols, position, sizeof(cols)); |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 247 | position += sizeof(cols); |
| 248 | |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 249 | const size_t matrix_bytes = |
| 250 | msg->message_size - |
| 251 | (sizeof(type) + sizeof(uint32_t) + sizeof(uint16_t) + |
| 252 | sizeof(uint16_t) + string_length); |
| 253 | CHECK_EQ(matrix_bytes, ::aos::MessageType::Sizeof(type) * rows * cols); |
| 254 | char buffer[2048]; |
| 255 | size_t output_length = sizeof(buffer); |
| 256 | if (!::aos::PrintMatrix(buffer, &output_length, |
| 257 | position + string_length, type, rows, cols)) { |
| 258 | LOG(FATAL, "printing %dx%d matrix of type %" PRIu32 " failed\n", rows, |
| 259 | cols, type); |
| 260 | } |
| 261 | fprintf(stdout, AOS_LOGGING_BASE_FORMAT "%.*s: %.*s\n", BASE_ARGS, |
| 262 | static_cast<int>(string_length), position, |
| 263 | static_cast<int>(sizeof(buffer) - output_length), buffer); |
| 264 | } break; |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 265 | case LogFileMessageHeader::MessageType::kStructType: |
| 266 | LOG(FATAL, "shouldn't get here\n"); |
| 267 | break; |
Brian Silverman | a7234c6 | 2014-03-24 20:23:25 -0700 | [diff] [blame] | 268 | } |
| 269 | #undef BASE_ARGS |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 270 | } while (msg != NULL); |
| 271 | } |