blob: 00a60bd863fcb21909a023e559532215153d3c33 [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 Salyzyn1c950472014-04-01 17:19:47 -0700235 fprintf(stderr, " -G <count> set size of log's ring buffer and exit\n"
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800236 " -p output prune white and ~black list\n"
237 " -P '<list> ...' set prune white and ~black list; UID, /PID or !(worst UID)\n"
Mark Salyzyn1c950472014-04-01 17:19:47 -0700238 " default is ~!, prune worst UID.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239
240 fprintf(stderr,"\nfilterspecs are a series of \n"
241 " <tag>[:priority]\n\n"
242 "where <tag> is a log component tag (or * for all) and priority is:\n"
243 " V Verbose\n"
244 " D Debug\n"
245 " I Info\n"
246 " W Warn\n"
247 " E Error\n"
248 " F Fatal\n"
249 " S Silent (supress all output)\n"
250 "\n'*' means '*:d' and <tag> by itself means <tag>:v\n"
251 "\nIf not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.\n"
252 "If no filterspec is found, filter defaults to '*:I'\n"
253 "\nIf not specified with -v, format is set from ANDROID_PRINTF_LOG\n"
254 "or defaults to \"brief\"\n\n");
255
256
257
258}
259
260
261} /* namespace android */
262
263static int setLogFormat(const char * formatString)
264{
265 static AndroidLogPrintFormat format;
266
267 format = android_log_formatFromString(formatString);
268
269 if (format == FORMAT_OFF) {
270 // FORMAT_OFF means invalid string
271 return -1;
272 }
273
274 android_log_setPrintFormat(g_logformat, format);
275
276 return 0;
277}
278
279extern "C" void logprint_run_tests(void);
280
Joe Onorato6fa09a02010-02-26 10:04:23 -0800281int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800283 int err;
284 int hasSetLogFormat = 0;
285 int clearLog = 0;
286 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800287 unsigned long setLogSize = 0;
288 int getPruneList = 0;
289 char *setPruneList = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800290 int printStatistics = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291 int mode = O_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800293 log_device_t* devices = NULL;
294 log_device_t* dev;
295 bool needBinary = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800296 struct logger_list *logger_list;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800297 unsigned int tail_lines = 0;
298 log_time tail_time(log_time::EPOCH);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800300 signal(SIGPIPE, exit);
301
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302 g_logformat = android_log_format_new();
303
304 if (argc == 2 && 0 == strcmp(argv[1], "--test")) {
305 logprint_run_tests();
306 exit(0);
307 }
308
309 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
310 android::show_help(argv[0]);
311 exit(0);
312 }
313
314 for (;;) {
315 int ret;
316
Mark Salyzyn1c950472014-04-01 17:19:47 -0700317 ret = getopt(argc, argv, "cdt:T:gG:sQf:r::n:v:b:BSpP:");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800318
319 if (ret < 0) {
320 break;
321 }
322
323 switch(ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800324 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 // default to all silent
326 android_log_addFilterRule(g_logformat, "*:s");
327 break;
328
329 case 'c':
330 clearLog = 1;
331 mode = O_WRONLY;
332 break;
333
334 case 'd':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800335 mode = O_RDONLY | O_NDELAY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 break;
337
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800338 case 't':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800339 mode = O_RDONLY | O_NDELAY;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800340 /* FALLTHRU */
341 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800342 if (strspn(optarg, "0123456789") != strlen(optarg)) {
343 char *cp = tail_time.strptime(optarg,
344 log_time::default_format);
345 if (!cp) {
346 fprintf(stderr,
347 "ERROR: -%c \"%s\" not in \"%s\" time format\n",
348 ret, optarg, log_time::default_format);
349 exit(1);
350 }
351 if (*cp) {
352 char c = *cp;
353 *cp = '\0';
354 fprintf(stderr,
355 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
356 ret, optarg, c, cp + 1);
357 *cp = c;
358 }
359 } else {
360 tail_lines = atoi(optarg);
361 if (!tail_lines) {
362 fprintf(stderr,
363 "WARNING: -%c %s invalid, setting to 1\n",
364 ret, optarg);
365 tail_lines = 1;
366 }
367 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800368 break;
369
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370 case 'g':
371 getLogSize = 1;
372 break;
373
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800374 case 'G': {
375 // would use atol if not for the multiplier
376 char *cp = optarg;
377 setLogSize = 0;
378 while (('0' <= *cp) && (*cp <= '9')) {
379 setLogSize *= 10;
380 setLogSize += *cp - '0';
381 ++cp;
382 }
383
384 switch(*cp) {
385 case 'g':
386 case 'G':
387 setLogSize *= 1024;
388 /* FALLTHRU */
389 case 'm':
390 case 'M':
391 setLogSize *= 1024;
392 /* FALLTHRU */
393 case 'k':
394 case 'K':
395 setLogSize *= 1024;
396 /* FALLTHRU */
397 case '\0':
398 break;
399
400 default:
401 setLogSize = 0;
402 }
403
404 if (!setLogSize) {
405 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
406 exit(1);
407 }
408 }
409 break;
410
411 case 'p':
412 getPruneList = 1;
413 break;
414
415 case 'P':
416 setPruneList = optarg;
417 break;
418
Joe Onorato6fa09a02010-02-26 10:04:23 -0800419 case 'b': {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800420 if (strcmp(optarg, "all") == 0) {
421 while (devices) {
422 dev = devices;
423 devices = dev->next;
424 delete dev;
425 }
426
427 dev = devices = new log_device_t("main", false, 'm');
428 android::g_devCount = 1;
429 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
430 dev->next = new log_device_t("system", false, 's');
431 if (dev->next) {
432 dev = dev->next;
433 android::g_devCount++;
434 }
435 }
436 if (android_name_to_log_id("radio") == LOG_ID_RADIO) {
437 dev->next = new log_device_t("radio", false, 'r');
438 if (dev->next) {
439 dev = dev->next;
440 android::g_devCount++;
441 }
442 }
443 if (android_name_to_log_id("events") == LOG_ID_EVENTS) {
444 dev->next = new log_device_t("events", true, 'e');
445 if (dev->next) {
446 android::g_devCount++;
447 needBinary = true;
448 }
449 }
450 break;
451 }
452
Joe Onorato6fa09a02010-02-26 10:04:23 -0800453 bool binary = strcmp(optarg, "events") == 0;
454 if (binary) {
455 needBinary = true;
456 }
457
458 if (devices) {
459 dev = devices;
460 while (dev->next) {
461 dev = dev->next;
462 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800463 dev->next = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800464 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800465 devices = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800466 }
467 android::g_devCount++;
468 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 break;
470
471 case 'B':
472 android::g_printBinary = 1;
473 break;
474
475 case 'f':
476 // redirect output to a file
477
478 android::g_outputFileName = optarg;
479
480 break;
481
482 case 'r':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800483 if (optarg == NULL) {
484 android::g_logRotateSizeKBytes
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485 = DEFAULT_LOG_ROTATE_SIZE_KBYTES;
486 } else {
487 long logRotateSize;
488 char *lastDigit;
489
490 if (!isdigit(optarg[0])) {
491 fprintf(stderr,"Invalid parameter to -r\n");
492 android::show_help(argv[0]);
493 exit(-1);
494 }
495 android::g_logRotateSizeKBytes = atoi(optarg);
496 }
497 break;
498
499 case 'n':
500 if (!isdigit(optarg[0])) {
501 fprintf(stderr,"Invalid parameter to -r\n");
502 android::show_help(argv[0]);
503 exit(-1);
504 }
505
506 android::g_maxRotatedLogs = atoi(optarg);
507 break;
508
509 case 'v':
510 err = setLogFormat (optarg);
511 if (err < 0) {
512 fprintf(stderr,"Invalid parameter to -v\n");
513 android::show_help(argv[0]);
514 exit(-1);
515 }
516
517 hasSetLogFormat = 1;
518 break;
519
520 case 'Q':
521 /* this is a *hidden* option used to start a version of logcat */
522 /* in an emulated device only. it basically looks for androidboot.logcat= */
523 /* on the kernel command line. If something is found, it extracts a log filter */
524 /* and uses it to run the program. If nothing is found, the program should */
525 /* quit immediately */
526#define KERNEL_OPTION "androidboot.logcat="
527#define CONSOLE_OPTION "androidboot.console="
528 {
529 int fd;
530 char* logcat;
531 char* console;
532 int force_exit = 1;
533 static char cmdline[1024];
534
535 fd = open("/proc/cmdline", O_RDONLY);
536 if (fd >= 0) {
537 int n = read(fd, cmdline, sizeof(cmdline)-1 );
538 if (n < 0) n = 0;
539 cmdline[n] = 0;
540 close(fd);
541 } else {
542 cmdline[0] = 0;
543 }
544
545 logcat = strstr( cmdline, KERNEL_OPTION );
546 console = strstr( cmdline, CONSOLE_OPTION );
547 if (logcat != NULL) {
548 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
549 char* q = strpbrk( p, " \t\n\r" );;
550
551 if (q != NULL)
552 *q = 0;
553
554 forceFilters = p;
555 force_exit = 0;
556 }
557 /* if nothing found or invalid filters, exit quietly */
558 if (force_exit)
559 exit(0);
560
561 /* redirect our output to the emulator console */
562 if (console) {
563 char* p = console + sizeof(CONSOLE_OPTION)-1;
564 char* q = strpbrk( p, " \t\n\r" );
565 char devname[64];
566 int len;
567
568 if (q != NULL) {
569 len = q - p;
570 } else
571 len = strlen(p);
572
573 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
574 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
575 if (len < (int)sizeof(devname)) {
576 fd = open( devname, O_WRONLY );
577 if (fd >= 0) {
578 dup2(fd, 1);
579 dup2(fd, 2);
580 close(fd);
581 }
582 }
583 }
584 }
585 break;
586
Mark Salyzyn34facab2014-02-06 14:48:50 -0800587 case 'S':
588 printStatistics = 1;
589 break;
590
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800591 default:
592 fprintf(stderr,"Unrecognized Option\n");
593 android::show_help(argv[0]);
594 exit(-1);
595 break;
596 }
597 }
598
Joe Onorato6fa09a02010-02-26 10:04:23 -0800599 if (!devices) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800600 devices = new log_device_t("main", false, 'm');
Joe Onorato6fa09a02010-02-26 10:04:23 -0800601 android::g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800602 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
603 devices->next = new log_device_t("system", false, 's');
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800604 android::g_devCount++;
605 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800606 }
607
Mark Salyzyn95132e92013-11-22 10:55:48 -0800608 if (android::g_logRotateSizeKBytes != 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800609 && android::g_outputFileName == NULL
610 ) {
611 fprintf(stderr,"-r requires -f as well\n");
612 android::show_help(argv[0]);
613 exit(-1);
614 }
615
616 android::setupOutput();
617
618 if (hasSetLogFormat == 0) {
619 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
620
621 if (logFormat != NULL) {
622 err = setLogFormat(logFormat);
623
624 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800625 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800626 logFormat);
627 }
628 }
629 }
630
631 if (forceFilters) {
632 err = android_log_addFilterString(g_logformat, forceFilters);
633 if (err < 0) {
634 fprintf (stderr, "Invalid filter expression in -logcat option\n");
635 exit(0);
636 }
637 } else if (argc == optind) {
638 // Add from environment variable
639 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
640
641 if (env_tags_orig != NULL) {
642 err = android_log_addFilterString(g_logformat, env_tags_orig);
643
Mark Salyzyn95132e92013-11-22 10:55:48 -0800644 if (err < 0) {
645 fprintf(stderr, "Invalid filter expression in"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800646 " ANDROID_LOG_TAGS\n");
647 android::show_help(argv[0]);
648 exit(-1);
649 }
650 }
651 } else {
652 // Add from commandline
653 for (int i = optind ; i < argc ; i++) {
654 err = android_log_addFilterString(g_logformat, argv[i]);
655
Mark Salyzyn95132e92013-11-22 10:55:48 -0800656 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800657 fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]);
658 android::show_help(argv[0]);
659 exit(-1);
660 }
661 }
662 }
663
Joe Onorato6fa09a02010-02-26 10:04:23 -0800664 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800665 if (tail_time != log_time::EPOCH) {
666 logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
667 } else {
668 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
669 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800670 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800671 dev->logger_list = logger_list;
672 dev->logger = android_logger_open(logger_list,
673 android_name_to_log_id(dev->device));
674 if (!dev->logger) {
675 fprintf(stderr, "Unable to open log device '%s'\n", dev->device);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800676 exit(EXIT_FAILURE);
677 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800678
679 if (clearLog) {
680 int ret;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800681 ret = android_logger_clear(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800682 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700683 perror("failed to clear the log");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800684 exit(EXIT_FAILURE);
685 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800686 }
687
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800688 if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700689 perror("failed to set the log size");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800690 exit(EXIT_FAILURE);
691 }
692
Joe Onorato6fa09a02010-02-26 10:04:23 -0800693 if (getLogSize) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800694 long size, readable;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800695
Mark Salyzyn95132e92013-11-22 10:55:48 -0800696 size = android_logger_get_log_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800697 if (size < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700698 perror("failed to get the log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800699 exit(EXIT_FAILURE);
700 }
701
Mark Salyzyn95132e92013-11-22 10:55:48 -0800702 readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800703 if (readable < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700704 perror("failed to get the readable log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800705 exit(EXIT_FAILURE);
706 }
707
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800708 printf("%s: ring buffer is %ldKb (%ldKb consumed), "
Joe Onorato6fa09a02010-02-26 10:04:23 -0800709 "max entry is %db, max payload is %db\n", dev->device,
710 size / 1024, readable / 1024,
711 (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
712 }
713
714 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800715 }
716
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800717 if (setPruneList) {
718 size_t len = strlen(setPruneList) + 32; // margin to allow rc
719 char *buf = (char *) malloc(len);
720
721 strcpy(buf, setPruneList);
722 int ret = android_logger_set_prune_list(logger_list, buf, len);
723 free(buf);
724
725 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700726 perror("failed to set the prune list");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800727 exit(EXIT_FAILURE);
728 }
729 }
730
Mark Salyzyn1c950472014-04-01 17:19:47 -0700731 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800732 size_t len = 8192;
733 char *buf;
734
735 for(int retry = 32;
736 (retry >= 0) && ((buf = new char [len]));
737 delete [] buf, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800738 if (getPruneList) {
739 android_logger_get_prune_list(logger_list, buf, len);
740 } else {
741 android_logger_get_statistics(logger_list, buf, len);
742 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800743 buf[len-1] = '\0';
744 size_t ret = atol(buf) + 1;
745 if (ret < 4) {
746 delete [] buf;
747 buf = NULL;
748 break;
749 }
750 bool check = ret <= len;
751 len = ret;
752 if (check) {
753 break;
754 }
755 }
756
757 if (!buf) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700758 perror("failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -0800759 exit(EXIT_FAILURE);
760 }
761
762 // remove trailing FF
763 char *cp = buf + len - 1;
764 *cp = '\0';
765 bool truncated = *--cp != '\f';
766 if (!truncated) {
767 *cp = '\0';
768 }
769
770 // squash out the byte count
771 cp = buf;
772 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -0700773 while (isdigit(*cp)) {
774 ++cp;
775 }
776 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800777 ++cp;
778 }
779 }
780
781 printf("%s", cp);
782 delete [] buf;
783 exit(0);
784 }
785
786
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800787 if (getLogSize) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700788 exit(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800789 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800790 if (setLogSize || setPruneList) {
791 exit(0);
792 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800793 if (clearLog) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700794 exit(0);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800795 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800796
797 //LOG_EVENT_INT(10, 12345);
798 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
799 //LOG_EVENT_STRING(0, "whassup, doc?");
800
Joe Onorato6fa09a02010-02-26 10:04:23 -0800801 if (needBinary)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800802 android::g_eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
803
Mark Salyzyn95132e92013-11-22 10:55:48 -0800804 while (1) {
805 struct log_msg log_msg;
806 int ret = android_logger_list_read(logger_list, &log_msg);
807
808 if (ret == 0) {
809 fprintf(stderr, "read: Unexpected EOF!\n");
810 exit(EXIT_FAILURE);
811 }
812
813 if (ret < 0) {
814 if (ret == -EAGAIN) {
815 break;
816 }
817
818 if (ret == -EIO) {
819 fprintf(stderr, "read: Unexpected EOF!\n");
820 exit(EXIT_FAILURE);
821 }
822 if (ret == -EINVAL) {
823 fprintf(stderr, "read: unexpected length.\n");
824 exit(EXIT_FAILURE);
825 }
Mark Salyzynfff04e32014-03-17 10:36:46 -0700826 perror("logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800827 exit(EXIT_FAILURE);
828 }
829
830 for(dev = devices; dev; dev = dev->next) {
831 if (android_name_to_log_id(dev->device) == log_msg.id()) {
832 break;
833 }
834 }
835 if (!dev) {
836 fprintf(stderr, "read: Unexpected log ID!\n");
837 exit(EXIT_FAILURE);
838 }
839
840 android::maybePrintStart(dev);
841 if (android::g_printBinary) {
842 android::printBinary(&log_msg);
843 } else {
844 android::processBuffer(dev, &log_msg);
845 }
846 }
847
848 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800849
850 return 0;
851}