blob: 829a0af881a44ce994c1a631671687b443f64993 [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;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800327 int mode = ANDROID_LOG_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;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800362 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363 break;
364
365 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800366 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 break;
368
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800369 case 't':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800370 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
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
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700458 devices = dev = NULL;
459 android::g_devCount = 0;
460 needBinary = false;
461 for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
462 const char *name = android_log_id_to_name((log_id_t)i);
463 log_id_t log_id = android_name_to_log_id(name);
464
465 if (log_id != (log_id_t)i) {
466 continue;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800467 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700468
469 bool binary = strcmp(name, "events") == 0;
470 log_device_t* d = new log_device_t(name, binary, *name);
471
472 if (dev) {
473 dev->next = d;
474 dev = d;
475 } else {
476 devices = dev = d;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800477 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700478 android::g_devCount++;
479 if (binary) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800480 needBinary = true;
481 }
482 }
483 break;
484 }
485
Joe Onorato6fa09a02010-02-26 10:04:23 -0800486 bool binary = strcmp(optarg, "events") == 0;
487 if (binary) {
488 needBinary = true;
489 }
490
491 if (devices) {
492 dev = devices;
493 while (dev->next) {
494 dev = dev->next;
495 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800496 dev->next = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800497 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800498 devices = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800499 }
500 android::g_devCount++;
501 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502 break;
503
504 case 'B':
505 android::g_printBinary = 1;
506 break;
507
508 case 'f':
509 // redirect output to a file
510
511 android::g_outputFileName = optarg;
512
513 break;
514
515 case 'r':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800516 if (optarg == NULL) {
517 android::g_logRotateSizeKBytes
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518 = DEFAULT_LOG_ROTATE_SIZE_KBYTES;
519 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800520 if (!isdigit(optarg[0])) {
521 fprintf(stderr,"Invalid parameter to -r\n");
522 android::show_help(argv[0]);
523 exit(-1);
524 }
525 android::g_logRotateSizeKBytes = atoi(optarg);
526 }
527 break;
528
529 case 'n':
530 if (!isdigit(optarg[0])) {
531 fprintf(stderr,"Invalid parameter to -r\n");
532 android::show_help(argv[0]);
533 exit(-1);
534 }
535
536 android::g_maxRotatedLogs = atoi(optarg);
537 break;
538
539 case 'v':
540 err = setLogFormat (optarg);
541 if (err < 0) {
542 fprintf(stderr,"Invalid parameter to -v\n");
543 android::show_help(argv[0]);
544 exit(-1);
545 }
546
Mark Salyzyn649fc602014-09-16 09:15:15 -0700547 if (strcmp("color", optarg)) { // exception for modifiers
548 hasSetLogFormat = 1;
549 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800550 break;
551
552 case 'Q':
553 /* this is a *hidden* option used to start a version of logcat */
554 /* in an emulated device only. it basically looks for androidboot.logcat= */
555 /* on the kernel command line. If something is found, it extracts a log filter */
556 /* and uses it to run the program. If nothing is found, the program should */
557 /* quit immediately */
558#define KERNEL_OPTION "androidboot.logcat="
559#define CONSOLE_OPTION "androidboot.console="
560 {
561 int fd;
562 char* logcat;
563 char* console;
564 int force_exit = 1;
565 static char cmdline[1024];
566
567 fd = open("/proc/cmdline", O_RDONLY);
568 if (fd >= 0) {
569 int n = read(fd, cmdline, sizeof(cmdline)-1 );
570 if (n < 0) n = 0;
571 cmdline[n] = 0;
572 close(fd);
573 } else {
574 cmdline[0] = 0;
575 }
576
577 logcat = strstr( cmdline, KERNEL_OPTION );
578 console = strstr( cmdline, CONSOLE_OPTION );
579 if (logcat != NULL) {
580 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
581 char* q = strpbrk( p, " \t\n\r" );;
582
583 if (q != NULL)
584 *q = 0;
585
586 forceFilters = p;
587 force_exit = 0;
588 }
589 /* if nothing found or invalid filters, exit quietly */
590 if (force_exit)
591 exit(0);
592
593 /* redirect our output to the emulator console */
594 if (console) {
595 char* p = console + sizeof(CONSOLE_OPTION)-1;
596 char* q = strpbrk( p, " \t\n\r" );
597 char devname[64];
598 int len;
599
600 if (q != NULL) {
601 len = q - p;
602 } else
603 len = strlen(p);
604
605 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
606 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
607 if (len < (int)sizeof(devname)) {
608 fd = open( devname, O_WRONLY );
609 if (fd >= 0) {
610 dup2(fd, 1);
611 dup2(fd, 2);
612 close(fd);
613 }
614 }
615 }
616 }
617 break;
618
Mark Salyzyn34facab2014-02-06 14:48:50 -0800619 case 'S':
620 printStatistics = 1;
621 break;
622
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800623 default:
624 fprintf(stderr,"Unrecognized Option\n");
625 android::show_help(argv[0]);
626 exit(-1);
627 break;
628 }
629 }
630
Joe Onorato6fa09a02010-02-26 10:04:23 -0800631 if (!devices) {
Mark Salyzyn989980c2014-05-14 12:37:22 -0700632 dev = devices = new log_device_t("main", false, 'm');
Joe Onorato6fa09a02010-02-26 10:04:23 -0800633 android::g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800634 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn989980c2014-05-14 12:37:22 -0700635 dev = dev->next = new log_device_t("system", false, 's');
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800636 android::g_devCount++;
637 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700638 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn989980c2014-05-14 12:37:22 -0700639 dev = dev->next = new log_device_t("crash", false, 'c');
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700640 android::g_devCount++;
641 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800642 }
643
Mark Salyzyn95132e92013-11-22 10:55:48 -0800644 if (android::g_logRotateSizeKBytes != 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800645 && android::g_outputFileName == NULL
646 ) {
647 fprintf(stderr,"-r requires -f as well\n");
648 android::show_help(argv[0]);
649 exit(-1);
650 }
651
652 android::setupOutput();
653
654 if (hasSetLogFormat == 0) {
655 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
656
657 if (logFormat != NULL) {
658 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800659 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800660 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800661 logFormat);
662 }
Mark Salyzyn649fc602014-09-16 09:15:15 -0700663 } else {
664 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800665 }
666 }
667
668 if (forceFilters) {
669 err = android_log_addFilterString(g_logformat, forceFilters);
670 if (err < 0) {
671 fprintf (stderr, "Invalid filter expression in -logcat option\n");
672 exit(0);
673 }
674 } else if (argc == optind) {
675 // Add from environment variable
676 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
677
678 if (env_tags_orig != NULL) {
679 err = android_log_addFilterString(g_logformat, env_tags_orig);
680
Mark Salyzyn95132e92013-11-22 10:55:48 -0800681 if (err < 0) {
682 fprintf(stderr, "Invalid filter expression in"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800683 " ANDROID_LOG_TAGS\n");
684 android::show_help(argv[0]);
685 exit(-1);
686 }
687 }
688 } else {
689 // Add from commandline
690 for (int i = optind ; i < argc ; i++) {
691 err = android_log_addFilterString(g_logformat, argv[i]);
692
Mark Salyzyn95132e92013-11-22 10:55:48 -0800693 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800694 fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]);
695 android::show_help(argv[0]);
696 exit(-1);
697 }
698 }
699 }
700
Joe Onorato6fa09a02010-02-26 10:04:23 -0800701 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800702 if (tail_time != log_time::EPOCH) {
703 logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
704 } else {
705 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
706 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800707 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800708 dev->logger_list = logger_list;
709 dev->logger = android_logger_open(logger_list,
710 android_name_to_log_id(dev->device));
711 if (!dev->logger) {
712 fprintf(stderr, "Unable to open log device '%s'\n", dev->device);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800713 exit(EXIT_FAILURE);
714 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800715
716 if (clearLog) {
717 int ret;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800718 ret = android_logger_clear(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800719 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700720 perror("failed to clear the log");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800721 exit(EXIT_FAILURE);
722 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800723 }
724
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800725 if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700726 perror("failed to set the log size");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800727 exit(EXIT_FAILURE);
728 }
729
Joe Onorato6fa09a02010-02-26 10:04:23 -0800730 if (getLogSize) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800731 long size, readable;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800732
Mark Salyzyn95132e92013-11-22 10:55:48 -0800733 size = android_logger_get_log_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800734 if (size < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700735 perror("failed to get the log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800736 exit(EXIT_FAILURE);
737 }
738
Mark Salyzyn95132e92013-11-22 10:55:48 -0800739 readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800740 if (readable < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700741 perror("failed to get the readable log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800742 exit(EXIT_FAILURE);
743 }
744
Mark Salyzyn671e3432014-05-06 07:34:59 -0700745 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
Joe Onorato6fa09a02010-02-26 10:04:23 -0800746 "max entry is %db, max payload is %db\n", dev->device,
Mark Salyzyn671e3432014-05-06 07:34:59 -0700747 value_of_size(size), multiplier_of_size(size),
748 value_of_size(readable), multiplier_of_size(readable),
Joe Onorato6fa09a02010-02-26 10:04:23 -0800749 (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
750 }
751
752 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800753 }
754
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800755 if (setPruneList) {
756 size_t len = strlen(setPruneList) + 32; // margin to allow rc
757 char *buf = (char *) malloc(len);
758
759 strcpy(buf, setPruneList);
760 int ret = android_logger_set_prune_list(logger_list, buf, len);
761 free(buf);
762
763 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700764 perror("failed to set the prune list");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800765 exit(EXIT_FAILURE);
766 }
767 }
768
Mark Salyzyn1c950472014-04-01 17:19:47 -0700769 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800770 size_t len = 8192;
771 char *buf;
772
773 for(int retry = 32;
774 (retry >= 0) && ((buf = new char [len]));
775 delete [] buf, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800776 if (getPruneList) {
777 android_logger_get_prune_list(logger_list, buf, len);
778 } else {
779 android_logger_get_statistics(logger_list, buf, len);
780 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800781 buf[len-1] = '\0';
782 size_t ret = atol(buf) + 1;
783 if (ret < 4) {
784 delete [] buf;
785 buf = NULL;
786 break;
787 }
788 bool check = ret <= len;
789 len = ret;
790 if (check) {
791 break;
792 }
793 }
794
795 if (!buf) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700796 perror("failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -0800797 exit(EXIT_FAILURE);
798 }
799
800 // remove trailing FF
801 char *cp = buf + len - 1;
802 *cp = '\0';
803 bool truncated = *--cp != '\f';
804 if (!truncated) {
805 *cp = '\0';
806 }
807
808 // squash out the byte count
809 cp = buf;
810 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -0700811 while (isdigit(*cp)) {
812 ++cp;
813 }
814 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800815 ++cp;
816 }
817 }
818
819 printf("%s", cp);
820 delete [] buf;
821 exit(0);
822 }
823
824
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800825 if (getLogSize) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700826 exit(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800827 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800828 if (setLogSize || setPruneList) {
829 exit(0);
830 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800831 if (clearLog) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700832 exit(0);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800833 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800834
835 //LOG_EVENT_INT(10, 12345);
836 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
837 //LOG_EVENT_STRING(0, "whassup, doc?");
838
Joe Onorato6fa09a02010-02-26 10:04:23 -0800839 if (needBinary)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800840 android::g_eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
841
Mark Salyzyn95132e92013-11-22 10:55:48 -0800842 while (1) {
843 struct log_msg log_msg;
844 int ret = android_logger_list_read(logger_list, &log_msg);
845
846 if (ret == 0) {
847 fprintf(stderr, "read: Unexpected EOF!\n");
848 exit(EXIT_FAILURE);
849 }
850
851 if (ret < 0) {
852 if (ret == -EAGAIN) {
853 break;
854 }
855
856 if (ret == -EIO) {
857 fprintf(stderr, "read: Unexpected EOF!\n");
858 exit(EXIT_FAILURE);
859 }
860 if (ret == -EINVAL) {
861 fprintf(stderr, "read: unexpected length.\n");
862 exit(EXIT_FAILURE);
863 }
Mark Salyzynfff04e32014-03-17 10:36:46 -0700864 perror("logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800865 exit(EXIT_FAILURE);
866 }
867
868 for(dev = devices; dev; dev = dev->next) {
869 if (android_name_to_log_id(dev->device) == log_msg.id()) {
870 break;
871 }
872 }
873 if (!dev) {
874 fprintf(stderr, "read: Unexpected log ID!\n");
875 exit(EXIT_FAILURE);
876 }
877
878 android::maybePrintStart(dev);
879 if (android::g_printBinary) {
880 android::printBinary(&log_msg);
881 } else {
882 android::processBuffer(dev, &log_msg);
883 }
884 }
885
886 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800887
888 return 0;
889}