blob: 5acdf77726887a78c1e8cf8d211987c75243d906 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001// Copyright 2006 The Android Open Source Project
2
3#include <cutils/logger.h>
4#include <cutils/logd.h>
5#include <cutils/sockets.h>
6#include <cutils/logprint.h>
7#include <cutils/event_tag_map.h>
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <stdarg.h>
12#include <string.h>
13#include <unistd.h>
14#include <fcntl.h>
15#include <time.h>
16#include <errno.h>
17#include <assert.h>
18#include <ctype.h>
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <arpa/inet.h>
22
23#define DEFAULT_LOG_ROTATE_SIZE_KBYTES 16
24#define DEFAULT_MAX_ROTATED_LOGS 4
25
26static AndroidLogFormat * g_logformat;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -080027static bool g_nonblock = false;
Dan Egnord1d3b6d2010-03-11 20:32:17 -080028static int g_tail_lines = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
30/* logd prefixes records with a length field */
31#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
32
33#define LOG_FILE_DIR "/dev/log/"
34
Joe Onorato6fa09a02010-02-26 10:04:23 -080035struct queued_entry_t {
36 union {
37 unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1] __attribute__((aligned(4)));
38 struct logger_entry entry __attribute__((aligned(4)));
39 };
40 queued_entry_t* next;
41
42 queued_entry_t() {
43 next = NULL;
44 }
45};
46
47static int cmp(queued_entry_t* a, queued_entry_t* b) {
48 int n = a->entry.sec - b->entry.sec;
49 if (n != 0) {
50 return n;
51 }
52 return a->entry.nsec - b->entry.nsec;
53}
54
55struct log_device_t {
56 char* device;
57 bool binary;
58 int fd;
59 bool printed;
60 char label;
61
62 queued_entry_t* queue;
63 log_device_t* next;
64
65 log_device_t(char* d, bool b, char l) {
66 device = d;
67 binary = b;
68 label = l;
Mathias Agopian50844522010-03-17 16:10:26 -070069 queue = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -080070 next = NULL;
71 printed = false;
72 }
73
74 void enqueue(queued_entry_t* entry) {
75 if (this->queue == NULL) {
76 this->queue = entry;
77 } else {
78 queued_entry_t** e = &this->queue;
79 while (*e && cmp(entry, *e) >= 0) {
80 e = &((*e)->next);
81 }
82 entry->next = *e;
83 *e = entry;
84 }
85 }
86};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087
88namespace android {
89
90/* Global Variables */
91
92static const char * g_outputFileName = NULL;
93static int g_logRotateSizeKBytes = 0; // 0 means "no log rotation"
94static int g_maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS; // 0 means "unbounded"
95static int g_outFD = -1;
96static off_t g_outByteCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097static int g_printBinary = 0;
Joe Onorato6fa09a02010-02-26 10:04:23 -080098static int g_devCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099
100static EventTagMap* g_eventTagMap = NULL;
101
102static int openLogFile (const char *pathname)
103{
104 return open(g_outputFileName, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
105}
106
107static void rotateLogs()
108{
109 int err;
110
111 // Can't rotate logs if we're not outputting to a file
112 if (g_outputFileName == NULL) {
113 return;
114 }
115
116 close(g_outFD);
117
118 for (int i = g_maxRotatedLogs ; i > 0 ; i--) {
119 char *file0, *file1;
120
121 asprintf(&file1, "%s.%d", g_outputFileName, i);
122
123 if (i - 1 == 0) {
124 asprintf(&file0, "%s", g_outputFileName);
125 } else {
126 asprintf(&file0, "%s.%d", g_outputFileName, i - 1);
127 }
128
129 err = rename (file0, file1);
130
131 if (err < 0 && errno != ENOENT) {
132 perror("while rotating log files");
133 }
134
135 free(file1);
136 free(file0);
137 }
138
139 g_outFD = openLogFile (g_outputFileName);
140
141 if (g_outFD < 0) {
142 perror ("couldn't open output file");
143 exit(-1);
144 }
145
146 g_outByteCount = 0;
147
148}
149
150void printBinary(struct logger_entry *buf)
151{
152 size_t size = sizeof(logger_entry) + buf->len;
153 int ret;
154
155 do {
156 ret = write(g_outFD, buf, size);
157 } while (ret < 0 && errno == EINTR);
158}
159
Joe Onorato6fa09a02010-02-26 10:04:23 -0800160static void processBuffer(log_device_t* dev, struct logger_entry *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161{
Mathias Agopian50844522010-03-17 16:10:26 -0700162 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163 int err;
164 AndroidLogEntry entry;
165 char binaryMsgBuf[1024];
166
Joe Onorato6fa09a02010-02-26 10:04:23 -0800167 if (dev->binary) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 err = android_log_processBinaryLogBuffer(buf, &entry, g_eventTagMap,
169 binaryMsgBuf, sizeof(binaryMsgBuf));
170 //printf(">>> pri=%d len=%d msg='%s'\n",
171 // entry.priority, entry.messageLen, entry.message);
172 } else {
173 err = android_log_processLogBuffer(buf, &entry);
174 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800175 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 goto error;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800177 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800179 if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) {
180 if (false && g_devCount > 1) {
181 binaryMsgBuf[0] = dev->label;
182 binaryMsgBuf[1] = ' ';
183 bytesWritten = write(g_outFD, binaryMsgBuf, 2);
184 if (bytesWritten < 0) {
185 perror("output error");
186 exit(-1);
187 }
188 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800190 bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry);
191
192 if (bytesWritten < 0) {
193 perror("output error");
194 exit(-1);
195 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 }
197
198 g_outByteCount += bytesWritten;
199
200 if (g_logRotateSizeKBytes > 0
201 && (g_outByteCount / 1024) >= g_logRotateSizeKBytes
202 ) {
203 rotateLogs();
204 }
205
206error:
207 //fprintf (stderr, "Error processing record\n");
208 return;
209}
210
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800211static void chooseFirst(log_device_t* dev, log_device_t** firstdev) {
212 for (*firstdev = NULL; dev != NULL; dev = dev->next) {
213 if (dev->queue != NULL && (*firstdev == NULL || cmp(dev->queue, (*firstdev)->queue) < 0)) {
214 *firstdev = dev;
215 }
216 }
217}
218
219static void maybePrintStart(log_device_t* dev) {
220 if (!dev->printed) {
221 dev->printed = true;
222 if (g_devCount > 1 && !g_printBinary) {
223 char buf[1024];
224 snprintf(buf, sizeof(buf), "--------- beginning of %s\n", dev->device);
225 if (write(g_outFD, buf, strlen(buf)) < 0) {
226 perror("output error");
227 exit(-1);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800228 }
229 }
230 }
231}
232
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800233static void skipNextEntry(log_device_t* dev) {
234 maybePrintStart(dev);
235 queued_entry_t* entry = dev->queue;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800236 dev->queue = entry->next;
237 delete entry;
238}
239
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800240static void printNextEntry(log_device_t* dev) {
241 maybePrintStart(dev);
242 if (g_printBinary) {
243 printBinary(&dev->queue->entry);
244 } else {
245 processBuffer(dev, &dev->queue->entry);
246 }
247 skipNextEntry(dev);
248}
249
Joe Onorato6fa09a02010-02-26 10:04:23 -0800250static void readLogLines(log_device_t* devices)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251{
Joe Onorato6fa09a02010-02-26 10:04:23 -0800252 log_device_t* dev;
253 int max = 0;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800254 int ret;
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800255 int queued_lines = 0;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800256 bool sleep = true;
257
258 int result;
259 fd_set readset;
260
261 for (dev=devices; dev; dev = dev->next) {
262 if (dev->fd > max) {
263 max = dev->fd;
264 }
265 }
266
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267 while (1) {
Joe Onorato6fa09a02010-02-26 10:04:23 -0800268 do {
269 timeval timeout = { 0, 5000 /* 5ms */ }; // If we oversleep it's ok, i.e. ignore EINTR.
270 FD_ZERO(&readset);
271 for (dev=devices; dev; dev = dev->next) {
272 FD_SET(dev->fd, &readset);
273 }
274 result = select(max + 1, &readset, NULL, NULL, sleep ? NULL : &timeout);
275 } while (result == -1 && errno == EINTR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800276
Joe Onorato6fa09a02010-02-26 10:04:23 -0800277 if (result >= 0) {
278 for (dev=devices; dev; dev = dev->next) {
279 if (FD_ISSET(dev->fd, &readset)) {
Mathias Agopian50844522010-03-17 16:10:26 -0700280 queued_entry_t* entry = new queued_entry_t();
Joe Onorato6fa09a02010-02-26 10:04:23 -0800281 /* NOTE: driver guarantees we read exactly one full entry */
282 ret = read(dev->fd, entry->buf, LOGGER_ENTRY_MAX_LEN);
283 if (ret < 0) {
284 if (errno == EINTR) {
285 delete entry;
286 goto next;
287 }
288 if (errno == EAGAIN) {
289 delete entry;
290 break;
291 }
292 perror("logcat read");
293 exit(EXIT_FAILURE);
294 }
295 else if (!ret) {
296 fprintf(stderr, "read: Unexpected EOF!\n");
297 exit(EXIT_FAILURE);
298 }
299
300 entry->entry.msg[entry->entry.len] = '\0';
301
302 dev->enqueue(entry);
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800303 ++queued_lines;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800304 }
305 }
306
307 if (result == 0) {
308 // we did our short timeout trick and there's nothing new
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800309 // print everything we have and wait for more data
Joe Onorato6fa09a02010-02-26 10:04:23 -0800310 sleep = true;
311 while (true) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800312 chooseFirst(devices, &dev);
313 if (dev == NULL) {
Joe Onorato6fa09a02010-02-26 10:04:23 -0800314 break;
315 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800316 if (g_tail_lines == 0 || queued_lines <= g_tail_lines) {
317 printNextEntry(dev);
318 } else {
319 skipNextEntry(dev);
320 }
321 --queued_lines;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800322 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800323
324 // the caller requested to just dump the log and exit
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800325 if (g_nonblock) {
326 exit(0);
327 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800328 } else {
329 // print all that aren't the last in their list
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800330 sleep = false;
331 while (g_tail_lines == 0 || queued_lines > g_tail_lines) {
332 chooseFirst(devices, &dev);
333 if (dev == NULL || dev->queue->next == NULL) {
Joe Onorato6fa09a02010-02-26 10:04:23 -0800334 break;
335 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800336 if (g_tail_lines == 0) {
337 printNextEntry(dev);
338 } else {
339 skipNextEntry(dev);
340 }
341 --queued_lines;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800342 }
343 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800345next:
346 ;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347 }
348}
349
350static int clearLog(int logfd)
351{
352 return ioctl(logfd, LOGGER_FLUSH_LOG);
353}
354
355/* returns the total size of the log's ring buffer */
356static int getLogSize(int logfd)
357{
358 return ioctl(logfd, LOGGER_GET_LOG_BUF_SIZE);
359}
360
361/* returns the readable size of the log's ring buffer (that is, amount of the log consumed) */
362static int getLogReadableSize(int logfd)
363{
364 return ioctl(logfd, LOGGER_GET_LOG_LEN);
365}
366
367static void setupOutput()
368{
369
370 if (g_outputFileName == NULL) {
371 g_outFD = STDOUT_FILENO;
372
373 } else {
374 struct stat statbuf;
375
376 g_outFD = openLogFile (g_outputFileName);
377
378 if (g_outFD < 0) {
379 perror ("couldn't open output file");
380 exit(-1);
381 }
382
383 fstat(g_outFD, &statbuf);
384
385 g_outByteCount = statbuf.st_size;
386 }
387}
388
389static void show_help(const char *cmd)
390{
391 fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd);
392
393 fprintf(stderr, "options include:\n"
394 " -s Set default filter to silent.\n"
395 " Like specifying filterspec '*:s'\n"
396 " -f <filename> Log to file. Default to stdout\n"
397 " -r [<kbytes>] Rotate log every kbytes. (16 if unspecified). Requires -f\n"
398 " -n <count> Sets max number of rotated logs to <count>, default 4\n"
399 " -v <format> Sets the log print format, where <format> is one of:\n\n"
400 " brief process tag thread raw time threadtime long\n\n"
401 " -c clear (flush) the entire log and exit\n"
402 " -d dump the log and then exit (don't block)\n"
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800403 " -t <count> print only the most recent <count> lines (implies -d)\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800404 " -g get the size of the log's ring buffer and exit\n"
405 " -b <buffer> request alternate ring buffer\n"
406 " ('main' (default), 'radio', 'events')\n"
407 " -B output the log in binary");
408
409
410 fprintf(stderr,"\nfilterspecs are a series of \n"
411 " <tag>[:priority]\n\n"
412 "where <tag> is a log component tag (or * for all) and priority is:\n"
413 " V Verbose\n"
414 " D Debug\n"
415 " I Info\n"
416 " W Warn\n"
417 " E Error\n"
418 " F Fatal\n"
419 " S Silent (supress all output)\n"
420 "\n'*' means '*:d' and <tag> by itself means <tag>:v\n"
421 "\nIf not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.\n"
422 "If no filterspec is found, filter defaults to '*:I'\n"
423 "\nIf not specified with -v, format is set from ANDROID_PRINTF_LOG\n"
424 "or defaults to \"brief\"\n\n");
425
426
427
428}
429
430
431} /* namespace android */
432
433static int setLogFormat(const char * formatString)
434{
435 static AndroidLogPrintFormat format;
436
437 format = android_log_formatFromString(formatString);
438
439 if (format == FORMAT_OFF) {
440 // FORMAT_OFF means invalid string
441 return -1;
442 }
443
444 android_log_setPrintFormat(g_logformat, format);
445
446 return 0;
447}
448
449extern "C" void logprint_run_tests(void);
450
Joe Onorato6fa09a02010-02-26 10:04:23 -0800451int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453 int err;
454 int hasSetLogFormat = 0;
455 int clearLog = 0;
456 int getLogSize = 0;
457 int mode = O_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800458 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800459 log_device_t* devices = NULL;
460 log_device_t* dev;
461 bool needBinary = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462
463 g_logformat = android_log_format_new();
464
465 if (argc == 2 && 0 == strcmp(argv[1], "--test")) {
466 logprint_run_tests();
467 exit(0);
468 }
469
470 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
471 android::show_help(argv[0]);
472 exit(0);
473 }
474
475 for (;;) {
476 int ret;
477
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800478 ret = getopt(argc, argv, "cdt:gsQf:r::n:v:b:B");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479
480 if (ret < 0) {
481 break;
482 }
483
484 switch(ret) {
485 case 's':
486 // default to all silent
487 android_log_addFilterRule(g_logformat, "*:s");
488 break;
489
490 case 'c':
491 clearLog = 1;
492 mode = O_WRONLY;
493 break;
494
495 case 'd':
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800496 g_nonblock = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497 break;
498
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800499 case 't':
500 g_nonblock = true;
501 g_tail_lines = atoi(optarg);
502 break;
503
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 case 'g':
505 getLogSize = 1;
506 break;
507
Joe Onorato6fa09a02010-02-26 10:04:23 -0800508 case 'b': {
509 char* buf = (char*) malloc(strlen(LOG_FILE_DIR) + strlen(optarg) + 1);
510 strcpy(buf, LOG_FILE_DIR);
511 strcat(buf, optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512
Joe Onorato6fa09a02010-02-26 10:04:23 -0800513 bool binary = strcmp(optarg, "events") == 0;
514 if (binary) {
515 needBinary = true;
516 }
517
518 if (devices) {
519 dev = devices;
520 while (dev->next) {
521 dev = dev->next;
522 }
523 dev->next = new log_device_t(buf, binary, optarg[0]);
524 } else {
525 devices = new log_device_t(buf, binary, optarg[0]);
526 }
527 android::g_devCount++;
528 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800529 break;
530
531 case 'B':
532 android::g_printBinary = 1;
533 break;
534
535 case 'f':
536 // redirect output to a file
537
538 android::g_outputFileName = optarg;
539
540 break;
541
542 case 'r':
543 if (optarg == NULL) {
544 android::g_logRotateSizeKBytes
545 = DEFAULT_LOG_ROTATE_SIZE_KBYTES;
546 } else {
547 long logRotateSize;
548 char *lastDigit;
549
550 if (!isdigit(optarg[0])) {
551 fprintf(stderr,"Invalid parameter to -r\n");
552 android::show_help(argv[0]);
553 exit(-1);
554 }
555 android::g_logRotateSizeKBytes = atoi(optarg);
556 }
557 break;
558
559 case 'n':
560 if (!isdigit(optarg[0])) {
561 fprintf(stderr,"Invalid parameter to -r\n");
562 android::show_help(argv[0]);
563 exit(-1);
564 }
565
566 android::g_maxRotatedLogs = atoi(optarg);
567 break;
568
569 case 'v':
570 err = setLogFormat (optarg);
571 if (err < 0) {
572 fprintf(stderr,"Invalid parameter to -v\n");
573 android::show_help(argv[0]);
574 exit(-1);
575 }
576
577 hasSetLogFormat = 1;
578 break;
579
580 case 'Q':
581 /* this is a *hidden* option used to start a version of logcat */
582 /* in an emulated device only. it basically looks for androidboot.logcat= */
583 /* on the kernel command line. If something is found, it extracts a log filter */
584 /* and uses it to run the program. If nothing is found, the program should */
585 /* quit immediately */
586#define KERNEL_OPTION "androidboot.logcat="
587#define CONSOLE_OPTION "androidboot.console="
588 {
589 int fd;
590 char* logcat;
591 char* console;
592 int force_exit = 1;
593 static char cmdline[1024];
594
595 fd = open("/proc/cmdline", O_RDONLY);
596 if (fd >= 0) {
597 int n = read(fd, cmdline, sizeof(cmdline)-1 );
598 if (n < 0) n = 0;
599 cmdline[n] = 0;
600 close(fd);
601 } else {
602 cmdline[0] = 0;
603 }
604
605 logcat = strstr( cmdline, KERNEL_OPTION );
606 console = strstr( cmdline, CONSOLE_OPTION );
607 if (logcat != NULL) {
608 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
609 char* q = strpbrk( p, " \t\n\r" );;
610
611 if (q != NULL)
612 *q = 0;
613
614 forceFilters = p;
615 force_exit = 0;
616 }
617 /* if nothing found or invalid filters, exit quietly */
618 if (force_exit)
619 exit(0);
620
621 /* redirect our output to the emulator console */
622 if (console) {
623 char* p = console + sizeof(CONSOLE_OPTION)-1;
624 char* q = strpbrk( p, " \t\n\r" );
625 char devname[64];
626 int len;
627
628 if (q != NULL) {
629 len = q - p;
630 } else
631 len = strlen(p);
632
633 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
634 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
635 if (len < (int)sizeof(devname)) {
636 fd = open( devname, O_WRONLY );
637 if (fd >= 0) {
638 dup2(fd, 1);
639 dup2(fd, 2);
640 close(fd);
641 }
642 }
643 }
644 }
645 break;
646
647 default:
648 fprintf(stderr,"Unrecognized Option\n");
649 android::show_help(argv[0]);
650 exit(-1);
651 break;
652 }
653 }
654
Joe Onorato6fa09a02010-02-26 10:04:23 -0800655 if (!devices) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800656 devices = new log_device_t(strdup("/dev/"LOGGER_LOG_MAIN), false, 'm');
Joe Onorato6fa09a02010-02-26 10:04:23 -0800657 android::g_devCount = 1;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800658 int accessmode =
659 (mode & O_RDONLY) ? R_OK : 0
660 | (mode & O_WRONLY) ? W_OK : 0;
661 // only add this if it's available
662 if (0 == access("/dev/"LOGGER_LOG_SYSTEM, accessmode)) {
663 devices->next = new log_device_t(strdup("/dev/"LOGGER_LOG_SYSTEM), false, 's');
664 android::g_devCount++;
665 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800666 }
667
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800668 if (android::g_logRotateSizeKBytes != 0
669 && android::g_outputFileName == NULL
670 ) {
671 fprintf(stderr,"-r requires -f as well\n");
672 android::show_help(argv[0]);
673 exit(-1);
674 }
675
676 android::setupOutput();
677
678 if (hasSetLogFormat == 0) {
679 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
680
681 if (logFormat != NULL) {
682 err = setLogFormat(logFormat);
683
684 if (err < 0) {
685 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
686 logFormat);
687 }
688 }
689 }
690
691 if (forceFilters) {
692 err = android_log_addFilterString(g_logformat, forceFilters);
693 if (err < 0) {
694 fprintf (stderr, "Invalid filter expression in -logcat option\n");
695 exit(0);
696 }
697 } else if (argc == optind) {
698 // Add from environment variable
699 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
700
701 if (env_tags_orig != NULL) {
702 err = android_log_addFilterString(g_logformat, env_tags_orig);
703
704 if (err < 0) {
705 fprintf(stderr, "Invalid filter expression in"
706 " ANDROID_LOG_TAGS\n");
707 android::show_help(argv[0]);
708 exit(-1);
709 }
710 }
711 } else {
712 // Add from commandline
713 for (int i = optind ; i < argc ; i++) {
714 err = android_log_addFilterString(g_logformat, argv[i]);
715
716 if (err < 0) {
717 fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]);
718 android::show_help(argv[0]);
719 exit(-1);
720 }
721 }
722 }
723
Joe Onorato6fa09a02010-02-26 10:04:23 -0800724 dev = devices;
725 while (dev) {
726 dev->fd = open(dev->device, mode);
727 if (dev->fd < 0) {
728 fprintf(stderr, "Unable to open log device '%s': %s\n",
729 dev->device, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800730 exit(EXIT_FAILURE);
731 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800732
733 if (clearLog) {
734 int ret;
735 ret = android::clearLog(dev->fd);
736 if (ret) {
737 perror("ioctl");
738 exit(EXIT_FAILURE);
739 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800740 }
741
742 if (getLogSize) {
743 int size, readable;
744
745 size = android::getLogSize(dev->fd);
746 if (size < 0) {
747 perror("ioctl");
748 exit(EXIT_FAILURE);
749 }
750
751 readable = android::getLogReadableSize(dev->fd);
752 if (readable < 0) {
753 perror("ioctl");
754 exit(EXIT_FAILURE);
755 }
756
757 printf("%s: ring buffer is %dKb (%dKb consumed), "
758 "max entry is %db, max payload is %db\n", dev->device,
759 size / 1024, readable / 1024,
760 (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
761 }
762
763 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800764 }
765
766 if (getLogSize) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800767 return 0;
768 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800769 if (clearLog) {
770 return 0;
771 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800772
773 //LOG_EVENT_INT(10, 12345);
774 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
775 //LOG_EVENT_STRING(0, "whassup, doc?");
776
Joe Onorato6fa09a02010-02-26 10:04:23 -0800777 if (needBinary)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800778 android::g_eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
779
Joe Onorato6fa09a02010-02-26 10:04:23 -0800780 android::readLogLines(devices);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800781
782 return 0;
783}