blob: 6306f5c657b26cb47a193a0e45b0c1306e7ab16b [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>
Aristidis Papaioannoueba73442014-10-16 22:19:55 -07007#include <math.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -08008#include <stdio.h>
9#include <stdlib.h>
10#include <stdarg.h>
11#include <string.h>
12#include <signal.h>
13#include <time.h>
14#include <unistd.h>
15#include <sys/socket.h>
16#include <sys/stat.h>
17#include <arpa/inet.h>
18
19#include <cutils/sockets.h>
Mark Salyzyn95132e92013-11-22 10:55:48 -080020#include <log/log.h>
Mark Salyzynfa3716b2014-02-14 16:05:05 -080021#include <log/log_read.h>
Colin Cross9227bd32013-07-23 16:59:20 -070022#include <log/logger.h>
23#include <log/logd.h>
24#include <log/logprint.h>
25#include <log/event_tag_map.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026
27#define DEFAULT_LOG_ROTATE_SIZE_KBYTES 16
28#define DEFAULT_MAX_ROTATED_LOGS 4
29
30static AndroidLogFormat * g_logformat;
31
32/* logd prefixes records with a length field */
33#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
34
Joe Onorato6fa09a02010-02-26 10:04:23 -080035struct log_device_t {
Mark Salyzyn95132e92013-11-22 10:55:48 -080036 const char* device;
Joe Onorato6fa09a02010-02-26 10:04:23 -080037 bool binary;
Mark Salyzyn95132e92013-11-22 10:55:48 -080038 struct logger *logger;
39 struct logger_list *logger_list;
Joe Onorato6fa09a02010-02-26 10:04:23 -080040 bool printed;
41 char label;
42
Joe Onorato6fa09a02010-02-26 10:04:23 -080043 log_device_t* next;
44
Mark Salyzyn95132e92013-11-22 10:55:48 -080045 log_device_t(const char* d, bool b, char l) {
Joe Onorato6fa09a02010-02-26 10:04:23 -080046 device = d;
47 binary = b;
48 label = l;
49 next = NULL;
50 printed = false;
51 }
Joe Onorato6fa09a02010-02-26 10:04:23 -080052};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
54namespace android {
55
56/* Global Variables */
57
58static const char * g_outputFileName = NULL;
59static int g_logRotateSizeKBytes = 0; // 0 means "no log rotation"
60static int g_maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS; // 0 means "unbounded"
61static int g_outFD = -1;
62static off_t g_outByteCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063static int g_printBinary = 0;
Joe Onorato6fa09a02010-02-26 10:04:23 -080064static int g_devCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065
66static EventTagMap* g_eventTagMap = NULL;
67
68static int openLogFile (const char *pathname)
69{
Edwin Vane80b221c2012-08-13 12:55:07 -040070 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071}
72
73static void rotateLogs()
74{
75 int err;
76
77 // Can't rotate logs if we're not outputting to a file
78 if (g_outputFileName == NULL) {
79 return;
80 }
81
82 close(g_outFD);
83
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070084 // Compute the maximum number of digits needed to count up to g_maxRotatedLogs in decimal.
85 // eg: g_maxRotatedLogs == 30 -> log10(30) == 1.477 -> maxRotationCountDigits == 2
86 int maxRotationCountDigits =
87 (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
88
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089 for (int i = g_maxRotatedLogs ; i > 0 ; i--) {
90 char *file0, *file1;
91
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070092 asprintf(&file1, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093
94 if (i - 1 == 0) {
95 asprintf(&file0, "%s", g_outputFileName);
96 } else {
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070097 asprintf(&file0, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098 }
99
100 err = rename (file0, file1);
101
102 if (err < 0 && errno != ENOENT) {
103 perror("while rotating log files");
104 }
105
106 free(file1);
107 free(file0);
108 }
109
110 g_outFD = openLogFile (g_outputFileName);
111
112 if (g_outFD < 0) {
113 perror ("couldn't open output file");
114 exit(-1);
115 }
116
117 g_outByteCount = 0;
118
119}
120
Mark Salyzyn95132e92013-11-22 10:55:48 -0800121void printBinary(struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122{
Mark Salyzyn95132e92013-11-22 10:55:48 -0800123 size_t size = buf->len();
124
125 TEMP_FAILURE_RETRY(write(g_outFD, buf, size));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126}
127
Mark Salyzyn95132e92013-11-22 10:55:48 -0800128static void processBuffer(log_device_t* dev, struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129{
Mathias Agopian50844522010-03-17 16:10:26 -0700130 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131 int err;
132 AndroidLogEntry entry;
133 char binaryMsgBuf[1024];
134
Joe Onorato6fa09a02010-02-26 10:04:23 -0800135 if (dev->binary) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800136 err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry,
137 g_eventTagMap,
138 binaryMsgBuf,
139 sizeof(binaryMsgBuf));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140 //printf(">>> pri=%d len=%d msg='%s'\n",
141 // entry.priority, entry.messageLen, entry.message);
142 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800143 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800145 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 goto error;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800147 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800149 if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) {
150 if (false && g_devCount > 1) {
151 binaryMsgBuf[0] = dev->label;
152 binaryMsgBuf[1] = ' ';
153 bytesWritten = write(g_outFD, binaryMsgBuf, 2);
154 if (bytesWritten < 0) {
155 perror("output error");
156 exit(-1);
157 }
158 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800160 bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry);
161
162 if (bytesWritten < 0) {
163 perror("output error");
164 exit(-1);
165 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166 }
167
168 g_outByteCount += bytesWritten;
169
Mark Salyzyn95132e92013-11-22 10:55:48 -0800170 if (g_logRotateSizeKBytes > 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171 && (g_outByteCount / 1024) >= g_logRotateSizeKBytes
172 ) {
173 rotateLogs();
174 }
175
176error:
177 //fprintf (stderr, "Error processing record\n");
178 return;
179}
180
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800181static void maybePrintStart(log_device_t* dev) {
182 if (!dev->printed) {
183 dev->printed = true;
184 if (g_devCount > 1 && !g_printBinary) {
185 char buf[1024];
Mark Salyzyn95132e92013-11-22 10:55:48 -0800186 snprintf(buf, sizeof(buf), "--------- beginning of %s\n",
187 dev->device);
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800188 if (write(g_outFD, buf, strlen(buf)) < 0) {
189 perror("output error");
190 exit(-1);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800191 }
192 }
193 }
194}
195
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196static void setupOutput()
197{
198
199 if (g_outputFileName == NULL) {
200 g_outFD = STDOUT_FILENO;
201
202 } else {
203 struct stat statbuf;
204
205 g_outFD = openLogFile (g_outputFileName);
206
207 if (g_outFD < 0) {
208 perror ("couldn't open output file");
209 exit(-1);
210 }
211
212 fstat(g_outFD, &statbuf);
213
214 g_outByteCount = statbuf.st_size;
215 }
216}
217
218static void show_help(const char *cmd)
219{
220 fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd);
221
222 fprintf(stderr, "options include:\n"
223 " -s Set default filter to silent.\n"
224 " Like specifying filterspec '*:s'\n"
225 " -f <filename> Log to file. Default to stdout\n"
226 " -r [<kbytes>] Rotate log every kbytes. (16 if unspecified). Requires -f\n"
227 " -n <count> Sets max number of rotated logs to <count>, default 4\n"
Pierre Zurekead88fc2010-10-17 22:39:37 +0200228 " -v <format> Sets the log print format, where <format> is:\n\n"
229 " brief color long process raw tag thread threadtime time\n\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230 " -c clear (flush) the entire log and exit\n"
231 " -d dump the log and then exit (don't block)\n"
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800232 " -t <count> print only the most recent <count> lines (implies -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700233 " -t '<time>' print most recent lines since specified time (implies -d)\n"
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800234 " -T <count> print only the most recent <count> lines (does not imply -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700235 " -T '<time>' print most recent lines since specified time (not imply -d)\n"
236 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm'\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237 " -g get the size of the log's ring buffer and exit\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800238 " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n"
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700239 " 'events', 'crash' or 'all'. Multiple -b parameters are\n"
240 " allowed and results are interleaved. The default is\n"
241 " -b main -b system -b crash.\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800242 " -B output the log in binary.\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700243 " -S output statistics.\n"
244 " -G <size> set size of log ring buffer, may suffix with K or M.\n"
245 " -p print prune white and ~black list. Service is specified as\n"
246 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
247 " with ~, otherwise weighed for longevity if unadorned. All\n"
248 " other pruning activity is oldest first. Special case ~!\n"
249 " represents an automatic quicker pruning for the noisiest\n"
250 " UID as determined by the current statistics.\n"
251 " -P '<list> ...' set prune white and ~black list, using same format as\n"
252 " printed above. Must be quoted.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253
254 fprintf(stderr,"\nfilterspecs are a series of \n"
255 " <tag>[:priority]\n\n"
256 "where <tag> is a log component tag (or * for all) and priority is:\n"
257 " V Verbose\n"
258 " D Debug\n"
259 " I Info\n"
260 " W Warn\n"
261 " E Error\n"
262 " F Fatal\n"
263 " S Silent (supress all output)\n"
264 "\n'*' means '*:d' and <tag> by itself means <tag>:v\n"
265 "\nIf not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.\n"
266 "If no filterspec is found, filter defaults to '*:I'\n"
267 "\nIf not specified with -v, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700268 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269
270
271
272}
273
274
275} /* namespace android */
276
277static int setLogFormat(const char * formatString)
278{
279 static AndroidLogPrintFormat format;
280
281 format = android_log_formatFromString(formatString);
282
283 if (format == FORMAT_OFF) {
284 // FORMAT_OFF means invalid string
285 return -1;
286 }
287
288 android_log_setPrintFormat(g_logformat, format);
289
290 return 0;
291}
292
Mark Salyzyn671e3432014-05-06 07:34:59 -0700293static const char multipliers[][2] = {
294 { "" },
295 { "K" },
296 { "M" },
297 { "G" }
298};
299
300static unsigned long value_of_size(unsigned long value)
301{
302 for (unsigned i = 0;
303 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
304 value /= 1024, ++i) ;
305 return value;
306}
307
308static const char *multiplier_of_size(unsigned long value)
309{
310 unsigned i;
311 for (i = 0;
312 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
313 value /= 1024, ++i) ;
314 return multipliers[i];
315}
316
Joe Onorato6fa09a02010-02-26 10:04:23 -0800317int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800318{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319 int err;
320 int hasSetLogFormat = 0;
321 int clearLog = 0;
322 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800323 unsigned long setLogSize = 0;
324 int getPruneList = 0;
325 char *setPruneList = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800326 int printStatistics = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327 int mode = O_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800329 log_device_t* devices = NULL;
330 log_device_t* dev;
331 bool needBinary = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800332 struct logger_list *logger_list;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800333 unsigned int tail_lines = 0;
334 log_time tail_time(log_time::EPOCH);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800336 signal(SIGPIPE, exit);
337
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 g_logformat = android_log_format_new();
339
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
341 android::show_help(argv[0]);
342 exit(0);
343 }
344
345 for (;;) {
346 int ret;
347
Mark Salyzyn0b2dac42014-07-07 08:55:53 -0700348 ret = getopt(argc, argv, "cdt:T:gG:sQf:r:n:v:b:BSpP:");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349
350 if (ret < 0) {
351 break;
352 }
353
354 switch(ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800355 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800356 // default to all silent
357 android_log_addFilterRule(g_logformat, "*:s");
358 break;
359
360 case 'c':
361 clearLog = 1;
362 mode = O_WRONLY;
363 break;
364
365 case 'd':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800366 mode = O_RDONLY | O_NDELAY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 break;
368
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800369 case 't':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800370 mode = O_RDONLY | O_NDELAY;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800371 /* FALLTHRU */
372 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800373 if (strspn(optarg, "0123456789") != strlen(optarg)) {
374 char *cp = tail_time.strptime(optarg,
375 log_time::default_format);
376 if (!cp) {
377 fprintf(stderr,
378 "ERROR: -%c \"%s\" not in \"%s\" time format\n",
379 ret, optarg, log_time::default_format);
380 exit(1);
381 }
382 if (*cp) {
383 char c = *cp;
384 *cp = '\0';
385 fprintf(stderr,
386 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
387 ret, optarg, c, cp + 1);
388 *cp = c;
389 }
390 } else {
391 tail_lines = atoi(optarg);
392 if (!tail_lines) {
393 fprintf(stderr,
394 "WARNING: -%c %s invalid, setting to 1\n",
395 ret, optarg);
396 tail_lines = 1;
397 }
398 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800399 break;
400
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401 case 'g':
402 getLogSize = 1;
403 break;
404
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800405 case 'G': {
406 // would use atol if not for the multiplier
407 char *cp = optarg;
408 setLogSize = 0;
409 while (('0' <= *cp) && (*cp <= '9')) {
410 setLogSize *= 10;
411 setLogSize += *cp - '0';
412 ++cp;
413 }
414
415 switch(*cp) {
416 case 'g':
417 case 'G':
418 setLogSize *= 1024;
419 /* FALLTHRU */
420 case 'm':
421 case 'M':
422 setLogSize *= 1024;
423 /* FALLTHRU */
424 case 'k':
425 case 'K':
426 setLogSize *= 1024;
427 /* FALLTHRU */
428 case '\0':
429 break;
430
431 default:
432 setLogSize = 0;
433 }
434
435 if (!setLogSize) {
436 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
437 exit(1);
438 }
439 }
440 break;
441
442 case 'p':
443 getPruneList = 1;
444 break;
445
446 case 'P':
447 setPruneList = optarg;
448 break;
449
Joe Onorato6fa09a02010-02-26 10:04:23 -0800450 case 'b': {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800451 if (strcmp(optarg, "all") == 0) {
452 while (devices) {
453 dev = devices;
454 devices = dev->next;
455 delete dev;
456 }
457
458 dev = devices = new log_device_t("main", false, 'm');
459 android::g_devCount = 1;
460 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
461 dev->next = new log_device_t("system", false, 's');
462 if (dev->next) {
463 dev = dev->next;
464 android::g_devCount++;
465 }
466 }
467 if (android_name_to_log_id("radio") == LOG_ID_RADIO) {
468 dev->next = new log_device_t("radio", false, 'r');
469 if (dev->next) {
470 dev = dev->next;
471 android::g_devCount++;
472 }
473 }
474 if (android_name_to_log_id("events") == LOG_ID_EVENTS) {
475 dev->next = new log_device_t("events", true, 'e');
476 if (dev->next) {
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700477 dev = dev->next;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800478 android::g_devCount++;
479 needBinary = true;
480 }
481 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700482 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
483 dev->next = new log_device_t("crash", false, 'c');
484 if (dev->next) {
485 android::g_devCount++;
486 }
487 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800488 break;
489 }
490
Joe Onorato6fa09a02010-02-26 10:04:23 -0800491 bool binary = strcmp(optarg, "events") == 0;
492 if (binary) {
493 needBinary = true;
494 }
495
496 if (devices) {
497 dev = devices;
498 while (dev->next) {
499 dev = dev->next;
500 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800501 dev->next = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800502 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800503 devices = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800504 }
505 android::g_devCount++;
506 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800507 break;
508
509 case 'B':
510 android::g_printBinary = 1;
511 break;
512
513 case 'f':
514 // redirect output to a file
515
516 android::g_outputFileName = optarg;
517
518 break;
519
520 case 'r':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800521 if (optarg == NULL) {
522 android::g_logRotateSizeKBytes
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523 = DEFAULT_LOG_ROTATE_SIZE_KBYTES;
524 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525 if (!isdigit(optarg[0])) {
526 fprintf(stderr,"Invalid parameter to -r\n");
527 android::show_help(argv[0]);
528 exit(-1);
529 }
530 android::g_logRotateSizeKBytes = atoi(optarg);
531 }
532 break;
533
534 case 'n':
535 if (!isdigit(optarg[0])) {
536 fprintf(stderr,"Invalid parameter to -r\n");
537 android::show_help(argv[0]);
538 exit(-1);
539 }
540
541 android::g_maxRotatedLogs = atoi(optarg);
542 break;
543
544 case 'v':
545 err = setLogFormat (optarg);
546 if (err < 0) {
547 fprintf(stderr,"Invalid parameter to -v\n");
548 android::show_help(argv[0]);
549 exit(-1);
550 }
551
Mark Salyzyn649fc602014-09-16 09:15:15 -0700552 if (strcmp("color", optarg)) { // exception for modifiers
553 hasSetLogFormat = 1;
554 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800555 break;
556
557 case 'Q':
558 /* this is a *hidden* option used to start a version of logcat */
559 /* in an emulated device only. it basically looks for androidboot.logcat= */
560 /* on the kernel command line. If something is found, it extracts a log filter */
561 /* and uses it to run the program. If nothing is found, the program should */
562 /* quit immediately */
563#define KERNEL_OPTION "androidboot.logcat="
564#define CONSOLE_OPTION "androidboot.console="
565 {
566 int fd;
567 char* logcat;
568 char* console;
569 int force_exit = 1;
570 static char cmdline[1024];
571
572 fd = open("/proc/cmdline", O_RDONLY);
573 if (fd >= 0) {
574 int n = read(fd, cmdline, sizeof(cmdline)-1 );
575 if (n < 0) n = 0;
576 cmdline[n] = 0;
577 close(fd);
578 } else {
579 cmdline[0] = 0;
580 }
581
582 logcat = strstr( cmdline, KERNEL_OPTION );
583 console = strstr( cmdline, CONSOLE_OPTION );
584 if (logcat != NULL) {
585 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
586 char* q = strpbrk( p, " \t\n\r" );;
587
588 if (q != NULL)
589 *q = 0;
590
591 forceFilters = p;
592 force_exit = 0;
593 }
594 /* if nothing found or invalid filters, exit quietly */
595 if (force_exit)
596 exit(0);
597
598 /* redirect our output to the emulator console */
599 if (console) {
600 char* p = console + sizeof(CONSOLE_OPTION)-1;
601 char* q = strpbrk( p, " \t\n\r" );
602 char devname[64];
603 int len;
604
605 if (q != NULL) {
606 len = q - p;
607 } else
608 len = strlen(p);
609
610 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
611 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
612 if (len < (int)sizeof(devname)) {
613 fd = open( devname, O_WRONLY );
614 if (fd >= 0) {
615 dup2(fd, 1);
616 dup2(fd, 2);
617 close(fd);
618 }
619 }
620 }
621 }
622 break;
623
Mark Salyzyn34facab2014-02-06 14:48:50 -0800624 case 'S':
625 printStatistics = 1;
626 break;
627
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800628 default:
629 fprintf(stderr,"Unrecognized Option\n");
630 android::show_help(argv[0]);
631 exit(-1);
632 break;
633 }
634 }
635
Joe Onorato6fa09a02010-02-26 10:04:23 -0800636 if (!devices) {
Mark Salyzyn989980c2014-05-14 12:37:22 -0700637 dev = devices = new log_device_t("main", false, 'm');
Joe Onorato6fa09a02010-02-26 10:04:23 -0800638 android::g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800639 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn989980c2014-05-14 12:37:22 -0700640 dev = dev->next = new log_device_t("system", false, 's');
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800641 android::g_devCount++;
642 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700643 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn989980c2014-05-14 12:37:22 -0700644 dev = dev->next = new log_device_t("crash", false, 'c');
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700645 android::g_devCount++;
646 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800647 }
648
Mark Salyzyn95132e92013-11-22 10:55:48 -0800649 if (android::g_logRotateSizeKBytes != 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800650 && android::g_outputFileName == NULL
651 ) {
652 fprintf(stderr,"-r requires -f as well\n");
653 android::show_help(argv[0]);
654 exit(-1);
655 }
656
657 android::setupOutput();
658
659 if (hasSetLogFormat == 0) {
660 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
661
662 if (logFormat != NULL) {
663 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800664 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800665 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800666 logFormat);
667 }
Mark Salyzyn649fc602014-09-16 09:15:15 -0700668 } else {
669 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800670 }
671 }
672
673 if (forceFilters) {
674 err = android_log_addFilterString(g_logformat, forceFilters);
675 if (err < 0) {
676 fprintf (stderr, "Invalid filter expression in -logcat option\n");
677 exit(0);
678 }
679 } else if (argc == optind) {
680 // Add from environment variable
681 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
682
683 if (env_tags_orig != NULL) {
684 err = android_log_addFilterString(g_logformat, env_tags_orig);
685
Mark Salyzyn95132e92013-11-22 10:55:48 -0800686 if (err < 0) {
687 fprintf(stderr, "Invalid filter expression in"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800688 " ANDROID_LOG_TAGS\n");
689 android::show_help(argv[0]);
690 exit(-1);
691 }
692 }
693 } else {
694 // Add from commandline
695 for (int i = optind ; i < argc ; i++) {
696 err = android_log_addFilterString(g_logformat, argv[i]);
697
Mark Salyzyn95132e92013-11-22 10:55:48 -0800698 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800699 fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]);
700 android::show_help(argv[0]);
701 exit(-1);
702 }
703 }
704 }
705
Joe Onorato6fa09a02010-02-26 10:04:23 -0800706 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800707 if (tail_time != log_time::EPOCH) {
708 logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
709 } else {
710 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
711 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800712 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800713 dev->logger_list = logger_list;
714 dev->logger = android_logger_open(logger_list,
715 android_name_to_log_id(dev->device));
716 if (!dev->logger) {
717 fprintf(stderr, "Unable to open log device '%s'\n", dev->device);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800718 exit(EXIT_FAILURE);
719 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800720
721 if (clearLog) {
722 int ret;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800723 ret = android_logger_clear(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800724 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700725 perror("failed to clear the log");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800726 exit(EXIT_FAILURE);
727 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800728 }
729
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800730 if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700731 perror("failed to set the log size");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800732 exit(EXIT_FAILURE);
733 }
734
Joe Onorato6fa09a02010-02-26 10:04:23 -0800735 if (getLogSize) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800736 long size, readable;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800737
Mark Salyzyn95132e92013-11-22 10:55:48 -0800738 size = android_logger_get_log_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800739 if (size < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700740 perror("failed to get the log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800741 exit(EXIT_FAILURE);
742 }
743
Mark Salyzyn95132e92013-11-22 10:55:48 -0800744 readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800745 if (readable < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700746 perror("failed to get the readable log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800747 exit(EXIT_FAILURE);
748 }
749
Mark Salyzyn671e3432014-05-06 07:34:59 -0700750 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
Joe Onorato6fa09a02010-02-26 10:04:23 -0800751 "max entry is %db, max payload is %db\n", dev->device,
Mark Salyzyn671e3432014-05-06 07:34:59 -0700752 value_of_size(size), multiplier_of_size(size),
753 value_of_size(readable), multiplier_of_size(readable),
Joe Onorato6fa09a02010-02-26 10:04:23 -0800754 (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
755 }
756
757 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800758 }
759
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800760 if (setPruneList) {
761 size_t len = strlen(setPruneList) + 32; // margin to allow rc
762 char *buf = (char *) malloc(len);
763
764 strcpy(buf, setPruneList);
765 int ret = android_logger_set_prune_list(logger_list, buf, len);
766 free(buf);
767
768 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700769 perror("failed to set the prune list");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800770 exit(EXIT_FAILURE);
771 }
772 }
773
Mark Salyzyn1c950472014-04-01 17:19:47 -0700774 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800775 size_t len = 8192;
776 char *buf;
777
778 for(int retry = 32;
779 (retry >= 0) && ((buf = new char [len]));
780 delete [] buf, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800781 if (getPruneList) {
782 android_logger_get_prune_list(logger_list, buf, len);
783 } else {
784 android_logger_get_statistics(logger_list, buf, len);
785 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800786 buf[len-1] = '\0';
787 size_t ret = atol(buf) + 1;
788 if (ret < 4) {
789 delete [] buf;
790 buf = NULL;
791 break;
792 }
793 bool check = ret <= len;
794 len = ret;
795 if (check) {
796 break;
797 }
798 }
799
800 if (!buf) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700801 perror("failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -0800802 exit(EXIT_FAILURE);
803 }
804
805 // remove trailing FF
806 char *cp = buf + len - 1;
807 *cp = '\0';
808 bool truncated = *--cp != '\f';
809 if (!truncated) {
810 *cp = '\0';
811 }
812
813 // squash out the byte count
814 cp = buf;
815 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -0700816 while (isdigit(*cp)) {
817 ++cp;
818 }
819 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800820 ++cp;
821 }
822 }
823
824 printf("%s", cp);
825 delete [] buf;
826 exit(0);
827 }
828
829
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800830 if (getLogSize) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700831 exit(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800832 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800833 if (setLogSize || setPruneList) {
834 exit(0);
835 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800836 if (clearLog) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700837 exit(0);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800838 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800839
840 //LOG_EVENT_INT(10, 12345);
841 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
842 //LOG_EVENT_STRING(0, "whassup, doc?");
843
Joe Onorato6fa09a02010-02-26 10:04:23 -0800844 if (needBinary)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800845 android::g_eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
846
Mark Salyzyn95132e92013-11-22 10:55:48 -0800847 while (1) {
848 struct log_msg log_msg;
849 int ret = android_logger_list_read(logger_list, &log_msg);
850
851 if (ret == 0) {
852 fprintf(stderr, "read: Unexpected EOF!\n");
853 exit(EXIT_FAILURE);
854 }
855
856 if (ret < 0) {
857 if (ret == -EAGAIN) {
858 break;
859 }
860
861 if (ret == -EIO) {
862 fprintf(stderr, "read: Unexpected EOF!\n");
863 exit(EXIT_FAILURE);
864 }
865 if (ret == -EINVAL) {
866 fprintf(stderr, "read: unexpected length.\n");
867 exit(EXIT_FAILURE);
868 }
Mark Salyzynfff04e32014-03-17 10:36:46 -0700869 perror("logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800870 exit(EXIT_FAILURE);
871 }
872
873 for(dev = devices; dev; dev = dev->next) {
874 if (android_name_to_log_id(dev->device) == log_msg.id()) {
875 break;
876 }
877 }
878 if (!dev) {
879 fprintf(stderr, "read: Unexpected log ID!\n");
880 exit(EXIT_FAILURE);
881 }
882
883 android::maybePrintStart(dev);
884 if (android::g_printBinary) {
885 android::printBinary(&log_msg);
886 } else {
887 android::processBuffer(dev, &log_msg);
888 }
889 }
890
891 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800892
893 return 0;
894}