logcat.cpp revision 22e287df0dfbc6e10c02f570d2fc0c42a2a6b7aa
1// Copyright 2006-2014 The Android Open Source Project
2
3#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>
19#include <log/log.h>
20#include <log/log_read.h>
21#include <log/logger.h>
22#include <log/logd.h>
23#include <log/logprint.h>
24#include <log/event_tag_map.h>
25
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
34struct log_device_t {
35    const char* device;
36    bool binary;
37    struct logger *logger;
38    struct logger_list *logger_list;
39    bool printed;
40    char label;
41
42    log_device_t* next;
43
44    log_device_t(const char* d, bool b, char l) {
45        device = d;
46        binary = b;
47        label = l;
48        next = NULL;
49        printed = false;
50    }
51};
52
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;
62static int g_printBinary = 0;
63static int g_devCount = 0;
64
65static EventTagMap* g_eventTagMap = NULL;
66
67static int openLogFile (const char *pathname)
68{
69    return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
70}
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
115void printBinary(struct log_msg *buf)
116{
117    size_t size = buf->len();
118
119    TEMP_FAILURE_RETRY(write(g_outFD, buf, size));
120}
121
122static void processBuffer(log_device_t* dev, struct log_msg *buf)
123{
124    int bytesWritten = 0;
125    int err;
126    AndroidLogEntry entry;
127    char binaryMsgBuf[1024];
128
129    if (dev->binary) {
130        err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry,
131                                                 g_eventTagMap,
132                                                 binaryMsgBuf,
133                                                 sizeof(binaryMsgBuf));
134        //printf(">>> pri=%d len=%d msg='%s'\n",
135        //    entry.priority, entry.messageLen, entry.message);
136    } else {
137        err = android_log_processLogBuffer(&buf->entry_v1, &entry);
138    }
139    if (err < 0) {
140        goto error;
141    }
142
143    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        }
153
154        bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry);
155
156        if (bytesWritten < 0) {
157            perror("output error");
158            exit(-1);
159        }
160    }
161
162    g_outByteCount += bytesWritten;
163
164    if (g_logRotateSizeKBytes > 0
165        && (g_outByteCount / 1024) >= g_logRotateSizeKBytes
166    ) {
167        rotateLogs();
168    }
169
170error:
171    //fprintf (stderr, "Error processing record\n");
172    return;
173}
174
175static 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];
180            snprintf(buf, sizeof(buf), "--------- beginning of %s\n",
181                     dev->device);
182            if (write(g_outFD, buf, strlen(buf)) < 0) {
183                perror("output error");
184                exit(-1);
185            }
186        }
187    }
188}
189
190static 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"
226                    "  -t <count>      print only the most recent <count> lines (implies -d)\n"
227                    "  -T <count>      print only the most recent <count> lines (does not imply -d)\n"
228                    "  -g              get the size of the log's ring buffer and exit\n"
229                    "  -b <buffer>     Request alternate ring buffer, 'main', 'system', 'radio',\n"
230                    "                  'events' or 'all'. Multiple -b parameters are allowed and\n"
231                    "                  results are interleaved. The default is -b main -b system.\n"
232                    "  -B              output the log in binary.\n"
233                    "  -S              output statistics.\n");
234
235#ifdef USERDEBUG_BUILD
236
237    fprintf(stderr, "--------------------- eng & userdebug builds only ---------------------------\n"
238                    "  -G <count>      set size of log's ring buffer and exit\n"
239                    "  -p              output prune white and ~black list\n"
240                    "  -P '<list> ...' set prune white and ~black list; UID, /PID or !(worst UID)\n"
241                    "                  default is ~!, prune worst UID.\n"
242                    "-----------------------------------------------------------------------------\n"
243    );
244
245#endif
246
247    fprintf(stderr,"\nfilterspecs are a series of \n"
248                   "  <tag>[:priority]\n\n"
249                   "where <tag> is a log component tag (or * for all) and priority is:\n"
250                   "  V    Verbose\n"
251                   "  D    Debug\n"
252                   "  I    Info\n"
253                   "  W    Warn\n"
254                   "  E    Error\n"
255                   "  F    Fatal\n"
256                   "  S    Silent (supress all output)\n"
257                   "\n'*' means '*:d' and <tag> by itself means <tag>:v\n"
258                   "\nIf not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.\n"
259                   "If no filterspec is found, filter defaults to '*:I'\n"
260                   "\nIf not specified with -v, format is set from ANDROID_PRINTF_LOG\n"
261                   "or defaults to \"brief\"\n\n");
262
263
264
265}
266
267
268} /* namespace android */
269
270static int setLogFormat(const char * formatString)
271{
272    static AndroidLogPrintFormat format;
273
274    format = android_log_formatFromString(formatString);
275
276    if (format == FORMAT_OFF) {
277        // FORMAT_OFF means invalid string
278        return -1;
279    }
280
281    android_log_setPrintFormat(g_logformat, format);
282
283    return 0;
284}
285
286extern "C" void logprint_run_tests(void);
287
288int main(int argc, char **argv)
289{
290    int err;
291    int hasSetLogFormat = 0;
292    int clearLog = 0;
293    int getLogSize = 0;
294#ifdef USERDEBUG_BUILD
295    unsigned long setLogSize = 0;
296    int getPruneList = 0;
297    char *setPruneList = NULL;
298#endif
299    int printStatistics = 0;
300    int mode = O_RDONLY;
301    const char *forceFilters = NULL;
302    log_device_t* devices = NULL;
303    log_device_t* dev;
304    bool needBinary = false;
305    struct logger_list *logger_list;
306    unsigned int tail_lines = 0;
307    log_time tail_time(log_time::EPOCH);
308
309    signal(SIGPIPE, exit);
310
311    g_logformat = android_log_format_new();
312
313    if (argc == 2 && 0 == strcmp(argv[1], "--test")) {
314        logprint_run_tests();
315        exit(0);
316    }
317
318    if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
319        android::show_help(argv[0]);
320        exit(0);
321    }
322
323    for (;;) {
324        int ret;
325
326        ret = getopt(argc, argv,
327#ifdef USERDEBUG_BUILD
328            "cdt:T:gG:sQf:r::n:v:b:BSpP:"
329#else
330            "cdt:T:gsQf:r::n:v:b:BS"
331#endif
332        );
333
334        if (ret < 0) {
335            break;
336        }
337
338        switch(ret) {
339            case 's':
340                // default to all silent
341                android_log_addFilterRule(g_logformat, "*:s");
342            break;
343
344            case 'c':
345                clearLog = 1;
346                mode = O_WRONLY;
347            break;
348
349            case 'd':
350                mode = O_RDONLY | O_NDELAY;
351            break;
352
353            case 't':
354                mode = O_RDONLY | O_NDELAY;
355                /* FALLTHRU */
356            case 'T':
357                if (strspn(optarg, "0123456789") != strlen(optarg)) {
358                    char *cp = tail_time.strptime(optarg,
359                                                  log_time::default_format);
360                    if (!cp) {
361                        fprintf(stderr,
362                                "ERROR: -%c \"%s\" not in \"%s\" time format\n",
363                                ret, optarg, log_time::default_format);
364                        exit(1);
365                    }
366                    if (*cp) {
367                        char c = *cp;
368                        *cp = '\0';
369                        fprintf(stderr,
370                                "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
371                                ret, optarg, c, cp + 1);
372                        *cp = c;
373                    }
374                } else {
375                    tail_lines = atoi(optarg);
376                    if (!tail_lines) {
377                        fprintf(stderr,
378                                "WARNING: -%c %s invalid, setting to 1\n",
379                                ret, optarg);
380                        tail_lines = 1;
381                    }
382                }
383            break;
384
385            case 'g':
386                getLogSize = 1;
387            break;
388
389#ifdef USERDEBUG_BUILD
390
391            case 'G': {
392                // would use atol if not for the multiplier
393                char *cp = optarg;
394                setLogSize = 0;
395                while (('0' <= *cp) && (*cp <= '9')) {
396                    setLogSize *= 10;
397                    setLogSize += *cp - '0';
398                    ++cp;
399                }
400
401                switch(*cp) {
402                case 'g':
403                case 'G':
404                    setLogSize *= 1024;
405                /* FALLTHRU */
406                case 'm':
407                case 'M':
408                    setLogSize *= 1024;
409                /* FALLTHRU */
410                case 'k':
411                case 'K':
412                    setLogSize *= 1024;
413                /* FALLTHRU */
414                case '\0':
415                break;
416
417                default:
418                    setLogSize = 0;
419                }
420
421                if (!setLogSize) {
422                    fprintf(stderr, "ERROR: -G <num><multiplier>\n");
423                    exit(1);
424                }
425            }
426            break;
427
428            case 'p':
429                getPruneList = 1;
430            break;
431
432            case 'P':
433                setPruneList = optarg;
434            break;
435
436#endif
437
438            case 'b': {
439                if (strcmp(optarg, "all") == 0) {
440                    while (devices) {
441                        dev = devices;
442                        devices = dev->next;
443                        delete dev;
444                    }
445
446                    dev = devices = new log_device_t("main", false, 'm');
447                    android::g_devCount = 1;
448                    if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
449                        dev->next = new log_device_t("system", false, 's');
450                        if (dev->next) {
451                            dev = dev->next;
452                            android::g_devCount++;
453                        }
454                    }
455                    if (android_name_to_log_id("radio") == LOG_ID_RADIO) {
456                        dev->next = new log_device_t("radio", false, 'r');
457                        if (dev->next) {
458                            dev = dev->next;
459                            android::g_devCount++;
460                        }
461                    }
462                    if (android_name_to_log_id("events") == LOG_ID_EVENTS) {
463                        dev->next = new log_device_t("events", true, 'e');
464                        if (dev->next) {
465                            android::g_devCount++;
466                            needBinary = true;
467                        }
468                    }
469                    break;
470                }
471
472                bool binary = strcmp(optarg, "events") == 0;
473                if (binary) {
474                    needBinary = true;
475                }
476
477                if (devices) {
478                    dev = devices;
479                    while (dev->next) {
480                        dev = dev->next;
481                    }
482                    dev->next = new log_device_t(optarg, binary, optarg[0]);
483                } else {
484                    devices = new log_device_t(optarg, binary, optarg[0]);
485                }
486                android::g_devCount++;
487            }
488            break;
489
490            case 'B':
491                android::g_printBinary = 1;
492            break;
493
494            case 'f':
495                // redirect output to a file
496
497                android::g_outputFileName = optarg;
498
499            break;
500
501            case 'r':
502                if (optarg == NULL) {
503                    android::g_logRotateSizeKBytes
504                                = DEFAULT_LOG_ROTATE_SIZE_KBYTES;
505                } else {
506                    long logRotateSize;
507                    char *lastDigit;
508
509                    if (!isdigit(optarg[0])) {
510                        fprintf(stderr,"Invalid parameter to -r\n");
511                        android::show_help(argv[0]);
512                        exit(-1);
513                    }
514                    android::g_logRotateSizeKBytes = atoi(optarg);
515                }
516            break;
517
518            case 'n':
519                if (!isdigit(optarg[0])) {
520                    fprintf(stderr,"Invalid parameter to -r\n");
521                    android::show_help(argv[0]);
522                    exit(-1);
523                }
524
525                android::g_maxRotatedLogs = atoi(optarg);
526            break;
527
528            case 'v':
529                err = setLogFormat (optarg);
530                if (err < 0) {
531                    fprintf(stderr,"Invalid parameter to -v\n");
532                    android::show_help(argv[0]);
533                    exit(-1);
534                }
535
536                hasSetLogFormat = 1;
537            break;
538
539            case 'Q':
540                /* this is a *hidden* option used to start a version of logcat                 */
541                /* in an emulated device only. it basically looks for androidboot.logcat=      */
542                /* on the kernel command line. If something is found, it extracts a log filter */
543                /* and uses it to run the program. If nothing is found, the program should     */
544                /* quit immediately                                                            */
545#define  KERNEL_OPTION  "androidboot.logcat="
546#define  CONSOLE_OPTION "androidboot.console="
547                {
548                    int          fd;
549                    char*        logcat;
550                    char*        console;
551                    int          force_exit = 1;
552                    static char  cmdline[1024];
553
554                    fd = open("/proc/cmdline", O_RDONLY);
555                    if (fd >= 0) {
556                        int  n = read(fd, cmdline, sizeof(cmdline)-1 );
557                        if (n < 0) n = 0;
558                        cmdline[n] = 0;
559                        close(fd);
560                    } else {
561                        cmdline[0] = 0;
562                    }
563
564                    logcat  = strstr( cmdline, KERNEL_OPTION );
565                    console = strstr( cmdline, CONSOLE_OPTION );
566                    if (logcat != NULL) {
567                        char*  p = logcat + sizeof(KERNEL_OPTION)-1;;
568                        char*  q = strpbrk( p, " \t\n\r" );;
569
570                        if (q != NULL)
571                            *q = 0;
572
573                        forceFilters = p;
574                        force_exit   = 0;
575                    }
576                    /* if nothing found or invalid filters, exit quietly */
577                    if (force_exit)
578                        exit(0);
579
580                    /* redirect our output to the emulator console */
581                    if (console) {
582                        char*  p = console + sizeof(CONSOLE_OPTION)-1;
583                        char*  q = strpbrk( p, " \t\n\r" );
584                        char   devname[64];
585                        int    len;
586
587                        if (q != NULL) {
588                            len = q - p;
589                        } else
590                            len = strlen(p);
591
592                        len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
593                        fprintf(stderr, "logcat using %s (%d)\n", devname, len);
594                        if (len < (int)sizeof(devname)) {
595                            fd = open( devname, O_WRONLY );
596                            if (fd >= 0) {
597                                dup2(fd, 1);
598                                dup2(fd, 2);
599                                close(fd);
600                            }
601                        }
602                    }
603                }
604                break;
605
606            case 'S':
607                printStatistics = 1;
608                break;
609
610            default:
611                fprintf(stderr,"Unrecognized Option\n");
612                android::show_help(argv[0]);
613                exit(-1);
614            break;
615        }
616    }
617
618    if (!devices) {
619        devices = new log_device_t("main", false, 'm');
620        android::g_devCount = 1;
621        if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
622            devices->next = new log_device_t("system", false, 's');
623            android::g_devCount++;
624        }
625    }
626
627    if (android::g_logRotateSizeKBytes != 0
628        && android::g_outputFileName == NULL
629    ) {
630        fprintf(stderr,"-r requires -f as well\n");
631        android::show_help(argv[0]);
632        exit(-1);
633    }
634
635    android::setupOutput();
636
637    if (hasSetLogFormat == 0) {
638        const char* logFormat = getenv("ANDROID_PRINTF_LOG");
639
640        if (logFormat != NULL) {
641            err = setLogFormat(logFormat);
642
643            if (err < 0) {
644                fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
645                                    logFormat);
646            }
647        }
648    }
649
650    if (forceFilters) {
651        err = android_log_addFilterString(g_logformat, forceFilters);
652        if (err < 0) {
653            fprintf (stderr, "Invalid filter expression in -logcat option\n");
654            exit(0);
655        }
656    } else if (argc == optind) {
657        // Add from environment variable
658        char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
659
660        if (env_tags_orig != NULL) {
661            err = android_log_addFilterString(g_logformat, env_tags_orig);
662
663            if (err < 0) {
664                fprintf(stderr, "Invalid filter expression in"
665                                    " ANDROID_LOG_TAGS\n");
666                android::show_help(argv[0]);
667                exit(-1);
668            }
669        }
670    } else {
671        // Add from commandline
672        for (int i = optind ; i < argc ; i++) {
673            err = android_log_addFilterString(g_logformat, argv[i]);
674
675            if (err < 0) {
676                fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]);
677                android::show_help(argv[0]);
678                exit(-1);
679            }
680        }
681    }
682
683    dev = devices;
684    if (tail_time != log_time::EPOCH) {
685        logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
686    } else {
687        logger_list = android_logger_list_alloc(mode, tail_lines, 0);
688    }
689    while (dev) {
690        dev->logger_list = logger_list;
691        dev->logger = android_logger_open(logger_list,
692                                          android_name_to_log_id(dev->device));
693        if (!dev->logger) {
694            fprintf(stderr, "Unable to open log device '%s'\n", dev->device);
695            exit(EXIT_FAILURE);
696        }
697
698        if (clearLog) {
699            int ret;
700            ret = android_logger_clear(dev->logger);
701            if (ret) {
702                perror("failed to clear the log");
703                exit(EXIT_FAILURE);
704            }
705        }
706
707#ifdef USERDEBUG_BUILD
708
709        if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) {
710            perror("failed to set the log size");
711            exit(EXIT_FAILURE);
712        }
713
714#endif
715
716        if (getLogSize) {
717            long size, readable;
718
719            size = android_logger_get_log_size(dev->logger);
720            if (size < 0) {
721                perror("failed to get the log size");
722                exit(EXIT_FAILURE);
723            }
724
725            readable = android_logger_get_log_readable_size(dev->logger);
726            if (readable < 0) {
727                perror("failed to get the readable log size");
728                exit(EXIT_FAILURE);
729            }
730
731            printf("%s: ring buffer is %ldKb (%ldKb consumed), "
732                   "max entry is %db, max payload is %db\n", dev->device,
733                   size / 1024, readable / 1024,
734                   (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
735        }
736
737        dev = dev->next;
738    }
739
740#ifdef USERDEBUG_BUILD
741
742    if (setPruneList) {
743        size_t len = strlen(setPruneList) + 32; // margin to allow rc
744        char *buf = (char *) malloc(len);
745
746        strcpy(buf, setPruneList);
747        int ret = android_logger_set_prune_list(logger_list, buf, len);
748        free(buf);
749
750        if (ret) {
751            perror("failed to set the prune list");
752            exit(EXIT_FAILURE);
753        }
754    }
755
756#endif
757
758    if (
759#ifdef USERDEBUG_BUILD
760        printStatistics || getPruneList
761#else
762        printStatistics
763#endif
764    ) {
765        size_t len = 8192;
766        char *buf;
767
768        for(int retry = 32;
769                (retry >= 0) && ((buf = new char [len]));
770                delete [] buf, --retry) {
771#ifdef USERDEBUG_BUILD
772            if (getPruneList) {
773                android_logger_get_prune_list(logger_list, buf, len);
774            } else {
775                android_logger_get_statistics(logger_list, buf, len);
776            }
777#else
778            android_logger_get_statistics(logger_list, buf, len);
779#endif
780            buf[len-1] = '\0';
781            size_t ret = atol(buf) + 1;
782            if (ret < 4) {
783                delete [] buf;
784                buf = NULL;
785                break;
786            }
787            bool check = ret <= len;
788            len = ret;
789            if (check) {
790                break;
791            }
792        }
793
794        if (!buf) {
795            perror("failed to read data");
796            exit(EXIT_FAILURE);
797        }
798
799        // remove trailing FF
800        char *cp = buf + len - 1;
801        *cp = '\0';
802        bool truncated = *--cp != '\f';
803        if (!truncated) {
804            *cp = '\0';
805        }
806
807        // squash out the byte count
808        cp = buf;
809        if (!truncated) {
810            while (isdigit(*cp)) {
811                ++cp;
812            }
813            if (*cp == '\n') {
814                ++cp;
815            }
816        }
817
818        printf("%s", cp);
819        delete [] buf;
820        exit(0);
821    }
822
823
824    if (getLogSize) {
825        exit(0);
826    }
827#ifdef USERDEBUG_BUILD
828    if (setLogSize || setPruneList) {
829        exit(0);
830    }
831#endif
832    if (clearLog) {
833        exit(0);
834    }
835
836    //LOG_EVENT_INT(10, 12345);
837    //LOG_EVENT_LONG(11, 0x1122334455667788LL);
838    //LOG_EVENT_STRING(0, "whassup, doc?");
839
840    if (needBinary)
841        android::g_eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
842
843    while (1) {
844        struct log_msg log_msg;
845        int ret = android_logger_list_read(logger_list, &log_msg);
846
847        if (ret == 0) {
848            fprintf(stderr, "read: Unexpected EOF!\n");
849            exit(EXIT_FAILURE);
850        }
851
852        if (ret < 0) {
853            if (ret == -EAGAIN) {
854                break;
855            }
856
857            if (ret == -EIO) {
858                fprintf(stderr, "read: Unexpected EOF!\n");
859                exit(EXIT_FAILURE);
860            }
861            if (ret == -EINVAL) {
862                fprintf(stderr, "read: unexpected length.\n");
863                exit(EXIT_FAILURE);
864            }
865            perror("logcat read failure");
866            exit(EXIT_FAILURE);
867        }
868
869        for(dev = devices; dev; dev = dev->next) {
870            if (android_name_to_log_id(dev->device) == log_msg.id()) {
871                break;
872            }
873        }
874        if (!dev) {
875            fprintf(stderr, "read: Unexpected log ID!\n");
876            exit(EXIT_FAILURE);
877        }
878
879        android::maybePrintStart(dev);
880        if (android::g_printBinary) {
881            android::printBinary(&log_msg);
882        } else {
883            android::processBuffer(dev, &log_msg);
884        }
885    }
886
887    android_logger_list_free(logger_list);
888
889    return 0;
890}
891