blob: cd5003edc4b2fe766346d7ab5c8f356560d546a3 [file] [log] [blame]
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001// Copyright 2006-2015 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
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800181static void maybePrintStart(log_device_t* dev, bool printDividers) {
182 if (!dev->printed || printDividers) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800183 if (g_devCount > 1 && !g_printBinary) {
184 char buf[1024];
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800185 snprintf(buf, sizeof(buf), "--------- %s %s\n",
186 dev->printed ? "switch to" : "beginning of",
Mark Salyzyn95132e92013-11-22 10:55:48 -0800187 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 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800193 dev->printed = true;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800194 }
195}
196
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197static void setupOutput()
198{
199
200 if (g_outputFileName == NULL) {
201 g_outFD = STDOUT_FILENO;
202
203 } else {
204 struct stat statbuf;
205
206 g_outFD = openLogFile (g_outputFileName);
207
208 if (g_outFD < 0) {
209 perror ("couldn't open output file");
210 exit(-1);
211 }
212
213 fstat(g_outFD, &statbuf);
214
215 g_outByteCount = statbuf.st_size;
216 }
217}
218
219static void show_help(const char *cmd)
220{
221 fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd);
222
223 fprintf(stderr, "options include:\n"
224 " -s Set default filter to silent.\n"
225 " Like specifying filterspec '*:s'\n"
226 " -f <filename> Log to file. Default to stdout\n"
227 " -r [<kbytes>] Rotate log every kbytes. (16 if unspecified). Requires -f\n"
228 " -n <count> Sets max number of rotated logs to <count>, default 4\n"
Pierre Zurekead88fc2010-10-17 22:39:37 +0200229 " -v <format> Sets the log print format, where <format> is:\n\n"
230 " brief color long process raw tag thread threadtime time\n\n"
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800231 " -D print dividers between each log buffer\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232 " -c clear (flush) the entire log and exit\n"
233 " -d dump the log and then exit (don't block)\n"
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800234 " -t <count> print only the most recent <count> lines (implies -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700235 " -t '<time>' print most recent lines since specified time (implies -d)\n"
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800236 " -T <count> print only the most recent <count> lines (does not imply -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700237 " -T '<time>' print most recent lines since specified time (not imply -d)\n"
238 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm'\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 " -g get the size of the log's ring buffer and exit\n"
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800240 " -L dump logs from prior to last reboot\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800241 " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n"
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700242 " 'events', 'crash' or 'all'. Multiple -b parameters are\n"
243 " allowed and results are interleaved. The default is\n"
244 " -b main -b system -b crash.\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800245 " -B output the log in binary.\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700246 " -S output statistics.\n"
247 " -G <size> set size of log ring buffer, may suffix with K or M.\n"
248 " -p print prune white and ~black list. Service is specified as\n"
249 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
250 " with ~, otherwise weighed for longevity if unadorned. All\n"
251 " other pruning activity is oldest first. Special case ~!\n"
252 " represents an automatic quicker pruning for the noisiest\n"
253 " UID as determined by the current statistics.\n"
254 " -P '<list> ...' set prune white and ~black list, using same format as\n"
255 " printed above. Must be quoted.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800256
257 fprintf(stderr,"\nfilterspecs are a series of \n"
258 " <tag>[:priority]\n\n"
259 "where <tag> is a log component tag (or * for all) and priority is:\n"
260 " V Verbose\n"
261 " D Debug\n"
262 " I Info\n"
263 " W Warn\n"
264 " E Error\n"
265 " F Fatal\n"
266 " S Silent (supress all output)\n"
267 "\n'*' means '*:d' and <tag> by itself means <tag>:v\n"
268 "\nIf not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.\n"
269 "If no filterspec is found, filter defaults to '*:I'\n"
270 "\nIf not specified with -v, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700271 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272
273
274
275}
276
277
278} /* namespace android */
279
280static int setLogFormat(const char * formatString)
281{
282 static AndroidLogPrintFormat format;
283
284 format = android_log_formatFromString(formatString);
285
286 if (format == FORMAT_OFF) {
287 // FORMAT_OFF means invalid string
288 return -1;
289 }
290
291 android_log_setPrintFormat(g_logformat, format);
292
293 return 0;
294}
295
Mark Salyzyn671e3432014-05-06 07:34:59 -0700296static const char multipliers[][2] = {
297 { "" },
298 { "K" },
299 { "M" },
300 { "G" }
301};
302
303static unsigned long value_of_size(unsigned long value)
304{
305 for (unsigned i = 0;
306 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
307 value /= 1024, ++i) ;
308 return value;
309}
310
311static const char *multiplier_of_size(unsigned long value)
312{
313 unsigned i;
314 for (i = 0;
315 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
316 value /= 1024, ++i) ;
317 return multipliers[i];
318}
319
Joe Onorato6fa09a02010-02-26 10:04:23 -0800320int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 int err;
323 int hasSetLogFormat = 0;
324 int clearLog = 0;
325 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800326 unsigned long setLogSize = 0;
327 int getPruneList = 0;
328 char *setPruneList = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800329 int printStatistics = 0;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800330 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800332 log_device_t* devices = NULL;
333 log_device_t* dev;
334 bool needBinary = false;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800335 bool printDividers = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800336 struct logger_list *logger_list;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800337 unsigned int tail_lines = 0;
338 log_time tail_time(log_time::EPOCH);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800339
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800340 signal(SIGPIPE, exit);
341
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800342 g_logformat = android_log_format_new();
343
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
345 android::show_help(argv[0]);
346 exit(0);
347 }
348
349 for (;;) {
350 int ret;
351
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800352 ret = getopt(argc, argv, "cdDLt:T:gG:sQf:r:n:v:b:BSpP:");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353
354 if (ret < 0) {
355 break;
356 }
357
358 switch(ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800359 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360 // default to all silent
361 android_log_addFilterRule(g_logformat, "*:s");
362 break;
363
364 case 'c':
365 clearLog = 1;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800366 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 break;
368
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800369 case 'L':
370 mode |= ANDROID_LOG_PSTORE;
371 break;
372
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800373 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800374 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375 break;
376
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800377 case 't':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800378 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800379 /* FALLTHRU */
380 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800381 if (strspn(optarg, "0123456789") != strlen(optarg)) {
382 char *cp = tail_time.strptime(optarg,
383 log_time::default_format);
384 if (!cp) {
385 fprintf(stderr,
386 "ERROR: -%c \"%s\" not in \"%s\" time format\n",
387 ret, optarg, log_time::default_format);
388 exit(1);
389 }
390 if (*cp) {
391 char c = *cp;
392 *cp = '\0';
393 fprintf(stderr,
394 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
395 ret, optarg, c, cp + 1);
396 *cp = c;
397 }
398 } else {
399 tail_lines = atoi(optarg);
400 if (!tail_lines) {
401 fprintf(stderr,
402 "WARNING: -%c %s invalid, setting to 1\n",
403 ret, optarg);
404 tail_lines = 1;
405 }
406 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800407 break;
408
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800409 case 'D':
410 printDividers = true;
411 break;
412
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413 case 'g':
414 getLogSize = 1;
415 break;
416
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800417 case 'G': {
418 // would use atol if not for the multiplier
419 char *cp = optarg;
420 setLogSize = 0;
421 while (('0' <= *cp) && (*cp <= '9')) {
422 setLogSize *= 10;
423 setLogSize += *cp - '0';
424 ++cp;
425 }
426
427 switch(*cp) {
428 case 'g':
429 case 'G':
430 setLogSize *= 1024;
431 /* FALLTHRU */
432 case 'm':
433 case 'M':
434 setLogSize *= 1024;
435 /* FALLTHRU */
436 case 'k':
437 case 'K':
438 setLogSize *= 1024;
439 /* FALLTHRU */
440 case '\0':
441 break;
442
443 default:
444 setLogSize = 0;
445 }
446
447 if (!setLogSize) {
448 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
449 exit(1);
450 }
451 }
452 break;
453
454 case 'p':
455 getPruneList = 1;
456 break;
457
458 case 'P':
459 setPruneList = optarg;
460 break;
461
Joe Onorato6fa09a02010-02-26 10:04:23 -0800462 case 'b': {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800463 if (strcmp(optarg, "all") == 0) {
464 while (devices) {
465 dev = devices;
466 devices = dev->next;
467 delete dev;
468 }
469
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700470 devices = dev = NULL;
471 android::g_devCount = 0;
472 needBinary = false;
473 for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
474 const char *name = android_log_id_to_name((log_id_t)i);
475 log_id_t log_id = android_name_to_log_id(name);
476
477 if (log_id != (log_id_t)i) {
478 continue;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800479 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700480
481 bool binary = strcmp(name, "events") == 0;
482 log_device_t* d = new log_device_t(name, binary, *name);
483
484 if (dev) {
485 dev->next = d;
486 dev = d;
487 } else {
488 devices = dev = d;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800489 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700490 android::g_devCount++;
491 if (binary) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800492 needBinary = true;
493 }
494 }
495 break;
496 }
497
Joe Onorato6fa09a02010-02-26 10:04:23 -0800498 bool binary = strcmp(optarg, "events") == 0;
499 if (binary) {
500 needBinary = true;
501 }
502
503 if (devices) {
504 dev = devices;
505 while (dev->next) {
506 dev = dev->next;
507 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800508 dev->next = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800509 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800510 devices = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800511 }
512 android::g_devCount++;
513 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800514 break;
515
516 case 'B':
517 android::g_printBinary = 1;
518 break;
519
520 case 'f':
521 // redirect output to a file
522
523 android::g_outputFileName = optarg;
524
525 break;
526
527 case 'r':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800528 if (optarg == NULL) {
529 android::g_logRotateSizeKBytes
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530 = DEFAULT_LOG_ROTATE_SIZE_KBYTES;
531 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800532 if (!isdigit(optarg[0])) {
533 fprintf(stderr,"Invalid parameter to -r\n");
534 android::show_help(argv[0]);
535 exit(-1);
536 }
537 android::g_logRotateSizeKBytes = atoi(optarg);
538 }
539 break;
540
541 case 'n':
542 if (!isdigit(optarg[0])) {
543 fprintf(stderr,"Invalid parameter to -r\n");
544 android::show_help(argv[0]);
545 exit(-1);
546 }
547
548 android::g_maxRotatedLogs = atoi(optarg);
549 break;
550
551 case 'v':
552 err = setLogFormat (optarg);
553 if (err < 0) {
554 fprintf(stderr,"Invalid parameter to -v\n");
555 android::show_help(argv[0]);
556 exit(-1);
557 }
558
Mark Salyzyn649fc602014-09-16 09:15:15 -0700559 if (strcmp("color", optarg)) { // exception for modifiers
560 hasSetLogFormat = 1;
561 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800562 break;
563
564 case 'Q':
565 /* this is a *hidden* option used to start a version of logcat */
566 /* in an emulated device only. it basically looks for androidboot.logcat= */
567 /* on the kernel command line. If something is found, it extracts a log filter */
568 /* and uses it to run the program. If nothing is found, the program should */
569 /* quit immediately */
570#define KERNEL_OPTION "androidboot.logcat="
571#define CONSOLE_OPTION "androidboot.console="
572 {
573 int fd;
574 char* logcat;
575 char* console;
576 int force_exit = 1;
577 static char cmdline[1024];
578
579 fd = open("/proc/cmdline", O_RDONLY);
580 if (fd >= 0) {
581 int n = read(fd, cmdline, sizeof(cmdline)-1 );
582 if (n < 0) n = 0;
583 cmdline[n] = 0;
584 close(fd);
585 } else {
586 cmdline[0] = 0;
587 }
588
589 logcat = strstr( cmdline, KERNEL_OPTION );
590 console = strstr( cmdline, CONSOLE_OPTION );
591 if (logcat != NULL) {
592 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
593 char* q = strpbrk( p, " \t\n\r" );;
594
595 if (q != NULL)
596 *q = 0;
597
598 forceFilters = p;
599 force_exit = 0;
600 }
601 /* if nothing found or invalid filters, exit quietly */
602 if (force_exit)
603 exit(0);
604
605 /* redirect our output to the emulator console */
606 if (console) {
607 char* p = console + sizeof(CONSOLE_OPTION)-1;
608 char* q = strpbrk( p, " \t\n\r" );
609 char devname[64];
610 int len;
611
612 if (q != NULL) {
613 len = q - p;
614 } else
615 len = strlen(p);
616
617 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
618 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
619 if (len < (int)sizeof(devname)) {
620 fd = open( devname, O_WRONLY );
621 if (fd >= 0) {
622 dup2(fd, 1);
623 dup2(fd, 2);
624 close(fd);
625 }
626 }
627 }
628 }
629 break;
630
Mark Salyzyn34facab2014-02-06 14:48:50 -0800631 case 'S':
632 printStatistics = 1;
633 break;
634
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800635 default:
636 fprintf(stderr,"Unrecognized Option\n");
637 android::show_help(argv[0]);
638 exit(-1);
639 break;
640 }
641 }
642
Joe Onorato6fa09a02010-02-26 10:04:23 -0800643 if (!devices) {
Mark Salyzyn989980c2014-05-14 12:37:22 -0700644 dev = devices = new log_device_t("main", false, 'm');
Joe Onorato6fa09a02010-02-26 10:04:23 -0800645 android::g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800646 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn989980c2014-05-14 12:37:22 -0700647 dev = dev->next = new log_device_t("system", false, 's');
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800648 android::g_devCount++;
649 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700650 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn989980c2014-05-14 12:37:22 -0700651 dev = dev->next = new log_device_t("crash", false, 'c');
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700652 android::g_devCount++;
653 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800654 }
655
Mark Salyzyn95132e92013-11-22 10:55:48 -0800656 if (android::g_logRotateSizeKBytes != 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800657 && android::g_outputFileName == NULL
658 ) {
659 fprintf(stderr,"-r requires -f as well\n");
660 android::show_help(argv[0]);
661 exit(-1);
662 }
663
664 android::setupOutput();
665
666 if (hasSetLogFormat == 0) {
667 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
668
669 if (logFormat != NULL) {
670 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800671 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800672 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800673 logFormat);
674 }
Mark Salyzyn649fc602014-09-16 09:15:15 -0700675 } else {
676 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800677 }
678 }
679
680 if (forceFilters) {
681 err = android_log_addFilterString(g_logformat, forceFilters);
682 if (err < 0) {
683 fprintf (stderr, "Invalid filter expression in -logcat option\n");
684 exit(0);
685 }
686 } else if (argc == optind) {
687 // Add from environment variable
688 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
689
690 if (env_tags_orig != NULL) {
691 err = android_log_addFilterString(g_logformat, env_tags_orig);
692
Mark Salyzyn95132e92013-11-22 10:55:48 -0800693 if (err < 0) {
694 fprintf(stderr, "Invalid filter expression in"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800695 " ANDROID_LOG_TAGS\n");
696 android::show_help(argv[0]);
697 exit(-1);
698 }
699 }
700 } else {
701 // Add from commandline
702 for (int i = optind ; i < argc ; i++) {
703 err = android_log_addFilterString(g_logformat, argv[i]);
704
Mark Salyzyn95132e92013-11-22 10:55:48 -0800705 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800706 fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]);
707 android::show_help(argv[0]);
708 exit(-1);
709 }
710 }
711 }
712
Joe Onorato6fa09a02010-02-26 10:04:23 -0800713 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800714 if (tail_time != log_time::EPOCH) {
715 logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
716 } else {
717 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
718 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800719 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800720 dev->logger_list = logger_list;
721 dev->logger = android_logger_open(logger_list,
722 android_name_to_log_id(dev->device));
723 if (!dev->logger) {
724 fprintf(stderr, "Unable to open log device '%s'\n", dev->device);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800725 exit(EXIT_FAILURE);
726 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800727
728 if (clearLog) {
729 int ret;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800730 ret = android_logger_clear(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800731 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700732 perror("failed to clear the log");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800733 exit(EXIT_FAILURE);
734 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800735 }
736
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800737 if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700738 perror("failed to set the log size");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800739 exit(EXIT_FAILURE);
740 }
741
Joe Onorato6fa09a02010-02-26 10:04:23 -0800742 if (getLogSize) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800743 long size, readable;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800744
Mark Salyzyn95132e92013-11-22 10:55:48 -0800745 size = android_logger_get_log_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800746 if (size < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700747 perror("failed to get the log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800748 exit(EXIT_FAILURE);
749 }
750
Mark Salyzyn95132e92013-11-22 10:55:48 -0800751 readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800752 if (readable < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700753 perror("failed to get the readable log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800754 exit(EXIT_FAILURE);
755 }
756
Mark Salyzyn671e3432014-05-06 07:34:59 -0700757 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
Joe Onorato6fa09a02010-02-26 10:04:23 -0800758 "max entry is %db, max payload is %db\n", dev->device,
Mark Salyzyn671e3432014-05-06 07:34:59 -0700759 value_of_size(size), multiplier_of_size(size),
760 value_of_size(readable), multiplier_of_size(readable),
Joe Onorato6fa09a02010-02-26 10:04:23 -0800761 (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
762 }
763
764 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800765 }
766
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800767 if (setPruneList) {
768 size_t len = strlen(setPruneList) + 32; // margin to allow rc
769 char *buf = (char *) malloc(len);
770
771 strcpy(buf, setPruneList);
772 int ret = android_logger_set_prune_list(logger_list, buf, len);
773 free(buf);
774
775 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700776 perror("failed to set the prune list");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800777 exit(EXIT_FAILURE);
778 }
779 }
780
Mark Salyzyn1c950472014-04-01 17:19:47 -0700781 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800782 size_t len = 8192;
783 char *buf;
784
785 for(int retry = 32;
786 (retry >= 0) && ((buf = new char [len]));
787 delete [] buf, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800788 if (getPruneList) {
789 android_logger_get_prune_list(logger_list, buf, len);
790 } else {
791 android_logger_get_statistics(logger_list, buf, len);
792 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800793 buf[len-1] = '\0';
794 size_t ret = atol(buf) + 1;
795 if (ret < 4) {
796 delete [] buf;
797 buf = NULL;
798 break;
799 }
800 bool check = ret <= len;
801 len = ret;
802 if (check) {
803 break;
804 }
805 }
806
807 if (!buf) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700808 perror("failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -0800809 exit(EXIT_FAILURE);
810 }
811
812 // remove trailing FF
813 char *cp = buf + len - 1;
814 *cp = '\0';
815 bool truncated = *--cp != '\f';
816 if (!truncated) {
817 *cp = '\0';
818 }
819
820 // squash out the byte count
821 cp = buf;
822 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -0700823 while (isdigit(*cp)) {
824 ++cp;
825 }
826 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800827 ++cp;
828 }
829 }
830
831 printf("%s", cp);
832 delete [] buf;
833 exit(0);
834 }
835
836
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800837 if (getLogSize) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700838 exit(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800839 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800840 if (setLogSize || setPruneList) {
841 exit(0);
842 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800843 if (clearLog) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700844 exit(0);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800845 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800846
847 //LOG_EVENT_INT(10, 12345);
848 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
849 //LOG_EVENT_STRING(0, "whassup, doc?");
850
Joe Onorato6fa09a02010-02-26 10:04:23 -0800851 if (needBinary)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800852 android::g_eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
853
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800854 dev = NULL;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800855 while (1) {
856 struct log_msg log_msg;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800857 log_device_t* d;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800858 int ret = android_logger_list_read(logger_list, &log_msg);
859
860 if (ret == 0) {
861 fprintf(stderr, "read: Unexpected EOF!\n");
862 exit(EXIT_FAILURE);
863 }
864
865 if (ret < 0) {
866 if (ret == -EAGAIN) {
867 break;
868 }
869
870 if (ret == -EIO) {
871 fprintf(stderr, "read: Unexpected EOF!\n");
872 exit(EXIT_FAILURE);
873 }
874 if (ret == -EINVAL) {
875 fprintf(stderr, "read: unexpected length.\n");
876 exit(EXIT_FAILURE);
877 }
Mark Salyzynfff04e32014-03-17 10:36:46 -0700878 perror("logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800879 exit(EXIT_FAILURE);
880 }
881
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800882 for(d = devices; d; d = d->next) {
883 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800884 break;
885 }
886 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800887 if (!d) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800888 fprintf(stderr, "read: Unexpected log ID!\n");
889 exit(EXIT_FAILURE);
890 }
891
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800892 if (dev != d) {
893 dev = d;
894 android::maybePrintStart(dev, printDividers);
895 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800896 if (android::g_printBinary) {
897 android::printBinary(&log_msg);
898 } else {
899 android::processBuffer(dev, &log_msg);
900 }
901 }
902
903 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800904
905 return 0;
906}