blob: ea46345860d70049012a6fca41feaa7825c26a23 [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
Joe Onorato6fa09a02010-02-26 10:04:23 -0800284int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800285{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286 int err;
287 int hasSetLogFormat = 0;
288 int clearLog = 0;
289 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800290 unsigned long setLogSize = 0;
291 int getPruneList = 0;
292 char *setPruneList = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800293 int printStatistics = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800294 int mode = O_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800296 log_device_t* devices = NULL;
297 log_device_t* dev;
298 bool needBinary = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800299 struct logger_list *logger_list;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800300 unsigned int tail_lines = 0;
301 log_time tail_time(log_time::EPOCH);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800303 signal(SIGPIPE, exit);
304
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305 g_logformat = android_log_format_new();
306
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
308 android::show_help(argv[0]);
309 exit(0);
310 }
311
312 for (;;) {
313 int ret;
314
Mark Salyzyn1c950472014-04-01 17:19:47 -0700315 ret = getopt(argc, argv, "cdt:T:gG:sQf:r::n:v:b:BSpP:");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316
317 if (ret < 0) {
318 break;
319 }
320
321 switch(ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800322 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323 // default to all silent
324 android_log_addFilterRule(g_logformat, "*:s");
325 break;
326
327 case 'c':
328 clearLog = 1;
329 mode = O_WRONLY;
330 break;
331
332 case 'd':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800333 mode = O_RDONLY | O_NDELAY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 break;
335
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800336 case 't':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800337 mode = O_RDONLY | O_NDELAY;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800338 /* FALLTHRU */
339 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800340 if (strspn(optarg, "0123456789") != strlen(optarg)) {
341 char *cp = tail_time.strptime(optarg,
342 log_time::default_format);
343 if (!cp) {
344 fprintf(stderr,
345 "ERROR: -%c \"%s\" not in \"%s\" time format\n",
346 ret, optarg, log_time::default_format);
347 exit(1);
348 }
349 if (*cp) {
350 char c = *cp;
351 *cp = '\0';
352 fprintf(stderr,
353 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
354 ret, optarg, c, cp + 1);
355 *cp = c;
356 }
357 } else {
358 tail_lines = atoi(optarg);
359 if (!tail_lines) {
360 fprintf(stderr,
361 "WARNING: -%c %s invalid, setting to 1\n",
362 ret, optarg);
363 tail_lines = 1;
364 }
365 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800366 break;
367
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368 case 'g':
369 getLogSize = 1;
370 break;
371
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800372 case 'G': {
373 // would use atol if not for the multiplier
374 char *cp = optarg;
375 setLogSize = 0;
376 while (('0' <= *cp) && (*cp <= '9')) {
377 setLogSize *= 10;
378 setLogSize += *cp - '0';
379 ++cp;
380 }
381
382 switch(*cp) {
383 case 'g':
384 case 'G':
385 setLogSize *= 1024;
386 /* FALLTHRU */
387 case 'm':
388 case 'M':
389 setLogSize *= 1024;
390 /* FALLTHRU */
391 case 'k':
392 case 'K':
393 setLogSize *= 1024;
394 /* FALLTHRU */
395 case '\0':
396 break;
397
398 default:
399 setLogSize = 0;
400 }
401
402 if (!setLogSize) {
403 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
404 exit(1);
405 }
406 }
407 break;
408
409 case 'p':
410 getPruneList = 1;
411 break;
412
413 case 'P':
414 setPruneList = optarg;
415 break;
416
Joe Onorato6fa09a02010-02-26 10:04:23 -0800417 case 'b': {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800418 if (strcmp(optarg, "all") == 0) {
419 while (devices) {
420 dev = devices;
421 devices = dev->next;
422 delete dev;
423 }
424
425 dev = devices = new log_device_t("main", false, 'm');
426 android::g_devCount = 1;
427 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
428 dev->next = new log_device_t("system", false, 's');
429 if (dev->next) {
430 dev = dev->next;
431 android::g_devCount++;
432 }
433 }
434 if (android_name_to_log_id("radio") == LOG_ID_RADIO) {
435 dev->next = new log_device_t("radio", false, 'r');
436 if (dev->next) {
437 dev = dev->next;
438 android::g_devCount++;
439 }
440 }
441 if (android_name_to_log_id("events") == LOG_ID_EVENTS) {
442 dev->next = new log_device_t("events", true, 'e');
443 if (dev->next) {
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700444 dev = dev->next;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800445 android::g_devCount++;
446 needBinary = true;
447 }
448 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700449 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
450 dev->next = new log_device_t("crash", false, 'c');
451 if (dev->next) {
452 android::g_devCount++;
453 }
454 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800455 break;
456 }
457
Joe Onorato6fa09a02010-02-26 10:04:23 -0800458 bool binary = strcmp(optarg, "events") == 0;
459 if (binary) {
460 needBinary = true;
461 }
462
463 if (devices) {
464 dev = devices;
465 while (dev->next) {
466 dev = dev->next;
467 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800468 dev->next = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800469 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800470 devices = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800471 }
472 android::g_devCount++;
473 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474 break;
475
476 case 'B':
477 android::g_printBinary = 1;
478 break;
479
480 case 'f':
481 // redirect output to a file
482
483 android::g_outputFileName = optarg;
484
485 break;
486
487 case 'r':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800488 if (optarg == NULL) {
489 android::g_logRotateSizeKBytes
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490 = DEFAULT_LOG_ROTATE_SIZE_KBYTES;
491 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800492 if (!isdigit(optarg[0])) {
493 fprintf(stderr,"Invalid parameter to -r\n");
494 android::show_help(argv[0]);
495 exit(-1);
496 }
497 android::g_logRotateSizeKBytes = atoi(optarg);
498 }
499 break;
500
501 case 'n':
502 if (!isdigit(optarg[0])) {
503 fprintf(stderr,"Invalid parameter to -r\n");
504 android::show_help(argv[0]);
505 exit(-1);
506 }
507
508 android::g_maxRotatedLogs = atoi(optarg);
509 break;
510
511 case 'v':
512 err = setLogFormat (optarg);
513 if (err < 0) {
514 fprintf(stderr,"Invalid parameter to -v\n");
515 android::show_help(argv[0]);
516 exit(-1);
517 }
518
519 hasSetLogFormat = 1;
520 break;
521
522 case 'Q':
523 /* this is a *hidden* option used to start a version of logcat */
524 /* in an emulated device only. it basically looks for androidboot.logcat= */
525 /* on the kernel command line. If something is found, it extracts a log filter */
526 /* and uses it to run the program. If nothing is found, the program should */
527 /* quit immediately */
528#define KERNEL_OPTION "androidboot.logcat="
529#define CONSOLE_OPTION "androidboot.console="
530 {
531 int fd;
532 char* logcat;
533 char* console;
534 int force_exit = 1;
535 static char cmdline[1024];
536
537 fd = open("/proc/cmdline", O_RDONLY);
538 if (fd >= 0) {
539 int n = read(fd, cmdline, sizeof(cmdline)-1 );
540 if (n < 0) n = 0;
541 cmdline[n] = 0;
542 close(fd);
543 } else {
544 cmdline[0] = 0;
545 }
546
547 logcat = strstr( cmdline, KERNEL_OPTION );
548 console = strstr( cmdline, CONSOLE_OPTION );
549 if (logcat != NULL) {
550 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
551 char* q = strpbrk( p, " \t\n\r" );;
552
553 if (q != NULL)
554 *q = 0;
555
556 forceFilters = p;
557 force_exit = 0;
558 }
559 /* if nothing found or invalid filters, exit quietly */
560 if (force_exit)
561 exit(0);
562
563 /* redirect our output to the emulator console */
564 if (console) {
565 char* p = console + sizeof(CONSOLE_OPTION)-1;
566 char* q = strpbrk( p, " \t\n\r" );
567 char devname[64];
568 int len;
569
570 if (q != NULL) {
571 len = q - p;
572 } else
573 len = strlen(p);
574
575 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
576 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
577 if (len < (int)sizeof(devname)) {
578 fd = open( devname, O_WRONLY );
579 if (fd >= 0) {
580 dup2(fd, 1);
581 dup2(fd, 2);
582 close(fd);
583 }
584 }
585 }
586 }
587 break;
588
Mark Salyzyn34facab2014-02-06 14:48:50 -0800589 case 'S':
590 printStatistics = 1;
591 break;
592
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800593 default:
594 fprintf(stderr,"Unrecognized Option\n");
595 android::show_help(argv[0]);
596 exit(-1);
597 break;
598 }
599 }
600
Joe Onorato6fa09a02010-02-26 10:04:23 -0800601 if (!devices) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800602 devices = new log_device_t("main", false, 'm');
Joe Onorato6fa09a02010-02-26 10:04:23 -0800603 android::g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800604 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
605 devices->next = new log_device_t("system", false, 's');
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800606 android::g_devCount++;
607 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700608 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
609 if (devices->next) {
610 devices->next->next = new log_device_t("crash", false, 'c');
611 } else {
612 devices->next = new log_device_t("crash", false, 'c');
613 }
614 android::g_devCount++;
615 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800616 }
617
Mark Salyzyn95132e92013-11-22 10:55:48 -0800618 if (android::g_logRotateSizeKBytes != 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800619 && android::g_outputFileName == NULL
620 ) {
621 fprintf(stderr,"-r requires -f as well\n");
622 android::show_help(argv[0]);
623 exit(-1);
624 }
625
626 android::setupOutput();
627
628 if (hasSetLogFormat == 0) {
629 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
630
631 if (logFormat != NULL) {
632 err = setLogFormat(logFormat);
633
634 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800635 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800636 logFormat);
637 }
638 }
639 }
640
641 if (forceFilters) {
642 err = android_log_addFilterString(g_logformat, forceFilters);
643 if (err < 0) {
644 fprintf (stderr, "Invalid filter expression in -logcat option\n");
645 exit(0);
646 }
647 } else if (argc == optind) {
648 // Add from environment variable
649 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
650
651 if (env_tags_orig != NULL) {
652 err = android_log_addFilterString(g_logformat, env_tags_orig);
653
Mark Salyzyn95132e92013-11-22 10:55:48 -0800654 if (err < 0) {
655 fprintf(stderr, "Invalid filter expression in"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800656 " ANDROID_LOG_TAGS\n");
657 android::show_help(argv[0]);
658 exit(-1);
659 }
660 }
661 } else {
662 // Add from commandline
663 for (int i = optind ; i < argc ; i++) {
664 err = android_log_addFilterString(g_logformat, argv[i]);
665
Mark Salyzyn95132e92013-11-22 10:55:48 -0800666 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800667 fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]);
668 android::show_help(argv[0]);
669 exit(-1);
670 }
671 }
672 }
673
Joe Onorato6fa09a02010-02-26 10:04:23 -0800674 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800675 if (tail_time != log_time::EPOCH) {
676 logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
677 } else {
678 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
679 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800680 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800681 dev->logger_list = logger_list;
682 dev->logger = android_logger_open(logger_list,
683 android_name_to_log_id(dev->device));
684 if (!dev->logger) {
685 fprintf(stderr, "Unable to open log device '%s'\n", dev->device);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686 exit(EXIT_FAILURE);
687 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800688
689 if (clearLog) {
690 int ret;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800691 ret = android_logger_clear(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800692 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700693 perror("failed to clear the log");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800694 exit(EXIT_FAILURE);
695 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800696 }
697
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800698 if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700699 perror("failed to set the log size");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800700 exit(EXIT_FAILURE);
701 }
702
Joe Onorato6fa09a02010-02-26 10:04:23 -0800703 if (getLogSize) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800704 long size, readable;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800705
Mark Salyzyn95132e92013-11-22 10:55:48 -0800706 size = android_logger_get_log_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800707 if (size < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700708 perror("failed to get the log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800709 exit(EXIT_FAILURE);
710 }
711
Mark Salyzyn95132e92013-11-22 10:55:48 -0800712 readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800713 if (readable < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700714 perror("failed to get the readable log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800715 exit(EXIT_FAILURE);
716 }
717
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800718 printf("%s: ring buffer is %ldKb (%ldKb consumed), "
Joe Onorato6fa09a02010-02-26 10:04:23 -0800719 "max entry is %db, max payload is %db\n", dev->device,
720 size / 1024, readable / 1024,
721 (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
722 }
723
724 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800725 }
726
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800727 if (setPruneList) {
728 size_t len = strlen(setPruneList) + 32; // margin to allow rc
729 char *buf = (char *) malloc(len);
730
731 strcpy(buf, setPruneList);
732 int ret = android_logger_set_prune_list(logger_list, buf, len);
733 free(buf);
734
735 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700736 perror("failed to set the prune list");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800737 exit(EXIT_FAILURE);
738 }
739 }
740
Mark Salyzyn1c950472014-04-01 17:19:47 -0700741 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800742 size_t len = 8192;
743 char *buf;
744
745 for(int retry = 32;
746 (retry >= 0) && ((buf = new char [len]));
747 delete [] buf, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800748 if (getPruneList) {
749 android_logger_get_prune_list(logger_list, buf, len);
750 } else {
751 android_logger_get_statistics(logger_list, buf, len);
752 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800753 buf[len-1] = '\0';
754 size_t ret = atol(buf) + 1;
755 if (ret < 4) {
756 delete [] buf;
757 buf = NULL;
758 break;
759 }
760 bool check = ret <= len;
761 len = ret;
762 if (check) {
763 break;
764 }
765 }
766
767 if (!buf) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700768 perror("failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -0800769 exit(EXIT_FAILURE);
770 }
771
772 // remove trailing FF
773 char *cp = buf + len - 1;
774 *cp = '\0';
775 bool truncated = *--cp != '\f';
776 if (!truncated) {
777 *cp = '\0';
778 }
779
780 // squash out the byte count
781 cp = buf;
782 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -0700783 while (isdigit(*cp)) {
784 ++cp;
785 }
786 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800787 ++cp;
788 }
789 }
790
791 printf("%s", cp);
792 delete [] buf;
793 exit(0);
794 }
795
796
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800797 if (getLogSize) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700798 exit(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800799 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800800 if (setLogSize || setPruneList) {
801 exit(0);
802 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800803 if (clearLog) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700804 exit(0);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800805 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800806
807 //LOG_EVENT_INT(10, 12345);
808 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
809 //LOG_EVENT_STRING(0, "whassup, doc?");
810
Joe Onorato6fa09a02010-02-26 10:04:23 -0800811 if (needBinary)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800812 android::g_eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
813
Mark Salyzyn95132e92013-11-22 10:55:48 -0800814 while (1) {
815 struct log_msg log_msg;
816 int ret = android_logger_list_read(logger_list, &log_msg);
817
818 if (ret == 0) {
819 fprintf(stderr, "read: Unexpected EOF!\n");
820 exit(EXIT_FAILURE);
821 }
822
823 if (ret < 0) {
824 if (ret == -EAGAIN) {
825 break;
826 }
827
828 if (ret == -EIO) {
829 fprintf(stderr, "read: Unexpected EOF!\n");
830 exit(EXIT_FAILURE);
831 }
832 if (ret == -EINVAL) {
833 fprintf(stderr, "read: unexpected length.\n");
834 exit(EXIT_FAILURE);
835 }
Mark Salyzynfff04e32014-03-17 10:36:46 -0700836 perror("logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800837 exit(EXIT_FAILURE);
838 }
839
840 for(dev = devices; dev; dev = dev->next) {
841 if (android_name_to_log_id(dev->device) == log_msg.id()) {
842 break;
843 }
844 }
845 if (!dev) {
846 fprintf(stderr, "read: Unexpected log ID!\n");
847 exit(EXIT_FAILURE);
848 }
849
850 android::maybePrintStart(dev);
851 if (android::g_printBinary) {
852 android::printBinary(&log_msg);
853 } else {
854 android::processBuffer(dev, &log_msg);
855 }
856 }
857
858 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800859
860 return 0;
861}