blob: a54167ee581691b32f009e7006a6bcaa42b892c1 [file] [log] [blame]
Mark Salyzyn65772ca2013-12-13 11:10:11 -08001// Copyright 2006-2014 The Android Open Source Project
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08002
Mark Salyzyn65772ca2013-12-13 11:10:11 -08003#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 Salyzyn95132e92013-11-22 10:55:48 -080019#include <log/log.h>
Colin Cross9227bd32013-07-23 16:59:20 -070020#include <log/logger.h>
21#include <log/logd.h>
22#include <log/logprint.h>
23#include <log/event_tag_map.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024
25#define DEFAULT_LOG_ROTATE_SIZE_KBYTES 16
26#define DEFAULT_MAX_ROTATED_LOGS 4
27
28static AndroidLogFormat * g_logformat;
29
30/* logd prefixes records with a length field */
31#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
32
Joe Onorato6fa09a02010-02-26 10:04:23 -080033struct log_device_t {
Mark Salyzyn95132e92013-11-22 10:55:48 -080034 const char* device;
Joe Onorato6fa09a02010-02-26 10:04:23 -080035 bool binary;
Mark Salyzyn95132e92013-11-22 10:55:48 -080036 struct logger *logger;
37 struct logger_list *logger_list;
Joe Onorato6fa09a02010-02-26 10:04:23 -080038 bool printed;
39 char label;
40
Joe Onorato6fa09a02010-02-26 10:04:23 -080041 log_device_t* next;
42
Mark Salyzyn95132e92013-11-22 10:55:48 -080043 log_device_t(const char* d, bool b, char l) {
Joe Onorato6fa09a02010-02-26 10:04:23 -080044 device = d;
45 binary = b;
46 label = l;
47 next = NULL;
48 printed = false;
49 }
Joe Onorato6fa09a02010-02-26 10:04:23 -080050};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051
52namespace android {
53
54/* Global Variables */
55
56static const char * g_outputFileName = NULL;
57static int g_logRotateSizeKBytes = 0; // 0 means "no log rotation"
58static int g_maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS; // 0 means "unbounded"
59static int g_outFD = -1;
60static off_t g_outByteCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061static int g_printBinary = 0;
Joe Onorato6fa09a02010-02-26 10:04:23 -080062static int g_devCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063
64static EventTagMap* g_eventTagMap = NULL;
65
66static int openLogFile (const char *pathname)
67{
Edwin Vane80b221c2012-08-13 12:55:07 -040068 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069}
70
71static void rotateLogs()
72{
73 int err;
74
75 // Can't rotate logs if we're not outputting to a file
76 if (g_outputFileName == NULL) {
77 return;
78 }
79
80 close(g_outFD);
81
82 for (int i = g_maxRotatedLogs ; i > 0 ; i--) {
83 char *file0, *file1;
84
85 asprintf(&file1, "%s.%d", g_outputFileName, i);
86
87 if (i - 1 == 0) {
88 asprintf(&file0, "%s", g_outputFileName);
89 } else {
90 asprintf(&file0, "%s.%d", g_outputFileName, i - 1);
91 }
92
93 err = rename (file0, file1);
94
95 if (err < 0 && errno != ENOENT) {
96 perror("while rotating log files");
97 }
98
99 free(file1);
100 free(file0);
101 }
102
103 g_outFD = openLogFile (g_outputFileName);
104
105 if (g_outFD < 0) {
106 perror ("couldn't open output file");
107 exit(-1);
108 }
109
110 g_outByteCount = 0;
111
112}
113
Mark Salyzyn95132e92013-11-22 10:55:48 -0800114void printBinary(struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115{
Mark Salyzyn95132e92013-11-22 10:55:48 -0800116 size_t size = buf->len();
117
118 TEMP_FAILURE_RETRY(write(g_outFD, buf, size));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119}
120
Mark Salyzyn95132e92013-11-22 10:55:48 -0800121static void processBuffer(log_device_t* dev, struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122{
Mathias Agopian50844522010-03-17 16:10:26 -0700123 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124 int err;
125 AndroidLogEntry entry;
126 char binaryMsgBuf[1024];
127
Joe Onorato6fa09a02010-02-26 10:04:23 -0800128 if (dev->binary) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800129 err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry,
130 g_eventTagMap,
131 binaryMsgBuf,
132 sizeof(binaryMsgBuf));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133 //printf(">>> pri=%d len=%d msg='%s'\n",
134 // entry.priority, entry.messageLen, entry.message);
135 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800136 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800138 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139 goto error;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800140 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800142 if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) {
143 if (false && g_devCount > 1) {
144 binaryMsgBuf[0] = dev->label;
145 binaryMsgBuf[1] = ' ';
146 bytesWritten = write(g_outFD, binaryMsgBuf, 2);
147 if (bytesWritten < 0) {
148 perror("output error");
149 exit(-1);
150 }
151 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800153 bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry);
154
155 if (bytesWritten < 0) {
156 perror("output error");
157 exit(-1);
158 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159 }
160
161 g_outByteCount += bytesWritten;
162
Mark Salyzyn95132e92013-11-22 10:55:48 -0800163 if (g_logRotateSizeKBytes > 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164 && (g_outByteCount / 1024) >= g_logRotateSizeKBytes
165 ) {
166 rotateLogs();
167 }
168
169error:
170 //fprintf (stderr, "Error processing record\n");
171 return;
172}
173
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800174static void maybePrintStart(log_device_t* dev) {
175 if (!dev->printed) {
176 dev->printed = true;
177 if (g_devCount > 1 && !g_printBinary) {
178 char buf[1024];
Mark Salyzyn95132e92013-11-22 10:55:48 -0800179 snprintf(buf, sizeof(buf), "--------- beginning of %s\n",
180 dev->device);
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800181 if (write(g_outFD, buf, strlen(buf)) < 0) {
182 perror("output error");
183 exit(-1);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800184 }
185 }
186 }
187}
188
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189static void setupOutput()
190{
191
192 if (g_outputFileName == NULL) {
193 g_outFD = STDOUT_FILENO;
194
195 } else {
196 struct stat statbuf;
197
198 g_outFD = openLogFile (g_outputFileName);
199
200 if (g_outFD < 0) {
201 perror ("couldn't open output file");
202 exit(-1);
203 }
204
205 fstat(g_outFD, &statbuf);
206
207 g_outByteCount = statbuf.st_size;
208 }
209}
210
211static void show_help(const char *cmd)
212{
213 fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd);
214
215 fprintf(stderr, "options include:\n"
216 " -s Set default filter to silent.\n"
217 " Like specifying filterspec '*:s'\n"
218 " -f <filename> Log to file. Default to stdout\n"
219 " -r [<kbytes>] Rotate log every kbytes. (16 if unspecified). Requires -f\n"
220 " -n <count> Sets max number of rotated logs to <count>, default 4\n"
221 " -v <format> Sets the log print format, where <format> is one of:\n\n"
222 " brief process tag thread raw time threadtime long\n\n"
223 " -c clear (flush) the entire log and exit\n"
224 " -d dump the log and then exit (don't block)\n"
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800225 " -t <count> print only the most recent <count> lines (implies -d)\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226 " -g get the size of the log's ring buffer and exit\n"
Wink Savilleba9608f2010-06-18 10:15:08 -0700227 " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio'\n"
228 " or 'events'. Multiple -b parameters are allowed and the\n"
229 " results are interleaved. The default is -b main -b system.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230 " -B output the log in binary");
231
232
233 fprintf(stderr,"\nfilterspecs are a series of \n"
234 " <tag>[:priority]\n\n"
235 "where <tag> is a log component tag (or * for all) and priority is:\n"
236 " V Verbose\n"
237 " D Debug\n"
238 " I Info\n"
239 " W Warn\n"
240 " E Error\n"
241 " F Fatal\n"
242 " S Silent (supress all output)\n"
243 "\n'*' means '*:d' and <tag> by itself means <tag>:v\n"
244 "\nIf not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.\n"
245 "If no filterspec is found, filter defaults to '*:I'\n"
246 "\nIf not specified with -v, format is set from ANDROID_PRINTF_LOG\n"
247 "or defaults to \"brief\"\n\n");
248
249
250
251}
252
253
254} /* namespace android */
255
256static int setLogFormat(const char * formatString)
257{
258 static AndroidLogPrintFormat format;
259
260 format = android_log_formatFromString(formatString);
261
262 if (format == FORMAT_OFF) {
263 // FORMAT_OFF means invalid string
264 return -1;
265 }
266
267 android_log_setPrintFormat(g_logformat, format);
268
269 return 0;
270}
271
272extern "C" void logprint_run_tests(void);
273
Joe Onorato6fa09a02010-02-26 10:04:23 -0800274int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800275{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800276 int err;
277 int hasSetLogFormat = 0;
278 int clearLog = 0;
279 int getLogSize = 0;
280 int mode = O_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800281 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800282 log_device_t* devices = NULL;
283 log_device_t* dev;
284 bool needBinary = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800285 struct logger_list *logger_list;
286 int tail_lines = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800287
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800288 signal(SIGPIPE, exit);
289
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800290 g_logformat = android_log_format_new();
291
292 if (argc == 2 && 0 == strcmp(argv[1], "--test")) {
293 logprint_run_tests();
294 exit(0);
295 }
296
297 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
298 android::show_help(argv[0]);
299 exit(0);
300 }
301
302 for (;;) {
303 int ret;
304
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800305 ret = getopt(argc, argv, "cdt:gsQf:r::n:v:b:B");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306
307 if (ret < 0) {
308 break;
309 }
310
311 switch(ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800312 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313 // default to all silent
314 android_log_addFilterRule(g_logformat, "*:s");
315 break;
316
317 case 'c':
318 clearLog = 1;
319 mode = O_WRONLY;
320 break;
321
322 case 'd':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800323 mode = O_RDONLY | O_NDELAY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324 break;
325
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800326 case 't':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800327 mode = O_RDONLY | O_NDELAY;
328 tail_lines = atoi(optarg);
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800329 break;
330
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 case 'g':
332 getLogSize = 1;
333 break;
334
Joe Onorato6fa09a02010-02-26 10:04:23 -0800335 case 'b': {
Joe Onorato6fa09a02010-02-26 10:04:23 -0800336 bool binary = strcmp(optarg, "events") == 0;
337 if (binary) {
338 needBinary = true;
339 }
340
341 if (devices) {
342 dev = devices;
343 while (dev->next) {
344 dev = dev->next;
345 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800346 dev->next = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800347 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800348 devices = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800349 }
350 android::g_devCount++;
351 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352 break;
353
354 case 'B':
355 android::g_printBinary = 1;
356 break;
357
358 case 'f':
359 // redirect output to a file
360
361 android::g_outputFileName = optarg;
362
363 break;
364
365 case 'r':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800366 if (optarg == NULL) {
367 android::g_logRotateSizeKBytes
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368 = DEFAULT_LOG_ROTATE_SIZE_KBYTES;
369 } else {
370 long logRotateSize;
371 char *lastDigit;
372
373 if (!isdigit(optarg[0])) {
374 fprintf(stderr,"Invalid parameter to -r\n");
375 android::show_help(argv[0]);
376 exit(-1);
377 }
378 android::g_logRotateSizeKBytes = atoi(optarg);
379 }
380 break;
381
382 case 'n':
383 if (!isdigit(optarg[0])) {
384 fprintf(stderr,"Invalid parameter to -r\n");
385 android::show_help(argv[0]);
386 exit(-1);
387 }
388
389 android::g_maxRotatedLogs = atoi(optarg);
390 break;
391
392 case 'v':
393 err = setLogFormat (optarg);
394 if (err < 0) {
395 fprintf(stderr,"Invalid parameter to -v\n");
396 android::show_help(argv[0]);
397 exit(-1);
398 }
399
400 hasSetLogFormat = 1;
401 break;
402
403 case 'Q':
404 /* this is a *hidden* option used to start a version of logcat */
405 /* in an emulated device only. it basically looks for androidboot.logcat= */
406 /* on the kernel command line. If something is found, it extracts a log filter */
407 /* and uses it to run the program. If nothing is found, the program should */
408 /* quit immediately */
409#define KERNEL_OPTION "androidboot.logcat="
410#define CONSOLE_OPTION "androidboot.console="
411 {
412 int fd;
413 char* logcat;
414 char* console;
415 int force_exit = 1;
416 static char cmdline[1024];
417
418 fd = open("/proc/cmdline", O_RDONLY);
419 if (fd >= 0) {
420 int n = read(fd, cmdline, sizeof(cmdline)-1 );
421 if (n < 0) n = 0;
422 cmdline[n] = 0;
423 close(fd);
424 } else {
425 cmdline[0] = 0;
426 }
427
428 logcat = strstr( cmdline, KERNEL_OPTION );
429 console = strstr( cmdline, CONSOLE_OPTION );
430 if (logcat != NULL) {
431 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
432 char* q = strpbrk( p, " \t\n\r" );;
433
434 if (q != NULL)
435 *q = 0;
436
437 forceFilters = p;
438 force_exit = 0;
439 }
440 /* if nothing found or invalid filters, exit quietly */
441 if (force_exit)
442 exit(0);
443
444 /* redirect our output to the emulator console */
445 if (console) {
446 char* p = console + sizeof(CONSOLE_OPTION)-1;
447 char* q = strpbrk( p, " \t\n\r" );
448 char devname[64];
449 int len;
450
451 if (q != NULL) {
452 len = q - p;
453 } else
454 len = strlen(p);
455
456 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
457 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
458 if (len < (int)sizeof(devname)) {
459 fd = open( devname, O_WRONLY );
460 if (fd >= 0) {
461 dup2(fd, 1);
462 dup2(fd, 2);
463 close(fd);
464 }
465 }
466 }
467 }
468 break;
469
470 default:
471 fprintf(stderr,"Unrecognized Option\n");
472 android::show_help(argv[0]);
473 exit(-1);
474 break;
475 }
476 }
477
Joe Onorato6fa09a02010-02-26 10:04:23 -0800478 if (!devices) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800479 devices = new log_device_t("main", false, 'm');
Joe Onorato6fa09a02010-02-26 10:04:23 -0800480 android::g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800481 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
482 devices->next = new log_device_t("system", false, 's');
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800483 android::g_devCount++;
484 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800485 }
486
Mark Salyzyn95132e92013-11-22 10:55:48 -0800487 if (android::g_logRotateSizeKBytes != 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800488 && android::g_outputFileName == NULL
489 ) {
490 fprintf(stderr,"-r requires -f as well\n");
491 android::show_help(argv[0]);
492 exit(-1);
493 }
494
495 android::setupOutput();
496
497 if (hasSetLogFormat == 0) {
498 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
499
500 if (logFormat != NULL) {
501 err = setLogFormat(logFormat);
502
503 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800504 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505 logFormat);
506 }
507 }
508 }
509
510 if (forceFilters) {
511 err = android_log_addFilterString(g_logformat, forceFilters);
512 if (err < 0) {
513 fprintf (stderr, "Invalid filter expression in -logcat option\n");
514 exit(0);
515 }
516 } else if (argc == optind) {
517 // Add from environment variable
518 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
519
520 if (env_tags_orig != NULL) {
521 err = android_log_addFilterString(g_logformat, env_tags_orig);
522
Mark Salyzyn95132e92013-11-22 10:55:48 -0800523 if (err < 0) {
524 fprintf(stderr, "Invalid filter expression in"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525 " ANDROID_LOG_TAGS\n");
526 android::show_help(argv[0]);
527 exit(-1);
528 }
529 }
530 } else {
531 // Add from commandline
532 for (int i = optind ; i < argc ; i++) {
533 err = android_log_addFilterString(g_logformat, argv[i]);
534
Mark Salyzyn95132e92013-11-22 10:55:48 -0800535 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800536 fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]);
537 android::show_help(argv[0]);
538 exit(-1);
539 }
540 }
541 }
542
Joe Onorato6fa09a02010-02-26 10:04:23 -0800543 dev = devices;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800544 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800545 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800546 dev->logger_list = logger_list;
547 dev->logger = android_logger_open(logger_list,
548 android_name_to_log_id(dev->device));
549 if (!dev->logger) {
550 fprintf(stderr, "Unable to open log device '%s'\n", dev->device);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800551 exit(EXIT_FAILURE);
552 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800553
554 if (clearLog) {
555 int ret;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800556 ret = android_logger_clear(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800557 if (ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800558 perror("clearLog");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800559 exit(EXIT_FAILURE);
560 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800561 }
562
563 if (getLogSize) {
564 int size, readable;
565
Mark Salyzyn95132e92013-11-22 10:55:48 -0800566 size = android_logger_get_log_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800567 if (size < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800568 perror("getLogSize");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800569 exit(EXIT_FAILURE);
570 }
571
Mark Salyzyn95132e92013-11-22 10:55:48 -0800572 readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800573 if (readable < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800574 perror("getLogReadableSize");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800575 exit(EXIT_FAILURE);
576 }
577
578 printf("%s: ring buffer is %dKb (%dKb consumed), "
579 "max entry is %db, max payload is %db\n", dev->device,
580 size / 1024, readable / 1024,
581 (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
582 }
583
584 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800585 }
586
587 if (getLogSize) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700588 exit(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800589 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800590 if (clearLog) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700591 exit(0);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800592 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800593
594 //LOG_EVENT_INT(10, 12345);
595 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
596 //LOG_EVENT_STRING(0, "whassup, doc?");
597
Joe Onorato6fa09a02010-02-26 10:04:23 -0800598 if (needBinary)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800599 android::g_eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
600
Mark Salyzyn95132e92013-11-22 10:55:48 -0800601 while (1) {
602 struct log_msg log_msg;
603 int ret = android_logger_list_read(logger_list, &log_msg);
604
605 if (ret == 0) {
606 fprintf(stderr, "read: Unexpected EOF!\n");
607 exit(EXIT_FAILURE);
608 }
609
610 if (ret < 0) {
611 if (ret == -EAGAIN) {
612 break;
613 }
614
615 if (ret == -EIO) {
616 fprintf(stderr, "read: Unexpected EOF!\n");
617 exit(EXIT_FAILURE);
618 }
619 if (ret == -EINVAL) {
620 fprintf(stderr, "read: unexpected length.\n");
621 exit(EXIT_FAILURE);
622 }
623 perror("logcat read");
624 exit(EXIT_FAILURE);
625 }
626
627 for(dev = devices; dev; dev = dev->next) {
628 if (android_name_to_log_id(dev->device) == log_msg.id()) {
629 break;
630 }
631 }
632 if (!dev) {
633 fprintf(stderr, "read: Unexpected log ID!\n");
634 exit(EXIT_FAILURE);
635 }
636
637 android::maybePrintStart(dev);
638 if (android::g_printBinary) {
639 android::printBinary(&log_msg);
640 } else {
641 android::processBuffer(dev, &log_msg);
642 }
643 }
644
645 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800646
647 return 0;
648}