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> |
| 8 | #include <errno.h> |
| 9 | |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 10 | #include <algorithm> |
| 11 | |
Brian Silverman | fe9b7a2 | 2014-02-10 15:03:42 -0800 | [diff] [blame] | 12 | #include "aos/linux_code/logging/binary_log_file.h" |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 13 | #include "aos/common/logging/logging_impl.h" |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 14 | #include "aos/common/queue_types.h" |
| 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 | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 60 | ::aos::logging::AddImplementation( |
| 61 | new ::aos::logging::StreamLogImplementation(stdout)); |
| 62 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 63 | while (true) { |
| 64 | static struct option long_options[] = { |
| 65 | {"name", required_argument, NULL, 'n'}, |
| 66 | {"level", required_argument, NULL, 'l'}, |
| 67 | {"pid", required_argument, NULL, 'p'}, |
| 68 | |
| 69 | {"follow", no_argument, NULL, 'f'}, |
| 70 | {"terminate", no_argument, NULL, 't'}, |
| 71 | {"beginning", no_argument, NULL, 'b'}, |
| 72 | {"end", no_argument, NULL, 'e'}, |
| 73 | {"skip", required_argument, NULL, 's'}, |
| 74 | {"max", required_argument, NULL, 'm'}, |
| 75 | |
| 76 | {"format", required_argument, NULL, 'o'}, |
| 77 | |
| 78 | {"help", no_argument, NULL, 'h'}, |
| 79 | {0, 0, 0, 0} |
| 80 | }; |
| 81 | int option_index = 0; |
| 82 | |
| 83 | const int c = getopt_long(argc, argv, "n:l:p:fts:m:o:h", |
| 84 | long_options, &option_index); |
| 85 | if (c == -1) { // if we're at the end |
| 86 | break; |
| 87 | } |
| 88 | switch (c) { |
| 89 | case 0: |
| 90 | fprintf(stderr, "LogDisplayer: got a 0 option but didn't set up any\n"); |
| 91 | abort(); |
| 92 | case 'n': |
| 93 | filter_name = optarg; |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 94 | filter_length = strlen(filter_name); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 95 | break; |
| 96 | case 'l': |
Brian Silverman | ab6615c | 2013-03-05 20:29:29 -0800 | [diff] [blame] | 97 | filter_level = ::aos::logging::str_log(optarg); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 98 | if (filter_level == LOG_UNKNOWN) { |
| 99 | fprintf(stderr, "LogDisplayer: unknown log level '%s'\n", optarg); |
| 100 | exit(EXIT_FAILURE); |
| 101 | } |
| 102 | break; |
| 103 | case 'p': |
| 104 | abort(); |
| 105 | break; |
| 106 | case 'f': |
| 107 | follow = true; |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 108 | skip_to_end = true; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 109 | break; |
| 110 | case 't': |
| 111 | follow = false; |
| 112 | break; |
| 113 | case 'b': |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 114 | skip_to_end = false; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 115 | break; |
| 116 | case 'e': |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 117 | skip_to_end = true; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 118 | break; |
| 119 | case 'm': |
| 120 | abort(); |
| 121 | break; |
| 122 | case 'o': |
| 123 | abort(); |
| 124 | break; |
| 125 | case 'h': |
| 126 | PrintHelpAndExit(); |
| 127 | break; |
| 128 | case '?': |
| 129 | break; |
| 130 | default: |
Brian Silverman | ab6615c | 2013-03-05 20:29:29 -0800 | [diff] [blame] | 131 | fprintf(stderr, "LogDisplayer: in a bad spot (%s: %d)\n", |
| 132 | __FILE__, __LINE__); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 133 | abort(); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | fprintf(stderr, "displaying down to level %s from file '%s'\n", |
Brian Silverman | ab6615c | 2013-03-05 20:29:29 -0800 | [diff] [blame] | 138 | ::aos::logging::log_str(filter_level), filename); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 139 | if (optind < argc) { |
| 140 | fprintf(stderr, "non-option ARGV-elements: "); |
| 141 | while (optind < argc) { |
| 142 | fprintf(stderr, "%s\n", argv[optind++]); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | int fd = open(filename, O_RDONLY); |
| 147 | if (fd == -1) { |
| 148 | fprintf(stderr, "error: couldn't open file '%s' for reading because of %s\n", |
| 149 | filename, strerror(errno)); |
| 150 | exit(EXIT_FAILURE); |
| 151 | } |
Brian Silverman | 003ba4b | 2014-02-10 16:56:18 -0800 | [diff] [blame] | 152 | ::aos::logging::linux_code::LogFileAccessor accessor(fd, false); |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 153 | |
| 154 | if (skip_to_end) { |
| 155 | fputs("skipping old logs...\n", stderr); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 156 | } |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 157 | |
| 158 | const LogFileMessageHeader *msg; |
Brian Silverman | ab6615c | 2013-03-05 20:29:29 -0800 | [diff] [blame] | 159 | ::aos::logging::LogMessage log_message; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 160 | do { |
| 161 | msg = accessor.ReadNextMessage(follow); |
Brian Silverman | 003ba4b | 2014-02-10 16:56:18 -0800 | [diff] [blame] | 162 | if (msg == NULL) { |
| 163 | fputs("reached end of file\n", stderr); |
| 164 | return 0; |
| 165 | } |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 166 | |
| 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); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 172 | continue; |
| 173 | } |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 174 | |
Brian Silverman | f778031 | 2014-02-16 17:26:15 -0800 | [diff] [blame] | 175 | 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 Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 184 | 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 Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 195 | log_message.source = msg->source; |
| 196 | log_message.sequence = msg->sequence; |
| 197 | log_message.level = msg->level; |
| 198 | log_message.seconds = msg->time_sec; |
| 199 | log_message.nseconds = msg->time_nsec; |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 200 | memcpy(log_message.name, reinterpret_cast<const char *>(msg) + sizeof(*msg), |
| 201 | ::std::min<size_t>(sizeof(log_message.name), msg->name_size)); |
| 202 | log_message.message_length = msg->message_size; |
| 203 | log_message.name_length = msg->name_size; |
| 204 | |
| 205 | switch (msg->type) { |
| 206 | case LogFileMessageHeader::MessageType::kStruct: { |
| 207 | const char *position = |
| 208 | reinterpret_cast<const char *>(msg + 1) + msg->name_size; |
| 209 | memcpy(&log_message.structure.type_id, position, |
| 210 | sizeof(log_message.structure.type_id)); |
| 211 | position += sizeof(log_message.structure.type_id); |
| 212 | |
| 213 | uint32_t length; |
| 214 | memcpy(&length, position, sizeof(length)); |
| 215 | log_message.structure.string_length = length; |
| 216 | position += sizeof(length); |
| 217 | memcpy(log_message.structure.serialized, position, length); |
| 218 | position += length; |
| 219 | |
| 220 | log_message.message_length -= |
| 221 | sizeof(log_message.structure.type_id) + sizeof(uint32_t) + |
| 222 | log_message.structure.string_length; |
| 223 | memcpy(log_message.structure.serialized + |
| 224 | log_message.structure.string_length, |
| 225 | position, log_message.message_length); |
| 226 | |
| 227 | log_message.type = ::aos::logging::LogMessage::Type::kStruct; |
| 228 | break; |
| 229 | } |
Brian Silverman | 664db1a | 2014-03-20 17:06:29 -0700 | [diff] [blame] | 230 | case LogFileMessageHeader::MessageType::kMatrix: { |
| 231 | const char *position = |
| 232 | reinterpret_cast<const char *>(msg + 1) + msg->name_size; |
| 233 | memcpy(&log_message.matrix.type, position, |
| 234 | sizeof(log_message.matrix.type)); |
| 235 | position += sizeof(log_message.matrix.type); |
| 236 | |
| 237 | uint32_t length; |
| 238 | memcpy(&length, position, sizeof(length)); |
| 239 | log_message.matrix.string_length = length; |
| 240 | position += sizeof(length); |
| 241 | |
| 242 | uint16_t rows; |
| 243 | memcpy(&rows, position, sizeof(rows)); |
| 244 | log_message.matrix.rows = rows; |
| 245 | position += sizeof(rows); |
| 246 | uint16_t cols; |
| 247 | memcpy(&cols, position, sizeof(cols)); |
| 248 | log_message.matrix.cols = cols; |
| 249 | position += sizeof(cols); |
| 250 | |
| 251 | log_message.message_length -= |
| 252 | sizeof(log_message.matrix.type) + sizeof(uint32_t) + |
| 253 | sizeof(uint16_t) + sizeof(uint16_t) + length; |
| 254 | CHECK_EQ( |
| 255 | log_message.message_length, |
| 256 | ::aos::MessageType::Sizeof(log_message.matrix.type) * rows * cols); |
| 257 | memcpy(log_message.matrix.data, position, |
| 258 | log_message.message_length + length); |
| 259 | |
| 260 | log_message.type = ::aos::logging::LogMessage::Type::kMatrix; |
| 261 | break; |
| 262 | } case LogFileMessageHeader::MessageType::kString: |
Brian Silverman | 88471dc | 2014-02-15 22:35:42 -0800 | [diff] [blame] | 263 | memcpy( |
| 264 | log_message.message, |
| 265 | reinterpret_cast<const char *>(msg) + sizeof(*msg) + msg->name_size, |
| 266 | ::std::min<size_t>(sizeof(log_message.message), msg->message_size)); |
| 267 | log_message.type = ::aos::logging::LogMessage::Type::kString; |
| 268 | break; |
| 269 | case LogFileMessageHeader::MessageType::kStructType: |
| 270 | LOG(FATAL, "shouldn't get here\n"); |
| 271 | break; |
| 272 | }; |
Brian Silverman | ab6615c | 2013-03-05 20:29:29 -0800 | [diff] [blame] | 273 | ::aos::logging::internal::PrintMessage(stdout, log_message); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 274 | } while (msg != NULL); |
| 275 | } |