blob: 812e2e08d26e07695fc38fd62b4f945585f9436d [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"
232 " -S output statistics");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233
234
235 fprintf(stderr,"\nfilterspecs are a series of \n"
236 " <tag>[:priority]\n\n"
237 "where <tag> is a log component tag (or * for all) and priority is:\n"
238 " V Verbose\n"
239 " D Debug\n"
240 " I Info\n"
241 " W Warn\n"
242 " E Error\n"
243 " F Fatal\n"
244 " S Silent (supress all output)\n"
245 "\n'*' means '*:d' and <tag> by itself means <tag>:v\n"
246 "\nIf not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.\n"
247 "If no filterspec is found, filter defaults to '*:I'\n"
248 "\nIf not specified with -v, format is set from ANDROID_PRINTF_LOG\n"
249 "or defaults to \"brief\"\n\n");
250
251
252
253}
254
255
256} /* namespace android */
257
258static int setLogFormat(const char * formatString)
259{
260 static AndroidLogPrintFormat format;
261
262 format = android_log_formatFromString(formatString);
263
264 if (format == FORMAT_OFF) {
265 // FORMAT_OFF means invalid string
266 return -1;
267 }
268
269 android_log_setPrintFormat(g_logformat, format);
270
271 return 0;
272}
273
274extern "C" void logprint_run_tests(void);
275
Joe Onorato6fa09a02010-02-26 10:04:23 -0800276int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800278 int err;
279 int hasSetLogFormat = 0;
280 int clearLog = 0;
281 int getLogSize = 0;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800282 int printStatistics = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800283 int mode = O_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800284 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800285 log_device_t* devices = NULL;
286 log_device_t* dev;
287 bool needBinary = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800288 struct logger_list *logger_list;
289 int tail_lines = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800290
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800291 signal(SIGPIPE, exit);
292
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800293 g_logformat = android_log_format_new();
294
295 if (argc == 2 && 0 == strcmp(argv[1], "--test")) {
296 logprint_run_tests();
297 exit(0);
298 }
299
300 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
301 android::show_help(argv[0]);
302 exit(0);
303 }
304
305 for (;;) {
306 int ret;
307
Mark Salyzyn34facab2014-02-06 14:48:50 -0800308 ret = getopt(argc, argv, "cdt:T:gsQf:r::n:v:b:BS");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309
310 if (ret < 0) {
311 break;
312 }
313
314 switch(ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800315 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316 // default to all silent
317 android_log_addFilterRule(g_logformat, "*:s");
318 break;
319
320 case 'c':
321 clearLog = 1;
322 mode = O_WRONLY;
323 break;
324
325 case 'd':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800326 mode = O_RDONLY | O_NDELAY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327 break;
328
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800329 case 't':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800330 mode = O_RDONLY | O_NDELAY;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800331 /* FALLTHRU */
332 case 'T':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800333 tail_lines = atoi(optarg);
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800334 break;
335
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 case 'g':
337 getLogSize = 1;
338 break;
339
Joe Onorato6fa09a02010-02-26 10:04:23 -0800340 case 'b': {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800341 if (strcmp(optarg, "all") == 0) {
342 while (devices) {
343 dev = devices;
344 devices = dev->next;
345 delete dev;
346 }
347
348 dev = devices = new log_device_t("main", false, 'm');
349 android::g_devCount = 1;
350 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
351 dev->next = new log_device_t("system", false, 's');
352 if (dev->next) {
353 dev = dev->next;
354 android::g_devCount++;
355 }
356 }
357 if (android_name_to_log_id("radio") == LOG_ID_RADIO) {
358 dev->next = new log_device_t("radio", false, 'r');
359 if (dev->next) {
360 dev = dev->next;
361 android::g_devCount++;
362 }
363 }
364 if (android_name_to_log_id("events") == LOG_ID_EVENTS) {
365 dev->next = new log_device_t("events", true, 'e');
366 if (dev->next) {
367 android::g_devCount++;
368 needBinary = true;
369 }
370 }
371 break;
372 }
373
Joe Onorato6fa09a02010-02-26 10:04:23 -0800374 bool binary = strcmp(optarg, "events") == 0;
375 if (binary) {
376 needBinary = true;
377 }
378
379 if (devices) {
380 dev = devices;
381 while (dev->next) {
382 dev = dev->next;
383 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800384 dev->next = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800385 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800386 devices = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800387 }
388 android::g_devCount++;
389 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390 break;
391
392 case 'B':
393 android::g_printBinary = 1;
394 break;
395
396 case 'f':
397 // redirect output to a file
398
399 android::g_outputFileName = optarg;
400
401 break;
402
403 case 'r':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800404 if (optarg == NULL) {
405 android::g_logRotateSizeKBytes
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800406 = DEFAULT_LOG_ROTATE_SIZE_KBYTES;
407 } else {
408 long logRotateSize;
409 char *lastDigit;
410
411 if (!isdigit(optarg[0])) {
412 fprintf(stderr,"Invalid parameter to -r\n");
413 android::show_help(argv[0]);
414 exit(-1);
415 }
416 android::g_logRotateSizeKBytes = atoi(optarg);
417 }
418 break;
419
420 case 'n':
421 if (!isdigit(optarg[0])) {
422 fprintf(stderr,"Invalid parameter to -r\n");
423 android::show_help(argv[0]);
424 exit(-1);
425 }
426
427 android::g_maxRotatedLogs = atoi(optarg);
428 break;
429
430 case 'v':
431 err = setLogFormat (optarg);
432 if (err < 0) {
433 fprintf(stderr,"Invalid parameter to -v\n");
434 android::show_help(argv[0]);
435 exit(-1);
436 }
437
438 hasSetLogFormat = 1;
439 break;
440
441 case 'Q':
442 /* this is a *hidden* option used to start a version of logcat */
443 /* in an emulated device only. it basically looks for androidboot.logcat= */
444 /* on the kernel command line. If something is found, it extracts a log filter */
445 /* and uses it to run the program. If nothing is found, the program should */
446 /* quit immediately */
447#define KERNEL_OPTION "androidboot.logcat="
448#define CONSOLE_OPTION "androidboot.console="
449 {
450 int fd;
451 char* logcat;
452 char* console;
453 int force_exit = 1;
454 static char cmdline[1024];
455
456 fd = open("/proc/cmdline", O_RDONLY);
457 if (fd >= 0) {
458 int n = read(fd, cmdline, sizeof(cmdline)-1 );
459 if (n < 0) n = 0;
460 cmdline[n] = 0;
461 close(fd);
462 } else {
463 cmdline[0] = 0;
464 }
465
466 logcat = strstr( cmdline, KERNEL_OPTION );
467 console = strstr( cmdline, CONSOLE_OPTION );
468 if (logcat != NULL) {
469 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
470 char* q = strpbrk( p, " \t\n\r" );;
471
472 if (q != NULL)
473 *q = 0;
474
475 forceFilters = p;
476 force_exit = 0;
477 }
478 /* if nothing found or invalid filters, exit quietly */
479 if (force_exit)
480 exit(0);
481
482 /* redirect our output to the emulator console */
483 if (console) {
484 char* p = console + sizeof(CONSOLE_OPTION)-1;
485 char* q = strpbrk( p, " \t\n\r" );
486 char devname[64];
487 int len;
488
489 if (q != NULL) {
490 len = q - p;
491 } else
492 len = strlen(p);
493
494 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
495 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
496 if (len < (int)sizeof(devname)) {
497 fd = open( devname, O_WRONLY );
498 if (fd >= 0) {
499 dup2(fd, 1);
500 dup2(fd, 2);
501 close(fd);
502 }
503 }
504 }
505 }
506 break;
507
Mark Salyzyn34facab2014-02-06 14:48:50 -0800508 case 'S':
509 printStatistics = 1;
510 break;
511
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512 default:
513 fprintf(stderr,"Unrecognized Option\n");
514 android::show_help(argv[0]);
515 exit(-1);
516 break;
517 }
518 }
519
Joe Onorato6fa09a02010-02-26 10:04:23 -0800520 if (!devices) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800521 devices = new log_device_t("main", false, 'm');
Joe Onorato6fa09a02010-02-26 10:04:23 -0800522 android::g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800523 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
524 devices->next = new log_device_t("system", false, 's');
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800525 android::g_devCount++;
526 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800527 }
528
Mark Salyzyn95132e92013-11-22 10:55:48 -0800529 if (android::g_logRotateSizeKBytes != 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530 && android::g_outputFileName == NULL
531 ) {
532 fprintf(stderr,"-r requires -f as well\n");
533 android::show_help(argv[0]);
534 exit(-1);
535 }
536
537 android::setupOutput();
538
539 if (hasSetLogFormat == 0) {
540 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
541
542 if (logFormat != NULL) {
543 err = setLogFormat(logFormat);
544
545 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800546 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800547 logFormat);
548 }
549 }
550 }
551
552 if (forceFilters) {
553 err = android_log_addFilterString(g_logformat, forceFilters);
554 if (err < 0) {
555 fprintf (stderr, "Invalid filter expression in -logcat option\n");
556 exit(0);
557 }
558 } else if (argc == optind) {
559 // Add from environment variable
560 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
561
562 if (env_tags_orig != NULL) {
563 err = android_log_addFilterString(g_logformat, env_tags_orig);
564
Mark Salyzyn95132e92013-11-22 10:55:48 -0800565 if (err < 0) {
566 fprintf(stderr, "Invalid filter expression in"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800567 " ANDROID_LOG_TAGS\n");
568 android::show_help(argv[0]);
569 exit(-1);
570 }
571 }
572 } else {
573 // Add from commandline
574 for (int i = optind ; i < argc ; i++) {
575 err = android_log_addFilterString(g_logformat, argv[i]);
576
Mark Salyzyn95132e92013-11-22 10:55:48 -0800577 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800578 fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]);
579 android::show_help(argv[0]);
580 exit(-1);
581 }
582 }
583 }
584
Joe Onorato6fa09a02010-02-26 10:04:23 -0800585 dev = devices;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800586 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800587 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800588 dev->logger_list = logger_list;
589 dev->logger = android_logger_open(logger_list,
590 android_name_to_log_id(dev->device));
591 if (!dev->logger) {
592 fprintf(stderr, "Unable to open log device '%s'\n", dev->device);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800593 exit(EXIT_FAILURE);
594 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800595
596 if (clearLog) {
597 int ret;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800598 ret = android_logger_clear(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800599 if (ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800600 perror("clearLog");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800601 exit(EXIT_FAILURE);
602 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800603 }
604
605 if (getLogSize) {
606 int size, readable;
607
Mark Salyzyn95132e92013-11-22 10:55:48 -0800608 size = android_logger_get_log_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800609 if (size < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800610 perror("getLogSize");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800611 exit(EXIT_FAILURE);
612 }
613
Mark Salyzyn95132e92013-11-22 10:55:48 -0800614 readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800615 if (readable < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800616 perror("getLogReadableSize");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800617 exit(EXIT_FAILURE);
618 }
619
620 printf("%s: ring buffer is %dKb (%dKb consumed), "
621 "max entry is %db, max payload is %db\n", dev->device,
622 size / 1024, readable / 1024,
623 (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
624 }
625
626 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800627 }
628
Mark Salyzyn34facab2014-02-06 14:48:50 -0800629 if (printStatistics) {
630 size_t len = 8192;
631 char *buf;
632
633 for(int retry = 32;
634 (retry >= 0) && ((buf = new char [len]));
635 delete [] buf, --retry) {
636 android_logger_get_statistics(logger_list, buf, len);
637
638 buf[len-1] = '\0';
639 size_t ret = atol(buf) + 1;
640 if (ret < 4) {
641 delete [] buf;
642 buf = NULL;
643 break;
644 }
645 bool check = ret <= len;
646 len = ret;
647 if (check) {
648 break;
649 }
650 }
651
652 if (!buf) {
653 perror("statistics read");
654 exit(EXIT_FAILURE);
655 }
656
657 // remove trailing FF
658 char *cp = buf + len - 1;
659 *cp = '\0';
660 bool truncated = *--cp != '\f';
661 if (!truncated) {
662 *cp = '\0';
663 }
664
665 // squash out the byte count
666 cp = buf;
667 if (!truncated) {
668 while (isdigit(*cp) || (*cp == '\n')) {
669 ++cp;
670 }
671 }
672
673 printf("%s", cp);
674 delete [] buf;
675 exit(0);
676 }
677
678
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800679 if (getLogSize) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700680 exit(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800681 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800682 if (clearLog) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700683 exit(0);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800684 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800685
686 //LOG_EVENT_INT(10, 12345);
687 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
688 //LOG_EVENT_STRING(0, "whassup, doc?");
689
Joe Onorato6fa09a02010-02-26 10:04:23 -0800690 if (needBinary)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800691 android::g_eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
692
Mark Salyzyn95132e92013-11-22 10:55:48 -0800693 while (1) {
694 struct log_msg log_msg;
695 int ret = android_logger_list_read(logger_list, &log_msg);
696
697 if (ret == 0) {
698 fprintf(stderr, "read: Unexpected EOF!\n");
699 exit(EXIT_FAILURE);
700 }
701
702 if (ret < 0) {
703 if (ret == -EAGAIN) {
704 break;
705 }
706
707 if (ret == -EIO) {
708 fprintf(stderr, "read: Unexpected EOF!\n");
709 exit(EXIT_FAILURE);
710 }
711 if (ret == -EINVAL) {
712 fprintf(stderr, "read: unexpected length.\n");
713 exit(EXIT_FAILURE);
714 }
715 perror("logcat read");
716 exit(EXIT_FAILURE);
717 }
718
719 for(dev = devices; dev; dev = dev->next) {
720 if (android_name_to_log_id(dev->device) == log_msg.id()) {
721 break;
722 }
723 }
724 if (!dev) {
725 fprintf(stderr, "read: Unexpected log ID!\n");
726 exit(EXIT_FAILURE);
727 }
728
729 android::maybePrintStart(dev);
730 if (android::g_printBinary) {
731 android::printBinary(&log_msg);
732 } else {
733 android::processBuffer(dev, &log_msg);
734 }
735 }
736
737 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800738
739 return 0;
740}