blob: 00b5ba94b55af34ef28af1e608286195625044ac [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"
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800226 " -T <count> print only the most recent <count> lines (does not imply -d)\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227 " -g get the size of the log's ring buffer and exit\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800228 " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n"
229 " 'events' or 'all'. Multiple -b parameters are allowed and\n"
Wink Savilleba9608f2010-06-18 10:15:08 -0700230 " results are interleaved. The default is -b main -b system.\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800231 " -B output the log in binary.\n"
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800232 " -S output statistics.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800234#ifdef USERDEBUG_BUILD
235
236 fprintf(stderr, "--------------------- eng & userdebug builds only ---------------------------\n"
237 " -G <count> set size of log's ring buffer and exit\n"
238 " -p output prune white and ~black list\n"
239 " -P '<list> ...' set prune white and ~black list; UID, /PID or !(worst UID)\n"
240 " default is ~!, prune worst UID.\n"
241 "-----------------------------------------------------------------------------\n"
242 );
243
244#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245
246 fprintf(stderr,"\nfilterspecs are a series of \n"
247 " <tag>[:priority]\n\n"
248 "where <tag> is a log component tag (or * for all) and priority is:\n"
249 " V Verbose\n"
250 " D Debug\n"
251 " I Info\n"
252 " W Warn\n"
253 " E Error\n"
254 " F Fatal\n"
255 " S Silent (supress all output)\n"
256 "\n'*' means '*:d' and <tag> by itself means <tag>:v\n"
257 "\nIf not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.\n"
258 "If no filterspec is found, filter defaults to '*:I'\n"
259 "\nIf not specified with -v, format is set from ANDROID_PRINTF_LOG\n"
260 "or defaults to \"brief\"\n\n");
261
262
263
264}
265
266
267} /* namespace android */
268
269static int setLogFormat(const char * formatString)
270{
271 static AndroidLogPrintFormat format;
272
273 format = android_log_formatFromString(formatString);
274
275 if (format == FORMAT_OFF) {
276 // FORMAT_OFF means invalid string
277 return -1;
278 }
279
280 android_log_setPrintFormat(g_logformat, format);
281
282 return 0;
283}
284
285extern "C" void logprint_run_tests(void);
286
Joe Onorato6fa09a02010-02-26 10:04:23 -0800287int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289 int err;
290 int hasSetLogFormat = 0;
291 int clearLog = 0;
292 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800293#ifdef USERDEBUG_BUILD
294 unsigned long setLogSize = 0;
295 int getPruneList = 0;
296 char *setPruneList = NULL;
297#endif
Mark Salyzyn34facab2014-02-06 14:48:50 -0800298 int printStatistics = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299 int mode = O_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800301 log_device_t* devices = NULL;
302 log_device_t* dev;
303 bool needBinary = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800304 struct logger_list *logger_list;
305 int tail_lines = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800307 signal(SIGPIPE, exit);
308
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309 g_logformat = android_log_format_new();
310
311 if (argc == 2 && 0 == strcmp(argv[1], "--test")) {
312 logprint_run_tests();
313 exit(0);
314 }
315
316 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
317 android::show_help(argv[0]);
318 exit(0);
319 }
320
321 for (;;) {
322 int ret;
323
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800324 ret = getopt(argc, argv,
325#ifdef USERDEBUG_BUILD
326 "cdt:T:gG:sQf:r::n:v:b:BSpP:"
327#else
328 "cdt:T:gsQf:r::n:v:b:BS"
329#endif
330 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331
332 if (ret < 0) {
333 break;
334 }
335
336 switch(ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800337 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 // default to all silent
339 android_log_addFilterRule(g_logformat, "*:s");
340 break;
341
342 case 'c':
343 clearLog = 1;
344 mode = O_WRONLY;
345 break;
346
347 case 'd':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800348 mode = O_RDONLY | O_NDELAY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349 break;
350
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800351 case 't':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800352 mode = O_RDONLY | O_NDELAY;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800353 /* FALLTHRU */
354 case 'T':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800355 tail_lines = atoi(optarg);
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800356 break;
357
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358 case 'g':
359 getLogSize = 1;
360 break;
361
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800362#ifdef USERDEBUG_BUILD
363
364 case 'G': {
365 // would use atol if not for the multiplier
366 char *cp = optarg;
367 setLogSize = 0;
368 while (('0' <= *cp) && (*cp <= '9')) {
369 setLogSize *= 10;
370 setLogSize += *cp - '0';
371 ++cp;
372 }
373
374 switch(*cp) {
375 case 'g':
376 case 'G':
377 setLogSize *= 1024;
378 /* FALLTHRU */
379 case 'm':
380 case 'M':
381 setLogSize *= 1024;
382 /* FALLTHRU */
383 case 'k':
384 case 'K':
385 setLogSize *= 1024;
386 /* FALLTHRU */
387 case '\0':
388 break;
389
390 default:
391 setLogSize = 0;
392 }
393
394 if (!setLogSize) {
395 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
396 exit(1);
397 }
398 }
399 break;
400
401 case 'p':
402 getPruneList = 1;
403 break;
404
405 case 'P':
406 setPruneList = optarg;
407 break;
408
409#endif
410
Joe Onorato6fa09a02010-02-26 10:04:23 -0800411 case 'b': {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800412 if (strcmp(optarg, "all") == 0) {
413 while (devices) {
414 dev = devices;
415 devices = dev->next;
416 delete dev;
417 }
418
419 dev = devices = new log_device_t("main", false, 'm');
420 android::g_devCount = 1;
421 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
422 dev->next = new log_device_t("system", false, 's');
423 if (dev->next) {
424 dev = dev->next;
425 android::g_devCount++;
426 }
427 }
428 if (android_name_to_log_id("radio") == LOG_ID_RADIO) {
429 dev->next = new log_device_t("radio", false, 'r');
430 if (dev->next) {
431 dev = dev->next;
432 android::g_devCount++;
433 }
434 }
435 if (android_name_to_log_id("events") == LOG_ID_EVENTS) {
436 dev->next = new log_device_t("events", true, 'e');
437 if (dev->next) {
438 android::g_devCount++;
439 needBinary = true;
440 }
441 }
442 break;
443 }
444
Joe Onorato6fa09a02010-02-26 10:04:23 -0800445 bool binary = strcmp(optarg, "events") == 0;
446 if (binary) {
447 needBinary = true;
448 }
449
450 if (devices) {
451 dev = devices;
452 while (dev->next) {
453 dev = dev->next;
454 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800455 dev->next = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800456 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800457 devices = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800458 }
459 android::g_devCount++;
460 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800461 break;
462
463 case 'B':
464 android::g_printBinary = 1;
465 break;
466
467 case 'f':
468 // redirect output to a file
469
470 android::g_outputFileName = optarg;
471
472 break;
473
474 case 'r':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800475 if (optarg == NULL) {
476 android::g_logRotateSizeKBytes
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477 = DEFAULT_LOG_ROTATE_SIZE_KBYTES;
478 } else {
479 long logRotateSize;
480 char *lastDigit;
481
482 if (!isdigit(optarg[0])) {
483 fprintf(stderr,"Invalid parameter to -r\n");
484 android::show_help(argv[0]);
485 exit(-1);
486 }
487 android::g_logRotateSizeKBytes = atoi(optarg);
488 }
489 break;
490
491 case 'n':
492 if (!isdigit(optarg[0])) {
493 fprintf(stderr,"Invalid parameter to -r\n");
494 android::show_help(argv[0]);
495 exit(-1);
496 }
497
498 android::g_maxRotatedLogs = atoi(optarg);
499 break;
500
501 case 'v':
502 err = setLogFormat (optarg);
503 if (err < 0) {
504 fprintf(stderr,"Invalid parameter to -v\n");
505 android::show_help(argv[0]);
506 exit(-1);
507 }
508
509 hasSetLogFormat = 1;
510 break;
511
512 case 'Q':
513 /* this is a *hidden* option used to start a version of logcat */
514 /* in an emulated device only. it basically looks for androidboot.logcat= */
515 /* on the kernel command line. If something is found, it extracts a log filter */
516 /* and uses it to run the program. If nothing is found, the program should */
517 /* quit immediately */
518#define KERNEL_OPTION "androidboot.logcat="
519#define CONSOLE_OPTION "androidboot.console="
520 {
521 int fd;
522 char* logcat;
523 char* console;
524 int force_exit = 1;
525 static char cmdline[1024];
526
527 fd = open("/proc/cmdline", O_RDONLY);
528 if (fd >= 0) {
529 int n = read(fd, cmdline, sizeof(cmdline)-1 );
530 if (n < 0) n = 0;
531 cmdline[n] = 0;
532 close(fd);
533 } else {
534 cmdline[0] = 0;
535 }
536
537 logcat = strstr( cmdline, KERNEL_OPTION );
538 console = strstr( cmdline, CONSOLE_OPTION );
539 if (logcat != NULL) {
540 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
541 char* q = strpbrk( p, " \t\n\r" );;
542
543 if (q != NULL)
544 *q = 0;
545
546 forceFilters = p;
547 force_exit = 0;
548 }
549 /* if nothing found or invalid filters, exit quietly */
550 if (force_exit)
551 exit(0);
552
553 /* redirect our output to the emulator console */
554 if (console) {
555 char* p = console + sizeof(CONSOLE_OPTION)-1;
556 char* q = strpbrk( p, " \t\n\r" );
557 char devname[64];
558 int len;
559
560 if (q != NULL) {
561 len = q - p;
562 } else
563 len = strlen(p);
564
565 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
566 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
567 if (len < (int)sizeof(devname)) {
568 fd = open( devname, O_WRONLY );
569 if (fd >= 0) {
570 dup2(fd, 1);
571 dup2(fd, 2);
572 close(fd);
573 }
574 }
575 }
576 }
577 break;
578
Mark Salyzyn34facab2014-02-06 14:48:50 -0800579 case 'S':
580 printStatistics = 1;
581 break;
582
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800583 default:
584 fprintf(stderr,"Unrecognized Option\n");
585 android::show_help(argv[0]);
586 exit(-1);
587 break;
588 }
589 }
590
Joe Onorato6fa09a02010-02-26 10:04:23 -0800591 if (!devices) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800592 devices = new log_device_t("main", false, 'm');
Joe Onorato6fa09a02010-02-26 10:04:23 -0800593 android::g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800594 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
595 devices->next = new log_device_t("system", false, 's');
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800596 android::g_devCount++;
597 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800598 }
599
Mark Salyzyn95132e92013-11-22 10:55:48 -0800600 if (android::g_logRotateSizeKBytes != 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800601 && android::g_outputFileName == NULL
602 ) {
603 fprintf(stderr,"-r requires -f as well\n");
604 android::show_help(argv[0]);
605 exit(-1);
606 }
607
608 android::setupOutput();
609
610 if (hasSetLogFormat == 0) {
611 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
612
613 if (logFormat != NULL) {
614 err = setLogFormat(logFormat);
615
616 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800617 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800618 logFormat);
619 }
620 }
621 }
622
623 if (forceFilters) {
624 err = android_log_addFilterString(g_logformat, forceFilters);
625 if (err < 0) {
626 fprintf (stderr, "Invalid filter expression in -logcat option\n");
627 exit(0);
628 }
629 } else if (argc == optind) {
630 // Add from environment variable
631 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
632
633 if (env_tags_orig != NULL) {
634 err = android_log_addFilterString(g_logformat, env_tags_orig);
635
Mark Salyzyn95132e92013-11-22 10:55:48 -0800636 if (err < 0) {
637 fprintf(stderr, "Invalid filter expression in"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800638 " ANDROID_LOG_TAGS\n");
639 android::show_help(argv[0]);
640 exit(-1);
641 }
642 }
643 } else {
644 // Add from commandline
645 for (int i = optind ; i < argc ; i++) {
646 err = android_log_addFilterString(g_logformat, argv[i]);
647
Mark Salyzyn95132e92013-11-22 10:55:48 -0800648 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800649 fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]);
650 android::show_help(argv[0]);
651 exit(-1);
652 }
653 }
654 }
655
Joe Onorato6fa09a02010-02-26 10:04:23 -0800656 dev = devices;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800657 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800658 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800659 dev->logger_list = logger_list;
660 dev->logger = android_logger_open(logger_list,
661 android_name_to_log_id(dev->device));
662 if (!dev->logger) {
663 fprintf(stderr, "Unable to open log device '%s'\n", dev->device);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800664 exit(EXIT_FAILURE);
665 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800666
667 if (clearLog) {
668 int ret;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800669 ret = android_logger_clear(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800670 if (ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800671 perror("clearLog");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800672 exit(EXIT_FAILURE);
673 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800674 }
675
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800676#ifdef USERDEBUG_BUILD
677
678 if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) {
679 perror("setLogSize");
680 exit(EXIT_FAILURE);
681 }
682
683#endif
684
Joe Onorato6fa09a02010-02-26 10:04:23 -0800685 if (getLogSize) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800686 long size, readable;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800687
Mark Salyzyn95132e92013-11-22 10:55:48 -0800688 size = android_logger_get_log_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800689 if (size < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800690 perror("getLogSize");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800691 exit(EXIT_FAILURE);
692 }
693
Mark Salyzyn95132e92013-11-22 10:55:48 -0800694 readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800695 if (readable < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800696 perror("getLogReadableSize");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800697 exit(EXIT_FAILURE);
698 }
699
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800700 printf("%s: ring buffer is %ldKb (%ldKb consumed), "
Joe Onorato6fa09a02010-02-26 10:04:23 -0800701 "max entry is %db, max payload is %db\n", dev->device,
702 size / 1024, readable / 1024,
703 (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
704 }
705
706 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800707 }
708
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800709#ifdef USERDEBUG_BUILD
710
711 if (setPruneList) {
712 size_t len = strlen(setPruneList) + 32; // margin to allow rc
713 char *buf = (char *) malloc(len);
714
715 strcpy(buf, setPruneList);
716 int ret = android_logger_set_prune_list(logger_list, buf, len);
717 free(buf);
718
719 if (ret) {
720 perror("setPruneList");
721 exit(EXIT_FAILURE);
722 }
723 }
724
725#endif
726
727 if (
728#ifdef USERDEBUG_BUILD
729 printStatistics || getPruneList
730#else
731 printStatistics
732#endif
733 ) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800734 size_t len = 8192;
735 char *buf;
736
737 for(int retry = 32;
738 (retry >= 0) && ((buf = new char [len]));
739 delete [] buf, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800740#ifdef USERDEBUG_BUILD
741 if (getPruneList) {
742 android_logger_get_prune_list(logger_list, buf, len);
743 } else {
744 android_logger_get_statistics(logger_list, buf, len);
745 }
746#else
Mark Salyzyn34facab2014-02-06 14:48:50 -0800747 android_logger_get_statistics(logger_list, buf, len);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800748#endif
Mark Salyzyn34facab2014-02-06 14:48:50 -0800749 buf[len-1] = '\0';
750 size_t ret = atol(buf) + 1;
751 if (ret < 4) {
752 delete [] buf;
753 buf = NULL;
754 break;
755 }
756 bool check = ret <= len;
757 len = ret;
758 if (check) {
759 break;
760 }
761 }
762
763 if (!buf) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800764 perror("response read");
Mark Salyzyn34facab2014-02-06 14:48:50 -0800765 exit(EXIT_FAILURE);
766 }
767
768 // remove trailing FF
769 char *cp = buf + len - 1;
770 *cp = '\0';
771 bool truncated = *--cp != '\f';
772 if (!truncated) {
773 *cp = '\0';
774 }
775
776 // squash out the byte count
777 cp = buf;
778 if (!truncated) {
779 while (isdigit(*cp) || (*cp == '\n')) {
780 ++cp;
781 }
782 }
783
784 printf("%s", cp);
785 delete [] buf;
786 exit(0);
787 }
788
789
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800790 if (getLogSize) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700791 exit(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800792 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800793#ifdef USERDEBUG_BUILD
794 if (setLogSize || setPruneList) {
795 exit(0);
796 }
797#endif
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800798 if (clearLog) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700799 exit(0);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800800 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800801
802 //LOG_EVENT_INT(10, 12345);
803 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
804 //LOG_EVENT_STRING(0, "whassup, doc?");
805
Joe Onorato6fa09a02010-02-26 10:04:23 -0800806 if (needBinary)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800807 android::g_eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
808
Mark Salyzyn95132e92013-11-22 10:55:48 -0800809 while (1) {
810 struct log_msg log_msg;
811 int ret = android_logger_list_read(logger_list, &log_msg);
812
813 if (ret == 0) {
814 fprintf(stderr, "read: Unexpected EOF!\n");
815 exit(EXIT_FAILURE);
816 }
817
818 if (ret < 0) {
819 if (ret == -EAGAIN) {
820 break;
821 }
822
823 if (ret == -EIO) {
824 fprintf(stderr, "read: Unexpected EOF!\n");
825 exit(EXIT_FAILURE);
826 }
827 if (ret == -EINVAL) {
828 fprintf(stderr, "read: unexpected length.\n");
829 exit(EXIT_FAILURE);
830 }
831 perror("logcat read");
832 exit(EXIT_FAILURE);
833 }
834
835 for(dev = devices; dev; dev = dev->next) {
836 if (android_name_to_log_id(dev->device) == log_msg.id()) {
837 break;
838 }
839 }
840 if (!dev) {
841 fprintf(stderr, "read: Unexpected log ID!\n");
842 exit(EXIT_FAILURE);
843 }
844
845 android::maybePrintStart(dev);
846 if (android::g_printBinary) {
847 android::printBinary(&log_msg);
848 } else {
849 android::processBuffer(dev, &log_msg);
850 }
851 }
852
853 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800854
855 return 0;
856}