blob: 995a42e07af88a3616bb1905723cf406791c298c [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"
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700230 " 'events', 'crash' or 'all'. Multiple -b parameters are\n"
231 " allowed and results are interleaved. The default is\n"
232 " -b main -b system -b crash.\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800233 " -B output the log in binary.\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700234 " -S output statistics.\n"
235 " -G <size> set size of log ring buffer, may suffix with K or M.\n"
236 " -p print prune white and ~black list. Service is specified as\n"
237 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
238 " with ~, otherwise weighed for longevity if unadorned. All\n"
239 " other pruning activity is oldest first. Special case ~!\n"
240 " represents an automatic quicker pruning for the noisiest\n"
241 " UID as determined by the current statistics.\n"
242 " -P '<list> ...' set prune white and ~black list, using same format as\n"
243 " printed above. Must be quoted.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244
245 fprintf(stderr,"\nfilterspecs are a series of \n"
246 " <tag>[:priority]\n\n"
247 "where <tag> is a log component tag (or * for all) and priority is:\n"
248 " V Verbose\n"
249 " D Debug\n"
250 " I Info\n"
251 " W Warn\n"
252 " E Error\n"
253 " F Fatal\n"
254 " S Silent (supress all output)\n"
255 "\n'*' means '*:d' and <tag> by itself means <tag>:v\n"
256 "\nIf not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.\n"
257 "If no filterspec is found, filter defaults to '*:I'\n"
258 "\nIf not specified with -v, format is set from ANDROID_PRINTF_LOG\n"
259 "or defaults to \"brief\"\n\n");
260
261
262
263}
264
265
266} /* namespace android */
267
268static int setLogFormat(const char * formatString)
269{
270 static AndroidLogPrintFormat format;
271
272 format = android_log_formatFromString(formatString);
273
274 if (format == FORMAT_OFF) {
275 // FORMAT_OFF means invalid string
276 return -1;
277 }
278
279 android_log_setPrintFormat(g_logformat, format);
280
281 return 0;
282}
283
284extern "C" void logprint_run_tests(void);
285
Joe Onorato6fa09a02010-02-26 10:04:23 -0800286int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800287{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288 int err;
289 int hasSetLogFormat = 0;
290 int clearLog = 0;
291 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800292 unsigned long setLogSize = 0;
293 int getPruneList = 0;
294 char *setPruneList = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800295 int printStatistics = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 int mode = O_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800298 log_device_t* devices = NULL;
299 log_device_t* dev;
300 bool needBinary = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800301 struct logger_list *logger_list;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800302 unsigned int tail_lines = 0;
303 log_time tail_time(log_time::EPOCH);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800305 signal(SIGPIPE, exit);
306
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307 g_logformat = android_log_format_new();
308
309 if (argc == 2 && 0 == strcmp(argv[1], "--test")) {
310 logprint_run_tests();
311 exit(0);
312 }
313
314 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
315 android::show_help(argv[0]);
316 exit(0);
317 }
318
319 for (;;) {
320 int ret;
321
Mark Salyzyn1c950472014-04-01 17:19:47 -0700322 ret = getopt(argc, argv, "cdt:T:gG:sQf:r::n:v:b:BSpP:");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323
324 if (ret < 0) {
325 break;
326 }
327
328 switch(ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800329 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330 // default to all silent
331 android_log_addFilterRule(g_logformat, "*:s");
332 break;
333
334 case 'c':
335 clearLog = 1;
336 mode = O_WRONLY;
337 break;
338
339 case 'd':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800340 mode = O_RDONLY | O_NDELAY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341 break;
342
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800343 case 't':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800344 mode = O_RDONLY | O_NDELAY;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800345 /* FALLTHRU */
346 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800347 if (strspn(optarg, "0123456789") != strlen(optarg)) {
348 char *cp = tail_time.strptime(optarg,
349 log_time::default_format);
350 if (!cp) {
351 fprintf(stderr,
352 "ERROR: -%c \"%s\" not in \"%s\" time format\n",
353 ret, optarg, log_time::default_format);
354 exit(1);
355 }
356 if (*cp) {
357 char c = *cp;
358 *cp = '\0';
359 fprintf(stderr,
360 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
361 ret, optarg, c, cp + 1);
362 *cp = c;
363 }
364 } else {
365 tail_lines = atoi(optarg);
366 if (!tail_lines) {
367 fprintf(stderr,
368 "WARNING: -%c %s invalid, setting to 1\n",
369 ret, optarg);
370 tail_lines = 1;
371 }
372 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800373 break;
374
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375 case 'g':
376 getLogSize = 1;
377 break;
378
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800379 case 'G': {
380 // would use atol if not for the multiplier
381 char *cp = optarg;
382 setLogSize = 0;
383 while (('0' <= *cp) && (*cp <= '9')) {
384 setLogSize *= 10;
385 setLogSize += *cp - '0';
386 ++cp;
387 }
388
389 switch(*cp) {
390 case 'g':
391 case 'G':
392 setLogSize *= 1024;
393 /* FALLTHRU */
394 case 'm':
395 case 'M':
396 setLogSize *= 1024;
397 /* FALLTHRU */
398 case 'k':
399 case 'K':
400 setLogSize *= 1024;
401 /* FALLTHRU */
402 case '\0':
403 break;
404
405 default:
406 setLogSize = 0;
407 }
408
409 if (!setLogSize) {
410 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
411 exit(1);
412 }
413 }
414 break;
415
416 case 'p':
417 getPruneList = 1;
418 break;
419
420 case 'P':
421 setPruneList = optarg;
422 break;
423
Joe Onorato6fa09a02010-02-26 10:04:23 -0800424 case 'b': {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800425 if (strcmp(optarg, "all") == 0) {
426 while (devices) {
427 dev = devices;
428 devices = dev->next;
429 delete dev;
430 }
431
432 dev = devices = new log_device_t("main", false, 'm');
433 android::g_devCount = 1;
434 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
435 dev->next = new log_device_t("system", false, 's');
436 if (dev->next) {
437 dev = dev->next;
438 android::g_devCount++;
439 }
440 }
441 if (android_name_to_log_id("radio") == LOG_ID_RADIO) {
442 dev->next = new log_device_t("radio", false, 'r');
443 if (dev->next) {
444 dev = dev->next;
445 android::g_devCount++;
446 }
447 }
448 if (android_name_to_log_id("events") == LOG_ID_EVENTS) {
449 dev->next = new log_device_t("events", true, 'e');
450 if (dev->next) {
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700451 dev = dev->next;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800452 android::g_devCount++;
453 needBinary = true;
454 }
455 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700456 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
457 dev->next = new log_device_t("crash", false, 'c');
458 if (dev->next) {
459 android::g_devCount++;
460 }
461 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800462 break;
463 }
464
Joe Onorato6fa09a02010-02-26 10:04:23 -0800465 bool binary = strcmp(optarg, "events") == 0;
466 if (binary) {
467 needBinary = true;
468 }
469
470 if (devices) {
471 dev = devices;
472 while (dev->next) {
473 dev = dev->next;
474 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800475 dev->next = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800476 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800477 devices = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800478 }
479 android::g_devCount++;
480 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481 break;
482
483 case 'B':
484 android::g_printBinary = 1;
485 break;
486
487 case 'f':
488 // redirect output to a file
489
490 android::g_outputFileName = optarg;
491
492 break;
493
494 case 'r':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800495 if (optarg == NULL) {
496 android::g_logRotateSizeKBytes
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497 = DEFAULT_LOG_ROTATE_SIZE_KBYTES;
498 } else {
499 long logRotateSize;
500 char *lastDigit;
501
502 if (!isdigit(optarg[0])) {
503 fprintf(stderr,"Invalid parameter to -r\n");
504 android::show_help(argv[0]);
505 exit(-1);
506 }
507 android::g_logRotateSizeKBytes = atoi(optarg);
508 }
509 break;
510
511 case 'n':
512 if (!isdigit(optarg[0])) {
513 fprintf(stderr,"Invalid parameter to -r\n");
514 android::show_help(argv[0]);
515 exit(-1);
516 }
517
518 android::g_maxRotatedLogs = atoi(optarg);
519 break;
520
521 case 'v':
522 err = setLogFormat (optarg);
523 if (err < 0) {
524 fprintf(stderr,"Invalid parameter to -v\n");
525 android::show_help(argv[0]);
526 exit(-1);
527 }
528
529 hasSetLogFormat = 1;
530 break;
531
532 case 'Q':
533 /* this is a *hidden* option used to start a version of logcat */
534 /* in an emulated device only. it basically looks for androidboot.logcat= */
535 /* on the kernel command line. If something is found, it extracts a log filter */
536 /* and uses it to run the program. If nothing is found, the program should */
537 /* quit immediately */
538#define KERNEL_OPTION "androidboot.logcat="
539#define CONSOLE_OPTION "androidboot.console="
540 {
541 int fd;
542 char* logcat;
543 char* console;
544 int force_exit = 1;
545 static char cmdline[1024];
546
547 fd = open("/proc/cmdline", O_RDONLY);
548 if (fd >= 0) {
549 int n = read(fd, cmdline, sizeof(cmdline)-1 );
550 if (n < 0) n = 0;
551 cmdline[n] = 0;
552 close(fd);
553 } else {
554 cmdline[0] = 0;
555 }
556
557 logcat = strstr( cmdline, KERNEL_OPTION );
558 console = strstr( cmdline, CONSOLE_OPTION );
559 if (logcat != NULL) {
560 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
561 char* q = strpbrk( p, " \t\n\r" );;
562
563 if (q != NULL)
564 *q = 0;
565
566 forceFilters = p;
567 force_exit = 0;
568 }
569 /* if nothing found or invalid filters, exit quietly */
570 if (force_exit)
571 exit(0);
572
573 /* redirect our output to the emulator console */
574 if (console) {
575 char* p = console + sizeof(CONSOLE_OPTION)-1;
576 char* q = strpbrk( p, " \t\n\r" );
577 char devname[64];
578 int len;
579
580 if (q != NULL) {
581 len = q - p;
582 } else
583 len = strlen(p);
584
585 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
586 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
587 if (len < (int)sizeof(devname)) {
588 fd = open( devname, O_WRONLY );
589 if (fd >= 0) {
590 dup2(fd, 1);
591 dup2(fd, 2);
592 close(fd);
593 }
594 }
595 }
596 }
597 break;
598
Mark Salyzyn34facab2014-02-06 14:48:50 -0800599 case 'S':
600 printStatistics = 1;
601 break;
602
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800603 default:
604 fprintf(stderr,"Unrecognized Option\n");
605 android::show_help(argv[0]);
606 exit(-1);
607 break;
608 }
609 }
610
Joe Onorato6fa09a02010-02-26 10:04:23 -0800611 if (!devices) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800612 devices = new log_device_t("main", false, 'm');
Joe Onorato6fa09a02010-02-26 10:04:23 -0800613 android::g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800614 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
615 devices->next = new log_device_t("system", false, 's');
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800616 android::g_devCount++;
617 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700618 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
619 if (devices->next) {
620 devices->next->next = new log_device_t("crash", false, 'c');
621 } else {
622 devices->next = new log_device_t("crash", false, 'c');
623 }
624 android::g_devCount++;
625 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800626 }
627
Mark Salyzyn95132e92013-11-22 10:55:48 -0800628 if (android::g_logRotateSizeKBytes != 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800629 && android::g_outputFileName == NULL
630 ) {
631 fprintf(stderr,"-r requires -f as well\n");
632 android::show_help(argv[0]);
633 exit(-1);
634 }
635
636 android::setupOutput();
637
638 if (hasSetLogFormat == 0) {
639 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
640
641 if (logFormat != NULL) {
642 err = setLogFormat(logFormat);
643
644 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800645 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800646 logFormat);
647 }
648 }
649 }
650
651 if (forceFilters) {
652 err = android_log_addFilterString(g_logformat, forceFilters);
653 if (err < 0) {
654 fprintf (stderr, "Invalid filter expression in -logcat option\n");
655 exit(0);
656 }
657 } else if (argc == optind) {
658 // Add from environment variable
659 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
660
661 if (env_tags_orig != NULL) {
662 err = android_log_addFilterString(g_logformat, env_tags_orig);
663
Mark Salyzyn95132e92013-11-22 10:55:48 -0800664 if (err < 0) {
665 fprintf(stderr, "Invalid filter expression in"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800666 " ANDROID_LOG_TAGS\n");
667 android::show_help(argv[0]);
668 exit(-1);
669 }
670 }
671 } else {
672 // Add from commandline
673 for (int i = optind ; i < argc ; i++) {
674 err = android_log_addFilterString(g_logformat, argv[i]);
675
Mark Salyzyn95132e92013-11-22 10:55:48 -0800676 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800677 fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]);
678 android::show_help(argv[0]);
679 exit(-1);
680 }
681 }
682 }
683
Joe Onorato6fa09a02010-02-26 10:04:23 -0800684 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800685 if (tail_time != log_time::EPOCH) {
686 logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
687 } else {
688 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
689 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800690 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800691 dev->logger_list = logger_list;
692 dev->logger = android_logger_open(logger_list,
693 android_name_to_log_id(dev->device));
694 if (!dev->logger) {
695 fprintf(stderr, "Unable to open log device '%s'\n", dev->device);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800696 exit(EXIT_FAILURE);
697 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800698
699 if (clearLog) {
700 int ret;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800701 ret = android_logger_clear(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800702 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700703 perror("failed to clear the log");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800704 exit(EXIT_FAILURE);
705 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800706 }
707
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800708 if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700709 perror("failed to set the log size");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800710 exit(EXIT_FAILURE);
711 }
712
Joe Onorato6fa09a02010-02-26 10:04:23 -0800713 if (getLogSize) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800714 long size, readable;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800715
Mark Salyzyn95132e92013-11-22 10:55:48 -0800716 size = android_logger_get_log_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800717 if (size < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700718 perror("failed to get the log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800719 exit(EXIT_FAILURE);
720 }
721
Mark Salyzyn95132e92013-11-22 10:55:48 -0800722 readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800723 if (readable < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700724 perror("failed to get the readable log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800725 exit(EXIT_FAILURE);
726 }
727
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800728 printf("%s: ring buffer is %ldKb (%ldKb consumed), "
Joe Onorato6fa09a02010-02-26 10:04:23 -0800729 "max entry is %db, max payload is %db\n", dev->device,
730 size / 1024, readable / 1024,
731 (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
732 }
733
734 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800735 }
736
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800737 if (setPruneList) {
738 size_t len = strlen(setPruneList) + 32; // margin to allow rc
739 char *buf = (char *) malloc(len);
740
741 strcpy(buf, setPruneList);
742 int ret = android_logger_set_prune_list(logger_list, buf, len);
743 free(buf);
744
745 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700746 perror("failed to set the prune list");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800747 exit(EXIT_FAILURE);
748 }
749 }
750
Mark Salyzyn1c950472014-04-01 17:19:47 -0700751 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800752 size_t len = 8192;
753 char *buf;
754
755 for(int retry = 32;
756 (retry >= 0) && ((buf = new char [len]));
757 delete [] buf, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800758 if (getPruneList) {
759 android_logger_get_prune_list(logger_list, buf, len);
760 } else {
761 android_logger_get_statistics(logger_list, buf, len);
762 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800763 buf[len-1] = '\0';
764 size_t ret = atol(buf) + 1;
765 if (ret < 4) {
766 delete [] buf;
767 buf = NULL;
768 break;
769 }
770 bool check = ret <= len;
771 len = ret;
772 if (check) {
773 break;
774 }
775 }
776
777 if (!buf) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700778 perror("failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -0800779 exit(EXIT_FAILURE);
780 }
781
782 // remove trailing FF
783 char *cp = buf + len - 1;
784 *cp = '\0';
785 bool truncated = *--cp != '\f';
786 if (!truncated) {
787 *cp = '\0';
788 }
789
790 // squash out the byte count
791 cp = buf;
792 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -0700793 while (isdigit(*cp)) {
794 ++cp;
795 }
796 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800797 ++cp;
798 }
799 }
800
801 printf("%s", cp);
802 delete [] buf;
803 exit(0);
804 }
805
806
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800807 if (getLogSize) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700808 exit(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800809 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800810 if (setLogSize || setPruneList) {
811 exit(0);
812 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800813 if (clearLog) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700814 exit(0);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800815 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800816
817 //LOG_EVENT_INT(10, 12345);
818 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
819 //LOG_EVENT_STRING(0, "whassup, doc?");
820
Joe Onorato6fa09a02010-02-26 10:04:23 -0800821 if (needBinary)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800822 android::g_eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
823
Mark Salyzyn95132e92013-11-22 10:55:48 -0800824 while (1) {
825 struct log_msg log_msg;
826 int ret = android_logger_list_read(logger_list, &log_msg);
827
828 if (ret == 0) {
829 fprintf(stderr, "read: Unexpected EOF!\n");
830 exit(EXIT_FAILURE);
831 }
832
833 if (ret < 0) {
834 if (ret == -EAGAIN) {
835 break;
836 }
837
838 if (ret == -EIO) {
839 fprintf(stderr, "read: Unexpected EOF!\n");
840 exit(EXIT_FAILURE);
841 }
842 if (ret == -EINVAL) {
843 fprintf(stderr, "read: unexpected length.\n");
844 exit(EXIT_FAILURE);
845 }
Mark Salyzynfff04e32014-03-17 10:36:46 -0700846 perror("logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800847 exit(EXIT_FAILURE);
848 }
849
850 for(dev = devices; dev; dev = dev->next) {
851 if (android_name_to_log_id(dev->device) == log_msg.id()) {
852 break;
853 }
854 }
855 if (!dev) {
856 fprintf(stderr, "read: Unexpected log ID!\n");
857 exit(EXIT_FAILURE);
858 }
859
860 android::maybePrintStart(dev);
861 if (android::g_printBinary) {
862 android::printBinary(&log_msg);
863 } else {
864 android::processBuffer(dev, &log_msg);
865 }
866 }
867
868 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800869
870 return 0;
871}