blob: be96fc4955104058576e35a933245fcad2222906 [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"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700218 " Like specifying filterspec '*:S'\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219 " -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"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700253 " V Verbose (default for <tag>)\n"
254 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255 " I Info\n"
256 " W Warn\n"
257 " E Error\n"
258 " F Fatal\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700259 " S Silent (suppress all output)\n"
260 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
261 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
262 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
263 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
264 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700265 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800266}
267
268
269} /* namespace android */
270
271static int setLogFormat(const char * formatString)
272{
273 static AndroidLogPrintFormat format;
274
275 format = android_log_formatFromString(formatString);
276
277 if (format == FORMAT_OFF) {
278 // FORMAT_OFF means invalid string
279 return -1;
280 }
281
282 android_log_setPrintFormat(g_logformat, format);
283
284 return 0;
285}
286
Mark Salyzyn671e3432014-05-06 07:34:59 -0700287static const char multipliers[][2] = {
288 { "" },
289 { "K" },
290 { "M" },
291 { "G" }
292};
293
294static unsigned long value_of_size(unsigned long value)
295{
296 for (unsigned i = 0;
297 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
298 value /= 1024, ++i) ;
299 return value;
300}
301
302static const char *multiplier_of_size(unsigned long value)
303{
304 unsigned i;
305 for (i = 0;
306 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
307 value /= 1024, ++i) ;
308 return multipliers[i];
309}
310
Joe Onorato6fa09a02010-02-26 10:04:23 -0800311int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313 int err;
314 int hasSetLogFormat = 0;
315 int clearLog = 0;
316 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800317 unsigned long setLogSize = 0;
318 int getPruneList = 0;
319 char *setPruneList = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800320 int printStatistics = 0;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800321 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800323 log_device_t* devices = NULL;
324 log_device_t* dev;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800325 bool printDividers = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800326 struct logger_list *logger_list;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800327 unsigned int tail_lines = 0;
328 log_time tail_time(log_time::EPOCH);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800330 signal(SIGPIPE, exit);
331
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332 g_logformat = android_log_format_new();
333
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
335 android::show_help(argv[0]);
336 exit(0);
337 }
338
339 for (;;) {
340 int ret;
341
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800342 ret = getopt(argc, argv, "cdDLt:T:gG:sQf:r:n:v:b:BSpP:");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343
344 if (ret < 0) {
345 break;
346 }
347
348 switch(ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800349 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350 // default to all silent
351 android_log_addFilterRule(g_logformat, "*:s");
352 break;
353
354 case 'c':
355 clearLog = 1;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800356 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800357 break;
358
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800359 case 'L':
360 mode |= ANDROID_LOG_PSTORE;
361 break;
362
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800364 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365 break;
366
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800367 case 't':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800368 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800369 /* FALLTHRU */
370 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800371 if (strspn(optarg, "0123456789") != strlen(optarg)) {
372 char *cp = tail_time.strptime(optarg,
373 log_time::default_format);
374 if (!cp) {
375 fprintf(stderr,
376 "ERROR: -%c \"%s\" not in \"%s\" time format\n",
377 ret, optarg, log_time::default_format);
378 exit(1);
379 }
380 if (*cp) {
381 char c = *cp;
382 *cp = '\0';
383 fprintf(stderr,
384 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
385 ret, optarg, c, cp + 1);
386 *cp = c;
387 }
388 } else {
389 tail_lines = atoi(optarg);
390 if (!tail_lines) {
391 fprintf(stderr,
392 "WARNING: -%c %s invalid, setting to 1\n",
393 ret, optarg);
394 tail_lines = 1;
395 }
396 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800397 break;
398
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800399 case 'D':
400 printDividers = true;
401 break;
402
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403 case 'g':
404 getLogSize = 1;
405 break;
406
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800407 case 'G': {
408 // would use atol if not for the multiplier
409 char *cp = optarg;
410 setLogSize = 0;
411 while (('0' <= *cp) && (*cp <= '9')) {
412 setLogSize *= 10;
413 setLogSize += *cp - '0';
414 ++cp;
415 }
416
417 switch(*cp) {
418 case 'g':
419 case 'G':
420 setLogSize *= 1024;
421 /* FALLTHRU */
422 case 'm':
423 case 'M':
424 setLogSize *= 1024;
425 /* FALLTHRU */
426 case 'k':
427 case 'K':
428 setLogSize *= 1024;
429 /* FALLTHRU */
430 case '\0':
431 break;
432
433 default:
434 setLogSize = 0;
435 }
436
437 if (!setLogSize) {
438 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
439 exit(1);
440 }
441 }
442 break;
443
444 case 'p':
445 getPruneList = 1;
446 break;
447
448 case 'P':
449 setPruneList = optarg;
450 break;
451
Joe Onorato6fa09a02010-02-26 10:04:23 -0800452 case 'b': {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800453 if (strcmp(optarg, "all") == 0) {
454 while (devices) {
455 dev = devices;
456 devices = dev->next;
457 delete dev;
458 }
459
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700460 devices = dev = NULL;
461 android::g_devCount = 0;
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700462 for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
463 const char *name = android_log_id_to_name((log_id_t)i);
464 log_id_t log_id = android_name_to_log_id(name);
465
466 if (log_id != (log_id_t)i) {
467 continue;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800468 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700469
470 bool binary = strcmp(name, "events") == 0;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800471 log_device_t* d = new log_device_t(name, binary);
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700472
473 if (dev) {
474 dev->next = d;
475 dev = d;
476 } else {
477 devices = dev = d;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800478 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700479 android::g_devCount++;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800480 }
481 break;
482 }
483
Joe Onorato6fa09a02010-02-26 10:04:23 -0800484 bool binary = strcmp(optarg, "events") == 0;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800485
486 if (devices) {
487 dev = devices;
488 while (dev->next) {
489 dev = dev->next;
490 }
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800491 dev->next = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800492 } else {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800493 devices = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800494 }
495 android::g_devCount++;
496 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497 break;
498
499 case 'B':
500 android::g_printBinary = 1;
501 break;
502
503 case 'f':
504 // redirect output to a file
505
506 android::g_outputFileName = optarg;
507
508 break;
509
510 case 'r':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800511 if (optarg == NULL) {
512 android::g_logRotateSizeKBytes
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800513 = DEFAULT_LOG_ROTATE_SIZE_KBYTES;
514 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515 if (!isdigit(optarg[0])) {
516 fprintf(stderr,"Invalid parameter to -r\n");
517 android::show_help(argv[0]);
518 exit(-1);
519 }
520 android::g_logRotateSizeKBytes = atoi(optarg);
521 }
522 break;
523
524 case 'n':
525 if (!isdigit(optarg[0])) {
526 fprintf(stderr,"Invalid parameter to -r\n");
527 android::show_help(argv[0]);
528 exit(-1);
529 }
530
531 android::g_maxRotatedLogs = atoi(optarg);
532 break;
533
534 case 'v':
535 err = setLogFormat (optarg);
536 if (err < 0) {
537 fprintf(stderr,"Invalid parameter to -v\n");
538 android::show_help(argv[0]);
539 exit(-1);
540 }
541
Mark Salyzyn649fc602014-09-16 09:15:15 -0700542 if (strcmp("color", optarg)) { // exception for modifiers
543 hasSetLogFormat = 1;
544 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800545 break;
546
547 case 'Q':
548 /* this is a *hidden* option used to start a version of logcat */
549 /* in an emulated device only. it basically looks for androidboot.logcat= */
550 /* on the kernel command line. If something is found, it extracts a log filter */
551 /* and uses it to run the program. If nothing is found, the program should */
552 /* quit immediately */
553#define KERNEL_OPTION "androidboot.logcat="
554#define CONSOLE_OPTION "androidboot.console="
555 {
556 int fd;
557 char* logcat;
558 char* console;
559 int force_exit = 1;
560 static char cmdline[1024];
561
562 fd = open("/proc/cmdline", O_RDONLY);
563 if (fd >= 0) {
564 int n = read(fd, cmdline, sizeof(cmdline)-1 );
565 if (n < 0) n = 0;
566 cmdline[n] = 0;
567 close(fd);
568 } else {
569 cmdline[0] = 0;
570 }
571
572 logcat = strstr( cmdline, KERNEL_OPTION );
573 console = strstr( cmdline, CONSOLE_OPTION );
574 if (logcat != NULL) {
575 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
576 char* q = strpbrk( p, " \t\n\r" );;
577
578 if (q != NULL)
579 *q = 0;
580
581 forceFilters = p;
582 force_exit = 0;
583 }
584 /* if nothing found or invalid filters, exit quietly */
585 if (force_exit)
586 exit(0);
587
588 /* redirect our output to the emulator console */
589 if (console) {
590 char* p = console + sizeof(CONSOLE_OPTION)-1;
591 char* q = strpbrk( p, " \t\n\r" );
592 char devname[64];
593 int len;
594
595 if (q != NULL) {
596 len = q - p;
597 } else
598 len = strlen(p);
599
600 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
601 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
602 if (len < (int)sizeof(devname)) {
603 fd = open( devname, O_WRONLY );
604 if (fd >= 0) {
605 dup2(fd, 1);
606 dup2(fd, 2);
607 close(fd);
608 }
609 }
610 }
611 }
612 break;
613
Mark Salyzyn34facab2014-02-06 14:48:50 -0800614 case 'S':
615 printStatistics = 1;
616 break;
617
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800618 default:
619 fprintf(stderr,"Unrecognized Option\n");
620 android::show_help(argv[0]);
621 exit(-1);
622 break;
623 }
624 }
625
Joe Onorato6fa09a02010-02-26 10:04:23 -0800626 if (!devices) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800627 dev = devices = new log_device_t("main", false);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800628 android::g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800629 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800630 dev = dev->next = new log_device_t("system", false);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800631 android::g_devCount++;
632 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700633 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800634 dev = dev->next = new log_device_t("crash", false);
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700635 android::g_devCount++;
636 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800637 }
638
Mark Salyzyn95132e92013-11-22 10:55:48 -0800639 if (android::g_logRotateSizeKBytes != 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800640 && android::g_outputFileName == NULL
641 ) {
642 fprintf(stderr,"-r requires -f as well\n");
643 android::show_help(argv[0]);
644 exit(-1);
645 }
646
647 android::setupOutput();
648
649 if (hasSetLogFormat == 0) {
650 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
651
652 if (logFormat != NULL) {
653 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800654 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800655 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800656 logFormat);
657 }
Mark Salyzyn649fc602014-09-16 09:15:15 -0700658 } else {
659 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800660 }
661 }
662
663 if (forceFilters) {
664 err = android_log_addFilterString(g_logformat, forceFilters);
665 if (err < 0) {
666 fprintf (stderr, "Invalid filter expression in -logcat option\n");
667 exit(0);
668 }
669 } else if (argc == optind) {
670 // Add from environment variable
671 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
672
673 if (env_tags_orig != NULL) {
674 err = android_log_addFilterString(g_logformat, env_tags_orig);
675
Mark Salyzyn95132e92013-11-22 10:55:48 -0800676 if (err < 0) {
677 fprintf(stderr, "Invalid filter expression in"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800678 " ANDROID_LOG_TAGS\n");
679 android::show_help(argv[0]);
680 exit(-1);
681 }
682 }
683 } else {
684 // Add from commandline
685 for (int i = optind ; i < argc ; i++) {
686 err = android_log_addFilterString(g_logformat, argv[i]);
687
Mark Salyzyn95132e92013-11-22 10:55:48 -0800688 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800689 fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]);
690 android::show_help(argv[0]);
691 exit(-1);
692 }
693 }
694 }
695
Joe Onorato6fa09a02010-02-26 10:04:23 -0800696 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800697 if (tail_time != log_time::EPOCH) {
698 logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
699 } else {
700 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
701 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800702 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800703 dev->logger_list = logger_list;
704 dev->logger = android_logger_open(logger_list,
705 android_name_to_log_id(dev->device));
706 if (!dev->logger) {
707 fprintf(stderr, "Unable to open log device '%s'\n", dev->device);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800708 exit(EXIT_FAILURE);
709 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800710
711 if (clearLog) {
712 int ret;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800713 ret = android_logger_clear(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800714 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700715 perror("failed to clear the log");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800716 exit(EXIT_FAILURE);
717 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800718 }
719
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800720 if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700721 perror("failed to set the log size");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800722 exit(EXIT_FAILURE);
723 }
724
Joe Onorato6fa09a02010-02-26 10:04:23 -0800725 if (getLogSize) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800726 long size, readable;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800727
Mark Salyzyn95132e92013-11-22 10:55:48 -0800728 size = android_logger_get_log_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800729 if (size < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700730 perror("failed to get the log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800731 exit(EXIT_FAILURE);
732 }
733
Mark Salyzyn95132e92013-11-22 10:55:48 -0800734 readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800735 if (readable < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700736 perror("failed to get the readable log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800737 exit(EXIT_FAILURE);
738 }
739
Mark Salyzyn671e3432014-05-06 07:34:59 -0700740 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
Joe Onorato6fa09a02010-02-26 10:04:23 -0800741 "max entry is %db, max payload is %db\n", dev->device,
Mark Salyzyn671e3432014-05-06 07:34:59 -0700742 value_of_size(size), multiplier_of_size(size),
743 value_of_size(readable), multiplier_of_size(readable),
Joe Onorato6fa09a02010-02-26 10:04:23 -0800744 (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
745 }
746
747 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800748 }
749
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800750 if (setPruneList) {
751 size_t len = strlen(setPruneList) + 32; // margin to allow rc
752 char *buf = (char *) malloc(len);
753
754 strcpy(buf, setPruneList);
755 int ret = android_logger_set_prune_list(logger_list, buf, len);
756 free(buf);
757
758 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700759 perror("failed to set the prune list");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800760 exit(EXIT_FAILURE);
761 }
762 }
763
Mark Salyzyn1c950472014-04-01 17:19:47 -0700764 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800765 size_t len = 8192;
766 char *buf;
767
768 for(int retry = 32;
769 (retry >= 0) && ((buf = new char [len]));
770 delete [] buf, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800771 if (getPruneList) {
772 android_logger_get_prune_list(logger_list, buf, len);
773 } else {
774 android_logger_get_statistics(logger_list, buf, len);
775 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800776 buf[len-1] = '\0';
777 size_t ret = atol(buf) + 1;
778 if (ret < 4) {
779 delete [] buf;
780 buf = NULL;
781 break;
782 }
783 bool check = ret <= len;
784 len = ret;
785 if (check) {
786 break;
787 }
788 }
789
790 if (!buf) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700791 perror("failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -0800792 exit(EXIT_FAILURE);
793 }
794
795 // remove trailing FF
796 char *cp = buf + len - 1;
797 *cp = '\0';
798 bool truncated = *--cp != '\f';
799 if (!truncated) {
800 *cp = '\0';
801 }
802
803 // squash out the byte count
804 cp = buf;
805 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -0700806 while (isdigit(*cp)) {
807 ++cp;
808 }
809 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800810 ++cp;
811 }
812 }
813
814 printf("%s", cp);
815 delete [] buf;
816 exit(0);
817 }
818
819
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800820 if (getLogSize) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700821 exit(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800822 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800823 if (setLogSize || setPruneList) {
824 exit(0);
825 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800826 if (clearLog) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700827 exit(0);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800828 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800829
830 //LOG_EVENT_INT(10, 12345);
831 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
832 //LOG_EVENT_STRING(0, "whassup, doc?");
833
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800834 dev = NULL;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800835 log_device_t unexpected("unexpected", false);
Mark Salyzyn95132e92013-11-22 10:55:48 -0800836 while (1) {
837 struct log_msg log_msg;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800838 log_device_t* d;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800839 int ret = android_logger_list_read(logger_list, &log_msg);
840
841 if (ret == 0) {
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800842 fprintf(stderr, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800843 exit(EXIT_FAILURE);
844 }
845
846 if (ret < 0) {
847 if (ret == -EAGAIN) {
848 break;
849 }
850
851 if (ret == -EIO) {
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800852 fprintf(stderr, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800853 exit(EXIT_FAILURE);
854 }
855 if (ret == -EINVAL) {
856 fprintf(stderr, "read: unexpected length.\n");
857 exit(EXIT_FAILURE);
858 }
Mark Salyzynfff04e32014-03-17 10:36:46 -0700859 perror("logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800860 exit(EXIT_FAILURE);
861 }
862
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800863 for(d = devices; d; d = d->next) {
864 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800865 break;
866 }
867 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800868 if (!d) {
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800869 android::g_devCount = 2; // set to Multiple
870 d = &unexpected;
871 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800872 }
873
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800874 if (dev != d) {
875 dev = d;
876 android::maybePrintStart(dev, printDividers);
877 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800878 if (android::g_printBinary) {
879 android::printBinary(&log_msg);
880 } else {
881 android::processBuffer(dev, &log_msg);
882 }
883 }
884
885 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800886
887 return 0;
888}