blob: 7e4eaddd42d87e4920abd19e18d34921ee6c4a8d [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;
Joe Onorato6fa09a02010-02-26 10:04:23 -080041
Joe Onorato6fa09a02010-02-26 10:04:23 -080042 log_device_t* next;
43
Mark Salyzyn5f6738a2015-02-27 13:41:34 -080044 log_device_t(const char* d, bool b) {
Joe Onorato6fa09a02010-02-26 10:04:23 -080045 device = d;
46 binary = b;
Joe Onorato6fa09a02010-02-26 10:04:23 -080047 next = NULL;
48 printed = false;
49 }
Joe Onorato6fa09a02010-02-26 10:04:23 -080050};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051
52namespace android {
53
54/* Global Variables */
55
56static const char * g_outputFileName = NULL;
57static int g_logRotateSizeKBytes = 0; // 0 means "no log rotation"
58static int g_maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS; // 0 means "unbounded"
59static int g_outFD = -1;
60static off_t g_outByteCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061static int g_printBinary = 0;
Mark Salyzyn9421b0c2015-02-26 14:33:35 -080062static int g_devCount = 0; // >1 means multiple
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063
64static int openLogFile (const char *pathname)
65{
Edwin Vane80b221c2012-08-13 12:55:07 -040066 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067}
68
69static void rotateLogs()
70{
71 int err;
72
73 // Can't rotate logs if we're not outputting to a file
74 if (g_outputFileName == NULL) {
75 return;
76 }
77
78 close(g_outFD);
79
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070080 // Compute the maximum number of digits needed to count up to g_maxRotatedLogs in decimal.
81 // eg: g_maxRotatedLogs == 30 -> log10(30) == 1.477 -> maxRotationCountDigits == 2
82 int maxRotationCountDigits =
83 (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
84
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085 for (int i = g_maxRotatedLogs ; i > 0 ; i--) {
86 char *file0, *file1;
87
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070088 asprintf(&file1, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089
90 if (i - 1 == 0) {
91 asprintf(&file0, "%s", g_outputFileName);
92 } else {
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070093 asprintf(&file0, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094 }
95
96 err = rename (file0, file1);
97
98 if (err < 0 && errno != ENOENT) {
99 perror("while rotating log files");
100 }
101
102 free(file1);
103 free(file0);
104 }
105
106 g_outFD = openLogFile (g_outputFileName);
107
108 if (g_outFD < 0) {
109 perror ("couldn't open output file");
110 exit(-1);
111 }
112
113 g_outByteCount = 0;
114
115}
116
Mark Salyzyn95132e92013-11-22 10:55:48 -0800117void printBinary(struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118{
Mark Salyzyn95132e92013-11-22 10:55:48 -0800119 size_t size = buf->len();
120
121 TEMP_FAILURE_RETRY(write(g_outFD, buf, size));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122}
123
Mark Salyzyn95132e92013-11-22 10:55:48 -0800124static void processBuffer(log_device_t* dev, struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125{
Mathias Agopian50844522010-03-17 16:10:26 -0700126 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127 int err;
128 AndroidLogEntry entry;
129 char binaryMsgBuf[1024];
130
Joe Onorato6fa09a02010-02-26 10:04:23 -0800131 if (dev->binary) {
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800132 static bool hasOpenedEventTagMap = false;
133 static EventTagMap *eventTagMap = NULL;
134
135 if (!eventTagMap && !hasOpenedEventTagMap) {
136 eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
137 hasOpenedEventTagMap = true;
138 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800139 err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry,
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800140 eventTagMap,
Mark Salyzyn95132e92013-11-22 10:55:48 -0800141 binaryMsgBuf,
142 sizeof(binaryMsgBuf));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 //printf(">>> pri=%d len=%d msg='%s'\n",
144 // entry.priority, entry.messageLen, entry.message);
145 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800146 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800148 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149 goto error;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800150 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800152 if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800153 bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry);
154
155 if (bytesWritten < 0) {
156 perror("output error");
157 exit(-1);
158 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159 }
160
161 g_outByteCount += bytesWritten;
162
Mark Salyzyn95132e92013-11-22 10:55:48 -0800163 if (g_logRotateSizeKBytes > 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164 && (g_outByteCount / 1024) >= g_logRotateSizeKBytes
165 ) {
166 rotateLogs();
167 }
168
169error:
170 //fprintf (stderr, "Error processing record\n");
171 return;
172}
173
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800174static void maybePrintStart(log_device_t* dev, bool printDividers) {
175 if (!dev->printed || printDividers) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800176 if (g_devCount > 1 && !g_printBinary) {
177 char buf[1024];
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800178 snprintf(buf, sizeof(buf), "--------- %s %s\n",
179 dev->printed ? "switch to" : "beginning of",
Mark Salyzyn95132e92013-11-22 10:55:48 -0800180 dev->device);
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800181 if (write(g_outFD, buf, strlen(buf)) < 0) {
182 perror("output error");
183 exit(-1);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800184 }
185 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800186 dev->printed = true;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800187 }
188}
189
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190static void setupOutput()
191{
192
193 if (g_outputFileName == NULL) {
194 g_outFD = STDOUT_FILENO;
195
196 } else {
197 struct stat statbuf;
198
199 g_outFD = openLogFile (g_outputFileName);
200
201 if (g_outFD < 0) {
202 perror ("couldn't open output file");
203 exit(-1);
204 }
205
206 fstat(g_outFD, &statbuf);
207
208 g_outByteCount = statbuf.st_size;
209 }
210}
211
212static void show_help(const char *cmd)
213{
214 fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd);
215
216 fprintf(stderr, "options include:\n"
217 " -s Set default filter to silent.\n"
218 " Like specifying filterspec '*:s'\n"
219 " -f <filename> Log to file. Default to stdout\n"
220 " -r [<kbytes>] Rotate log every kbytes. (16 if unspecified). Requires -f\n"
221 " -n <count> Sets max number of rotated logs to <count>, default 4\n"
Pierre Zurekead88fc2010-10-17 22:39:37 +0200222 " -v <format> Sets the log print format, where <format> is:\n\n"
223 " brief color long process raw tag thread threadtime time\n\n"
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800224 " -D print dividers between each log buffer\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225 " -c clear (flush) the entire log and exit\n"
226 " -d dump the log and then exit (don't block)\n"
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800227 " -t <count> print only the most recent <count> lines (implies -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700228 " -t '<time>' print most recent lines since specified time (implies -d)\n"
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800229 " -T <count> print only the most recent <count> lines (does not imply -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700230 " -T '<time>' print most recent lines since specified time (not imply -d)\n"
231 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm'\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232 " -g get the size of the log's ring buffer and exit\n"
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800233 " -L dump logs from prior to last reboot\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800234 " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n"
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700235 " 'events', 'crash' or 'all'. Multiple -b parameters are\n"
236 " allowed and results are interleaved. The default is\n"
237 " -b main -b system -b crash.\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800238 " -B output the log in binary.\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700239 " -S output statistics.\n"
240 " -G <size> set size of log ring buffer, may suffix with K or M.\n"
241 " -p print prune white and ~black list. Service is specified as\n"
242 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
243 " with ~, otherwise weighed for longevity if unadorned. All\n"
244 " other pruning activity is oldest first. Special case ~!\n"
245 " represents an automatic quicker pruning for the noisiest\n"
246 " UID as determined by the current statistics.\n"
247 " -P '<list> ...' set prune white and ~black list, using same format as\n"
248 " printed above. Must be quoted.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249
250 fprintf(stderr,"\nfilterspecs are a series of \n"
251 " <tag>[:priority]\n\n"
252 "where <tag> is a log component tag (or * for all) and priority is:\n"
253 " V Verbose\n"
254 " D Debug\n"
255 " I Info\n"
256 " W Warn\n"
257 " E Error\n"
258 " F Fatal\n"
259 " S Silent (supress all output)\n"
260 "\n'*' means '*:d' and <tag> by itself means <tag>:v\n"
261 "\nIf not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.\n"
262 "If no filterspec is found, filter defaults to '*:I'\n"
263 "\nIf not specified with -v, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700264 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265
266
267
268}
269
270
271} /* namespace android */
272
273static int setLogFormat(const char * formatString)
274{
275 static AndroidLogPrintFormat format;
276
277 format = android_log_formatFromString(formatString);
278
279 if (format == FORMAT_OFF) {
280 // FORMAT_OFF means invalid string
281 return -1;
282 }
283
284 android_log_setPrintFormat(g_logformat, format);
285
286 return 0;
287}
288
Mark Salyzyn671e3432014-05-06 07:34:59 -0700289static const char multipliers[][2] = {
290 { "" },
291 { "K" },
292 { "M" },
293 { "G" }
294};
295
296static unsigned long value_of_size(unsigned long value)
297{
298 for (unsigned i = 0;
299 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
300 value /= 1024, ++i) ;
301 return value;
302}
303
304static const char *multiplier_of_size(unsigned long value)
305{
306 unsigned i;
307 for (i = 0;
308 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
309 value /= 1024, ++i) ;
310 return multipliers[i];
311}
312
Joe Onorato6fa09a02010-02-26 10:04:23 -0800313int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 int err;
316 int hasSetLogFormat = 0;
317 int clearLog = 0;
318 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800319 unsigned long setLogSize = 0;
320 int getPruneList = 0;
321 char *setPruneList = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800322 int printStatistics = 0;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800323 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800325 log_device_t* devices = NULL;
326 log_device_t* dev;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800327 bool printDividers = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800328 struct logger_list *logger_list;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800329 unsigned int tail_lines = 0;
330 log_time tail_time(log_time::EPOCH);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800332 signal(SIGPIPE, exit);
333
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 g_logformat = android_log_format_new();
335
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
337 android::show_help(argv[0]);
338 exit(0);
339 }
340
341 for (;;) {
342 int ret;
343
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800344 ret = getopt(argc, argv, "cdDLt:T:gG:sQf:r:n:v:b:BSpP:");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345
346 if (ret < 0) {
347 break;
348 }
349
350 switch(ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800351 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352 // default to all silent
353 android_log_addFilterRule(g_logformat, "*:s");
354 break;
355
356 case 'c':
357 clearLog = 1;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800358 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359 break;
360
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800361 case 'L':
362 mode |= ANDROID_LOG_PSTORE;
363 break;
364
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365 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
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800401 case 'D':
402 printDividers = true;
403 break;
404
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405 case 'g':
406 getLogSize = 1;
407 break;
408
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800409 case 'G': {
410 // would use atol if not for the multiplier
411 char *cp = optarg;
412 setLogSize = 0;
413 while (('0' <= *cp) && (*cp <= '9')) {
414 setLogSize *= 10;
415 setLogSize += *cp - '0';
416 ++cp;
417 }
418
419 switch(*cp) {
420 case 'g':
421 case 'G':
422 setLogSize *= 1024;
423 /* FALLTHRU */
424 case 'm':
425 case 'M':
426 setLogSize *= 1024;
427 /* FALLTHRU */
428 case 'k':
429 case 'K':
430 setLogSize *= 1024;
431 /* FALLTHRU */
432 case '\0':
433 break;
434
435 default:
436 setLogSize = 0;
437 }
438
439 if (!setLogSize) {
440 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
441 exit(1);
442 }
443 }
444 break;
445
446 case 'p':
447 getPruneList = 1;
448 break;
449
450 case 'P':
451 setPruneList = optarg;
452 break;
453
Joe Onorato6fa09a02010-02-26 10:04:23 -0800454 case 'b': {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800455 if (strcmp(optarg, "all") == 0) {
456 while (devices) {
457 dev = devices;
458 devices = dev->next;
459 delete dev;
460 }
461
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700462 devices = dev = NULL;
463 android::g_devCount = 0;
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700464 for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
465 const char *name = android_log_id_to_name((log_id_t)i);
466 log_id_t log_id = android_name_to_log_id(name);
467
468 if (log_id != (log_id_t)i) {
469 continue;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800470 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700471
472 bool binary = strcmp(name, "events") == 0;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800473 log_device_t* d = new log_device_t(name, binary);
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700474
475 if (dev) {
476 dev->next = d;
477 dev = d;
478 } else {
479 devices = dev = d;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800480 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700481 android::g_devCount++;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800482 }
483 break;
484 }
485
Joe Onorato6fa09a02010-02-26 10:04:23 -0800486 bool binary = strcmp(optarg, "events") == 0;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800487
488 if (devices) {
489 dev = devices;
490 while (dev->next) {
491 dev = dev->next;
492 }
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800493 dev->next = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800494 } else {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800495 devices = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800496 }
497 android::g_devCount++;
498 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 break;
500
501 case 'B':
502 android::g_printBinary = 1;
503 break;
504
505 case 'f':
506 // redirect output to a file
507
508 android::g_outputFileName = optarg;
509
510 break;
511
512 case 'r':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800513 if (optarg == NULL) {
514 android::g_logRotateSizeKBytes
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515 = DEFAULT_LOG_ROTATE_SIZE_KBYTES;
516 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517 if (!isdigit(optarg[0])) {
518 fprintf(stderr,"Invalid parameter to -r\n");
519 android::show_help(argv[0]);
520 exit(-1);
521 }
522 android::g_logRotateSizeKBytes = atoi(optarg);
523 }
524 break;
525
526 case 'n':
527 if (!isdigit(optarg[0])) {
528 fprintf(stderr,"Invalid parameter to -r\n");
529 android::show_help(argv[0]);
530 exit(-1);
531 }
532
533 android::g_maxRotatedLogs = atoi(optarg);
534 break;
535
536 case 'v':
537 err = setLogFormat (optarg);
538 if (err < 0) {
539 fprintf(stderr,"Invalid parameter to -v\n");
540 android::show_help(argv[0]);
541 exit(-1);
542 }
543
Mark Salyzyn649fc602014-09-16 09:15:15 -0700544 if (strcmp("color", optarg)) { // exception for modifiers
545 hasSetLogFormat = 1;
546 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800547 break;
548
549 case 'Q':
550 /* this is a *hidden* option used to start a version of logcat */
551 /* in an emulated device only. it basically looks for androidboot.logcat= */
552 /* on the kernel command line. If something is found, it extracts a log filter */
553 /* and uses it to run the program. If nothing is found, the program should */
554 /* quit immediately */
555#define KERNEL_OPTION "androidboot.logcat="
556#define CONSOLE_OPTION "androidboot.console="
557 {
558 int fd;
559 char* logcat;
560 char* console;
561 int force_exit = 1;
562 static char cmdline[1024];
563
564 fd = open("/proc/cmdline", O_RDONLY);
565 if (fd >= 0) {
566 int n = read(fd, cmdline, sizeof(cmdline)-1 );
567 if (n < 0) n = 0;
568 cmdline[n] = 0;
569 close(fd);
570 } else {
571 cmdline[0] = 0;
572 }
573
574 logcat = strstr( cmdline, KERNEL_OPTION );
575 console = strstr( cmdline, CONSOLE_OPTION );
576 if (logcat != NULL) {
577 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
578 char* q = strpbrk( p, " \t\n\r" );;
579
580 if (q != NULL)
581 *q = 0;
582
583 forceFilters = p;
584 force_exit = 0;
585 }
586 /* if nothing found or invalid filters, exit quietly */
587 if (force_exit)
588 exit(0);
589
590 /* redirect our output to the emulator console */
591 if (console) {
592 char* p = console + sizeof(CONSOLE_OPTION)-1;
593 char* q = strpbrk( p, " \t\n\r" );
594 char devname[64];
595 int len;
596
597 if (q != NULL) {
598 len = q - p;
599 } else
600 len = strlen(p);
601
602 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
603 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
604 if (len < (int)sizeof(devname)) {
605 fd = open( devname, O_WRONLY );
606 if (fd >= 0) {
607 dup2(fd, 1);
608 dup2(fd, 2);
609 close(fd);
610 }
611 }
612 }
613 }
614 break;
615
Mark Salyzyn34facab2014-02-06 14:48:50 -0800616 case 'S':
617 printStatistics = 1;
618 break;
619
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800620 default:
621 fprintf(stderr,"Unrecognized Option\n");
622 android::show_help(argv[0]);
623 exit(-1);
624 break;
625 }
626 }
627
Joe Onorato6fa09a02010-02-26 10:04:23 -0800628 if (!devices) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800629 dev = devices = new log_device_t("main", false);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800630 android::g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800631 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800632 dev = dev->next = new log_device_t("system", false);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800633 android::g_devCount++;
634 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700635 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800636 dev = dev->next = new log_device_t("crash", false);
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700637 android::g_devCount++;
638 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800639 }
640
Mark Salyzyn95132e92013-11-22 10:55:48 -0800641 if (android::g_logRotateSizeKBytes != 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800642 && android::g_outputFileName == NULL
643 ) {
644 fprintf(stderr,"-r requires -f as well\n");
645 android::show_help(argv[0]);
646 exit(-1);
647 }
648
649 android::setupOutput();
650
651 if (hasSetLogFormat == 0) {
652 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
653
654 if (logFormat != NULL) {
655 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800656 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800657 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800658 logFormat);
659 }
Mark Salyzyn649fc602014-09-16 09:15:15 -0700660 } else {
661 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800662 }
663 }
664
665 if (forceFilters) {
666 err = android_log_addFilterString(g_logformat, forceFilters);
667 if (err < 0) {
668 fprintf (stderr, "Invalid filter expression in -logcat option\n");
669 exit(0);
670 }
671 } else if (argc == optind) {
672 // Add from environment variable
673 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
674
675 if (env_tags_orig != NULL) {
676 err = android_log_addFilterString(g_logformat, env_tags_orig);
677
Mark Salyzyn95132e92013-11-22 10:55:48 -0800678 if (err < 0) {
679 fprintf(stderr, "Invalid filter expression in"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800680 " ANDROID_LOG_TAGS\n");
681 android::show_help(argv[0]);
682 exit(-1);
683 }
684 }
685 } else {
686 // Add from commandline
687 for (int i = optind ; i < argc ; i++) {
688 err = android_log_addFilterString(g_logformat, argv[i]);
689
Mark Salyzyn95132e92013-11-22 10:55:48 -0800690 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800691 fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]);
692 android::show_help(argv[0]);
693 exit(-1);
694 }
695 }
696 }
697
Joe Onorato6fa09a02010-02-26 10:04:23 -0800698 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800699 if (tail_time != log_time::EPOCH) {
700 logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
701 } else {
702 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
703 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800704 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800705 dev->logger_list = logger_list;
706 dev->logger = android_logger_open(logger_list,
707 android_name_to_log_id(dev->device));
708 if (!dev->logger) {
709 fprintf(stderr, "Unable to open log device '%s'\n", dev->device);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800710 exit(EXIT_FAILURE);
711 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800712
713 if (clearLog) {
714 int ret;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800715 ret = android_logger_clear(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800716 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700717 perror("failed to clear the log");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800718 exit(EXIT_FAILURE);
719 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800720 }
721
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800722 if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700723 perror("failed to set the log size");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800724 exit(EXIT_FAILURE);
725 }
726
Joe Onorato6fa09a02010-02-26 10:04:23 -0800727 if (getLogSize) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800728 long size, readable;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800729
Mark Salyzyn95132e92013-11-22 10:55:48 -0800730 size = android_logger_get_log_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800731 if (size < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700732 perror("failed to get the log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800733 exit(EXIT_FAILURE);
734 }
735
Mark Salyzyn95132e92013-11-22 10:55:48 -0800736 readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800737 if (readable < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700738 perror("failed to get the readable log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800739 exit(EXIT_FAILURE);
740 }
741
Mark Salyzyn671e3432014-05-06 07:34:59 -0700742 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
Joe Onorato6fa09a02010-02-26 10:04:23 -0800743 "max entry is %db, max payload is %db\n", dev->device,
Mark Salyzyn671e3432014-05-06 07:34:59 -0700744 value_of_size(size), multiplier_of_size(size),
745 value_of_size(readable), multiplier_of_size(readable),
Joe Onorato6fa09a02010-02-26 10:04:23 -0800746 (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
747 }
748
749 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800750 }
751
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800752 if (setPruneList) {
753 size_t len = strlen(setPruneList) + 32; // margin to allow rc
754 char *buf = (char *) malloc(len);
755
756 strcpy(buf, setPruneList);
757 int ret = android_logger_set_prune_list(logger_list, buf, len);
758 free(buf);
759
760 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700761 perror("failed to set the prune list");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800762 exit(EXIT_FAILURE);
763 }
764 }
765
Mark Salyzyn1c950472014-04-01 17:19:47 -0700766 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800767 size_t len = 8192;
768 char *buf;
769
770 for(int retry = 32;
771 (retry >= 0) && ((buf = new char [len]));
772 delete [] buf, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800773 if (getPruneList) {
774 android_logger_get_prune_list(logger_list, buf, len);
775 } else {
776 android_logger_get_statistics(logger_list, buf, len);
777 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800778 buf[len-1] = '\0';
779 size_t ret = atol(buf) + 1;
780 if (ret < 4) {
781 delete [] buf;
782 buf = NULL;
783 break;
784 }
785 bool check = ret <= len;
786 len = ret;
787 if (check) {
788 break;
789 }
790 }
791
792 if (!buf) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700793 perror("failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -0800794 exit(EXIT_FAILURE);
795 }
796
797 // remove trailing FF
798 char *cp = buf + len - 1;
799 *cp = '\0';
800 bool truncated = *--cp != '\f';
801 if (!truncated) {
802 *cp = '\0';
803 }
804
805 // squash out the byte count
806 cp = buf;
807 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -0700808 while (isdigit(*cp)) {
809 ++cp;
810 }
811 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800812 ++cp;
813 }
814 }
815
816 printf("%s", cp);
817 delete [] buf;
818 exit(0);
819 }
820
821
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800822 if (getLogSize) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700823 exit(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800824 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800825 if (setLogSize || setPruneList) {
826 exit(0);
827 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800828 if (clearLog) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700829 exit(0);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800830 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800831
832 //LOG_EVENT_INT(10, 12345);
833 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
834 //LOG_EVENT_STRING(0, "whassup, doc?");
835
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800836 dev = NULL;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800837 log_device_t unexpected("unexpected", false);
Mark Salyzyn95132e92013-11-22 10:55:48 -0800838 while (1) {
839 struct log_msg log_msg;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800840 log_device_t* d;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800841 int ret = android_logger_list_read(logger_list, &log_msg);
842
843 if (ret == 0) {
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800844 fprintf(stderr, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800845 exit(EXIT_FAILURE);
846 }
847
848 if (ret < 0) {
849 if (ret == -EAGAIN) {
850 break;
851 }
852
853 if (ret == -EIO) {
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800854 fprintf(stderr, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800855 exit(EXIT_FAILURE);
856 }
857 if (ret == -EINVAL) {
858 fprintf(stderr, "read: unexpected length.\n");
859 exit(EXIT_FAILURE);
860 }
Mark Salyzynfff04e32014-03-17 10:36:46 -0700861 perror("logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800862 exit(EXIT_FAILURE);
863 }
864
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800865 for(d = devices; d; d = d->next) {
866 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800867 break;
868 }
869 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800870 if (!d) {
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800871 android::g_devCount = 2; // set to Multiple
872 d = &unexpected;
873 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800874 }
875
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800876 if (dev != d) {
877 dev = d;
878 android::maybePrintStart(dev, printDividers);
879 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800880 if (android::g_printBinary) {
881 android::printBinary(&log_msg);
882 } else {
883 android::processBuffer(dev, &log_msg);
884 }
885 }
886
887 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800888
889 return 0;
890}