blob: ea109150091b53bf4538efcddbc72c479f5a5b56 [file] [log] [blame]
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001// Copyright 2006-2015 The Android Open Source Project
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08002
Mark Salyzyn65772ca2013-12-13 11:10:11 -08003#include <assert.h>
4#include <ctype.h>
5#include <errno.h>
6#include <fcntl.h>
Aristidis Papaioannoueba73442014-10-16 22:19:55 -07007#include <math.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -08008#include <stdio.h>
9#include <stdlib.h>
10#include <stdarg.h>
11#include <string.h>
12#include <signal.h>
13#include <time.h>
14#include <unistd.h>
15#include <sys/socket.h>
16#include <sys/stat.h>
17#include <arpa/inet.h>
18
19#include <cutils/sockets.h>
Mark Salyzyn95132e92013-11-22 10:55:48 -080020#include <log/log.h>
Mark Salyzynfa3716b2014-02-14 16:05:05 -080021#include <log/log_read.h>
Colin Cross9227bd32013-07-23 16:59:20 -070022#include <log/logger.h>
23#include <log/logd.h>
24#include <log/logprint.h>
25#include <log/event_tag_map.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026
27#define DEFAULT_LOG_ROTATE_SIZE_KBYTES 16
28#define DEFAULT_MAX_ROTATED_LOGS 4
29
30static AndroidLogFormat * g_logformat;
31
32/* logd prefixes records with a length field */
33#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
34
Joe Onorato6fa09a02010-02-26 10:04:23 -080035struct log_device_t {
Mark Salyzyn95132e92013-11-22 10:55:48 -080036 const char* device;
Joe Onorato6fa09a02010-02-26 10:04:23 -080037 bool binary;
Mark Salyzyn95132e92013-11-22 10:55:48 -080038 struct logger *logger;
39 struct logger_list *logger_list;
Joe Onorato6fa09a02010-02-26 10:04:23 -080040 bool printed;
41 char label;
42
Joe Onorato6fa09a02010-02-26 10:04:23 -080043 log_device_t* next;
44
Mark Salyzyn95132e92013-11-22 10:55:48 -080045 log_device_t(const char* d, bool b, char l) {
Joe Onorato6fa09a02010-02-26 10:04:23 -080046 device = d;
47 binary = b;
48 label = l;
49 next = NULL;
50 printed = false;
51 }
Joe Onorato6fa09a02010-02-26 10:04:23 -080052};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
54namespace android {
55
56/* Global Variables */
57
58static const char * g_outputFileName = NULL;
59static int g_logRotateSizeKBytes = 0; // 0 means "no log rotation"
60static int g_maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS; // 0 means "unbounded"
61static int g_outFD = -1;
62static off_t g_outByteCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063static int g_printBinary = 0;
Mark Salyzyn9421b0c2015-02-26 14:33:35 -080064static int g_devCount = 0; // >1 means multiple
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065
66static int openLogFile (const char *pathname)
67{
Edwin Vane80b221c2012-08-13 12:55:07 -040068 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069}
70
71static void rotateLogs()
72{
73 int err;
74
75 // Can't rotate logs if we're not outputting to a file
76 if (g_outputFileName == NULL) {
77 return;
78 }
79
80 close(g_outFD);
81
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070082 // Compute the maximum number of digits needed to count up to g_maxRotatedLogs in decimal.
83 // eg: g_maxRotatedLogs == 30 -> log10(30) == 1.477 -> maxRotationCountDigits == 2
84 int maxRotationCountDigits =
85 (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
86
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087 for (int i = g_maxRotatedLogs ; i > 0 ; i--) {
88 char *file0, *file1;
89
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070090 asprintf(&file1, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091
92 if (i - 1 == 0) {
93 asprintf(&file0, "%s", g_outputFileName);
94 } else {
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070095 asprintf(&file0, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080096 }
97
98 err = rename (file0, file1);
99
100 if (err < 0 && errno != ENOENT) {
101 perror("while rotating log files");
102 }
103
104 free(file1);
105 free(file0);
106 }
107
108 g_outFD = openLogFile (g_outputFileName);
109
110 if (g_outFD < 0) {
111 perror ("couldn't open output file");
112 exit(-1);
113 }
114
115 g_outByteCount = 0;
116
117}
118
Mark Salyzyn95132e92013-11-22 10:55:48 -0800119void printBinary(struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120{
Mark Salyzyn95132e92013-11-22 10:55:48 -0800121 size_t size = buf->len();
122
123 TEMP_FAILURE_RETRY(write(g_outFD, buf, size));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124}
125
Mark Salyzyn95132e92013-11-22 10:55:48 -0800126static void processBuffer(log_device_t* dev, struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127{
Mathias Agopian50844522010-03-17 16:10:26 -0700128 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129 int err;
130 AndroidLogEntry entry;
131 char binaryMsgBuf[1024];
132
Joe Onorato6fa09a02010-02-26 10:04:23 -0800133 if (dev->binary) {
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800134 static bool hasOpenedEventTagMap = false;
135 static EventTagMap *eventTagMap = NULL;
136
137 if (!eventTagMap && !hasOpenedEventTagMap) {
138 eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
139 hasOpenedEventTagMap = true;
140 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800141 err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry,
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800142 eventTagMap,
Mark Salyzyn95132e92013-11-22 10:55:48 -0800143 binaryMsgBuf,
144 sizeof(binaryMsgBuf));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145 //printf(">>> pri=%d len=%d msg='%s'\n",
146 // entry.priority, entry.messageLen, entry.message);
147 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800148 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800150 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151 goto error;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800152 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800154 if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) {
155 if (false && g_devCount > 1) {
156 binaryMsgBuf[0] = dev->label;
157 binaryMsgBuf[1] = ' ';
158 bytesWritten = write(g_outFD, binaryMsgBuf, 2);
159 if (bytesWritten < 0) {
160 perror("output error");
161 exit(-1);
162 }
163 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800165 bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry);
166
167 if (bytesWritten < 0) {
168 perror("output error");
169 exit(-1);
170 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171 }
172
173 g_outByteCount += bytesWritten;
174
Mark Salyzyn95132e92013-11-22 10:55:48 -0800175 if (g_logRotateSizeKBytes > 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 && (g_outByteCount / 1024) >= g_logRotateSizeKBytes
177 ) {
178 rotateLogs();
179 }
180
181error:
182 //fprintf (stderr, "Error processing record\n");
183 return;
184}
185
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800186static void maybePrintStart(log_device_t* dev, bool printDividers) {
187 if (!dev->printed || printDividers) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800188 if (g_devCount > 1 && !g_printBinary) {
189 char buf[1024];
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800190 snprintf(buf, sizeof(buf), "--------- %s %s\n",
191 dev->printed ? "switch to" : "beginning of",
Mark Salyzyn95132e92013-11-22 10:55:48 -0800192 dev->device);
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800193 if (write(g_outFD, buf, strlen(buf)) < 0) {
194 perror("output error");
195 exit(-1);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800196 }
197 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800198 dev->printed = true;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800199 }
200}
201
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202static void setupOutput()
203{
204
205 if (g_outputFileName == NULL) {
206 g_outFD = STDOUT_FILENO;
207
208 } else {
209 struct stat statbuf;
210
211 g_outFD = openLogFile (g_outputFileName);
212
213 if (g_outFD < 0) {
214 perror ("couldn't open output file");
215 exit(-1);
216 }
217
218 fstat(g_outFD, &statbuf);
219
220 g_outByteCount = statbuf.st_size;
221 }
222}
223
224static void show_help(const char *cmd)
225{
226 fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd);
227
228 fprintf(stderr, "options include:\n"
229 " -s Set default filter to silent.\n"
230 " Like specifying filterspec '*:s'\n"
231 " -f <filename> Log to file. Default to stdout\n"
232 " -r [<kbytes>] Rotate log every kbytes. (16 if unspecified). Requires -f\n"
233 " -n <count> Sets max number of rotated logs to <count>, default 4\n"
Pierre Zurekead88fc2010-10-17 22:39:37 +0200234 " -v <format> Sets the log print format, where <format> is:\n\n"
235 " brief color long process raw tag thread threadtime time\n\n"
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800236 " -D print dividers between each log buffer\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237 " -c clear (flush) the entire log and exit\n"
238 " -d dump the log and then exit (don't block)\n"
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800239 " -t <count> print only the most recent <count> lines (implies -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700240 " -t '<time>' print most recent lines since specified time (implies -d)\n"
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800241 " -T <count> print only the most recent <count> lines (does not imply -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700242 " -T '<time>' print most recent lines since specified time (not imply -d)\n"
243 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm'\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 " -g get the size of the log's ring buffer and exit\n"
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800245 " -L dump logs from prior to last reboot\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800246 " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n"
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700247 " 'events', 'crash' or 'all'. Multiple -b parameters are\n"
248 " allowed and results are interleaved. The default is\n"
249 " -b main -b system -b crash.\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800250 " -B output the log in binary.\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700251 " -S output statistics.\n"
252 " -G <size> set size of log ring buffer, may suffix with K or M.\n"
253 " -p print prune white and ~black list. Service is specified as\n"
254 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
255 " with ~, otherwise weighed for longevity if unadorned. All\n"
256 " other pruning activity is oldest first. Special case ~!\n"
257 " represents an automatic quicker pruning for the noisiest\n"
258 " UID as determined by the current statistics.\n"
259 " -P '<list> ...' set prune white and ~black list, using same format as\n"
260 " printed above. Must be quoted.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261
262 fprintf(stderr,"\nfilterspecs are a series of \n"
263 " <tag>[:priority]\n\n"
264 "where <tag> is a log component tag (or * for all) and priority is:\n"
265 " V Verbose\n"
266 " D Debug\n"
267 " I Info\n"
268 " W Warn\n"
269 " E Error\n"
270 " F Fatal\n"
271 " S Silent (supress all output)\n"
272 "\n'*' means '*:d' and <tag> by itself means <tag>:v\n"
273 "\nIf not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.\n"
274 "If no filterspec is found, filter defaults to '*:I'\n"
275 "\nIf not specified with -v, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700276 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277
278
279
280}
281
282
283} /* namespace android */
284
285static int setLogFormat(const char * formatString)
286{
287 static AndroidLogPrintFormat format;
288
289 format = android_log_formatFromString(formatString);
290
291 if (format == FORMAT_OFF) {
292 // FORMAT_OFF means invalid string
293 return -1;
294 }
295
296 android_log_setPrintFormat(g_logformat, format);
297
298 return 0;
299}
300
Mark Salyzyn671e3432014-05-06 07:34:59 -0700301static const char multipliers[][2] = {
302 { "" },
303 { "K" },
304 { "M" },
305 { "G" }
306};
307
308static unsigned long value_of_size(unsigned long value)
309{
310 for (unsigned i = 0;
311 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
312 value /= 1024, ++i) ;
313 return value;
314}
315
316static const char *multiplier_of_size(unsigned long value)
317{
318 unsigned i;
319 for (i = 0;
320 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
321 value /= 1024, ++i) ;
322 return multipliers[i];
323}
324
Joe Onorato6fa09a02010-02-26 10:04:23 -0800325int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327 int err;
328 int hasSetLogFormat = 0;
329 int clearLog = 0;
330 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800331 unsigned long setLogSize = 0;
332 int getPruneList = 0;
333 char *setPruneList = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800334 int printStatistics = 0;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800335 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800337 log_device_t* devices = NULL;
338 log_device_t* dev;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800339 bool printDividers = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800340 struct logger_list *logger_list;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800341 unsigned int tail_lines = 0;
342 log_time tail_time(log_time::EPOCH);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800344 signal(SIGPIPE, exit);
345
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346 g_logformat = android_log_format_new();
347
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
349 android::show_help(argv[0]);
350 exit(0);
351 }
352
353 for (;;) {
354 int ret;
355
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800356 ret = getopt(argc, argv, "cdDLt:T:gG:sQf:r:n:v:b:BSpP:");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800357
358 if (ret < 0) {
359 break;
360 }
361
362 switch(ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800363 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364 // default to all silent
365 android_log_addFilterRule(g_logformat, "*:s");
366 break;
367
368 case 'c':
369 clearLog = 1;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800370 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371 break;
372
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800373 case 'L':
374 mode |= ANDROID_LOG_PSTORE;
375 break;
376
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800377 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800378 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379 break;
380
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800381 case 't':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800382 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800383 /* FALLTHRU */
384 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800385 if (strspn(optarg, "0123456789") != strlen(optarg)) {
386 char *cp = tail_time.strptime(optarg,
387 log_time::default_format);
388 if (!cp) {
389 fprintf(stderr,
390 "ERROR: -%c \"%s\" not in \"%s\" time format\n",
391 ret, optarg, log_time::default_format);
392 exit(1);
393 }
394 if (*cp) {
395 char c = *cp;
396 *cp = '\0';
397 fprintf(stderr,
398 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
399 ret, optarg, c, cp + 1);
400 *cp = c;
401 }
402 } else {
403 tail_lines = atoi(optarg);
404 if (!tail_lines) {
405 fprintf(stderr,
406 "WARNING: -%c %s invalid, setting to 1\n",
407 ret, optarg);
408 tail_lines = 1;
409 }
410 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800411 break;
412
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800413 case 'D':
414 printDividers = true;
415 break;
416
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417 case 'g':
418 getLogSize = 1;
419 break;
420
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800421 case 'G': {
422 // would use atol if not for the multiplier
423 char *cp = optarg;
424 setLogSize = 0;
425 while (('0' <= *cp) && (*cp <= '9')) {
426 setLogSize *= 10;
427 setLogSize += *cp - '0';
428 ++cp;
429 }
430
431 switch(*cp) {
432 case 'g':
433 case 'G':
434 setLogSize *= 1024;
435 /* FALLTHRU */
436 case 'm':
437 case 'M':
438 setLogSize *= 1024;
439 /* FALLTHRU */
440 case 'k':
441 case 'K':
442 setLogSize *= 1024;
443 /* FALLTHRU */
444 case '\0':
445 break;
446
447 default:
448 setLogSize = 0;
449 }
450
451 if (!setLogSize) {
452 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
453 exit(1);
454 }
455 }
456 break;
457
458 case 'p':
459 getPruneList = 1;
460 break;
461
462 case 'P':
463 setPruneList = optarg;
464 break;
465
Joe Onorato6fa09a02010-02-26 10:04:23 -0800466 case 'b': {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800467 if (strcmp(optarg, "all") == 0) {
468 while (devices) {
469 dev = devices;
470 devices = dev->next;
471 delete dev;
472 }
473
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700474 devices = dev = NULL;
475 android::g_devCount = 0;
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700476 for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
477 const char *name = android_log_id_to_name((log_id_t)i);
478 log_id_t log_id = android_name_to_log_id(name);
479
480 if (log_id != (log_id_t)i) {
481 continue;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800482 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700483
484 bool binary = strcmp(name, "events") == 0;
485 log_device_t* d = new log_device_t(name, binary, *name);
486
487 if (dev) {
488 dev->next = d;
489 dev = d;
490 } else {
491 devices = dev = d;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800492 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700493 android::g_devCount++;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800494 }
495 break;
496 }
497
Joe Onorato6fa09a02010-02-26 10:04:23 -0800498 bool binary = strcmp(optarg, "events") == 0;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800499
500 if (devices) {
501 dev = devices;
502 while (dev->next) {
503 dev = dev->next;
504 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800505 dev->next = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800506 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800507 devices = new log_device_t(optarg, binary, optarg[0]);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800508 }
509 android::g_devCount++;
510 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511 break;
512
513 case 'B':
514 android::g_printBinary = 1;
515 break;
516
517 case 'f':
518 // redirect output to a file
519
520 android::g_outputFileName = optarg;
521
522 break;
523
524 case 'r':
Mark Salyzyn95132e92013-11-22 10:55:48 -0800525 if (optarg == NULL) {
526 android::g_logRotateSizeKBytes
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800527 = DEFAULT_LOG_ROTATE_SIZE_KBYTES;
528 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800529 if (!isdigit(optarg[0])) {
530 fprintf(stderr,"Invalid parameter to -r\n");
531 android::show_help(argv[0]);
532 exit(-1);
533 }
534 android::g_logRotateSizeKBytes = atoi(optarg);
535 }
536 break;
537
538 case 'n':
539 if (!isdigit(optarg[0])) {
540 fprintf(stderr,"Invalid parameter to -r\n");
541 android::show_help(argv[0]);
542 exit(-1);
543 }
544
545 android::g_maxRotatedLogs = atoi(optarg);
546 break;
547
548 case 'v':
549 err = setLogFormat (optarg);
550 if (err < 0) {
551 fprintf(stderr,"Invalid parameter to -v\n");
552 android::show_help(argv[0]);
553 exit(-1);
554 }
555
Mark Salyzyn649fc602014-09-16 09:15:15 -0700556 if (strcmp("color", optarg)) { // exception for modifiers
557 hasSetLogFormat = 1;
558 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800559 break;
560
561 case 'Q':
562 /* this is a *hidden* option used to start a version of logcat */
563 /* in an emulated device only. it basically looks for androidboot.logcat= */
564 /* on the kernel command line. If something is found, it extracts a log filter */
565 /* and uses it to run the program. If nothing is found, the program should */
566 /* quit immediately */
567#define KERNEL_OPTION "androidboot.logcat="
568#define CONSOLE_OPTION "androidboot.console="
569 {
570 int fd;
571 char* logcat;
572 char* console;
573 int force_exit = 1;
574 static char cmdline[1024];
575
576 fd = open("/proc/cmdline", O_RDONLY);
577 if (fd >= 0) {
578 int n = read(fd, cmdline, sizeof(cmdline)-1 );
579 if (n < 0) n = 0;
580 cmdline[n] = 0;
581 close(fd);
582 } else {
583 cmdline[0] = 0;
584 }
585
586 logcat = strstr( cmdline, KERNEL_OPTION );
587 console = strstr( cmdline, CONSOLE_OPTION );
588 if (logcat != NULL) {
589 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
590 char* q = strpbrk( p, " \t\n\r" );;
591
592 if (q != NULL)
593 *q = 0;
594
595 forceFilters = p;
596 force_exit = 0;
597 }
598 /* if nothing found or invalid filters, exit quietly */
599 if (force_exit)
600 exit(0);
601
602 /* redirect our output to the emulator console */
603 if (console) {
604 char* p = console + sizeof(CONSOLE_OPTION)-1;
605 char* q = strpbrk( p, " \t\n\r" );
606 char devname[64];
607 int len;
608
609 if (q != NULL) {
610 len = q - p;
611 } else
612 len = strlen(p);
613
614 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
615 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
616 if (len < (int)sizeof(devname)) {
617 fd = open( devname, O_WRONLY );
618 if (fd >= 0) {
619 dup2(fd, 1);
620 dup2(fd, 2);
621 close(fd);
622 }
623 }
624 }
625 }
626 break;
627
Mark Salyzyn34facab2014-02-06 14:48:50 -0800628 case 'S':
629 printStatistics = 1;
630 break;
631
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800632 default:
633 fprintf(stderr,"Unrecognized Option\n");
634 android::show_help(argv[0]);
635 exit(-1);
636 break;
637 }
638 }
639
Joe Onorato6fa09a02010-02-26 10:04:23 -0800640 if (!devices) {
Mark Salyzyn989980c2014-05-14 12:37:22 -0700641 dev = devices = new log_device_t("main", false, 'm');
Joe Onorato6fa09a02010-02-26 10:04:23 -0800642 android::g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800643 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn989980c2014-05-14 12:37:22 -0700644 dev = dev->next = new log_device_t("system", false, 's');
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800645 android::g_devCount++;
646 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700647 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn989980c2014-05-14 12:37:22 -0700648 dev = dev->next = new log_device_t("crash", false, 'c');
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700649 android::g_devCount++;
650 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800651 }
652
Mark Salyzyn95132e92013-11-22 10:55:48 -0800653 if (android::g_logRotateSizeKBytes != 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800654 && android::g_outputFileName == NULL
655 ) {
656 fprintf(stderr,"-r requires -f as well\n");
657 android::show_help(argv[0]);
658 exit(-1);
659 }
660
661 android::setupOutput();
662
663 if (hasSetLogFormat == 0) {
664 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
665
666 if (logFormat != NULL) {
667 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800668 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800669 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800670 logFormat);
671 }
Mark Salyzyn649fc602014-09-16 09:15:15 -0700672 } else {
673 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800674 }
675 }
676
677 if (forceFilters) {
678 err = android_log_addFilterString(g_logformat, forceFilters);
679 if (err < 0) {
680 fprintf (stderr, "Invalid filter expression in -logcat option\n");
681 exit(0);
682 }
683 } else if (argc == optind) {
684 // Add from environment variable
685 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
686
687 if (env_tags_orig != NULL) {
688 err = android_log_addFilterString(g_logformat, env_tags_orig);
689
Mark Salyzyn95132e92013-11-22 10:55:48 -0800690 if (err < 0) {
691 fprintf(stderr, "Invalid filter expression in"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692 " ANDROID_LOG_TAGS\n");
693 android::show_help(argv[0]);
694 exit(-1);
695 }
696 }
697 } else {
698 // Add from commandline
699 for (int i = optind ; i < argc ; i++) {
700 err = android_log_addFilterString(g_logformat, argv[i]);
701
Mark Salyzyn95132e92013-11-22 10:55:48 -0800702 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800703 fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]);
704 android::show_help(argv[0]);
705 exit(-1);
706 }
707 }
708 }
709
Joe Onorato6fa09a02010-02-26 10:04:23 -0800710 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800711 if (tail_time != log_time::EPOCH) {
712 logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
713 } else {
714 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
715 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800716 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800717 dev->logger_list = logger_list;
718 dev->logger = android_logger_open(logger_list,
719 android_name_to_log_id(dev->device));
720 if (!dev->logger) {
721 fprintf(stderr, "Unable to open log device '%s'\n", dev->device);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800722 exit(EXIT_FAILURE);
723 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800724
725 if (clearLog) {
726 int ret;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800727 ret = android_logger_clear(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800728 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700729 perror("failed to clear the log");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800730 exit(EXIT_FAILURE);
731 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800732 }
733
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800734 if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700735 perror("failed to set the log size");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800736 exit(EXIT_FAILURE);
737 }
738
Joe Onorato6fa09a02010-02-26 10:04:23 -0800739 if (getLogSize) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800740 long size, readable;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800741
Mark Salyzyn95132e92013-11-22 10:55:48 -0800742 size = android_logger_get_log_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800743 if (size < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700744 perror("failed to get the log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800745 exit(EXIT_FAILURE);
746 }
747
Mark Salyzyn95132e92013-11-22 10:55:48 -0800748 readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800749 if (readable < 0) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700750 perror("failed to get the readable log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800751 exit(EXIT_FAILURE);
752 }
753
Mark Salyzyn671e3432014-05-06 07:34:59 -0700754 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
Joe Onorato6fa09a02010-02-26 10:04:23 -0800755 "max entry is %db, max payload is %db\n", dev->device,
Mark Salyzyn671e3432014-05-06 07:34:59 -0700756 value_of_size(size), multiplier_of_size(size),
757 value_of_size(readable), multiplier_of_size(readable),
Joe Onorato6fa09a02010-02-26 10:04:23 -0800758 (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
759 }
760
761 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800762 }
763
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800764 if (setPruneList) {
765 size_t len = strlen(setPruneList) + 32; // margin to allow rc
766 char *buf = (char *) malloc(len);
767
768 strcpy(buf, setPruneList);
769 int ret = android_logger_set_prune_list(logger_list, buf, len);
770 free(buf);
771
772 if (ret) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700773 perror("failed to set the prune list");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800774 exit(EXIT_FAILURE);
775 }
776 }
777
Mark Salyzyn1c950472014-04-01 17:19:47 -0700778 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800779 size_t len = 8192;
780 char *buf;
781
782 for(int retry = 32;
783 (retry >= 0) && ((buf = new char [len]));
784 delete [] buf, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800785 if (getPruneList) {
786 android_logger_get_prune_list(logger_list, buf, len);
787 } else {
788 android_logger_get_statistics(logger_list, buf, len);
789 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800790 buf[len-1] = '\0';
791 size_t ret = atol(buf) + 1;
792 if (ret < 4) {
793 delete [] buf;
794 buf = NULL;
795 break;
796 }
797 bool check = ret <= len;
798 len = ret;
799 if (check) {
800 break;
801 }
802 }
803
804 if (!buf) {
Mark Salyzynfff04e32014-03-17 10:36:46 -0700805 perror("failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -0800806 exit(EXIT_FAILURE);
807 }
808
809 // remove trailing FF
810 char *cp = buf + len - 1;
811 *cp = '\0';
812 bool truncated = *--cp != '\f';
813 if (!truncated) {
814 *cp = '\0';
815 }
816
817 // squash out the byte count
818 cp = buf;
819 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -0700820 while (isdigit(*cp)) {
821 ++cp;
822 }
823 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800824 ++cp;
825 }
826 }
827
828 printf("%s", cp);
829 delete [] buf;
830 exit(0);
831 }
832
833
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800834 if (getLogSize) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700835 exit(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800836 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800837 if (setLogSize || setPruneList) {
838 exit(0);
839 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800840 if (clearLog) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700841 exit(0);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800842 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800843
844 //LOG_EVENT_INT(10, 12345);
845 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
846 //LOG_EVENT_STRING(0, "whassup, doc?");
847
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800848 dev = NULL;
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800849 log_device_t unexpected("unexpected", false, '?');
Mark Salyzyn95132e92013-11-22 10:55:48 -0800850 while (1) {
851 struct log_msg log_msg;
Mark Salyzyn7b30ff82015-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) {
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800856 fprintf(stderr, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800857 exit(EXIT_FAILURE);
858 }
859
860 if (ret < 0) {
861 if (ret == -EAGAIN) {
862 break;
863 }
864
865 if (ret == -EIO) {
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800866 fprintf(stderr, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800867 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 Salyzyn7b30ff82015-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 Salyzyn7b30ff82015-01-26 13:41:33 -0800882 if (!d) {
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800883 android::g_devCount = 2; // set to Multiple
884 d = &unexpected;
885 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800886 }
887
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800888 if (dev != d) {
889 dev = d;
890 android::maybePrintStart(dev, printDividers);
891 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800892 if (android::g_printBinary) {
893 android::printBinary(&log_msg);
894 } else {
895 android::processBuffer(dev, &log_msg);
896 }
897 }
898
899 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800900
901 return 0;
902}