blob: ca972087a9d344c6a8719148e98d5eb530a7d083 [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 Salyzynbbbe14f2014-04-11 13:49:43 -0700233 " -S output statistics.\n"
234 " -G <size> set size of log ring buffer, may suffix with K or M.\n"
235 " -p print prune white and ~black list. Service is specified as\n"
236 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
237 " with ~, otherwise weighed for longevity if unadorned. All\n"
238 " other pruning activity is oldest first. Special case ~!\n"
239 " represents an automatic quicker pruning for the noisiest\n"
240 " UID as determined by the current statistics.\n"
241 " -P '<list> ...' set prune white and ~black list, using same format as\n"
242 " printed above. Must be quoted.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243
244 fprintf(stderr,"\nfilterspecs are a series of \n"
245 " <tag>[:priority]\n\n"
246 "where <tag> is a log component tag (or * for all) and priority is:\n"
247 " V Verbose\n"
248 " D Debug\n"
249 " I Info\n"
250 " W Warn\n"
251 " E Error\n"
252 " F Fatal\n"
253 " S Silent (supress all output)\n"
254 "\n'*' means '*:d' and <tag> by itself means <tag>:v\n"
255 "\nIf not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.\n"
256 "If no filterspec is found, filter defaults to '*:I'\n"
257 "\nIf not specified with -v, format is set from ANDROID_PRINTF_LOG\n"
258 "or defaults to \"brief\"\n\n");
259
260
261
262}
263
264
265} /* namespace android */
266
267static int setLogFormat(const char * formatString)
268{
269 static AndroidLogPrintFormat format;
270
271 format = android_log_formatFromString(formatString);
272
273 if (format == FORMAT_OFF) {
274 // FORMAT_OFF means invalid string
275 return -1;
276 }
277
278 android_log_setPrintFormat(g_logformat, format);
279
280 return 0;
281}
282
283extern "C" void logprint_run_tests(void);
284
Joe Onorato6fa09a02010-02-26 10:04:23 -0800285int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800287 int err;
288 int hasSetLogFormat = 0;
289 int clearLog = 0;
290 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800291 unsigned long setLogSize = 0;
292 int getPruneList = 0;
293 char *setPruneList = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800294 int printStatistics = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295 int mode = O_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800297 log_device_t* devices = NULL;
298 log_device_t* dev;
299 bool needBinary = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800300 struct logger_list *logger_list;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800301 unsigned int tail_lines = 0;
302 log_time tail_time(log_time::EPOCH);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800304 signal(SIGPIPE, exit);
305
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306 g_logformat = android_log_format_new();
307
308 if (argc == 2 && 0 == strcmp(argv[1], "--test")) {
309 logprint_run_tests();
310 exit(0);
311 }
312
313 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
314 android::show_help(argv[0]);
315 exit(0);
316 }
317
318 for (;;) {
319 int ret;
320
Mark Salyzyn1c950472014-04-01 17:19:47 -0700321 ret = getopt(argc, argv, "cdt:T:gG:sQf:r::n:v:b:BSpP:");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322
323 if (ret < 0) {
324 break;
325 }
326
327 switch(ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800328 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329 // default to all silent
330 android_log_addFilterRule(g_logformat, "*:s");
331 break;
332
333 case 'c':
334 clearLog = 1;
335 mode = O_WRONLY;
336 break;
337
338 case 'd':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800339 mode = O_RDONLY | O_NDELAY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340 break;
341
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800342 case 't':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800343 mode = O_RDONLY | O_NDELAY;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800344 /* FALLTHRU */
345 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800346 if (strspn(optarg, "0123456789") != strlen(optarg)) {
347 char *cp = tail_time.strptime(optarg,
348 log_time::default_format);
349 if (!cp) {
350 fprintf(stderr,
351 "ERROR: -%c \"%s\" not in \"%s\" time format\n",
352 ret, optarg, log_time::default_format);
353 exit(1);
354 }
355 if (*cp) {
356 char c = *cp;
357 *cp = '\0';
358 fprintf(stderr,
359 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
360 ret, optarg, c, cp + 1);
361 *cp = c;
362 }
363 } else {
364 tail_lines = atoi(optarg);
365 if (!tail_lines) {
366 fprintf(stderr,
367 "WARNING: -%c %s invalid, setting to 1\n",
368 ret, optarg);
369 tail_lines = 1;
370 }
371 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800372 break;
373
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374 case 'g':
375 getLogSize = 1;
376 break;
377
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800378 case 'G': {
379 // would use atol if not for the multiplier
380 char *cp = optarg;
381 setLogSize = 0;
382 while (('0' <= *cp) && (*cp <= '9')) {
383 setLogSize *= 10;
384 setLogSize += *cp - '0';
385 ++cp;
386 }
387
388 switch(*cp) {
389 case 'g':
390 case 'G':
391 setLogSize *= 1024;
392 /* FALLTHRU */
393 case 'm':
394 case 'M':
395 setLogSize *= 1024;
396 /* FALLTHRU */
397 case 'k':
398 case 'K':
399 setLogSize *= 1024;
400 /* FALLTHRU */
401 case '\0':
402 break;
403
404 default:
405 setLogSize = 0;
406 }
407
408 if (!setLogSize) {
409 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
410 exit(1);
411 }
412 }
413 break;
414
415 case 'p':
416 getPruneList = 1;
417 break;
418
419 case 'P':
420 setPruneList = optarg;
421 break;
422
Joe Onorato6fa09a02010-02-26 10:04:23 -0800423 case 'b': {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800424 if (strcmp(optarg, "all") == 0) {
425 while (devices) {
426 dev = devices;
427 devices = dev->next;
428 delete dev;
429 }
430
431 dev = devices = new log_device_t("main", false, 'm');
432 android::g_devCount = 1;
433 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
434 dev->next = new log_device_t("system", false, 's');
435 if (dev->next) {
436 dev = dev->next;
437 android::g_devCount++;
438 }
439 }
440 if (android_name_to_log_id("radio") == LOG_ID_RADIO) {
441 dev->next = new log_device_t("radio", false, 'r');
442 if (dev->next) {
443 dev = dev->next;
444 android::g_devCount++;
445 }
446 }
447 if (android_name_to_log_id("events") == LOG_ID_EVENTS) {
448 dev->next = new log_device_t("events", true, 'e');
449 if (dev->next) {
450 android::g_devCount++;
451 needBinary = true;
452 }
453 }
454 break;
455 }
456
Joe Onorato6fa09a02010-02-26 10:04:23 -0800457 bool binary = strcmp(optarg, "events") == 0;
458 if (binary) {
459 needBinary = true;
460 }
461
462 if (devices) {
463 dev = devices;
464 while (dev->next) {
465 dev = dev->next;
466 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800467 dev->next = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800468 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800469 devices = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800470 }
471 android::g_devCount++;
472 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473 break;
474
475 case 'B':
476 android::g_printBinary = 1;
477 break;
478
479 case 'f':
480 // redirect output to a file
481
482 android::g_outputFileName = optarg;
483
484 break;
485
486 case 'r':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800487 if (optarg == NULL) {
488 android::g_logRotateSizeKBytes
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800489 = DEFAULT_LOG_ROTATE_SIZE_KBYTES;
490 } else {
491 long logRotateSize;
492 char *lastDigit;
493
494 if (!isdigit(optarg[0])) {
495 fprintf(stderr,"Invalid parameter to -r\n");
496 android::show_help(argv[0]);
497 exit(-1);
498 }
499 android::g_logRotateSizeKBytes = atoi(optarg);
500 }
501 break;
502
503 case 'n':
504 if (!isdigit(optarg[0])) {
505 fprintf(stderr,"Invalid parameter to -r\n");
506 android::show_help(argv[0]);
507 exit(-1);
508 }
509
510 android::g_maxRotatedLogs = atoi(optarg);
511 break;
512
513 case 'v':
514 err = setLogFormat (optarg);
515 if (err < 0) {
516 fprintf(stderr,"Invalid parameter to -v\n");
517 android::show_help(argv[0]);
518 exit(-1);
519 }
520
521 hasSetLogFormat = 1;
522 break;
523
524 case 'Q':
525 /* this is a *hidden* option used to start a version of logcat */
526 /* in an emulated device only. it basically looks for androidboot.logcat= */
527 /* on the kernel command line. If something is found, it extracts a log filter */
528 /* and uses it to run the program. If nothing is found, the program should */
529 /* quit immediately */
530#define KERNEL_OPTION "androidboot.logcat="
531#define CONSOLE_OPTION "androidboot.console="
532 {
533 int fd;
534 char* logcat;
535 char* console;
536 int force_exit = 1;
537 static char cmdline[1024];
538
539 fd = open("/proc/cmdline", O_RDONLY);
540 if (fd >= 0) {
541 int n = read(fd, cmdline, sizeof(cmdline)-1 );
542 if (n < 0) n = 0;
543 cmdline[n] = 0;
544 close(fd);
545 } else {
546 cmdline[0] = 0;
547 }
548
549 logcat = strstr( cmdline, KERNEL_OPTION );
550 console = strstr( cmdline, CONSOLE_OPTION );
551 if (logcat != NULL) {
552 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
553 char* q = strpbrk( p, " \t\n\r" );;
554
555 if (q != NULL)
556 *q = 0;
557
558 forceFilters = p;
559 force_exit = 0;
560 }
561 /* if nothing found or invalid filters, exit quietly */
562 if (force_exit)
563 exit(0);
564
565 /* redirect our output to the emulator console */
566 if (console) {
567 char* p = console + sizeof(CONSOLE_OPTION)-1;
568 char* q = strpbrk( p, " \t\n\r" );
569 char devname[64];
570 int len;
571
572 if (q != NULL) {
573 len = q - p;
574 } else
575 len = strlen(p);
576
577 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
578 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
579 if (len < (int)sizeof(devname)) {
580 fd = open( devname, O_WRONLY );
581 if (fd >= 0) {
582 dup2(fd, 1);
583 dup2(fd, 2);
584 close(fd);
585 }
586 }
587 }
588 }
589 break;
590
Mark Salyzyn34facab2014-02-06 14:48:50 -0800591 case 'S':
592 printStatistics = 1;
593 break;
594
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800595 default:
596 fprintf(stderr,"Unrecognized Option\n");
597 android::show_help(argv[0]);
598 exit(-1);
599 break;
600 }
601 }
602
Joe Onorato6fa09a02010-02-26 10:04:23 -0800603 if (!devices) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800604 devices = new log_device_t("main", false, 'm');
Joe Onorato6fa09a02010-02-26 10:04:23 -0800605 android::g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800606 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
607 devices->next = new log_device_t("system", false, 's');
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800608 android::g_devCount++;
609 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800610 }
611
Mark Salyzyn95132e92013-11-22 10:55:48 -0800612 if (android::g_logRotateSizeKBytes != 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800613 && android::g_outputFileName == NULL
614 ) {
615 fprintf(stderr,"-r requires -f as well\n");
616 android::show_help(argv[0]);
617 exit(-1);
618 }
619
620 android::setupOutput();
621
622 if (hasSetLogFormat == 0) {
623 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
624
625 if (logFormat != NULL) {
626 err = setLogFormat(logFormat);
627
628 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800629 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800630 logFormat);
631 }
632 }
633 }
634
635 if (forceFilters) {
636 err = android_log_addFilterString(g_logformat, forceFilters);
637 if (err < 0) {
638 fprintf (stderr, "Invalid filter expression in -logcat option\n");
639 exit(0);
640 }
641 } else if (argc == optind) {
642 // Add from environment variable
643 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
644
645 if (env_tags_orig != NULL) {
646 err = android_log_addFilterString(g_logformat, env_tags_orig);
647
Mark Salyzyn95132e92013-11-22 10:55:48 -0800648 if (err < 0) {
649 fprintf(stderr, "Invalid filter expression in"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800650 " ANDROID_LOG_TAGS\n");
651 android::show_help(argv[0]);
652 exit(-1);
653 }
654 }
655 } else {
656 // Add from commandline
657 for (int i = optind ; i < argc ; i++) {
658 err = android_log_addFilterString(g_logformat, argv[i]);
659
Mark Salyzyn95132e92013-11-22 10:55:48 -0800660 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800661 fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]);
662 android::show_help(argv[0]);
663 exit(-1);
664 }
665 }
666 }
667
Joe Onorato6fa09a02010-02-26 10:04:23 -0800668 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800669 if (tail_time != log_time::EPOCH) {
670 logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
671 } else {
672 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
673 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800674 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800675 dev->logger_list = logger_list;
676 dev->logger = android_logger_open(logger_list,
677 android_name_to_log_id(dev->device));
678 if (!dev->logger) {
679 fprintf(stderr, "Unable to open log device '%s'\n", dev->device);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800680 exit(EXIT_FAILURE);
681 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800682
683 if (clearLog) {
684 int ret;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800685 ret = android_logger_clear(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800686 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700687 perror("failed to clear the log");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800688 exit(EXIT_FAILURE);
689 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800690 }
691
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800692 if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700693 perror("failed to set the log size");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800694 exit(EXIT_FAILURE);
695 }
696
Joe Onorato6fa09a02010-02-26 10:04:23 -0800697 if (getLogSize) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800698 long size, readable;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800699
Mark Salyzyn95132e92013-11-22 10:55:48 -0800700 size = android_logger_get_log_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800701 if (size < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700702 perror("failed to get the log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800703 exit(EXIT_FAILURE);
704 }
705
Mark Salyzyn95132e92013-11-22 10:55:48 -0800706 readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800707 if (readable < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700708 perror("failed to get the readable log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800709 exit(EXIT_FAILURE);
710 }
711
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800712 printf("%s: ring buffer is %ldKb (%ldKb consumed), "
Joe Onorato6fa09a02010-02-26 10:04:23 -0800713 "max entry is %db, max payload is %db\n", dev->device,
714 size / 1024, readable / 1024,
715 (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
716 }
717
718 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800719 }
720
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800721 if (setPruneList) {
722 size_t len = strlen(setPruneList) + 32; // margin to allow rc
723 char *buf = (char *) malloc(len);
724
725 strcpy(buf, setPruneList);
726 int ret = android_logger_set_prune_list(logger_list, buf, len);
727 free(buf);
728
729 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700730 perror("failed to set the prune list");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800731 exit(EXIT_FAILURE);
732 }
733 }
734
Mark Salyzyn1c950472014-04-01 17:19:47 -0700735 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800736 size_t len = 8192;
737 char *buf;
738
739 for(int retry = 32;
740 (retry >= 0) && ((buf = new char [len]));
741 delete [] buf, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800742 if (getPruneList) {
743 android_logger_get_prune_list(logger_list, buf, len);
744 } else {
745 android_logger_get_statistics(logger_list, buf, len);
746 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800747 buf[len-1] = '\0';
748 size_t ret = atol(buf) + 1;
749 if (ret < 4) {
750 delete [] buf;
751 buf = NULL;
752 break;
753 }
754 bool check = ret <= len;
755 len = ret;
756 if (check) {
757 break;
758 }
759 }
760
761 if (!buf) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700762 perror("failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -0800763 exit(EXIT_FAILURE);
764 }
765
766 // remove trailing FF
767 char *cp = buf + len - 1;
768 *cp = '\0';
769 bool truncated = *--cp != '\f';
770 if (!truncated) {
771 *cp = '\0';
772 }
773
774 // squash out the byte count
775 cp = buf;
776 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -0700777 while (isdigit(*cp)) {
778 ++cp;
779 }
780 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800781 ++cp;
782 }
783 }
784
785 printf("%s", cp);
786 delete [] buf;
787 exit(0);
788 }
789
790
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800791 if (getLogSize) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700792 exit(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800793 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800794 if (setLogSize || setPruneList) {
795 exit(0);
796 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800797 if (clearLog) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700798 exit(0);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800799 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800800
801 //LOG_EVENT_INT(10, 12345);
802 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
803 //LOG_EVENT_STRING(0, "whassup, doc?");
804
Joe Onorato6fa09a02010-02-26 10:04:23 -0800805 if (needBinary)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800806 android::g_eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
807
Mark Salyzyn95132e92013-11-22 10:55:48 -0800808 while (1) {
809 struct log_msg log_msg;
810 int ret = android_logger_list_read(logger_list, &log_msg);
811
812 if (ret == 0) {
813 fprintf(stderr, "read: Unexpected EOF!\n");
814 exit(EXIT_FAILURE);
815 }
816
817 if (ret < 0) {
818 if (ret == -EAGAIN) {
819 break;
820 }
821
822 if (ret == -EIO) {
823 fprintf(stderr, "read: Unexpected EOF!\n");
824 exit(EXIT_FAILURE);
825 }
826 if (ret == -EINVAL) {
827 fprintf(stderr, "read: unexpected length.\n");
828 exit(EXIT_FAILURE);
829 }
Mark Salyzynfff04e32014-03-17 10:36:46 -0700830 perror("logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800831 exit(EXIT_FAILURE);
832 }
833
834 for(dev = devices; dev; dev = dev->next) {
835 if (android_name_to_log_id(dev->device) == log_msg.id()) {
836 break;
837 }
838 }
839 if (!dev) {
840 fprintf(stderr, "read: Unexpected log ID!\n");
841 exit(EXIT_FAILURE);
842 }
843
844 android::maybePrintStart(dev);
845 if (android::g_printBinary) {
846 android::printBinary(&log_msg);
847 } else {
848 android::processBuffer(dev, &log_msg);
849 }
850 }
851
852 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800853
854 return 0;
855}