blob: 7bf91a9afad08f2e900ffe854a1149921f3d3574 [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>
Mark Salyzynfa3716b2014-02-14 16:05:05 -080020#include <log/log_read.h>
Colin Cross9227bd32013-07-23 16:59:20 -070021#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 Projectdd7bc332009-03-03 19:32:55 -080025
26#define DEFAULT_LOG_ROTATE_SIZE_KBYTES 16
27#define DEFAULT_MAX_ROTATED_LOGS 4
28
29static AndroidLogFormat * g_logformat;
30
31/* logd prefixes records with a length field */
32#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
33
Joe Onorato6fa09a02010-02-26 10:04:23 -080034struct log_device_t {
Mark Salyzyn95132e92013-11-22 10:55:48 -080035 const char* device;
Joe Onorato6fa09a02010-02-26 10:04:23 -080036 bool binary;
Mark Salyzyn95132e92013-11-22 10:55:48 -080037 struct logger *logger;
38 struct logger_list *logger_list;
Joe Onorato6fa09a02010-02-26 10:04:23 -080039 bool printed;
40 char label;
41
Joe Onorato6fa09a02010-02-26 10:04:23 -080042 log_device_t* next;
43
Mark Salyzyn95132e92013-11-22 10:55:48 -080044 log_device_t(const char* d, bool b, char l) {
Joe Onorato6fa09a02010-02-26 10:04:23 -080045 device = d;
46 binary = b;
47 label = l;
48 next = NULL;
49 printed = false;
50 }
Joe Onorato6fa09a02010-02-26 10:04:23 -080051};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052
53namespace android {
54
55/* Global Variables */
56
57static const char * g_outputFileName = NULL;
58static int g_logRotateSizeKBytes = 0; // 0 means "no log rotation"
59static int g_maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS; // 0 means "unbounded"
60static int g_outFD = -1;
61static off_t g_outByteCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062static int g_printBinary = 0;
Joe Onorato6fa09a02010-02-26 10:04:23 -080063static int g_devCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064
65static EventTagMap* g_eventTagMap = NULL;
66
67static int openLogFile (const char *pathname)
68{
Edwin Vane80b221c2012-08-13 12:55:07 -040069 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070}
71
72static 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 Salyzyn95132e92013-11-22 10:55:48 -0800115void printBinary(struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116{
Mark Salyzyn95132e92013-11-22 10:55:48 -0800117 size_t size = buf->len();
118
119 TEMP_FAILURE_RETRY(write(g_outFD, buf, size));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120}
121
Mark Salyzyn95132e92013-11-22 10:55:48 -0800122static void processBuffer(log_device_t* dev, struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800123{
Mathias Agopian50844522010-03-17 16:10:26 -0700124 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125 int err;
126 AndroidLogEntry entry;
127 char binaryMsgBuf[1024];
128
Joe Onorato6fa09a02010-02-26 10:04:23 -0800129 if (dev->binary) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800130 err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry,
131 g_eventTagMap,
132 binaryMsgBuf,
133 sizeof(binaryMsgBuf));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134 //printf(">>> pri=%d len=%d msg='%s'\n",
135 // entry.priority, entry.messageLen, entry.message);
136 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800137 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800139 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140 goto error;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800141 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800143 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 Projectdd7bc332009-03-03 19:32:55 -0800153
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800154 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 Projectdd7bc332009-03-03 19:32:55 -0800160 }
161
162 g_outByteCount += bytesWritten;
163
Mark Salyzyn95132e92013-11-22 10:55:48 -0800164 if (g_logRotateSizeKBytes > 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165 && (g_outByteCount / 1024) >= g_logRotateSizeKBytes
166 ) {
167 rotateLogs();
168 }
169
170error:
171 //fprintf (stderr, "Error processing record\n");
172 return;
173}
174
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800175static 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 Salyzyn95132e92013-11-22 10:55:48 -0800180 snprintf(buf, sizeof(buf), "--------- beginning of %s\n",
181 dev->device);
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800182 if (write(g_outFD, buf, strlen(buf)) < 0) {
183 perror("output error");
184 exit(-1);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800185 }
186 }
187 }
188}
189
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190static 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
212static 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 Egnord1d3b6d2010-03-11 20:32:17 -0800226 " -t <count> print only the most recent <count> lines (implies -d)\n"
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800227 " -T <count> print only the most recent <count> lines (does not imply -d)\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228 " -g get the size of the log's ring buffer and exit\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800229 " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n"
230 " 'events' or 'all'. Multiple -b parameters are allowed and\n"
Wink Savilleba9608f2010-06-18 10:15:08 -0700231 " results are interleaved. The default is -b main -b system.\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800232 " -B output the log in binary.\n"
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800233 " -S output statistics.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800235#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 Projectdd7bc332009-03-03 19:32:55 -0800246
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
270static 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
286extern "C" void logprint_run_tests(void);
287
Joe Onorato6fa09a02010-02-26 10:04:23 -0800288int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800290 int err;
291 int hasSetLogFormat = 0;
292 int clearLog = 0;
293 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800294#ifdef USERDEBUG_BUILD
295 unsigned long setLogSize = 0;
296 int getPruneList = 0;
297 char *setPruneList = NULL;
298#endif
Mark Salyzyn34facab2014-02-06 14:48:50 -0800299 int printStatistics = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 int mode = O_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800302 log_device_t* devices = NULL;
303 log_device_t* dev;
304 bool needBinary = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800305 struct logger_list *logger_list;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800306 unsigned int tail_lines = 0;
307 log_time tail_time(log_time::EPOCH);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800309 signal(SIGPIPE, exit);
310
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800311 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 Salyzyndfa7a072014-02-11 12:29:31 -0800326 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 Projectdd7bc332009-03-03 19:32:55 -0800333
334 if (ret < 0) {
335 break;
336 }
337
338 switch(ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800339 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340 // 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 Salyzyn95132e92013-11-22 10:55:48 -0800350 mode = O_RDONLY | O_NDELAY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351 break;
352
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800353 case 't':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800354 mode = O_RDONLY | O_NDELAY;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800355 /* FALLTHRU */
356 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800357 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 Egnord1d3b6d2010-03-11 20:32:17 -0800383 break;
384
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385 case 'g':
386 getLogSize = 1;
387 break;
388
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800389#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 Onorato6fa09a02010-02-26 10:04:23 -0800438 case 'b': {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800439 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 Onorato6fa09a02010-02-26 10:04:23 -0800472 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 Salyzyn95132e92013-11-22 10:55:48 -0800482 dev->next = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800483 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800484 devices = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800485 }
486 android::g_devCount++;
487 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800488 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 Salyzyn95132e92013-11-22 10:55:48 -0800502 if (optarg == NULL) {
503 android::g_logRotateSizeKBytes
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 = 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 Salyzyn34facab2014-02-06 14:48:50 -0800606 case 'S':
607 printStatistics = 1;
608 break;
609
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800610 default:
611 fprintf(stderr,"Unrecognized Option\n");
612 android::show_help(argv[0]);
613 exit(-1);
614 break;
615 }
616 }
617
Joe Onorato6fa09a02010-02-26 10:04:23 -0800618 if (!devices) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800619 devices = new log_device_t("main", false, 'm');
Joe Onorato6fa09a02010-02-26 10:04:23 -0800620 android::g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800621 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
622 devices->next = new log_device_t("system", false, 's');
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800623 android::g_devCount++;
624 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800625 }
626
Mark Salyzyn95132e92013-11-22 10:55:48 -0800627 if (android::g_logRotateSizeKBytes != 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800628 && 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 Salyzyn95132e92013-11-22 10:55:48 -0800644 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800645 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 Salyzyn95132e92013-11-22 10:55:48 -0800663 if (err < 0) {
664 fprintf(stderr, "Invalid filter expression in"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800665 " 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 Salyzyn95132e92013-11-22 10:55:48 -0800675 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800676 fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]);
677 android::show_help(argv[0]);
678 exit(-1);
679 }
680 }
681 }
682
Joe Onorato6fa09a02010-02-26 10:04:23 -0800683 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800684 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 Onorato6fa09a02010-02-26 10:04:23 -0800689 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800690 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 Projectdd7bc332009-03-03 19:32:55 -0800695 exit(EXIT_FAILURE);
696 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800697
698 if (clearLog) {
699 int ret;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800700 ret = android_logger_clear(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800701 if (ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800702 perror("clearLog");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800703 exit(EXIT_FAILURE);
704 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800705 }
706
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800707#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 Onorato6fa09a02010-02-26 10:04:23 -0800716 if (getLogSize) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800717 long size, readable;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800718
Mark Salyzyn95132e92013-11-22 10:55:48 -0800719 size = android_logger_get_log_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800720 if (size < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800721 perror("getLogSize");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800722 exit(EXIT_FAILURE);
723 }
724
Mark Salyzyn95132e92013-11-22 10:55:48 -0800725 readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800726 if (readable < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800727 perror("getLogReadableSize");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800728 exit(EXIT_FAILURE);
729 }
730
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800731 printf("%s: ring buffer is %ldKb (%ldKb consumed), "
Joe Onorato6fa09a02010-02-26 10:04:23 -0800732 "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 Projectdd7bc332009-03-03 19:32:55 -0800738 }
739
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800740#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 Salyzyn34facab2014-02-06 14:48:50 -0800765 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 Salyzyndfa7a072014-02-11 12:29:31 -0800771#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 Salyzyn34facab2014-02-06 14:48:50 -0800778 android_logger_get_statistics(logger_list, buf, len);
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800779#endif
Mark Salyzyn34facab2014-02-06 14:48:50 -0800780 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 Salyzyndfa7a072014-02-11 12:29:31 -0800795 perror("response read");
Mark Salyzyn34facab2014-02-06 14:48:50 -0800796 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 Projectdd7bc332009-03-03 19:32:55 -0800821 if (getLogSize) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700822 exit(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800823 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800824#ifdef USERDEBUG_BUILD
825 if (setLogSize || setPruneList) {
826 exit(0);
827 }
828#endif
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800829 if (clearLog) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700830 exit(0);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800831 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800832
833 //LOG_EVENT_INT(10, 12345);
834 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
835 //LOG_EVENT_STRING(0, "whassup, doc?");
836
Joe Onorato6fa09a02010-02-26 10:04:23 -0800837 if (needBinary)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800838 android::g_eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
839
Mark Salyzyn95132e92013-11-22 10:55:48 -0800840 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 Projectdd7bc332009-03-03 19:32:55 -0800885
886 return 0;
887}