blob: d831d667741b32836cba07887f1d5aa60a2fc955 [file] [log] [blame]
Mark Salyzyn0a890f92015-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 Salyzyn0a890f92015-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 Salyzyn0a890f92015-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 Salyzyn0a890f92015-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 Salyzyn0a890f92015-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 Salyzyn34facab2014-02-06 14:48:50 -0800240 " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n"
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700241 " 'events', 'crash' or 'all'. Multiple -b parameters are\n"
242 " allowed and results are interleaved. The default is\n"
243 " -b main -b system -b crash.\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800244 " -B output the log in binary.\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700245 " -S output statistics.\n"
246 " -G <size> set size of log ring buffer, may suffix with K or M.\n"
247 " -p print prune white and ~black list. Service is specified as\n"
248 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
249 " with ~, otherwise weighed for longevity if unadorned. All\n"
250 " other pruning activity is oldest first. Special case ~!\n"
251 " represents an automatic quicker pruning for the noisiest\n"
252 " UID as determined by the current statistics.\n"
253 " -P '<list> ...' set prune white and ~black list, using same format as\n"
254 " printed above. Must be quoted.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255
256 fprintf(stderr,"\nfilterspecs are a series of \n"
257 " <tag>[:priority]\n\n"
258 "where <tag> is a log component tag (or * for all) and priority is:\n"
259 " V Verbose\n"
260 " D Debug\n"
261 " I Info\n"
262 " W Warn\n"
263 " E Error\n"
264 " F Fatal\n"
265 " S Silent (supress all output)\n"
266 "\n'*' means '*:d' and <tag> by itself means <tag>:v\n"
267 "\nIf not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.\n"
268 "If no filterspec is found, filter defaults to '*:I'\n"
269 "\nIf not specified with -v, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700270 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271
272
273
274}
275
276
277} /* namespace android */
278
279static int setLogFormat(const char * formatString)
280{
281 static AndroidLogPrintFormat format;
282
283 format = android_log_formatFromString(formatString);
284
285 if (format == FORMAT_OFF) {
286 // FORMAT_OFF means invalid string
287 return -1;
288 }
289
290 android_log_setPrintFormat(g_logformat, format);
291
292 return 0;
293}
294
Mark Salyzyn671e3432014-05-06 07:34:59 -0700295static const char multipliers[][2] = {
296 { "" },
297 { "K" },
298 { "M" },
299 { "G" }
300};
301
302static unsigned long value_of_size(unsigned long value)
303{
304 for (unsigned i = 0;
305 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
306 value /= 1024, ++i) ;
307 return value;
308}
309
310static const char *multiplier_of_size(unsigned long value)
311{
312 unsigned i;
313 for (i = 0;
314 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
315 value /= 1024, ++i) ;
316 return multipliers[i];
317}
318
Joe Onorato6fa09a02010-02-26 10:04:23 -0800319int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321 int err;
322 int hasSetLogFormat = 0;
323 int clearLog = 0;
324 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800325 unsigned long setLogSize = 0;
326 int getPruneList = 0;
327 char *setPruneList = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800328 int printStatistics = 0;
Mark Salyzyneae52fb2015-01-26 10:46:44 -0800329 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800331 log_device_t* devices = NULL;
332 log_device_t* dev;
333 bool needBinary = false;
Mark Salyzyn0a890f92015-01-26 13:41:33 -0800334 bool printDividers = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800335 struct logger_list *logger_list;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800336 unsigned int tail_lines = 0;
337 log_time tail_time(log_time::EPOCH);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800339 signal(SIGPIPE, exit);
340
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341 g_logformat = android_log_format_new();
342
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
344 android::show_help(argv[0]);
345 exit(0);
346 }
347
348 for (;;) {
349 int ret;
350
Mark Salyzyn0a890f92015-01-26 13:41:33 -0800351 ret = getopt(argc, argv, "cdDt:T:gG:sQf:r:n:v:b:BSpP:");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352
353 if (ret < 0) {
354 break;
355 }
356
357 switch(ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800358 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359 // default to all silent
360 android_log_addFilterRule(g_logformat, "*:s");
361 break;
362
363 case 'c':
364 clearLog = 1;
Mark Salyzyneae52fb2015-01-26 10:46:44 -0800365 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366 break;
367
368 case 'd':
Mark Salyzyneae52fb2015-01-26 10:46:44 -0800369 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370 break;
371
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800372 case 't':
Mark Salyzyneae52fb2015-01-26 10:46:44 -0800373 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800374 /* FALLTHRU */
375 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800376 if (strspn(optarg, "0123456789") != strlen(optarg)) {
377 char *cp = tail_time.strptime(optarg,
378 log_time::default_format);
379 if (!cp) {
380 fprintf(stderr,
381 "ERROR: -%c \"%s\" not in \"%s\" time format\n",
382 ret, optarg, log_time::default_format);
383 exit(1);
384 }
385 if (*cp) {
386 char c = *cp;
387 *cp = '\0';
388 fprintf(stderr,
389 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
390 ret, optarg, c, cp + 1);
391 *cp = c;
392 }
393 } else {
394 tail_lines = atoi(optarg);
395 if (!tail_lines) {
396 fprintf(stderr,
397 "WARNING: -%c %s invalid, setting to 1\n",
398 ret, optarg);
399 tail_lines = 1;
400 }
401 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800402 break;
403
Mark Salyzyn0a890f92015-01-26 13:41:33 -0800404 case 'D':
405 printDividers = true;
406 break;
407
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408 case 'g':
409 getLogSize = 1;
410 break;
411
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800412 case 'G': {
413 // would use atol if not for the multiplier
414 char *cp = optarg;
415 setLogSize = 0;
416 while (('0' <= *cp) && (*cp <= '9')) {
417 setLogSize *= 10;
418 setLogSize += *cp - '0';
419 ++cp;
420 }
421
422 switch(*cp) {
423 case 'g':
424 case 'G':
425 setLogSize *= 1024;
426 /* FALLTHRU */
427 case 'm':
428 case 'M':
429 setLogSize *= 1024;
430 /* FALLTHRU */
431 case 'k':
432 case 'K':
433 setLogSize *= 1024;
434 /* FALLTHRU */
435 case '\0':
436 break;
437
438 default:
439 setLogSize = 0;
440 }
441
442 if (!setLogSize) {
443 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
444 exit(1);
445 }
446 }
447 break;
448
449 case 'p':
450 getPruneList = 1;
451 break;
452
453 case 'P':
454 setPruneList = optarg;
455 break;
456
Joe Onorato6fa09a02010-02-26 10:04:23 -0800457 case 'b': {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800458 if (strcmp(optarg, "all") == 0) {
459 while (devices) {
460 dev = devices;
461 devices = dev->next;
462 delete dev;
463 }
464
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700465 devices = dev = NULL;
466 android::g_devCount = 0;
467 needBinary = false;
468 for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
469 const char *name = android_log_id_to_name((log_id_t)i);
470 log_id_t log_id = android_name_to_log_id(name);
471
472 if (log_id != (log_id_t)i) {
473 continue;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800474 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700475
476 bool binary = strcmp(name, "events") == 0;
477 log_device_t* d = new log_device_t(name, binary, *name);
478
479 if (dev) {
480 dev->next = d;
481 dev = d;
482 } else {
483 devices = dev = d;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800484 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700485 android::g_devCount++;
486 if (binary) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800487 needBinary = true;
488 }
489 }
490 break;
491 }
492
Joe Onorato6fa09a02010-02-26 10:04:23 -0800493 bool binary = strcmp(optarg, "events") == 0;
494 if (binary) {
495 needBinary = true;
496 }
497
498 if (devices) {
499 dev = devices;
500 while (dev->next) {
501 dev = dev->next;
502 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800503 dev->next = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800504 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800505 devices = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800506 }
507 android::g_devCount++;
508 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 break;
510
511 case 'B':
512 android::g_printBinary = 1;
513 break;
514
515 case 'f':
516 // redirect output to a file
517
518 android::g_outputFileName = optarg;
519
520 break;
521
522 case 'r':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800523 if (optarg == NULL) {
524 android::g_logRotateSizeKBytes
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525 = DEFAULT_LOG_ROTATE_SIZE_KBYTES;
526 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800527 if (!isdigit(optarg[0])) {
528 fprintf(stderr,"Invalid parameter to -r\n");
529 android::show_help(argv[0]);
530 exit(-1);
531 }
532 android::g_logRotateSizeKBytes = atoi(optarg);
533 }
534 break;
535
536 case 'n':
537 if (!isdigit(optarg[0])) {
538 fprintf(stderr,"Invalid parameter to -r\n");
539 android::show_help(argv[0]);
540 exit(-1);
541 }
542
543 android::g_maxRotatedLogs = atoi(optarg);
544 break;
545
546 case 'v':
547 err = setLogFormat (optarg);
548 if (err < 0) {
549 fprintf(stderr,"Invalid parameter to -v\n");
550 android::show_help(argv[0]);
551 exit(-1);
552 }
553
Mark Salyzyn649fc602014-09-16 09:15:15 -0700554 if (strcmp("color", optarg)) { // exception for modifiers
555 hasSetLogFormat = 1;
556 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800557 break;
558
559 case 'Q':
560 /* this is a *hidden* option used to start a version of logcat */
561 /* in an emulated device only. it basically looks for androidboot.logcat= */
562 /* on the kernel command line. If something is found, it extracts a log filter */
563 /* and uses it to run the program. If nothing is found, the program should */
564 /* quit immediately */
565#define KERNEL_OPTION "androidboot.logcat="
566#define CONSOLE_OPTION "androidboot.console="
567 {
568 int fd;
569 char* logcat;
570 char* console;
571 int force_exit = 1;
572 static char cmdline[1024];
573
574 fd = open("/proc/cmdline", O_RDONLY);
575 if (fd >= 0) {
576 int n = read(fd, cmdline, sizeof(cmdline)-1 );
577 if (n < 0) n = 0;
578 cmdline[n] = 0;
579 close(fd);
580 } else {
581 cmdline[0] = 0;
582 }
583
584 logcat = strstr( cmdline, KERNEL_OPTION );
585 console = strstr( cmdline, CONSOLE_OPTION );
586 if (logcat != NULL) {
587 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
588 char* q = strpbrk( p, " \t\n\r" );;
589
590 if (q != NULL)
591 *q = 0;
592
593 forceFilters = p;
594 force_exit = 0;
595 }
596 /* if nothing found or invalid filters, exit quietly */
597 if (force_exit)
598 exit(0);
599
600 /* redirect our output to the emulator console */
601 if (console) {
602 char* p = console + sizeof(CONSOLE_OPTION)-1;
603 char* q = strpbrk( p, " \t\n\r" );
604 char devname[64];
605 int len;
606
607 if (q != NULL) {
608 len = q - p;
609 } else
610 len = strlen(p);
611
612 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
613 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
614 if (len < (int)sizeof(devname)) {
615 fd = open( devname, O_WRONLY );
616 if (fd >= 0) {
617 dup2(fd, 1);
618 dup2(fd, 2);
619 close(fd);
620 }
621 }
622 }
623 }
624 break;
625
Mark Salyzyn34facab2014-02-06 14:48:50 -0800626 case 'S':
627 printStatistics = 1;
628 break;
629
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800630 default:
631 fprintf(stderr,"Unrecognized Option\n");
632 android::show_help(argv[0]);
633 exit(-1);
634 break;
635 }
636 }
637
Joe Onorato6fa09a02010-02-26 10:04:23 -0800638 if (!devices) {
Mark Salyzyn989980c2014-05-14 12:37:22 -0700639 dev = devices = new log_device_t("main", false, 'm');
Joe Onorato6fa09a02010-02-26 10:04:23 -0800640 android::g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800641 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn989980c2014-05-14 12:37:22 -0700642 dev = dev->next = new log_device_t("system", false, 's');
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800643 android::g_devCount++;
644 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700645 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn989980c2014-05-14 12:37:22 -0700646 dev = dev->next = new log_device_t("crash", false, 'c');
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700647 android::g_devCount++;
648 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800649 }
650
Mark Salyzyn95132e92013-11-22 10:55:48 -0800651 if (android::g_logRotateSizeKBytes != 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800652 && android::g_outputFileName == NULL
653 ) {
654 fprintf(stderr,"-r requires -f as well\n");
655 android::show_help(argv[0]);
656 exit(-1);
657 }
658
659 android::setupOutput();
660
661 if (hasSetLogFormat == 0) {
662 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
663
664 if (logFormat != NULL) {
665 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800666 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800667 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800668 logFormat);
669 }
Mark Salyzyn649fc602014-09-16 09:15:15 -0700670 } else {
671 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800672 }
673 }
674
675 if (forceFilters) {
676 err = android_log_addFilterString(g_logformat, forceFilters);
677 if (err < 0) {
678 fprintf (stderr, "Invalid filter expression in -logcat option\n");
679 exit(0);
680 }
681 } else if (argc == optind) {
682 // Add from environment variable
683 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
684
685 if (env_tags_orig != NULL) {
686 err = android_log_addFilterString(g_logformat, env_tags_orig);
687
Mark Salyzyn95132e92013-11-22 10:55:48 -0800688 if (err < 0) {
689 fprintf(stderr, "Invalid filter expression in"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690 " ANDROID_LOG_TAGS\n");
691 android::show_help(argv[0]);
692 exit(-1);
693 }
694 }
695 } else {
696 // Add from commandline
697 for (int i = optind ; i < argc ; i++) {
698 err = android_log_addFilterString(g_logformat, argv[i]);
699
Mark Salyzyn95132e92013-11-22 10:55:48 -0800700 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800701 fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]);
702 android::show_help(argv[0]);
703 exit(-1);
704 }
705 }
706 }
707
Joe Onorato6fa09a02010-02-26 10:04:23 -0800708 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800709 if (tail_time != log_time::EPOCH) {
710 logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
711 } else {
712 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
713 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800714 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800715 dev->logger_list = logger_list;
716 dev->logger = android_logger_open(logger_list,
717 android_name_to_log_id(dev->device));
718 if (!dev->logger) {
719 fprintf(stderr, "Unable to open log device '%s'\n", dev->device);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800720 exit(EXIT_FAILURE);
721 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800722
723 if (clearLog) {
724 int ret;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800725 ret = android_logger_clear(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800726 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700727 perror("failed to clear the log");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800728 exit(EXIT_FAILURE);
729 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800730 }
731
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800732 if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700733 perror("failed to set the log size");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800734 exit(EXIT_FAILURE);
735 }
736
Joe Onorato6fa09a02010-02-26 10:04:23 -0800737 if (getLogSize) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800738 long size, readable;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800739
Mark Salyzyn95132e92013-11-22 10:55:48 -0800740 size = android_logger_get_log_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800741 if (size < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700742 perror("failed to get the log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800743 exit(EXIT_FAILURE);
744 }
745
Mark Salyzyn95132e92013-11-22 10:55:48 -0800746 readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800747 if (readable < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700748 perror("failed to get the readable log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800749 exit(EXIT_FAILURE);
750 }
751
Mark Salyzyn671e3432014-05-06 07:34:59 -0700752 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
Joe Onorato6fa09a02010-02-26 10:04:23 -0800753 "max entry is %db, max payload is %db\n", dev->device,
Mark Salyzyn671e3432014-05-06 07:34:59 -0700754 value_of_size(size), multiplier_of_size(size),
755 value_of_size(readable), multiplier_of_size(readable),
Joe Onorato6fa09a02010-02-26 10:04:23 -0800756 (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
757 }
758
759 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800760 }
761
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800762 if (setPruneList) {
763 size_t len = strlen(setPruneList) + 32; // margin to allow rc
764 char *buf = (char *) malloc(len);
765
766 strcpy(buf, setPruneList);
767 int ret = android_logger_set_prune_list(logger_list, buf, len);
768 free(buf);
769
770 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700771 perror("failed to set the prune list");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800772 exit(EXIT_FAILURE);
773 }
774 }
775
Mark Salyzyn1c950472014-04-01 17:19:47 -0700776 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800777 size_t len = 8192;
778 char *buf;
779
780 for(int retry = 32;
781 (retry >= 0) && ((buf = new char [len]));
782 delete [] buf, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800783 if (getPruneList) {
784 android_logger_get_prune_list(logger_list, buf, len);
785 } else {
786 android_logger_get_statistics(logger_list, buf, len);
787 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800788 buf[len-1] = '\0';
789 size_t ret = atol(buf) + 1;
790 if (ret < 4) {
791 delete [] buf;
792 buf = NULL;
793 break;
794 }
795 bool check = ret <= len;
796 len = ret;
797 if (check) {
798 break;
799 }
800 }
801
802 if (!buf) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700803 perror("failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -0800804 exit(EXIT_FAILURE);
805 }
806
807 // remove trailing FF
808 char *cp = buf + len - 1;
809 *cp = '\0';
810 bool truncated = *--cp != '\f';
811 if (!truncated) {
812 *cp = '\0';
813 }
814
815 // squash out the byte count
816 cp = buf;
817 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -0700818 while (isdigit(*cp)) {
819 ++cp;
820 }
821 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800822 ++cp;
823 }
824 }
825
826 printf("%s", cp);
827 delete [] buf;
828 exit(0);
829 }
830
831
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800832 if (getLogSize) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700833 exit(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800834 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800835 if (setLogSize || setPruneList) {
836 exit(0);
837 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800838 if (clearLog) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700839 exit(0);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800840 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800841
842 //LOG_EVENT_INT(10, 12345);
843 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
844 //LOG_EVENT_STRING(0, "whassup, doc?");
845
Joe Onorato6fa09a02010-02-26 10:04:23 -0800846 if (needBinary)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800847 android::g_eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
848
Mark Salyzyn0a890f92015-01-26 13:41:33 -0800849 dev = NULL;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800850 while (1) {
851 struct log_msg log_msg;
Mark Salyzyn0a890f92015-01-26 13:41:33 -0800852 log_device_t* d;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800853 int ret = android_logger_list_read(logger_list, &log_msg);
854
855 if (ret == 0) {
856 fprintf(stderr, "read: Unexpected EOF!\n");
857 exit(EXIT_FAILURE);
858 }
859
860 if (ret < 0) {
861 if (ret == -EAGAIN) {
862 break;
863 }
864
865 if (ret == -EIO) {
866 fprintf(stderr, "read: Unexpected EOF!\n");
867 exit(EXIT_FAILURE);
868 }
869 if (ret == -EINVAL) {
870 fprintf(stderr, "read: unexpected length.\n");
871 exit(EXIT_FAILURE);
872 }
Mark Salyzynfff04e32014-03-17 10:36:46 -0700873 perror("logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800874 exit(EXIT_FAILURE);
875 }
876
Mark Salyzyn0a890f92015-01-26 13:41:33 -0800877 for(d = devices; d; d = d->next) {
878 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800879 break;
880 }
881 }
Mark Salyzyn0a890f92015-01-26 13:41:33 -0800882 if (!d) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800883 fprintf(stderr, "read: Unexpected log ID!\n");
884 exit(EXIT_FAILURE);
885 }
886
Mark Salyzyn0a890f92015-01-26 13:41:33 -0800887 if (dev != d) {
888 dev = d;
889 android::maybePrintStart(dev, printDividers);
890 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800891 if (android::g_printBinary) {
892 android::printBinary(&log_msg);
893 } else {
894 android::processBuffer(dev, &log_msg);
895 }
896 }
897
898 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800899
900 return 0;
901}