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