Mark Salyzyn | 12af965 | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 1 | // Copyright 2006-2014 The Android Open Source Project |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 2 | |
Mark Salyzyn | 12af965 | 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 | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 19 | #include <log/log.h> |
Mark Salyzyn | 9addf59 | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 20 | #include <log/log_read.h> |
Colin Cross | e355ded | 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 | 190995d | 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 | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 34 | struct log_device_t { |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 35 | const char* device; |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 36 | bool binary; |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 37 | struct logger *logger; |
| 38 | struct logger_list *logger_list; |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 39 | bool printed; |
| 40 | char label; |
| 41 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 42 | log_device_t* next; |
| 43 | |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 44 | log_device_t(const char* d, bool b, char l) { |
Joe Onorato | d23b9cf | 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 | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 51 | }; |
The Android Open Source Project | 190995d | 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 | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 62 | static int g_printBinary = 0; |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 63 | static int g_devCount = 0; |
The Android Open Source Project | 190995d | 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 | ff5c94d | 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 | 190995d | 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 | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 115 | void printBinary(struct log_msg *buf) |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 116 | { |
Mark Salyzyn | 2a8a6aa | 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 | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Mark Salyzyn | 2a8a6aa | 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 | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 123 | { |
Mathias Agopian | b9218fb | 2010-03-17 16:10:26 -0700 | [diff] [blame] | 124 | int bytesWritten = 0; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 125 | int err; |
| 126 | AndroidLogEntry entry; |
| 127 | char binaryMsgBuf[1024]; |
| 128 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 129 | if (dev->binary) { |
Mark Salyzyn | 2a8a6aa | 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 | 190995d | 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 | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 137 | err = android_log_processLogBuffer(&buf->entry_v1, &entry); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 138 | } |
Joe Onorato | 400da4a | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 139 | if (err < 0) { |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 140 | goto error; |
Joe Onorato | 400da4a | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 141 | } |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 142 | |
Joe Onorato | 400da4a | 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 | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 153 | |
Joe Onorato | 400da4a | 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 | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | g_outByteCount += bytesWritten; |
| 163 | |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 164 | if (g_logRotateSizeKBytes > 0 |
The Android Open Source Project | 190995d | 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 | cce2b08 | 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 | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 180 | snprintf(buf, sizeof(buf), "--------- beginning of %s\n", |
| 181 | dev->device); |
Dan Egnor | cce2b08 | 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 | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
The Android Open Source Project | 190995d | 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" |
Pierre Zurek | 507ea56 | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 222 | " -v <format> Sets the log print format, where <format> is:\n\n" |
| 223 | " brief color long process raw tag thread threadtime time\n\n" |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 224 | " -c clear (flush) the entire log and exit\n" |
| 225 | " -d dump the log and then exit (don't block)\n" |
Dan Egnor | cce2b08 | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 226 | " -t <count> print only the most recent <count> lines (implies -d)\n" |
Mark Salyzyn | 1ef4818 | 2014-09-01 10:41:33 -0700 | [diff] [blame] | 227 | " -t '<time>' print most recent lines since specified time (implies -d)\n" |
Mark Salyzyn | 2256e2f | 2013-12-09 13:47:00 -0800 | [diff] [blame] | 228 | " -T <count> print only the most recent <count> lines (does not imply -d)\n" |
Mark Salyzyn | 1ef4818 | 2014-09-01 10:41:33 -0700 | [diff] [blame] | 229 | " -T '<time>' print most recent lines since specified time (not imply -d)\n" |
| 230 | " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm'\n" |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 231 | " -g get the size of the log's ring buffer and exit\n" |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 232 | " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n" |
Mark Salyzyn | 855c730 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 233 | " 'events', 'crash' or 'all'. Multiple -b parameters are\n" |
| 234 | " allowed and results are interleaved. The default is\n" |
| 235 | " -b main -b system -b crash.\n" |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 236 | " -B output the log in binary.\n" |
Mark Salyzyn | e00fbe5 | 2014-04-11 13:49:43 -0700 | [diff] [blame] | 237 | " -S output statistics.\n" |
| 238 | " -G <size> set size of log ring buffer, may suffix with K or M.\n" |
| 239 | " -p print prune white and ~black list. Service is specified as\n" |
| 240 | " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n" |
| 241 | " with ~, otherwise weighed for longevity if unadorned. All\n" |
| 242 | " other pruning activity is oldest first. Special case ~!\n" |
| 243 | " represents an automatic quicker pruning for the noisiest\n" |
| 244 | " UID as determined by the current statistics.\n" |
| 245 | " -P '<list> ...' set prune white and ~black list, using same format as\n" |
| 246 | " printed above. Must be quoted.\n"); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 247 | |
| 248 | fprintf(stderr,"\nfilterspecs are a series of \n" |
| 249 | " <tag>[:priority]\n\n" |
| 250 | "where <tag> is a log component tag (or * for all) and priority is:\n" |
| 251 | " V Verbose\n" |
| 252 | " D Debug\n" |
| 253 | " I Info\n" |
| 254 | " W Warn\n" |
| 255 | " E Error\n" |
| 256 | " F Fatal\n" |
| 257 | " S Silent (supress all output)\n" |
| 258 | "\n'*' means '*:d' and <tag> by itself means <tag>:v\n" |
| 259 | "\nIf not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.\n" |
| 260 | "If no filterspec is found, filter defaults to '*:I'\n" |
| 261 | "\nIf not specified with -v, format is set from ANDROID_PRINTF_LOG\n" |
Mark Salyzyn | dd569af | 2014-09-16 09:15:15 -0700 | [diff] [blame^] | 262 | "or defaults to \"threadtime\"\n\n"); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 263 | |
| 264 | |
| 265 | |
| 266 | } |
| 267 | |
| 268 | |
| 269 | } /* namespace android */ |
| 270 | |
| 271 | static int setLogFormat(const char * formatString) |
| 272 | { |
| 273 | static AndroidLogPrintFormat format; |
| 274 | |
| 275 | format = android_log_formatFromString(formatString); |
| 276 | |
| 277 | if (format == FORMAT_OFF) { |
| 278 | // FORMAT_OFF means invalid string |
| 279 | return -1; |
| 280 | } |
| 281 | |
| 282 | android_log_setPrintFormat(g_logformat, format); |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 287 | static const char multipliers[][2] = { |
| 288 | { "" }, |
| 289 | { "K" }, |
| 290 | { "M" }, |
| 291 | { "G" } |
| 292 | }; |
| 293 | |
| 294 | static unsigned long value_of_size(unsigned long value) |
| 295 | { |
| 296 | for (unsigned i = 0; |
| 297 | (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024); |
| 298 | value /= 1024, ++i) ; |
| 299 | return value; |
| 300 | } |
| 301 | |
| 302 | static const char *multiplier_of_size(unsigned long value) |
| 303 | { |
| 304 | unsigned i; |
| 305 | for (i = 0; |
| 306 | (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024); |
| 307 | value /= 1024, ++i) ; |
| 308 | return multipliers[i]; |
| 309 | } |
| 310 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 311 | int main(int argc, char **argv) |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 312 | { |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 313 | int err; |
| 314 | int hasSetLogFormat = 0; |
| 315 | int clearLog = 0; |
| 316 | int getLogSize = 0; |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 317 | unsigned long setLogSize = 0; |
| 318 | int getPruneList = 0; |
| 319 | char *setPruneList = NULL; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 320 | int printStatistics = 0; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 321 | int mode = O_RDONLY; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 322 | const char *forceFilters = NULL; |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 323 | log_device_t* devices = NULL; |
| 324 | log_device_t* dev; |
| 325 | bool needBinary = false; |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 326 | struct logger_list *logger_list; |
Mark Salyzyn | 9addf59 | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 327 | unsigned int tail_lines = 0; |
| 328 | log_time tail_time(log_time::EPOCH); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 329 | |
Mark Salyzyn | 12af965 | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 330 | signal(SIGPIPE, exit); |
| 331 | |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 332 | g_logformat = android_log_format_new(); |
| 333 | |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 334 | if (argc == 2 && 0 == strcmp(argv[1], "--help")) { |
| 335 | android::show_help(argv[0]); |
| 336 | exit(0); |
| 337 | } |
| 338 | |
| 339 | for (;;) { |
| 340 | int ret; |
| 341 | |
Mark Salyzyn | e689b5b | 2014-07-07 08:55:53 -0700 | [diff] [blame] | 342 | ret = getopt(argc, argv, "cdt:T:gG:sQf:r:n:v:b:BSpP:"); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 343 | |
| 344 | if (ret < 0) { |
| 345 | break; |
| 346 | } |
| 347 | |
| 348 | switch(ret) { |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 349 | case 's': |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 350 | // default to all silent |
| 351 | android_log_addFilterRule(g_logformat, "*:s"); |
| 352 | break; |
| 353 | |
| 354 | case 'c': |
| 355 | clearLog = 1; |
| 356 | mode = O_WRONLY; |
| 357 | break; |
| 358 | |
| 359 | case 'd': |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 360 | mode = O_RDONLY | O_NDELAY; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 361 | break; |
| 362 | |
Dan Egnor | cce2b08 | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 363 | case 't': |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 364 | mode = O_RDONLY | O_NDELAY; |
Mark Salyzyn | 2256e2f | 2013-12-09 13:47:00 -0800 | [diff] [blame] | 365 | /* FALLTHRU */ |
| 366 | case 'T': |
Mark Salyzyn | 9addf59 | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 367 | if (strspn(optarg, "0123456789") != strlen(optarg)) { |
| 368 | char *cp = tail_time.strptime(optarg, |
| 369 | log_time::default_format); |
| 370 | if (!cp) { |
| 371 | fprintf(stderr, |
| 372 | "ERROR: -%c \"%s\" not in \"%s\" time format\n", |
| 373 | ret, optarg, log_time::default_format); |
| 374 | exit(1); |
| 375 | } |
| 376 | if (*cp) { |
| 377 | char c = *cp; |
| 378 | *cp = '\0'; |
| 379 | fprintf(stderr, |
| 380 | "WARNING: -%c \"%s\"\"%c%s\" time truncated\n", |
| 381 | ret, optarg, c, cp + 1); |
| 382 | *cp = c; |
| 383 | } |
| 384 | } else { |
| 385 | tail_lines = atoi(optarg); |
| 386 | if (!tail_lines) { |
| 387 | fprintf(stderr, |
| 388 | "WARNING: -%c %s invalid, setting to 1\n", |
| 389 | ret, optarg); |
| 390 | tail_lines = 1; |
| 391 | } |
| 392 | } |
Dan Egnor | cce2b08 | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 393 | break; |
| 394 | |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 395 | case 'g': |
| 396 | getLogSize = 1; |
| 397 | break; |
| 398 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 399 | case 'G': { |
| 400 | // would use atol if not for the multiplier |
| 401 | char *cp = optarg; |
| 402 | setLogSize = 0; |
| 403 | while (('0' <= *cp) && (*cp <= '9')) { |
| 404 | setLogSize *= 10; |
| 405 | setLogSize += *cp - '0'; |
| 406 | ++cp; |
| 407 | } |
| 408 | |
| 409 | switch(*cp) { |
| 410 | case 'g': |
| 411 | case 'G': |
| 412 | setLogSize *= 1024; |
| 413 | /* FALLTHRU */ |
| 414 | case 'm': |
| 415 | case 'M': |
| 416 | setLogSize *= 1024; |
| 417 | /* FALLTHRU */ |
| 418 | case 'k': |
| 419 | case 'K': |
| 420 | setLogSize *= 1024; |
| 421 | /* FALLTHRU */ |
| 422 | case '\0': |
| 423 | break; |
| 424 | |
| 425 | default: |
| 426 | setLogSize = 0; |
| 427 | } |
| 428 | |
| 429 | if (!setLogSize) { |
| 430 | fprintf(stderr, "ERROR: -G <num><multiplier>\n"); |
| 431 | exit(1); |
| 432 | } |
| 433 | } |
| 434 | break; |
| 435 | |
| 436 | case 'p': |
| 437 | getPruneList = 1; |
| 438 | break; |
| 439 | |
| 440 | case 'P': |
| 441 | setPruneList = optarg; |
| 442 | break; |
| 443 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 444 | case 'b': { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 445 | if (strcmp(optarg, "all") == 0) { |
| 446 | while (devices) { |
| 447 | dev = devices; |
| 448 | devices = dev->next; |
| 449 | delete dev; |
| 450 | } |
| 451 | |
| 452 | dev = devices = new log_device_t("main", false, 'm'); |
| 453 | android::g_devCount = 1; |
| 454 | if (android_name_to_log_id("system") == LOG_ID_SYSTEM) { |
| 455 | dev->next = new log_device_t("system", false, 's'); |
| 456 | if (dev->next) { |
| 457 | dev = dev->next; |
| 458 | android::g_devCount++; |
| 459 | } |
| 460 | } |
| 461 | if (android_name_to_log_id("radio") == LOG_ID_RADIO) { |
| 462 | dev->next = new log_device_t("radio", false, 'r'); |
| 463 | if (dev->next) { |
| 464 | dev = dev->next; |
| 465 | android::g_devCount++; |
| 466 | } |
| 467 | } |
| 468 | if (android_name_to_log_id("events") == LOG_ID_EVENTS) { |
| 469 | dev->next = new log_device_t("events", true, 'e'); |
| 470 | if (dev->next) { |
Mark Salyzyn | 855c730 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 471 | dev = dev->next; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 472 | android::g_devCount++; |
| 473 | needBinary = true; |
| 474 | } |
| 475 | } |
Mark Salyzyn | 855c730 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 476 | if (android_name_to_log_id("crash") == LOG_ID_CRASH) { |
| 477 | dev->next = new log_device_t("crash", false, 'c'); |
| 478 | if (dev->next) { |
| 479 | android::g_devCount++; |
| 480 | } |
| 481 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 482 | break; |
| 483 | } |
| 484 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 485 | bool binary = strcmp(optarg, "events") == 0; |
| 486 | if (binary) { |
| 487 | needBinary = true; |
| 488 | } |
| 489 | |
| 490 | if (devices) { |
| 491 | dev = devices; |
| 492 | while (dev->next) { |
| 493 | dev = dev->next; |
| 494 | } |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 495 | dev->next = new log_device_t(optarg, binary, optarg[0]); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 496 | } else { |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 497 | devices = new log_device_t(optarg, binary, optarg[0]); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 498 | } |
| 499 | android::g_devCount++; |
| 500 | } |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 501 | break; |
| 502 | |
| 503 | case 'B': |
| 504 | android::g_printBinary = 1; |
| 505 | break; |
| 506 | |
| 507 | case 'f': |
| 508 | // redirect output to a file |
| 509 | |
| 510 | android::g_outputFileName = optarg; |
| 511 | |
| 512 | break; |
| 513 | |
| 514 | case 'r': |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 515 | if (optarg == NULL) { |
| 516 | android::g_logRotateSizeKBytes |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 517 | = DEFAULT_LOG_ROTATE_SIZE_KBYTES; |
| 518 | } else { |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 519 | if (!isdigit(optarg[0])) { |
| 520 | fprintf(stderr,"Invalid parameter to -r\n"); |
| 521 | android::show_help(argv[0]); |
| 522 | exit(-1); |
| 523 | } |
| 524 | android::g_logRotateSizeKBytes = atoi(optarg); |
| 525 | } |
| 526 | break; |
| 527 | |
| 528 | case 'n': |
| 529 | if (!isdigit(optarg[0])) { |
| 530 | fprintf(stderr,"Invalid parameter to -r\n"); |
| 531 | android::show_help(argv[0]); |
| 532 | exit(-1); |
| 533 | } |
| 534 | |
| 535 | android::g_maxRotatedLogs = atoi(optarg); |
| 536 | break; |
| 537 | |
| 538 | case 'v': |
| 539 | err = setLogFormat (optarg); |
| 540 | if (err < 0) { |
| 541 | fprintf(stderr,"Invalid parameter to -v\n"); |
| 542 | android::show_help(argv[0]); |
| 543 | exit(-1); |
| 544 | } |
| 545 | |
Mark Salyzyn | dd569af | 2014-09-16 09:15:15 -0700 | [diff] [blame^] | 546 | if (strcmp("color", optarg)) { // exception for modifiers |
| 547 | hasSetLogFormat = 1; |
| 548 | } |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 549 | break; |
| 550 | |
| 551 | case 'Q': |
| 552 | /* this is a *hidden* option used to start a version of logcat */ |
| 553 | /* in an emulated device only. it basically looks for androidboot.logcat= */ |
| 554 | /* on the kernel command line. If something is found, it extracts a log filter */ |
| 555 | /* and uses it to run the program. If nothing is found, the program should */ |
| 556 | /* quit immediately */ |
| 557 | #define KERNEL_OPTION "androidboot.logcat=" |
| 558 | #define CONSOLE_OPTION "androidboot.console=" |
| 559 | { |
| 560 | int fd; |
| 561 | char* logcat; |
| 562 | char* console; |
| 563 | int force_exit = 1; |
| 564 | static char cmdline[1024]; |
| 565 | |
| 566 | fd = open("/proc/cmdline", O_RDONLY); |
| 567 | if (fd >= 0) { |
| 568 | int n = read(fd, cmdline, sizeof(cmdline)-1 ); |
| 569 | if (n < 0) n = 0; |
| 570 | cmdline[n] = 0; |
| 571 | close(fd); |
| 572 | } else { |
| 573 | cmdline[0] = 0; |
| 574 | } |
| 575 | |
| 576 | logcat = strstr( cmdline, KERNEL_OPTION ); |
| 577 | console = strstr( cmdline, CONSOLE_OPTION ); |
| 578 | if (logcat != NULL) { |
| 579 | char* p = logcat + sizeof(KERNEL_OPTION)-1;; |
| 580 | char* q = strpbrk( p, " \t\n\r" );; |
| 581 | |
| 582 | if (q != NULL) |
| 583 | *q = 0; |
| 584 | |
| 585 | forceFilters = p; |
| 586 | force_exit = 0; |
| 587 | } |
| 588 | /* if nothing found or invalid filters, exit quietly */ |
| 589 | if (force_exit) |
| 590 | exit(0); |
| 591 | |
| 592 | /* redirect our output to the emulator console */ |
| 593 | if (console) { |
| 594 | char* p = console + sizeof(CONSOLE_OPTION)-1; |
| 595 | char* q = strpbrk( p, " \t\n\r" ); |
| 596 | char devname[64]; |
| 597 | int len; |
| 598 | |
| 599 | if (q != NULL) { |
| 600 | len = q - p; |
| 601 | } else |
| 602 | len = strlen(p); |
| 603 | |
| 604 | len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p ); |
| 605 | fprintf(stderr, "logcat using %s (%d)\n", devname, len); |
| 606 | if (len < (int)sizeof(devname)) { |
| 607 | fd = open( devname, O_WRONLY ); |
| 608 | if (fd >= 0) { |
| 609 | dup2(fd, 1); |
| 610 | dup2(fd, 2); |
| 611 | close(fd); |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | break; |
| 617 | |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 618 | case 'S': |
| 619 | printStatistics = 1; |
| 620 | break; |
| 621 | |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 622 | default: |
| 623 | fprintf(stderr,"Unrecognized Option\n"); |
| 624 | android::show_help(argv[0]); |
| 625 | exit(-1); |
| 626 | break; |
| 627 | } |
| 628 | } |
| 629 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 630 | if (!devices) { |
Mark Salyzyn | fd12ed5 | 2014-05-14 12:37:22 -0700 | [diff] [blame] | 631 | dev = devices = new log_device_t("main", false, 'm'); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 632 | android::g_devCount = 1; |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 633 | if (android_name_to_log_id("system") == LOG_ID_SYSTEM) { |
Mark Salyzyn | fd12ed5 | 2014-05-14 12:37:22 -0700 | [diff] [blame] | 634 | dev = dev->next = new log_device_t("system", false, 's'); |
Joe Onorato | 400da4a | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 635 | android::g_devCount++; |
| 636 | } |
Mark Salyzyn | 855c730 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 637 | if (android_name_to_log_id("crash") == LOG_ID_CRASH) { |
Mark Salyzyn | fd12ed5 | 2014-05-14 12:37:22 -0700 | [diff] [blame] | 638 | dev = dev->next = new log_device_t("crash", false, 'c'); |
Mark Salyzyn | 855c730 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 639 | android::g_devCount++; |
| 640 | } |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 641 | } |
| 642 | |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 643 | if (android::g_logRotateSizeKBytes != 0 |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 644 | && android::g_outputFileName == NULL |
| 645 | ) { |
| 646 | fprintf(stderr,"-r requires -f as well\n"); |
| 647 | android::show_help(argv[0]); |
| 648 | exit(-1); |
| 649 | } |
| 650 | |
| 651 | android::setupOutput(); |
| 652 | |
| 653 | if (hasSetLogFormat == 0) { |
| 654 | const char* logFormat = getenv("ANDROID_PRINTF_LOG"); |
| 655 | |
| 656 | if (logFormat != NULL) { |
| 657 | err = setLogFormat(logFormat); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 658 | if (err < 0) { |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 659 | fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n", |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 660 | logFormat); |
| 661 | } |
Mark Salyzyn | dd569af | 2014-09-16 09:15:15 -0700 | [diff] [blame^] | 662 | } else { |
| 663 | setLogFormat("threadtime"); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
| 667 | if (forceFilters) { |
| 668 | err = android_log_addFilterString(g_logformat, forceFilters); |
| 669 | if (err < 0) { |
| 670 | fprintf (stderr, "Invalid filter expression in -logcat option\n"); |
| 671 | exit(0); |
| 672 | } |
| 673 | } else if (argc == optind) { |
| 674 | // Add from environment variable |
| 675 | char *env_tags_orig = getenv("ANDROID_LOG_TAGS"); |
| 676 | |
| 677 | if (env_tags_orig != NULL) { |
| 678 | err = android_log_addFilterString(g_logformat, env_tags_orig); |
| 679 | |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 680 | if (err < 0) { |
| 681 | fprintf(stderr, "Invalid filter expression in" |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 682 | " ANDROID_LOG_TAGS\n"); |
| 683 | android::show_help(argv[0]); |
| 684 | exit(-1); |
| 685 | } |
| 686 | } |
| 687 | } else { |
| 688 | // Add from commandline |
| 689 | for (int i = optind ; i < argc ; i++) { |
| 690 | err = android_log_addFilterString(g_logformat, argv[i]); |
| 691 | |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 692 | if (err < 0) { |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 693 | fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]); |
| 694 | android::show_help(argv[0]); |
| 695 | exit(-1); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 700 | dev = devices; |
Mark Salyzyn | 9addf59 | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 701 | if (tail_time != log_time::EPOCH) { |
| 702 | logger_list = android_logger_list_alloc_time(mode, tail_time, 0); |
| 703 | } else { |
| 704 | logger_list = android_logger_list_alloc(mode, tail_lines, 0); |
| 705 | } |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 706 | while (dev) { |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 707 | dev->logger_list = logger_list; |
| 708 | dev->logger = android_logger_open(logger_list, |
| 709 | android_name_to_log_id(dev->device)); |
| 710 | if (!dev->logger) { |
| 711 | fprintf(stderr, "Unable to open log device '%s'\n", dev->device); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 712 | exit(EXIT_FAILURE); |
| 713 | } |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 714 | |
| 715 | if (clearLog) { |
| 716 | int ret; |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 717 | ret = android_logger_clear(dev->logger); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 718 | if (ret) { |
Mark Salyzyn | 0f498aa | 2014-03-17 10:36:46 -0700 | [diff] [blame] | 719 | perror("failed to clear the log"); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 720 | exit(EXIT_FAILURE); |
| 721 | } |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 722 | } |
| 723 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 724 | if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) { |
Mark Salyzyn | 0f498aa | 2014-03-17 10:36:46 -0700 | [diff] [blame] | 725 | perror("failed to set the log size"); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 726 | exit(EXIT_FAILURE); |
| 727 | } |
| 728 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 729 | if (getLogSize) { |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 730 | long size, readable; |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 731 | |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 732 | size = android_logger_get_log_size(dev->logger); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 733 | if (size < 0) { |
Mark Salyzyn | 0f498aa | 2014-03-17 10:36:46 -0700 | [diff] [blame] | 734 | perror("failed to get the log size"); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 735 | exit(EXIT_FAILURE); |
| 736 | } |
| 737 | |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 738 | readable = android_logger_get_log_readable_size(dev->logger); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 739 | if (readable < 0) { |
Mark Salyzyn | 0f498aa | 2014-03-17 10:36:46 -0700 | [diff] [blame] | 740 | perror("failed to get the readable log size"); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 741 | exit(EXIT_FAILURE); |
| 742 | } |
| 743 | |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 744 | printf("%s: ring buffer is %ld%sb (%ld%sb consumed), " |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 745 | "max entry is %db, max payload is %db\n", dev->device, |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 746 | value_of_size(size), multiplier_of_size(size), |
| 747 | value_of_size(readable), multiplier_of_size(readable), |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 748 | (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD); |
| 749 | } |
| 750 | |
| 751 | dev = dev->next; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 752 | } |
| 753 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 754 | if (setPruneList) { |
| 755 | size_t len = strlen(setPruneList) + 32; // margin to allow rc |
| 756 | char *buf = (char *) malloc(len); |
| 757 | |
| 758 | strcpy(buf, setPruneList); |
| 759 | int ret = android_logger_set_prune_list(logger_list, buf, len); |
| 760 | free(buf); |
| 761 | |
| 762 | if (ret) { |
Mark Salyzyn | 0f498aa | 2014-03-17 10:36:46 -0700 | [diff] [blame] | 763 | perror("failed to set the prune list"); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 764 | exit(EXIT_FAILURE); |
| 765 | } |
| 766 | } |
| 767 | |
Mark Salyzyn | c402bc7 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 768 | if (printStatistics || getPruneList) { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 769 | size_t len = 8192; |
| 770 | char *buf; |
| 771 | |
| 772 | for(int retry = 32; |
| 773 | (retry >= 0) && ((buf = new char [len])); |
| 774 | delete [] buf, --retry) { |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 775 | if (getPruneList) { |
| 776 | android_logger_get_prune_list(logger_list, buf, len); |
| 777 | } else { |
| 778 | android_logger_get_statistics(logger_list, buf, len); |
| 779 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 780 | buf[len-1] = '\0'; |
| 781 | size_t ret = atol(buf) + 1; |
| 782 | if (ret < 4) { |
| 783 | delete [] buf; |
| 784 | buf = NULL; |
| 785 | break; |
| 786 | } |
| 787 | bool check = ret <= len; |
| 788 | len = ret; |
| 789 | if (check) { |
| 790 | break; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | if (!buf) { |
Mark Salyzyn | 0f498aa | 2014-03-17 10:36:46 -0700 | [diff] [blame] | 795 | perror("failed to read data"); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 796 | exit(EXIT_FAILURE); |
| 797 | } |
| 798 | |
| 799 | // remove trailing FF |
| 800 | char *cp = buf + len - 1; |
| 801 | *cp = '\0'; |
| 802 | bool truncated = *--cp != '\f'; |
| 803 | if (!truncated) { |
| 804 | *cp = '\0'; |
| 805 | } |
| 806 | |
| 807 | // squash out the byte count |
| 808 | cp = buf; |
| 809 | if (!truncated) { |
Mark Salyzyn | 63a4ad5 | 2014-03-21 13:12:16 -0700 | [diff] [blame] | 810 | while (isdigit(*cp)) { |
| 811 | ++cp; |
| 812 | } |
| 813 | if (*cp == '\n') { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 814 | ++cp; |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | printf("%s", cp); |
| 819 | delete [] buf; |
| 820 | exit(0); |
| 821 | } |
| 822 | |
| 823 | |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 824 | if (getLogSize) { |
Kenny Root | fa7c09a | 2011-09-30 17:10:14 -0700 | [diff] [blame] | 825 | exit(0); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 826 | } |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 827 | if (setLogSize || setPruneList) { |
| 828 | exit(0); |
| 829 | } |
Joe Onorato | 400da4a | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 830 | if (clearLog) { |
Kenny Root | fa7c09a | 2011-09-30 17:10:14 -0700 | [diff] [blame] | 831 | exit(0); |
Joe Onorato | 400da4a | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 832 | } |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 833 | |
| 834 | //LOG_EVENT_INT(10, 12345); |
| 835 | //LOG_EVENT_LONG(11, 0x1122334455667788LL); |
| 836 | //LOG_EVENT_STRING(0, "whassup, doc?"); |
| 837 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 838 | if (needBinary) |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 839 | android::g_eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE); |
| 840 | |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 841 | while (1) { |
| 842 | struct log_msg log_msg; |
| 843 | int ret = android_logger_list_read(logger_list, &log_msg); |
| 844 | |
| 845 | if (ret == 0) { |
| 846 | fprintf(stderr, "read: Unexpected EOF!\n"); |
| 847 | exit(EXIT_FAILURE); |
| 848 | } |
| 849 | |
| 850 | if (ret < 0) { |
| 851 | if (ret == -EAGAIN) { |
| 852 | break; |
| 853 | } |
| 854 | |
| 855 | if (ret == -EIO) { |
| 856 | fprintf(stderr, "read: Unexpected EOF!\n"); |
| 857 | exit(EXIT_FAILURE); |
| 858 | } |
| 859 | if (ret == -EINVAL) { |
| 860 | fprintf(stderr, "read: unexpected length.\n"); |
| 861 | exit(EXIT_FAILURE); |
| 862 | } |
Mark Salyzyn | 0f498aa | 2014-03-17 10:36:46 -0700 | [diff] [blame] | 863 | perror("logcat read failure"); |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 864 | exit(EXIT_FAILURE); |
| 865 | } |
| 866 | |
| 867 | for(dev = devices; dev; dev = dev->next) { |
| 868 | if (android_name_to_log_id(dev->device) == log_msg.id()) { |
| 869 | break; |
| 870 | } |
| 871 | } |
| 872 | if (!dev) { |
| 873 | fprintf(stderr, "read: Unexpected log ID!\n"); |
| 874 | exit(EXIT_FAILURE); |
| 875 | } |
| 876 | |
| 877 | android::maybePrintStart(dev); |
| 878 | if (android::g_printBinary) { |
| 879 | android::printBinary(&log_msg); |
| 880 | } else { |
| 881 | android::processBuffer(dev, &log_msg); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | android_logger_list_free(logger_list); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 886 | |
| 887 | return 0; |
| 888 | } |