Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 1 | // Copyright 2006-2014 The Android Open Source Project |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 2 | |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 3 | #include <assert.h> |
| 4 | #include <ctype.h> |
| 5 | #include <errno.h> |
| 6 | #include <fcntl.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <stdarg.h> |
| 10 | #include <string.h> |
| 11 | #include <signal.h> |
| 12 | #include <time.h> |
| 13 | #include <unistd.h> |
| 14 | #include <sys/socket.h> |
| 15 | #include <sys/stat.h> |
| 16 | #include <arpa/inet.h> |
| 17 | |
| 18 | #include <cutils/sockets.h> |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 19 | #include <log/log.h> |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 20 | #include <log/log_read.h> |
Colin Cross | 9227bd3 | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 21 | #include <log/logger.h> |
| 22 | #include <log/logd.h> |
| 23 | #include <log/logprint.h> |
| 24 | #include <log/event_tag_map.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | |
| 26 | #define DEFAULT_LOG_ROTATE_SIZE_KBYTES 16 |
| 27 | #define DEFAULT_MAX_ROTATED_LOGS 4 |
| 28 | |
| 29 | static AndroidLogFormat * g_logformat; |
| 30 | |
| 31 | /* logd prefixes records with a length field */ |
| 32 | #define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t) |
| 33 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 34 | struct log_device_t { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 35 | const char* device; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 36 | bool binary; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 37 | struct logger *logger; |
| 38 | struct logger_list *logger_list; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 39 | bool printed; |
| 40 | char label; |
| 41 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 42 | log_device_t* next; |
| 43 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 44 | log_device_t(const char* d, bool b, char l) { |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 45 | device = d; |
| 46 | binary = b; |
| 47 | label = l; |
| 48 | next = NULL; |
| 49 | printed = false; |
| 50 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 51 | }; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 52 | |
| 53 | namespace android { |
| 54 | |
| 55 | /* Global Variables */ |
| 56 | |
| 57 | static const char * g_outputFileName = NULL; |
| 58 | static int g_logRotateSizeKBytes = 0; // 0 means "no log rotation" |
| 59 | static int g_maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS; // 0 means "unbounded" |
| 60 | static int g_outFD = -1; |
| 61 | static off_t g_outByteCount = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 62 | static int g_printBinary = 0; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 63 | static int g_devCount = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 64 | |
| 65 | static EventTagMap* g_eventTagMap = NULL; |
| 66 | |
| 67 | static int openLogFile (const char *pathname) |
| 68 | { |
Edwin Vane | 80b221c | 2012-08-13 12:55:07 -0400 | [diff] [blame] | 69 | return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static void rotateLogs() |
| 73 | { |
| 74 | int err; |
| 75 | |
| 76 | // Can't rotate logs if we're not outputting to a file |
| 77 | if (g_outputFileName == NULL) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | close(g_outFD); |
| 82 | |
| 83 | for (int i = g_maxRotatedLogs ; i > 0 ; i--) { |
| 84 | char *file0, *file1; |
| 85 | |
| 86 | asprintf(&file1, "%s.%d", g_outputFileName, i); |
| 87 | |
| 88 | if (i - 1 == 0) { |
| 89 | asprintf(&file0, "%s", g_outputFileName); |
| 90 | } else { |
| 91 | asprintf(&file0, "%s.%d", g_outputFileName, i - 1); |
| 92 | } |
| 93 | |
| 94 | err = rename (file0, file1); |
| 95 | |
| 96 | if (err < 0 && errno != ENOENT) { |
| 97 | perror("while rotating log files"); |
| 98 | } |
| 99 | |
| 100 | free(file1); |
| 101 | free(file0); |
| 102 | } |
| 103 | |
| 104 | g_outFD = openLogFile (g_outputFileName); |
| 105 | |
| 106 | if (g_outFD < 0) { |
| 107 | perror ("couldn't open output file"); |
| 108 | exit(-1); |
| 109 | } |
| 110 | |
| 111 | g_outByteCount = 0; |
| 112 | |
| 113 | } |
| 114 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 115 | void printBinary(struct log_msg *buf) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 116 | { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 117 | size_t size = buf->len(); |
| 118 | |
| 119 | TEMP_FAILURE_RETRY(write(g_outFD, buf, size)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 122 | static void processBuffer(log_device_t* dev, struct log_msg *buf) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 123 | { |
Mathias Agopian | 5084452 | 2010-03-17 16:10:26 -0700 | [diff] [blame] | 124 | int bytesWritten = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 125 | int err; |
| 126 | AndroidLogEntry entry; |
| 127 | char binaryMsgBuf[1024]; |
| 128 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 129 | if (dev->binary) { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 130 | err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry, |
| 131 | g_eventTagMap, |
| 132 | binaryMsgBuf, |
| 133 | sizeof(binaryMsgBuf)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 134 | //printf(">>> pri=%d len=%d msg='%s'\n", |
| 135 | // entry.priority, entry.messageLen, entry.message); |
| 136 | } else { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 137 | err = android_log_processLogBuffer(&buf->entry_v1, &entry); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 138 | } |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 139 | if (err < 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 140 | goto error; |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 141 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 142 | |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 143 | if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) { |
| 144 | if (false && g_devCount > 1) { |
| 145 | binaryMsgBuf[0] = dev->label; |
| 146 | binaryMsgBuf[1] = ' '; |
| 147 | bytesWritten = write(g_outFD, binaryMsgBuf, 2); |
| 148 | if (bytesWritten < 0) { |
| 149 | perror("output error"); |
| 150 | exit(-1); |
| 151 | } |
| 152 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 153 | |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 154 | bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry); |
| 155 | |
| 156 | if (bytesWritten < 0) { |
| 157 | perror("output error"); |
| 158 | exit(-1); |
| 159 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | g_outByteCount += bytesWritten; |
| 163 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 164 | if (g_logRotateSizeKBytes > 0 |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 165 | && (g_outByteCount / 1024) >= g_logRotateSizeKBytes |
| 166 | ) { |
| 167 | rotateLogs(); |
| 168 | } |
| 169 | |
| 170 | error: |
| 171 | //fprintf (stderr, "Error processing record\n"); |
| 172 | return; |
| 173 | } |
| 174 | |
Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 175 | static void maybePrintStart(log_device_t* dev) { |
| 176 | if (!dev->printed) { |
| 177 | dev->printed = true; |
| 178 | if (g_devCount > 1 && !g_printBinary) { |
| 179 | char buf[1024]; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 180 | snprintf(buf, sizeof(buf), "--------- beginning of %s\n", |
| 181 | dev->device); |
Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 182 | if (write(g_outFD, buf, strlen(buf)) < 0) { |
| 183 | perror("output error"); |
| 184 | exit(-1); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 190 | static void setupOutput() |
| 191 | { |
| 192 | |
| 193 | if (g_outputFileName == NULL) { |
| 194 | g_outFD = STDOUT_FILENO; |
| 195 | |
| 196 | } else { |
| 197 | struct stat statbuf; |
| 198 | |
| 199 | g_outFD = openLogFile (g_outputFileName); |
| 200 | |
| 201 | if (g_outFD < 0) { |
| 202 | perror ("couldn't open output file"); |
| 203 | exit(-1); |
| 204 | } |
| 205 | |
| 206 | fstat(g_outFD, &statbuf); |
| 207 | |
| 208 | g_outByteCount = statbuf.st_size; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | static void show_help(const char *cmd) |
| 213 | { |
| 214 | fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd); |
| 215 | |
| 216 | fprintf(stderr, "options include:\n" |
| 217 | " -s Set default filter to silent.\n" |
| 218 | " Like specifying filterspec '*:s'\n" |
| 219 | " -f <filename> Log to file. Default to stdout\n" |
| 220 | " -r [<kbytes>] Rotate log every kbytes. (16 if unspecified). Requires -f\n" |
| 221 | " -n <count> Sets max number of rotated logs to <count>, default 4\n" |
| 222 | " -v <format> Sets the log print format, where <format> is one of:\n\n" |
| 223 | " brief process tag thread raw time threadtime long\n\n" |
| 224 | " -c clear (flush) the entire log and exit\n" |
| 225 | " -d dump the log and then exit (don't block)\n" |
Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 226 | " -t <count> print only the most recent <count> lines (implies -d)\n" |
Mark Salyzyn | 5d3d1f1 | 2013-12-09 13:47:00 -0800 | [diff] [blame] | 227 | " -T <count> print only the most recent <count> lines (does not imply -d)\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 228 | " -g get the size of the log's ring buffer and exit\n" |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 229 | " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n" |
Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 230 | " 'events', 'crash' or 'all'. Multiple -b parameters are\n" |
| 231 | " allowed and results are interleaved. The default is\n" |
| 232 | " -b main -b system -b crash.\n" |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 233 | " -B output the log in binary.\n" |
Mark Salyzyn | bbbe14f | 2014-04-11 13:49:43 -0700 | [diff] [blame] | 234 | " -S output statistics.\n" |
| 235 | " -G <size> set size of log ring buffer, may suffix with K or M.\n" |
| 236 | " -p print prune white and ~black list. Service is specified as\n" |
| 237 | " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n" |
| 238 | " with ~, otherwise weighed for longevity if unadorned. All\n" |
| 239 | " other pruning activity is oldest first. Special case ~!\n" |
| 240 | " represents an automatic quicker pruning for the noisiest\n" |
| 241 | " UID as determined by the current statistics.\n" |
| 242 | " -P '<list> ...' set prune white and ~black list, using same format as\n" |
| 243 | " printed above. Must be quoted.\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 244 | |
| 245 | fprintf(stderr,"\nfilterspecs are a series of \n" |
| 246 | " <tag>[:priority]\n\n" |
| 247 | "where <tag> is a log component tag (or * for all) and priority is:\n" |
| 248 | " V Verbose\n" |
| 249 | " D Debug\n" |
| 250 | " I Info\n" |
| 251 | " W Warn\n" |
| 252 | " E Error\n" |
| 253 | " F Fatal\n" |
| 254 | " S Silent (supress all output)\n" |
| 255 | "\n'*' means '*:d' and <tag> by itself means <tag>:v\n" |
| 256 | "\nIf not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.\n" |
| 257 | "If no filterspec is found, filter defaults to '*:I'\n" |
| 258 | "\nIf not specified with -v, format is set from ANDROID_PRINTF_LOG\n" |
| 259 | "or defaults to \"brief\"\n\n"); |
| 260 | |
| 261 | |
| 262 | |
| 263 | } |
| 264 | |
| 265 | |
| 266 | } /* namespace android */ |
| 267 | |
| 268 | static int setLogFormat(const char * formatString) |
| 269 | { |
| 270 | static AndroidLogPrintFormat format; |
| 271 | |
| 272 | format = android_log_formatFromString(formatString); |
| 273 | |
| 274 | if (format == FORMAT_OFF) { |
| 275 | // FORMAT_OFF means invalid string |
| 276 | return -1; |
| 277 | } |
| 278 | |
| 279 | android_log_setPrintFormat(g_logformat, format); |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 284 | static const char multipliers[][2] = { |
| 285 | { "" }, |
| 286 | { "K" }, |
| 287 | { "M" }, |
| 288 | { "G" } |
| 289 | }; |
| 290 | |
| 291 | static unsigned long value_of_size(unsigned long value) |
| 292 | { |
| 293 | for (unsigned i = 0; |
| 294 | (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024); |
| 295 | value /= 1024, ++i) ; |
| 296 | return value; |
| 297 | } |
| 298 | |
| 299 | static const char *multiplier_of_size(unsigned long value) |
| 300 | { |
| 301 | unsigned i; |
| 302 | for (i = 0; |
| 303 | (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024); |
| 304 | value /= 1024, ++i) ; |
| 305 | return multipliers[i]; |
| 306 | } |
| 307 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 308 | int main(int argc, char **argv) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 309 | { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 310 | int err; |
| 311 | int hasSetLogFormat = 0; |
| 312 | int clearLog = 0; |
| 313 | int getLogSize = 0; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 314 | unsigned long setLogSize = 0; |
| 315 | int getPruneList = 0; |
| 316 | char *setPruneList = NULL; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 317 | int printStatistics = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 318 | int mode = O_RDONLY; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 319 | const char *forceFilters = NULL; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 320 | log_device_t* devices = NULL; |
| 321 | log_device_t* dev; |
| 322 | bool needBinary = false; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 323 | struct logger_list *logger_list; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 324 | unsigned int tail_lines = 0; |
| 325 | log_time tail_time(log_time::EPOCH); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 326 | |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 327 | signal(SIGPIPE, exit); |
| 328 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 329 | g_logformat = android_log_format_new(); |
| 330 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 331 | if (argc == 2 && 0 == strcmp(argv[1], "--help")) { |
| 332 | android::show_help(argv[0]); |
| 333 | exit(0); |
| 334 | } |
| 335 | |
| 336 | for (;;) { |
| 337 | int ret; |
| 338 | |
Mark Salyzyn | 0b2dac4 | 2014-07-07 08:55:53 -0700 | [diff] [blame^] | 339 | ret = getopt(argc, argv, "cdt:T:gG:sQf:r:n:v:b:BSpP:"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 340 | |
| 341 | if (ret < 0) { |
| 342 | break; |
| 343 | } |
| 344 | |
| 345 | switch(ret) { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 346 | case 's': |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 347 | // default to all silent |
| 348 | android_log_addFilterRule(g_logformat, "*:s"); |
| 349 | break; |
| 350 | |
| 351 | case 'c': |
| 352 | clearLog = 1; |
| 353 | mode = O_WRONLY; |
| 354 | break; |
| 355 | |
| 356 | case 'd': |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 357 | mode = O_RDONLY | O_NDELAY; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 358 | break; |
| 359 | |
Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 360 | case 't': |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 361 | mode = O_RDONLY | O_NDELAY; |
Mark Salyzyn | 5d3d1f1 | 2013-12-09 13:47:00 -0800 | [diff] [blame] | 362 | /* FALLTHRU */ |
| 363 | case 'T': |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 364 | if (strspn(optarg, "0123456789") != strlen(optarg)) { |
| 365 | char *cp = tail_time.strptime(optarg, |
| 366 | log_time::default_format); |
| 367 | if (!cp) { |
| 368 | fprintf(stderr, |
| 369 | "ERROR: -%c \"%s\" not in \"%s\" time format\n", |
| 370 | ret, optarg, log_time::default_format); |
| 371 | exit(1); |
| 372 | } |
| 373 | if (*cp) { |
| 374 | char c = *cp; |
| 375 | *cp = '\0'; |
| 376 | fprintf(stderr, |
| 377 | "WARNING: -%c \"%s\"\"%c%s\" time truncated\n", |
| 378 | ret, optarg, c, cp + 1); |
| 379 | *cp = c; |
| 380 | } |
| 381 | } else { |
| 382 | tail_lines = atoi(optarg); |
| 383 | if (!tail_lines) { |
| 384 | fprintf(stderr, |
| 385 | "WARNING: -%c %s invalid, setting to 1\n", |
| 386 | ret, optarg); |
| 387 | tail_lines = 1; |
| 388 | } |
| 389 | } |
Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 390 | break; |
| 391 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 392 | case 'g': |
| 393 | getLogSize = 1; |
| 394 | break; |
| 395 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 396 | case 'G': { |
| 397 | // would use atol if not for the multiplier |
| 398 | char *cp = optarg; |
| 399 | setLogSize = 0; |
| 400 | while (('0' <= *cp) && (*cp <= '9')) { |
| 401 | setLogSize *= 10; |
| 402 | setLogSize += *cp - '0'; |
| 403 | ++cp; |
| 404 | } |
| 405 | |
| 406 | switch(*cp) { |
| 407 | case 'g': |
| 408 | case 'G': |
| 409 | setLogSize *= 1024; |
| 410 | /* FALLTHRU */ |
| 411 | case 'm': |
| 412 | case 'M': |
| 413 | setLogSize *= 1024; |
| 414 | /* FALLTHRU */ |
| 415 | case 'k': |
| 416 | case 'K': |
| 417 | setLogSize *= 1024; |
| 418 | /* FALLTHRU */ |
| 419 | case '\0': |
| 420 | break; |
| 421 | |
| 422 | default: |
| 423 | setLogSize = 0; |
| 424 | } |
| 425 | |
| 426 | if (!setLogSize) { |
| 427 | fprintf(stderr, "ERROR: -G <num><multiplier>\n"); |
| 428 | exit(1); |
| 429 | } |
| 430 | } |
| 431 | break; |
| 432 | |
| 433 | case 'p': |
| 434 | getPruneList = 1; |
| 435 | break; |
| 436 | |
| 437 | case 'P': |
| 438 | setPruneList = optarg; |
| 439 | break; |
| 440 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 441 | case 'b': { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 442 | if (strcmp(optarg, "all") == 0) { |
| 443 | while (devices) { |
| 444 | dev = devices; |
| 445 | devices = dev->next; |
| 446 | delete dev; |
| 447 | } |
| 448 | |
| 449 | dev = devices = new log_device_t("main", false, 'm'); |
| 450 | android::g_devCount = 1; |
| 451 | if (android_name_to_log_id("system") == LOG_ID_SYSTEM) { |
| 452 | dev->next = new log_device_t("system", false, 's'); |
| 453 | if (dev->next) { |
| 454 | dev = dev->next; |
| 455 | android::g_devCount++; |
| 456 | } |
| 457 | } |
| 458 | if (android_name_to_log_id("radio") == LOG_ID_RADIO) { |
| 459 | dev->next = new log_device_t("radio", false, 'r'); |
| 460 | if (dev->next) { |
| 461 | dev = dev->next; |
| 462 | android::g_devCount++; |
| 463 | } |
| 464 | } |
| 465 | if (android_name_to_log_id("events") == LOG_ID_EVENTS) { |
| 466 | dev->next = new log_device_t("events", true, 'e'); |
| 467 | if (dev->next) { |
Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 468 | dev = dev->next; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 469 | android::g_devCount++; |
| 470 | needBinary = true; |
| 471 | } |
| 472 | } |
Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 473 | if (android_name_to_log_id("crash") == LOG_ID_CRASH) { |
| 474 | dev->next = new log_device_t("crash", false, 'c'); |
| 475 | if (dev->next) { |
| 476 | android::g_devCount++; |
| 477 | } |
| 478 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 479 | break; |
| 480 | } |
| 481 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 482 | bool binary = strcmp(optarg, "events") == 0; |
| 483 | if (binary) { |
| 484 | needBinary = true; |
| 485 | } |
| 486 | |
| 487 | if (devices) { |
| 488 | dev = devices; |
| 489 | while (dev->next) { |
| 490 | dev = dev->next; |
| 491 | } |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 492 | dev->next = new log_device_t(optarg, binary, optarg[0]); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 493 | } else { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 494 | devices = new log_device_t(optarg, binary, optarg[0]); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 495 | } |
| 496 | android::g_devCount++; |
| 497 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 498 | break; |
| 499 | |
| 500 | case 'B': |
| 501 | android::g_printBinary = 1; |
| 502 | break; |
| 503 | |
| 504 | case 'f': |
| 505 | // redirect output to a file |
| 506 | |
| 507 | android::g_outputFileName = optarg; |
| 508 | |
| 509 | break; |
| 510 | |
| 511 | case 'r': |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 512 | if (optarg == NULL) { |
| 513 | android::g_logRotateSizeKBytes |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 514 | = DEFAULT_LOG_ROTATE_SIZE_KBYTES; |
| 515 | } else { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 516 | if (!isdigit(optarg[0])) { |
| 517 | fprintf(stderr,"Invalid parameter to -r\n"); |
| 518 | android::show_help(argv[0]); |
| 519 | exit(-1); |
| 520 | } |
| 521 | android::g_logRotateSizeKBytes = atoi(optarg); |
| 522 | } |
| 523 | break; |
| 524 | |
| 525 | case 'n': |
| 526 | if (!isdigit(optarg[0])) { |
| 527 | fprintf(stderr,"Invalid parameter to -r\n"); |
| 528 | android::show_help(argv[0]); |
| 529 | exit(-1); |
| 530 | } |
| 531 | |
| 532 | android::g_maxRotatedLogs = atoi(optarg); |
| 533 | break; |
| 534 | |
| 535 | case 'v': |
| 536 | err = setLogFormat (optarg); |
| 537 | if (err < 0) { |
| 538 | fprintf(stderr,"Invalid parameter to -v\n"); |
| 539 | android::show_help(argv[0]); |
| 540 | exit(-1); |
| 541 | } |
| 542 | |
| 543 | hasSetLogFormat = 1; |
| 544 | break; |
| 545 | |
| 546 | case 'Q': |
| 547 | /* this is a *hidden* option used to start a version of logcat */ |
| 548 | /* in an emulated device only. it basically looks for androidboot.logcat= */ |
| 549 | /* on the kernel command line. If something is found, it extracts a log filter */ |
| 550 | /* and uses it to run the program. If nothing is found, the program should */ |
| 551 | /* quit immediately */ |
| 552 | #define KERNEL_OPTION "androidboot.logcat=" |
| 553 | #define CONSOLE_OPTION "androidboot.console=" |
| 554 | { |
| 555 | int fd; |
| 556 | char* logcat; |
| 557 | char* console; |
| 558 | int force_exit = 1; |
| 559 | static char cmdline[1024]; |
| 560 | |
| 561 | fd = open("/proc/cmdline", O_RDONLY); |
| 562 | if (fd >= 0) { |
| 563 | int n = read(fd, cmdline, sizeof(cmdline)-1 ); |
| 564 | if (n < 0) n = 0; |
| 565 | cmdline[n] = 0; |
| 566 | close(fd); |
| 567 | } else { |
| 568 | cmdline[0] = 0; |
| 569 | } |
| 570 | |
| 571 | logcat = strstr( cmdline, KERNEL_OPTION ); |
| 572 | console = strstr( cmdline, CONSOLE_OPTION ); |
| 573 | if (logcat != NULL) { |
| 574 | char* p = logcat + sizeof(KERNEL_OPTION)-1;; |
| 575 | char* q = strpbrk( p, " \t\n\r" );; |
| 576 | |
| 577 | if (q != NULL) |
| 578 | *q = 0; |
| 579 | |
| 580 | forceFilters = p; |
| 581 | force_exit = 0; |
| 582 | } |
| 583 | /* if nothing found or invalid filters, exit quietly */ |
| 584 | if (force_exit) |
| 585 | exit(0); |
| 586 | |
| 587 | /* redirect our output to the emulator console */ |
| 588 | if (console) { |
| 589 | char* p = console + sizeof(CONSOLE_OPTION)-1; |
| 590 | char* q = strpbrk( p, " \t\n\r" ); |
| 591 | char devname[64]; |
| 592 | int len; |
| 593 | |
| 594 | if (q != NULL) { |
| 595 | len = q - p; |
| 596 | } else |
| 597 | len = strlen(p); |
| 598 | |
| 599 | len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p ); |
| 600 | fprintf(stderr, "logcat using %s (%d)\n", devname, len); |
| 601 | if (len < (int)sizeof(devname)) { |
| 602 | fd = open( devname, O_WRONLY ); |
| 603 | if (fd >= 0) { |
| 604 | dup2(fd, 1); |
| 605 | dup2(fd, 2); |
| 606 | close(fd); |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | break; |
| 612 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 613 | case 'S': |
| 614 | printStatistics = 1; |
| 615 | break; |
| 616 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 617 | default: |
| 618 | fprintf(stderr,"Unrecognized Option\n"); |
| 619 | android::show_help(argv[0]); |
| 620 | exit(-1); |
| 621 | break; |
| 622 | } |
| 623 | } |
| 624 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 625 | if (!devices) { |
Mark Salyzyn | 989980c | 2014-05-14 12:37:22 -0700 | [diff] [blame] | 626 | dev = devices = new log_device_t("main", false, 'm'); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 627 | android::g_devCount = 1; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 628 | if (android_name_to_log_id("system") == LOG_ID_SYSTEM) { |
Mark Salyzyn | 989980c | 2014-05-14 12:37:22 -0700 | [diff] [blame] | 629 | dev = dev->next = new log_device_t("system", false, 's'); |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 630 | android::g_devCount++; |
| 631 | } |
Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 632 | if (android_name_to_log_id("crash") == LOG_ID_CRASH) { |
Mark Salyzyn | 989980c | 2014-05-14 12:37:22 -0700 | [diff] [blame] | 633 | dev = dev->next = new log_device_t("crash", false, 'c'); |
Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 634 | android::g_devCount++; |
| 635 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 638 | if (android::g_logRotateSizeKBytes != 0 |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 639 | && android::g_outputFileName == NULL |
| 640 | ) { |
| 641 | fprintf(stderr,"-r requires -f as well\n"); |
| 642 | android::show_help(argv[0]); |
| 643 | exit(-1); |
| 644 | } |
| 645 | |
| 646 | android::setupOutput(); |
| 647 | |
| 648 | if (hasSetLogFormat == 0) { |
| 649 | const char* logFormat = getenv("ANDROID_PRINTF_LOG"); |
| 650 | |
| 651 | if (logFormat != NULL) { |
| 652 | err = setLogFormat(logFormat); |
| 653 | |
| 654 | if (err < 0) { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 655 | fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n", |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 656 | logFormat); |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | if (forceFilters) { |
| 662 | err = android_log_addFilterString(g_logformat, forceFilters); |
| 663 | if (err < 0) { |
| 664 | fprintf (stderr, "Invalid filter expression in -logcat option\n"); |
| 665 | exit(0); |
| 666 | } |
| 667 | } else if (argc == optind) { |
| 668 | // Add from environment variable |
| 669 | char *env_tags_orig = getenv("ANDROID_LOG_TAGS"); |
| 670 | |
| 671 | if (env_tags_orig != NULL) { |
| 672 | err = android_log_addFilterString(g_logformat, env_tags_orig); |
| 673 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 674 | if (err < 0) { |
| 675 | fprintf(stderr, "Invalid filter expression in" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 676 | " ANDROID_LOG_TAGS\n"); |
| 677 | android::show_help(argv[0]); |
| 678 | exit(-1); |
| 679 | } |
| 680 | } |
| 681 | } else { |
| 682 | // Add from commandline |
| 683 | for (int i = optind ; i < argc ; i++) { |
| 684 | err = android_log_addFilterString(g_logformat, argv[i]); |
| 685 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 686 | if (err < 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 687 | fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]); |
| 688 | android::show_help(argv[0]); |
| 689 | exit(-1); |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 694 | dev = devices; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 695 | if (tail_time != log_time::EPOCH) { |
| 696 | logger_list = android_logger_list_alloc_time(mode, tail_time, 0); |
| 697 | } else { |
| 698 | logger_list = android_logger_list_alloc(mode, tail_lines, 0); |
| 699 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 700 | while (dev) { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 701 | dev->logger_list = logger_list; |
| 702 | dev->logger = android_logger_open(logger_list, |
| 703 | android_name_to_log_id(dev->device)); |
| 704 | if (!dev->logger) { |
| 705 | fprintf(stderr, "Unable to open log device '%s'\n", dev->device); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 706 | exit(EXIT_FAILURE); |
| 707 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 708 | |
| 709 | if (clearLog) { |
| 710 | int ret; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 711 | ret = android_logger_clear(dev->logger); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 712 | if (ret) { |
Mark Salyzyn | fff04e3 | 2014-03-17 10:36:46 -0700 | [diff] [blame] | 713 | perror("failed to clear the log"); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 714 | exit(EXIT_FAILURE); |
| 715 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 716 | } |
| 717 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 718 | if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) { |
Mark Salyzyn | fff04e3 | 2014-03-17 10:36:46 -0700 | [diff] [blame] | 719 | perror("failed to set the log size"); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 720 | exit(EXIT_FAILURE); |
| 721 | } |
| 722 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 723 | if (getLogSize) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 724 | long size, readable; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 725 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 726 | size = android_logger_get_log_size(dev->logger); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 727 | if (size < 0) { |
Mark Salyzyn | fff04e3 | 2014-03-17 10:36:46 -0700 | [diff] [blame] | 728 | perror("failed to get the log size"); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 729 | exit(EXIT_FAILURE); |
| 730 | } |
| 731 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 732 | readable = android_logger_get_log_readable_size(dev->logger); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 733 | if (readable < 0) { |
Mark Salyzyn | fff04e3 | 2014-03-17 10:36:46 -0700 | [diff] [blame] | 734 | perror("failed to get the readable log size"); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 735 | exit(EXIT_FAILURE); |
| 736 | } |
| 737 | |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 738 | printf("%s: ring buffer is %ld%sb (%ld%sb consumed), " |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 739 | "max entry is %db, max payload is %db\n", dev->device, |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 740 | value_of_size(size), multiplier_of_size(size), |
| 741 | value_of_size(readable), multiplier_of_size(readable), |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 742 | (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD); |
| 743 | } |
| 744 | |
| 745 | dev = dev->next; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 746 | } |
| 747 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 748 | if (setPruneList) { |
| 749 | size_t len = strlen(setPruneList) + 32; // margin to allow rc |
| 750 | char *buf = (char *) malloc(len); |
| 751 | |
| 752 | strcpy(buf, setPruneList); |
| 753 | int ret = android_logger_set_prune_list(logger_list, buf, len); |
| 754 | free(buf); |
| 755 | |
| 756 | if (ret) { |
Mark Salyzyn | fff04e3 | 2014-03-17 10:36:46 -0700 | [diff] [blame] | 757 | perror("failed to set the prune list"); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 758 | exit(EXIT_FAILURE); |
| 759 | } |
| 760 | } |
| 761 | |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 762 | if (printStatistics || getPruneList) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 763 | size_t len = 8192; |
| 764 | char *buf; |
| 765 | |
| 766 | for(int retry = 32; |
| 767 | (retry >= 0) && ((buf = new char [len])); |
| 768 | delete [] buf, --retry) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 769 | if (getPruneList) { |
| 770 | android_logger_get_prune_list(logger_list, buf, len); |
| 771 | } else { |
| 772 | android_logger_get_statistics(logger_list, buf, len); |
| 773 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 774 | buf[len-1] = '\0'; |
| 775 | size_t ret = atol(buf) + 1; |
| 776 | if (ret < 4) { |
| 777 | delete [] buf; |
| 778 | buf = NULL; |
| 779 | break; |
| 780 | } |
| 781 | bool check = ret <= len; |
| 782 | len = ret; |
| 783 | if (check) { |
| 784 | break; |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | if (!buf) { |
Mark Salyzyn | fff04e3 | 2014-03-17 10:36:46 -0700 | [diff] [blame] | 789 | perror("failed to read data"); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 790 | exit(EXIT_FAILURE); |
| 791 | } |
| 792 | |
| 793 | // remove trailing FF |
| 794 | char *cp = buf + len - 1; |
| 795 | *cp = '\0'; |
| 796 | bool truncated = *--cp != '\f'; |
| 797 | if (!truncated) { |
| 798 | *cp = '\0'; |
| 799 | } |
| 800 | |
| 801 | // squash out the byte count |
| 802 | cp = buf; |
| 803 | if (!truncated) { |
Mark Salyzyn | 22e287d | 2014-03-21 13:12:16 -0700 | [diff] [blame] | 804 | while (isdigit(*cp)) { |
| 805 | ++cp; |
| 806 | } |
| 807 | if (*cp == '\n') { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 808 | ++cp; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | printf("%s", cp); |
| 813 | delete [] buf; |
| 814 | exit(0); |
| 815 | } |
| 816 | |
| 817 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 818 | if (getLogSize) { |
Kenny Root | 4bf3c02 | 2011-09-30 17:10:14 -0700 | [diff] [blame] | 819 | exit(0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 820 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 821 | if (setLogSize || setPruneList) { |
| 822 | exit(0); |
| 823 | } |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 824 | if (clearLog) { |
Kenny Root | 4bf3c02 | 2011-09-30 17:10:14 -0700 | [diff] [blame] | 825 | exit(0); |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 826 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 827 | |
| 828 | //LOG_EVENT_INT(10, 12345); |
| 829 | //LOG_EVENT_LONG(11, 0x1122334455667788LL); |
| 830 | //LOG_EVENT_STRING(0, "whassup, doc?"); |
| 831 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 832 | if (needBinary) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 833 | android::g_eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE); |
| 834 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 835 | while (1) { |
| 836 | struct log_msg log_msg; |
| 837 | int ret = android_logger_list_read(logger_list, &log_msg); |
| 838 | |
| 839 | if (ret == 0) { |
| 840 | fprintf(stderr, "read: Unexpected EOF!\n"); |
| 841 | exit(EXIT_FAILURE); |
| 842 | } |
| 843 | |
| 844 | if (ret < 0) { |
| 845 | if (ret == -EAGAIN) { |
| 846 | break; |
| 847 | } |
| 848 | |
| 849 | if (ret == -EIO) { |
| 850 | fprintf(stderr, "read: Unexpected EOF!\n"); |
| 851 | exit(EXIT_FAILURE); |
| 852 | } |
| 853 | if (ret == -EINVAL) { |
| 854 | fprintf(stderr, "read: unexpected length.\n"); |
| 855 | exit(EXIT_FAILURE); |
| 856 | } |
Mark Salyzyn | fff04e3 | 2014-03-17 10:36:46 -0700 | [diff] [blame] | 857 | perror("logcat read failure"); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 858 | exit(EXIT_FAILURE); |
| 859 | } |
| 860 | |
| 861 | for(dev = devices; dev; dev = dev->next) { |
| 862 | if (android_name_to_log_id(dev->device) == log_msg.id()) { |
| 863 | break; |
| 864 | } |
| 865 | } |
| 866 | if (!dev) { |
| 867 | fprintf(stderr, "read: Unexpected log ID!\n"); |
| 868 | exit(EXIT_FAILURE); |
| 869 | } |
| 870 | |
| 871 | android::maybePrintStart(dev); |
| 872 | if (android::g_printBinary) { |
| 873 | android::printBinary(&log_msg); |
| 874 | } else { |
| 875 | android::processBuffer(dev, &log_msg); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | android_logger_list_free(logger_list); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 880 | |
| 881 | return 0; |
| 882 | } |