blob: 5f73f49a75b811140872bce35c2e7b97bdd7b934 [file] [log] [blame]
Mark Salyzyn1ff18092015-01-26 13:41:33 -08001// Copyright 2006-2015 The Android Open Source Project
The Android Open Source Project190995d2009-03-03 19:32:55 -08002
Mark Salyzyn38fc72c2015-06-02 07:57:16 -07003#include <arpa/inet.h>
Mark Salyzyn12af9652013-12-13 11:10:11 -08004#include <assert.h>
5#include <ctype.h>
Mark Salyzyn3fa657e2015-05-27 07:39:56 -07006#include <dirent.h>
Mark Salyzyn12af9652013-12-13 11:10:11 -08007#include <errno.h>
8#include <fcntl.h>
Kristian Monsen694cdf62015-06-05 14:10:12 -07009#include <getopt.h>
Aristidis Papaioannouf7cc6622014-10-16 22:19:55 -070010#include <math.h>
Mark Salyzyn38fc72c2015-06-02 07:57:16 -070011#include <sched.h>
12#include <signal.h>
13#include <stdarg.h>
Mark Salyzyn12af9652013-12-13 11:10:11 -080014#include <stdio.h>
15#include <stdlib.h>
Mark Salyzyn12af9652013-12-13 11:10:11 -080016#include <string.h>
Traian Schiau9f978602015-04-10 15:51:39 +030017#include <sys/cdefs.h>
Riley Andrews53fe1b02015-06-08 23:36:34 -070018#include <sys/resource.h>
Mark Salyzyn12af9652013-12-13 11:10:11 -080019#include <sys/socket.h>
20#include <sys/stat.h>
Mark Salyzyn3fa657e2015-05-27 07:39:56 -070021#include <sys/types.h>
Mark Salyzyn38fc72c2015-06-02 07:57:16 -070022#include <time.h>
23#include <unistd.h>
Mark Salyzyn12af9652013-12-13 11:10:11 -080024
Mark Salyzyn3fa657e2015-05-27 07:39:56 -070025#include <memory>
26#include <string>
27
Elliott Hughes0a7adf02015-12-04 22:00:26 -080028#include <android-base/file.h>
29#include <android-base/strings.h>
Mark Salyzyn38fc72c2015-06-02 07:57:16 -070030#include <cutils/sched_policy.h>
Mark Salyzyn12af9652013-12-13 11:10:11 -080031#include <cutils/sockets.h>
Mark Salyzyn38fc72c2015-06-02 07:57:16 -070032#include <log/event_tag_map.h>
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -080033#include <log/log.h>
Mark Salyzyn9addf592014-02-14 16:05:05 -080034#include <log/log_read.h>
Colin Crosse355ded2013-07-23 16:59:20 -070035#include <log/logd.h>
Mark Salyzyn38fc72c2015-06-02 07:57:16 -070036#include <log/logger.h>
Colin Crosse355ded2013-07-23 16:59:20 -070037#include <log/logprint.h>
Riley Andrews53fe1b02015-06-08 23:36:34 -070038#include <utils/threads.h>
The Android Open Source Project190995d2009-03-03 19:32:55 -080039
The Android Open Source Project190995d2009-03-03 19:32:55 -080040#define DEFAULT_MAX_ROTATED_LOGS 4
41
42static AndroidLogFormat * g_logformat;
43
44/* logd prefixes records with a length field */
45#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
46
Joe Onoratod23b9cf2010-02-26 10:04:23 -080047struct log_device_t {
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -080048 const char* device;
Joe Onoratod23b9cf2010-02-26 10:04:23 -080049 bool binary;
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -080050 struct logger *logger;
51 struct logger_list *logger_list;
Joe Onoratod23b9cf2010-02-26 10:04:23 -080052 bool printed;
Joe Onoratod23b9cf2010-02-26 10:04:23 -080053
Joe Onoratod23b9cf2010-02-26 10:04:23 -080054 log_device_t* next;
55
Mark Salyzyna0443902015-02-27 13:41:34 -080056 log_device_t(const char* d, bool b) {
Joe Onoratod23b9cf2010-02-26 10:04:23 -080057 device = d;
58 binary = b;
Joe Onoratod23b9cf2010-02-26 10:04:23 -080059 next = NULL;
60 printed = false;
Traian Schiau9f978602015-04-10 15:51:39 +030061 logger = NULL;
62 logger_list = NULL;
Joe Onoratod23b9cf2010-02-26 10:04:23 -080063 }
Joe Onoratod23b9cf2010-02-26 10:04:23 -080064};
The Android Open Source Project190995d2009-03-03 19:32:55 -080065
66namespace android {
67
68/* Global Variables */
69
70static const char * g_outputFileName = NULL;
Traian Schiau9f978602015-04-10 15:51:39 +030071// 0 means "no log rotation"
72static size_t g_logRotateSizeKBytes = 0;
73// 0 means "unbounded"
74static size_t g_maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS;
The Android Open Source Project190995d2009-03-03 19:32:55 -080075static int g_outFD = -1;
Traian Schiau9f978602015-04-10 15:51:39 +030076static size_t g_outByteCount = 0;
The Android Open Source Project190995d2009-03-03 19:32:55 -080077static int g_printBinary = 0;
Mark Salyzyn784d64f2015-02-26 14:33:35 -080078static int g_devCount = 0; // >1 means multiple
The Android Open Source Project190995d2009-03-03 19:32:55 -080079
Traian Schiau9f978602015-04-10 15:51:39 +030080__noreturn static void logcat_panic(bool showHelp, const char *fmt, ...) __printflike(2,3);
81
The Android Open Source Project190995d2009-03-03 19:32:55 -080082static int openLogFile (const char *pathname)
83{
Edwin Vaneff5c94d2012-08-13 12:55:07 -040084 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Project190995d2009-03-03 19:32:55 -080085}
86
87static void rotateLogs()
88{
89 int err;
90
91 // Can't rotate logs if we're not outputting to a file
92 if (g_outputFileName == NULL) {
93 return;
94 }
95
96 close(g_outFD);
97
Aristidis Papaioannouf7cc6622014-10-16 22:19:55 -070098 // Compute the maximum number of digits needed to count up to g_maxRotatedLogs in decimal.
99 // eg: g_maxRotatedLogs == 30 -> log10(30) == 1.477 -> maxRotationCountDigits == 2
100 int maxRotationCountDigits =
101 (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
102
The Android Open Source Project190995d2009-03-03 19:32:55 -0800103 for (int i = g_maxRotatedLogs ; i > 0 ; i--) {
104 char *file0, *file1;
105
Aristidis Papaioannouf7cc6622014-10-16 22:19:55 -0700106 asprintf(&file1, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800107
108 if (i - 1 == 0) {
109 asprintf(&file0, "%s", g_outputFileName);
110 } else {
Aristidis Papaioannouf7cc6622014-10-16 22:19:55 -0700111 asprintf(&file0, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800112 }
113
Traian Schiau9f978602015-04-10 15:51:39 +0300114 if (!file0 || !file1) {
115 perror("while rotating log files");
116 break;
117 }
118
119 err = rename(file0, file1);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800120
121 if (err < 0 && errno != ENOENT) {
122 perror("while rotating log files");
123 }
124
125 free(file1);
126 free(file0);
127 }
128
Traian Schiau9f978602015-04-10 15:51:39 +0300129 g_outFD = openLogFile(g_outputFileName);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800130
131 if (g_outFD < 0) {
Traian Schiau9f978602015-04-10 15:51:39 +0300132 logcat_panic(false, "couldn't open output file");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800133 }
134
135 g_outByteCount = 0;
136
137}
138
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800139void printBinary(struct log_msg *buf)
The Android Open Source Project190995d2009-03-03 19:32:55 -0800140{
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800141 size_t size = buf->len();
142
143 TEMP_FAILURE_RETRY(write(g_outFD, buf, size));
The Android Open Source Project190995d2009-03-03 19:32:55 -0800144}
145
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800146static void processBuffer(log_device_t* dev, struct log_msg *buf)
The Android Open Source Project190995d2009-03-03 19:32:55 -0800147{
Mathias Agopianb9218fb2010-03-17 16:10:26 -0700148 int bytesWritten = 0;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800149 int err;
150 AndroidLogEntry entry;
151 char binaryMsgBuf[1024];
152
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800153 if (dev->binary) {
Mark Salyzyn784d64f2015-02-26 14:33:35 -0800154 static bool hasOpenedEventTagMap = false;
155 static EventTagMap *eventTagMap = NULL;
156
157 if (!eventTagMap && !hasOpenedEventTagMap) {
158 eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
159 hasOpenedEventTagMap = true;
160 }
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800161 err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry,
Mark Salyzyn784d64f2015-02-26 14:33:35 -0800162 eventTagMap,
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800163 binaryMsgBuf,
164 sizeof(binaryMsgBuf));
The Android Open Source Project190995d2009-03-03 19:32:55 -0800165 //printf(">>> pri=%d len=%d msg='%s'\n",
166 // entry.priority, entry.messageLen, entry.message);
167 } else {
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800168 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800169 }
Joe Onorato400da4a2010-03-01 09:11:54 -0800170 if (err < 0) {
The Android Open Source Project190995d2009-03-03 19:32:55 -0800171 goto error;
Joe Onorato400da4a2010-03-01 09:11:54 -0800172 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800173
Joe Onorato400da4a2010-03-01 09:11:54 -0800174 if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) {
Joe Onorato400da4a2010-03-01 09:11:54 -0800175 bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry);
176
177 if (bytesWritten < 0) {
Traian Schiau9f978602015-04-10 15:51:39 +0300178 logcat_panic(false, "output error");
Joe Onorato400da4a2010-03-01 09:11:54 -0800179 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800180 }
181
182 g_outByteCount += bytesWritten;
183
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800184 if (g_logRotateSizeKBytes > 0
The Android Open Source Project190995d2009-03-03 19:32:55 -0800185 && (g_outByteCount / 1024) >= g_logRotateSizeKBytes
186 ) {
187 rotateLogs();
188 }
189
190error:
191 //fprintf (stderr, "Error processing record\n");
192 return;
193}
194
Mark Salyzyn1ff18092015-01-26 13:41:33 -0800195static void maybePrintStart(log_device_t* dev, bool printDividers) {
196 if (!dev->printed || printDividers) {
Dan Egnorcce2b082010-03-11 20:32:17 -0800197 if (g_devCount > 1 && !g_printBinary) {
198 char buf[1024];
Mark Salyzyn1ff18092015-01-26 13:41:33 -0800199 snprintf(buf, sizeof(buf), "--------- %s %s\n",
200 dev->printed ? "switch to" : "beginning of",
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800201 dev->device);
Dan Egnorcce2b082010-03-11 20:32:17 -0800202 if (write(g_outFD, buf, strlen(buf)) < 0) {
Traian Schiau9f978602015-04-10 15:51:39 +0300203 logcat_panic(false, "output error");
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800204 }
205 }
Mark Salyzyn1ff18092015-01-26 13:41:33 -0800206 dev->printed = true;
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800207 }
208}
209
The Android Open Source Project190995d2009-03-03 19:32:55 -0800210static void setupOutput()
211{
212
213 if (g_outputFileName == NULL) {
214 g_outFD = STDOUT_FILENO;
215
216 } else {
Mark Salyzyn38fc72c2015-06-02 07:57:16 -0700217 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
218 fprintf(stderr, "failed to set background scheduling policy\n");
219 }
220
221 struct sched_param param;
222 memset(&param, 0, sizeof(param));
223 if (sched_setscheduler((pid_t) 0, SCHED_BATCH, &param) < 0) {
224 fprintf(stderr, "failed to set to batch scheduler\n");
225 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800226
Riley Andrews53fe1b02015-06-08 23:36:34 -0700227 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
228 fprintf(stderr, "failed set to priority\n");
229 }
230
The Android Open Source Project190995d2009-03-03 19:32:55 -0800231 g_outFD = openLogFile (g_outputFileName);
232
233 if (g_outFD < 0) {
Traian Schiau9f978602015-04-10 15:51:39 +0300234 logcat_panic(false, "couldn't open output file");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800235 }
236
Mark Salyzyn38fc72c2015-06-02 07:57:16 -0700237 struct stat statbuf;
Traian Schiau9f978602015-04-10 15:51:39 +0300238 if (fstat(g_outFD, &statbuf) == -1) {
239 close(g_outFD);
240 logcat_panic(false, "couldn't get output file stat\n");
241 }
242
243 if ((size_t) statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
244 close(g_outFD);
245 logcat_panic(false, "invalid output file stat\n");
246 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800247
248 g_outByteCount = statbuf.st_size;
249 }
250}
251
252static void show_help(const char *cmd)
253{
254 fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd);
255
256 fprintf(stderr, "options include:\n"
257 " -s Set default filter to silent.\n"
Mark Salyzyn438b8222015-03-09 09:32:56 -0700258 " Like specifying filterspec '*:S'\n"
Mark Salyzyn3fa657e2015-05-27 07:39:56 -0700259 " -f <filename> Log to file. Default is stdout\n"
Traian Schiau9f978602015-04-10 15:51:39 +0300260 " -r <kbytes> Rotate log every kbytes. Requires -f\n"
The Android Open Source Project190995d2009-03-03 19:32:55 -0800261 " -n <count> Sets max number of rotated logs to <count>, default 4\n"
Pierre Zurek507ea562010-10-17 22:39:37 +0200262 " -v <format> Sets the log print format, where <format> is:\n\n"
Mark Salyzyn4d0473f2015-08-31 15:53:41 -0700263 " brief color epoch long monotonic printable process raw\n"
264 " tag thread threadtime time usec UTC year zone\n\n"
Mark Salyzyn1ff18092015-01-26 13:41:33 -0800265 " -D print dividers between each log buffer\n"
The Android Open Source Project190995d2009-03-03 19:32:55 -0800266 " -c clear (flush) the entire log and exit\n"
267 " -d dump the log and then exit (don't block)\n"
Dan Egnorcce2b082010-03-11 20:32:17 -0800268 " -t <count> print only the most recent <count> lines (implies -d)\n"
Mark Salyzyn1ef48182014-09-01 10:41:33 -0700269 " -t '<time>' print most recent lines since specified time (implies -d)\n"
Mark Salyzyn2256e2f2013-12-09 13:47:00 -0800270 " -T <count> print only the most recent <count> lines (does not imply -d)\n"
Mark Salyzyn1ef48182014-09-01 10:41:33 -0700271 " -T '<time>' print most recent lines since specified time (not imply -d)\n"
Mark Salyzyneb0456d2015-08-31 08:01:33 -0700272 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
Mark Salyzyn4d0473f2015-08-31 15:53:41 -0700273 " 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n"
The Android Open Source Project190995d2009-03-03 19:32:55 -0800274 " -g get the size of the log's ring buffer and exit\n"
Mark Salyzyn66460fc2014-12-15 10:01:31 -0800275 " -L dump logs from prior to last reboot\n"
Mark Salyzynd774bce2014-02-06 14:48:50 -0800276 " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n"
Mark Salyzyn855c7302014-04-07 14:58:08 -0700277 " 'events', 'crash' or 'all'. Multiple -b parameters are\n"
278 " allowed and results are interleaved. The default is\n"
279 " -b main -b system -b crash.\n"
Mark Salyzynd774bce2014-02-06 14:48:50 -0800280 " -B output the log in binary.\n"
Mark Salyzyne00fbe52014-04-11 13:49:43 -0700281 " -S output statistics.\n"
282 " -G <size> set size of log ring buffer, may suffix with K or M.\n"
283 " -p print prune white and ~black list. Service is specified as\n"
284 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
285 " with ~, otherwise weighed for longevity if unadorned. All\n"
286 " other pruning activity is oldest first. Special case ~!\n"
287 " represents an automatic quicker pruning for the noisiest\n"
288 " UID as determined by the current statistics.\n"
289 " -P '<list> ...' set prune white and ~black list, using same format as\n"
Kristian Monsen694cdf62015-06-05 14:10:12 -0700290 " printed above. Must be quoted.\n"
291 " --pid=<pid> Only prints logs from the given pid.\n");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800292
293 fprintf(stderr,"\nfilterspecs are a series of \n"
294 " <tag>[:priority]\n\n"
295 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzyn438b8222015-03-09 09:32:56 -0700296 " V Verbose (default for <tag>)\n"
297 " D Debug (default for '*')\n"
The Android Open Source Project190995d2009-03-03 19:32:55 -0800298 " I Info\n"
299 " W Warn\n"
300 " E Error\n"
301 " F Fatal\n"
Mark Salyzyn438b8222015-03-09 09:32:56 -0700302 " S Silent (suppress all output)\n"
303 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
304 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
305 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
306 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
307 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyndd569af2014-09-16 09:15:15 -0700308 "or defaults to \"threadtime\"\n\n");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800309}
310
The Android Open Source Project190995d2009-03-03 19:32:55 -0800311static int setLogFormat(const char * formatString)
312{
313 static AndroidLogPrintFormat format;
314
315 format = android_log_formatFromString(formatString);
316
317 if (format == FORMAT_OFF) {
318 // FORMAT_OFF means invalid string
319 return -1;
320 }
321
Mark Salyzyn7661bad2015-05-06 08:40:40 -0700322 return android_log_setPrintFormat(g_logformat, format);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800323}
324
Mark Salyzyn4e756fb2014-05-06 07:34:59 -0700325static const char multipliers[][2] = {
326 { "" },
327 { "K" },
328 { "M" },
329 { "G" }
330};
331
332static unsigned long value_of_size(unsigned long value)
333{
334 for (unsigned i = 0;
335 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
336 value /= 1024, ++i) ;
337 return value;
338}
339
340static const char *multiplier_of_size(unsigned long value)
341{
342 unsigned i;
343 for (i = 0;
344 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
345 value /= 1024, ++i) ;
346 return multipliers[i];
347}
348
Traian Schiau9f978602015-04-10 15:51:39 +0300349/*String to unsigned int, returns -1 if it fails*/
350static bool getSizeTArg(char *ptr, size_t *val, size_t min = 0,
351 size_t max = SIZE_MAX)
352{
Kristian Monsen694cdf62015-06-05 14:10:12 -0700353 if (!ptr) {
Traian Schiau9f978602015-04-10 15:51:39 +0300354 return false;
355 }
356
Kristian Monsen694cdf62015-06-05 14:10:12 -0700357 char *endp;
358 errno = 0;
359 size_t ret = (size_t)strtoll(ptr, &endp, 0);
360
361 if (endp[0] || errno) {
362 return false;
363 }
364
365 if ((ret > max) || (ret < min)) {
Traian Schiau9f978602015-04-10 15:51:39 +0300366 return false;
367 }
368
369 *val = ret;
370 return true;
371}
372
373static void logcat_panic(bool showHelp, const char *fmt, ...)
374{
Mark Salyzynf4fbfad2015-04-13 09:27:57 -0700375 va_list args;
376 va_start(args, fmt);
377 vfprintf(stderr, fmt, args);
378 va_end(args);
Traian Schiau9f978602015-04-10 15:51:39 +0300379
380 if (showHelp) {
381 show_help(getprogname());
382 }
383
384 exit(EXIT_FAILURE);
385}
386
Mark Salyzyneb0456d2015-08-31 08:01:33 -0700387static char *parseTime(log_time &t, const char *cp) {
388
389 char *ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
390 if (ep) {
391 return ep;
392 }
Mark Salyzyn4d0473f2015-08-31 15:53:41 -0700393 ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
394 if (ep) {
395 return ep;
396 }
397 return t.strptime(cp, "%s.%q");
Mark Salyzyneb0456d2015-08-31 08:01:33 -0700398}
Mark Salyzyn3fa657e2015-05-27 07:39:56 -0700399
400// Find last logged line in gestalt of all matching existing output files
401static log_time lastLogTime(char *outputFileName) {
402 log_time retval(log_time::EPOCH);
403 if (!outputFileName) {
404 return retval;
405 }
406
Mark Salyzyn11a3e702015-12-01 15:57:25 -0800407 clockid_t clock_type = android_log_clockid();
408 log_time now(clock_type);
409 bool monotonic = clock_type == CLOCK_MONOTONIC;
Mark Salyzyn3fa657e2015-05-27 07:39:56 -0700410
411 std::string directory;
412 char *file = strrchr(outputFileName, '/');
413 if (!file) {
414 directory = ".";
415 file = outputFileName;
416 } else {
417 *file = '\0';
418 directory = outputFileName;
419 *file = '/';
420 ++file;
421 }
422 size_t len = strlen(file);
423 log_time modulo(0, NS_PER_SEC);
424 std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(directory.c_str()), closedir);
425 struct dirent *dp;
426 while ((dp = readdir(dir.get())) != NULL) {
427 if ((dp->d_type != DT_REG)
Mark Salyzyn0e2fe422015-09-08 08:56:32 -0700428 // If we are using realtime, check all files that match the
429 // basename for latest time. If we are using monotonic time
430 // then only check the main file because time cycles on
431 // every reboot.
432 || strncmp(dp->d_name, file, len + monotonic)
Mark Salyzyn3fa657e2015-05-27 07:39:56 -0700433 || (dp->d_name[len]
434 && ((dp->d_name[len] != '.')
435 || !isdigit(dp->d_name[len+1])))) {
436 continue;
437 }
438
439 std::string file_name = directory;
440 file_name += "/";
441 file_name += dp->d_name;
442 std::string file;
443 if (!android::base::ReadFileToString(file_name, &file)) {
444 continue;
445 }
446
447 bool found = false;
448 for (const auto& line : android::base::Split(file, "\n")) {
449 log_time t(log_time::EPOCH);
Mark Salyzyneb0456d2015-08-31 08:01:33 -0700450 char *ep = parseTime(t, line.c_str());
Mark Salyzyn3fa657e2015-05-27 07:39:56 -0700451 if (!ep || (*ep != ' ')) {
452 continue;
453 }
454 // determine the time precision of the logs (eg: msec or usec)
455 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
456 if (t.tv_nsec % (mod * 10)) {
457 modulo.tv_nsec = mod;
458 break;
459 }
460 }
461 // We filter any times later than current as we may not have the
462 // year stored with each log entry. Also, since it is possible for
463 // entries to be recorded out of order (very rare) we select the
464 // maximum we find just in case.
465 if ((t < now) && (t > retval)) {
466 retval = t;
467 found = true;
468 }
469 }
470 // We count on the basename file to be the definitive end, so stop here.
471 if (!dp->d_name[len] && found) {
472 break;
473 }
474 }
475 if (retval == log_time::EPOCH) {
476 return retval;
477 }
478 // tail_time prints matching or higher, round up by the modulo to prevent
479 // a replay of the last entry we have just checked.
480 retval += modulo;
481 return retval;
482}
483
Traian Schiau9f978602015-04-10 15:51:39 +0300484} /* namespace android */
485
486
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800487int main(int argc, char **argv)
The Android Open Source Project190995d2009-03-03 19:32:55 -0800488{
Traian Schiau9f978602015-04-10 15:51:39 +0300489 using namespace android;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800490 int err;
491 int hasSetLogFormat = 0;
492 int clearLog = 0;
493 int getLogSize = 0;
Mark Salyzync89839a2014-02-11 12:29:31 -0800494 unsigned long setLogSize = 0;
495 int getPruneList = 0;
496 char *setPruneList = NULL;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800497 int printStatistics = 0;
Mark Salyzyn4bdf2532015-01-26 10:46:44 -0800498 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800499 const char *forceFilters = NULL;
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800500 log_device_t* devices = NULL;
501 log_device_t* dev;
Mark Salyzyn1ff18092015-01-26 13:41:33 -0800502 bool printDividers = false;
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800503 struct logger_list *logger_list;
Traian Schiau9f978602015-04-10 15:51:39 +0300504 size_t tail_lines = 0;
Mark Salyzyn9addf592014-02-14 16:05:05 -0800505 log_time tail_time(log_time::EPOCH);
Kristian Monsen694cdf62015-06-05 14:10:12 -0700506 size_t pid = 0;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800507
Mark Salyzyn12af9652013-12-13 11:10:11 -0800508 signal(SIGPIPE, exit);
509
The Android Open Source Project190995d2009-03-03 19:32:55 -0800510 g_logformat = android_log_format_new();
511
The Android Open Source Project190995d2009-03-03 19:32:55 -0800512 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
Traian Schiau9f978602015-04-10 15:51:39 +0300513 show_help(argv[0]);
514 return EXIT_SUCCESS;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800515 }
516
517 for (;;) {
518 int ret;
519
Kristian Monsen694cdf62015-06-05 14:10:12 -0700520 int option_index = 0;
521 static const char pid_str[] = "pid";
522 static const struct option long_options[] = {
523 { pid_str, required_argument, NULL, 0 },
524 { NULL, 0, NULL, 0 }
525 };
526
527 ret = getopt_long(argc, argv, ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:",
528 long_options, &option_index);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800529
530 if (ret < 0) {
531 break;
532 }
533
Kristian Monsen694cdf62015-06-05 14:10:12 -0700534 switch (ret) {
535 case 0:
536 // One of the long options
537 if (long_options[option_index].name == pid_str) {
538 // ToDo: determine runtime PID_MAX?
539 if (!getSizeTArg(optarg, &pid, 1)) {
540 logcat_panic(true, "%s %s out of range\n",
541 long_options[option_index].name, optarg);
542 }
543 break;
544 }
545 break;
546
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800547 case 's':
The Android Open Source Project190995d2009-03-03 19:32:55 -0800548 // default to all silent
549 android_log_addFilterRule(g_logformat, "*:s");
550 break;
551
552 case 'c':
553 clearLog = 1;
Mark Salyzyn4bdf2532015-01-26 10:46:44 -0800554 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800555 break;
556
Mark Salyzyn66460fc2014-12-15 10:01:31 -0800557 case 'L':
558 mode |= ANDROID_LOG_PSTORE;
559 break;
560
The Android Open Source Project190995d2009-03-03 19:32:55 -0800561 case 'd':
Mark Salyzyn4bdf2532015-01-26 10:46:44 -0800562 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800563 break;
564
Dan Egnorcce2b082010-03-11 20:32:17 -0800565 case 't':
Mark Salyzyn4bdf2532015-01-26 10:46:44 -0800566 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn2256e2f2013-12-09 13:47:00 -0800567 /* FALLTHRU */
568 case 'T':
Mark Salyzyn9addf592014-02-14 16:05:05 -0800569 if (strspn(optarg, "0123456789") != strlen(optarg)) {
Mark Salyzyneb0456d2015-08-31 08:01:33 -0700570 char *cp = parseTime(tail_time, optarg);
Mark Salyzyn9addf592014-02-14 16:05:05 -0800571 if (!cp) {
Mark Salyzyneb0456d2015-08-31 08:01:33 -0700572 logcat_panic(false, "-%c \"%s\" not in time format\n",
573 ret, optarg);
Mark Salyzyn9addf592014-02-14 16:05:05 -0800574 }
575 if (*cp) {
576 char c = *cp;
577 *cp = '\0';
578 fprintf(stderr,
579 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
580 ret, optarg, c, cp + 1);
581 *cp = c;
582 }
583 } else {
Traian Schiau9f978602015-04-10 15:51:39 +0300584 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzyn9addf592014-02-14 16:05:05 -0800585 fprintf(stderr,
586 "WARNING: -%c %s invalid, setting to 1\n",
587 ret, optarg);
588 tail_lines = 1;
589 }
590 }
Dan Egnorcce2b082010-03-11 20:32:17 -0800591 break;
592
Mark Salyzyn1ff18092015-01-26 13:41:33 -0800593 case 'D':
594 printDividers = true;
595 break;
596
The Android Open Source Project190995d2009-03-03 19:32:55 -0800597 case 'g':
598 getLogSize = 1;
599 break;
600
Mark Salyzync89839a2014-02-11 12:29:31 -0800601 case 'G': {
Traian Schiau9f978602015-04-10 15:51:39 +0300602 char *cp;
603 if (strtoll(optarg, &cp, 0) > 0) {
604 setLogSize = strtoll(optarg, &cp, 0);
605 } else {
606 setLogSize = 0;
Mark Salyzync89839a2014-02-11 12:29:31 -0800607 }
608
609 switch(*cp) {
610 case 'g':
611 case 'G':
612 setLogSize *= 1024;
613 /* FALLTHRU */
614 case 'm':
615 case 'M':
616 setLogSize *= 1024;
617 /* FALLTHRU */
618 case 'k':
619 case 'K':
620 setLogSize *= 1024;
621 /* FALLTHRU */
622 case '\0':
623 break;
624
625 default:
626 setLogSize = 0;
627 }
628
629 if (!setLogSize) {
630 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
Traian Schiau9f978602015-04-10 15:51:39 +0300631 return EXIT_FAILURE;
Mark Salyzync89839a2014-02-11 12:29:31 -0800632 }
633 }
634 break;
635
636 case 'p':
637 getPruneList = 1;
638 break;
639
640 case 'P':
641 setPruneList = optarg;
642 break;
643
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800644 case 'b': {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800645 if (strcmp(optarg, "all") == 0) {
646 while (devices) {
647 dev = devices;
648 devices = dev->next;
649 delete dev;
650 }
651
Mark Salyzyn65777692014-10-10 15:25:38 -0700652 devices = dev = NULL;
Traian Schiau9f978602015-04-10 15:51:39 +0300653 g_devCount = 0;
Mark Salyzyn65777692014-10-10 15:25:38 -0700654 for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
655 const char *name = android_log_id_to_name((log_id_t)i);
656 log_id_t log_id = android_name_to_log_id(name);
657
658 if (log_id != (log_id_t)i) {
659 continue;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800660 }
Mark Salyzyn65777692014-10-10 15:25:38 -0700661
662 bool binary = strcmp(name, "events") == 0;
Mark Salyzyna0443902015-02-27 13:41:34 -0800663 log_device_t* d = new log_device_t(name, binary);
Mark Salyzyn65777692014-10-10 15:25:38 -0700664
665 if (dev) {
666 dev->next = d;
667 dev = d;
668 } else {
669 devices = dev = d;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800670 }
Traian Schiau9f978602015-04-10 15:51:39 +0300671 g_devCount++;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800672 }
673 break;
674 }
675
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800676 bool binary = strcmp(optarg, "events") == 0;
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800677
678 if (devices) {
679 dev = devices;
680 while (dev->next) {
681 dev = dev->next;
682 }
Mark Salyzyna0443902015-02-27 13:41:34 -0800683 dev->next = new log_device_t(optarg, binary);
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800684 } else {
Mark Salyzyna0443902015-02-27 13:41:34 -0800685 devices = new log_device_t(optarg, binary);
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800686 }
Traian Schiau9f978602015-04-10 15:51:39 +0300687 g_devCount++;
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800688 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800689 break;
690
691 case 'B':
Traian Schiau9f978602015-04-10 15:51:39 +0300692 g_printBinary = 1;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800693 break;
694
695 case 'f':
Mark Salyzyn518c3b72015-10-06 08:59:02 -0700696 if ((tail_time == log_time::EPOCH) && (tail_lines == 0)) {
Mark Salyzyn3fa657e2015-05-27 07:39:56 -0700697 tail_time = lastLogTime(optarg);
698 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800699 // redirect output to a file
Traian Schiau9f978602015-04-10 15:51:39 +0300700 g_outputFileName = optarg;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800701 break;
702
703 case 'r':
Traian Schiau9f978602015-04-10 15:51:39 +0300704 if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) {
705 logcat_panic(true, "Invalid parameter %s to -r\n", optarg);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800706 }
707 break;
708
709 case 'n':
Traian Schiau9f978602015-04-10 15:51:39 +0300710 if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) {
711 logcat_panic(true, "Invalid parameter %s to -n\n", optarg);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800712 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800713 break;
714
715 case 'v':
716 err = setLogFormat (optarg);
717 if (err < 0) {
Traian Schiau9f978602015-04-10 15:51:39 +0300718 logcat_panic(true, "Invalid parameter %s to -v\n", optarg);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800719 }
Mark Salyzyn7661bad2015-05-06 08:40:40 -0700720 hasSetLogFormat |= err;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800721 break;
722
723 case 'Q':
724 /* this is a *hidden* option used to start a version of logcat */
725 /* in an emulated device only. it basically looks for androidboot.logcat= */
726 /* on the kernel command line. If something is found, it extracts a log filter */
727 /* and uses it to run the program. If nothing is found, the program should */
728 /* quit immediately */
729#define KERNEL_OPTION "androidboot.logcat="
730#define CONSOLE_OPTION "androidboot.console="
731 {
732 int fd;
733 char* logcat;
734 char* console;
735 int force_exit = 1;
736 static char cmdline[1024];
737
738 fd = open("/proc/cmdline", O_RDONLY);
739 if (fd >= 0) {
740 int n = read(fd, cmdline, sizeof(cmdline)-1 );
741 if (n < 0) n = 0;
742 cmdline[n] = 0;
743 close(fd);
744 } else {
745 cmdline[0] = 0;
746 }
747
748 logcat = strstr( cmdline, KERNEL_OPTION );
749 console = strstr( cmdline, CONSOLE_OPTION );
750 if (logcat != NULL) {
751 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
752 char* q = strpbrk( p, " \t\n\r" );;
753
754 if (q != NULL)
755 *q = 0;
756
757 forceFilters = p;
758 force_exit = 0;
759 }
760 /* if nothing found or invalid filters, exit quietly */
Traian Schiau9f978602015-04-10 15:51:39 +0300761 if (force_exit) {
762 return EXIT_SUCCESS;
763 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800764
765 /* redirect our output to the emulator console */
766 if (console) {
767 char* p = console + sizeof(CONSOLE_OPTION)-1;
768 char* q = strpbrk( p, " \t\n\r" );
769 char devname[64];
770 int len;
771
772 if (q != NULL) {
773 len = q - p;
774 } else
775 len = strlen(p);
776
777 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
778 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
779 if (len < (int)sizeof(devname)) {
780 fd = open( devname, O_WRONLY );
781 if (fd >= 0) {
782 dup2(fd, 1);
783 dup2(fd, 2);
784 close(fd);
785 }
786 }
787 }
788 }
789 break;
790
Mark Salyzynd774bce2014-02-06 14:48:50 -0800791 case 'S':
792 printStatistics = 1;
793 break;
794
Traian Schiau9f978602015-04-10 15:51:39 +0300795 case ':':
796 logcat_panic(true, "Option -%c needs an argument\n", optopt);
797 break;
798
The Android Open Source Project190995d2009-03-03 19:32:55 -0800799 default:
Traian Schiau9f978602015-04-10 15:51:39 +0300800 logcat_panic(true, "Unrecognized Option %c\n", optopt);
801 break;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800802 }
803 }
804
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800805 if (!devices) {
Mark Salyzyna0443902015-02-27 13:41:34 -0800806 dev = devices = new log_device_t("main", false);
Traian Schiau9f978602015-04-10 15:51:39 +0300807 g_devCount = 1;
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800808 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyna0443902015-02-27 13:41:34 -0800809 dev = dev->next = new log_device_t("system", false);
Traian Schiau9f978602015-04-10 15:51:39 +0300810 g_devCount++;
Joe Onorato400da4a2010-03-01 09:11:54 -0800811 }
Mark Salyzyn855c7302014-04-07 14:58:08 -0700812 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyna0443902015-02-27 13:41:34 -0800813 dev = dev->next = new log_device_t("crash", false);
Traian Schiau9f978602015-04-10 15:51:39 +0300814 g_devCount++;
Mark Salyzyn855c7302014-04-07 14:58:08 -0700815 }
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800816 }
817
Traian Schiau9f978602015-04-10 15:51:39 +0300818 if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) {
819 logcat_panic(true, "-r requires -f as well\n");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800820 }
821
Traian Schiau9f978602015-04-10 15:51:39 +0300822 setupOutput();
The Android Open Source Project190995d2009-03-03 19:32:55 -0800823
824 if (hasSetLogFormat == 0) {
825 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
826
827 if (logFormat != NULL) {
828 err = setLogFormat(logFormat);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800829 if (err < 0) {
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800830 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Project190995d2009-03-03 19:32:55 -0800831 logFormat);
832 }
Mark Salyzyndd569af2014-09-16 09:15:15 -0700833 } else {
834 setLogFormat("threadtime");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800835 }
836 }
837
838 if (forceFilters) {
839 err = android_log_addFilterString(g_logformat, forceFilters);
840 if (err < 0) {
Traian Schiau9f978602015-04-10 15:51:39 +0300841 logcat_panic(false, "Invalid filter expression in logcat args\n");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800842 }
843 } else if (argc == optind) {
844 // Add from environment variable
845 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
846
847 if (env_tags_orig != NULL) {
848 err = android_log_addFilterString(g_logformat, env_tags_orig);
849
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800850 if (err < 0) {
Traian Schiau9f978602015-04-10 15:51:39 +0300851 logcat_panic(true,
852 "Invalid filter expression in ANDROID_LOG_TAGS\n");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800853 }
854 }
855 } else {
856 // Add from commandline
857 for (int i = optind ; i < argc ; i++) {
858 err = android_log_addFilterString(g_logformat, argv[i]);
859
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800860 if (err < 0) {
Traian Schiau9f978602015-04-10 15:51:39 +0300861 logcat_panic(true, "Invalid filter expression '%s'\n", argv[i]);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800862 }
863 }
864 }
865
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800866 dev = devices;
Mark Salyzyn9addf592014-02-14 16:05:05 -0800867 if (tail_time != log_time::EPOCH) {
Kristian Monsen694cdf62015-06-05 14:10:12 -0700868 logger_list = android_logger_list_alloc_time(mode, tail_time, pid);
Mark Salyzyn9addf592014-02-14 16:05:05 -0800869 } else {
Kristian Monsen694cdf62015-06-05 14:10:12 -0700870 logger_list = android_logger_list_alloc(mode, tail_lines, pid);
Mark Salyzyn9addf592014-02-14 16:05:05 -0800871 }
Mark Salyzyn7ccdb902015-09-16 15:34:00 -0700872 const char *openDeviceFail = NULL;
873 const char *clearFail = NULL;
874 const char *setSizeFail = NULL;
875 const char *getSizeFail = NULL;
876 // We have three orthogonal actions below to clear, set log size and
877 // get log size. All sharing the same iteration loop.
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800878 while (dev) {
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800879 dev->logger_list = logger_list;
880 dev->logger = android_logger_open(logger_list,
881 android_name_to_log_id(dev->device));
882 if (!dev->logger) {
Mark Salyzyn7ccdb902015-09-16 15:34:00 -0700883 openDeviceFail = openDeviceFail ?: dev->device;
884 dev = dev->next;
885 continue;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800886 }
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800887
888 if (clearLog) {
Mark Salyzyn7ccdb902015-09-16 15:34:00 -0700889 if (android_logger_clear(dev->logger)) {
890 clearFail = clearFail ?: dev->device;
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800891 }
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800892 }
893
Mark Salyzyn7ccdb902015-09-16 15:34:00 -0700894 if (setLogSize) {
895 if (android_logger_set_log_size(dev->logger, setLogSize)) {
896 setSizeFail = setSizeFail ?: dev->device;
897 }
Mark Salyzync89839a2014-02-11 12:29:31 -0800898 }
899
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800900 if (getLogSize) {
Mark Salyzyn7ccdb902015-09-16 15:34:00 -0700901 long size = android_logger_get_log_size(dev->logger);
902 long readable = android_logger_get_log_readable_size(dev->logger);
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800903
Mark Salyzyn7ccdb902015-09-16 15:34:00 -0700904 if ((size < 0) || (readable < 0)) {
905 getSizeFail = getSizeFail ?: dev->device;
906 } else {
907 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
908 "max entry is %db, max payload is %db\n", dev->device,
909 value_of_size(size), multiplier_of_size(size),
910 value_of_size(readable), multiplier_of_size(readable),
911 (int) LOGGER_ENTRY_MAX_LEN,
912 (int) LOGGER_ENTRY_MAX_PAYLOAD);
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800913 }
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800914 }
915
916 dev = dev->next;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800917 }
Mark Salyzyn7ccdb902015-09-16 15:34:00 -0700918 // report any errors in the above loop and exit
919 if (openDeviceFail) {
920 logcat_panic(false, "Unable to open log device '%s'\n", openDeviceFail);
921 }
922 if (clearFail) {
923 logcat_panic(false, "failed to clear the '%s' log\n", clearFail);
924 }
925 if (setSizeFail) {
926 logcat_panic(false, "failed to set the '%s' log size\n", setSizeFail);
927 }
928 if (getSizeFail) {
929 logcat_panic(false, "failed to get the readable '%s' log size",
930 getSizeFail);
931 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800932
Mark Salyzync89839a2014-02-11 12:29:31 -0800933 if (setPruneList) {
Traian Schiau9f978602015-04-10 15:51:39 +0300934 size_t len = strlen(setPruneList);
935 /*extra 32 bytes are needed by android_logger_set_prune_list */
936 size_t bLen = len + 32;
937 char *buf = NULL;
938 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
939 buf[len] = '\0';
940 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
941 logcat_panic(false, "failed to set the prune list");
942 }
943 free(buf);
944 } else {
945 logcat_panic(false, "failed to set the prune list (alloc)");
Mark Salyzync89839a2014-02-11 12:29:31 -0800946 }
947 }
948
Mark Salyzync402bc72014-04-01 17:19:47 -0700949 if (printStatistics || getPruneList) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800950 size_t len = 8192;
951 char *buf;
952
953 for(int retry = 32;
954 (retry >= 0) && ((buf = new char [len]));
Traian Schiau9f978602015-04-10 15:51:39 +0300955 delete [] buf, buf = NULL, --retry) {
Mark Salyzync89839a2014-02-11 12:29:31 -0800956 if (getPruneList) {
957 android_logger_get_prune_list(logger_list, buf, len);
958 } else {
959 android_logger_get_statistics(logger_list, buf, len);
960 }
Mark Salyzynd774bce2014-02-06 14:48:50 -0800961 buf[len-1] = '\0';
Traian Schiau9f978602015-04-10 15:51:39 +0300962 if (atol(buf) < 3) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800963 delete [] buf;
964 buf = NULL;
965 break;
966 }
Traian Schiau9f978602015-04-10 15:51:39 +0300967 size_t ret = atol(buf) + 1;
968 if (ret <= len) {
969 len = ret;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800970 break;
971 }
Traian Schiau9f978602015-04-10 15:51:39 +0300972 len = ret;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800973 }
974
975 if (!buf) {
Traian Schiau9f978602015-04-10 15:51:39 +0300976 logcat_panic(false, "failed to read data");
Mark Salyzynd774bce2014-02-06 14:48:50 -0800977 }
978
979 // remove trailing FF
980 char *cp = buf + len - 1;
981 *cp = '\0';
982 bool truncated = *--cp != '\f';
983 if (!truncated) {
984 *cp = '\0';
985 }
986
987 // squash out the byte count
988 cp = buf;
989 if (!truncated) {
Mark Salyzyn63a4ad52014-03-21 13:12:16 -0700990 while (isdigit(*cp)) {
991 ++cp;
992 }
993 if (*cp == '\n') {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800994 ++cp;
995 }
996 }
997
998 printf("%s", cp);
999 delete [] buf;
Traian Schiau9f978602015-04-10 15:51:39 +03001000 return EXIT_SUCCESS;
Mark Salyzynd774bce2014-02-06 14:48:50 -08001001 }
1002
1003
The Android Open Source Project190995d2009-03-03 19:32:55 -08001004 if (getLogSize) {
Traian Schiau9f978602015-04-10 15:51:39 +03001005 return EXIT_SUCCESS;
The Android Open Source Project190995d2009-03-03 19:32:55 -08001006 }
Mark Salyzync89839a2014-02-11 12:29:31 -08001007 if (setLogSize || setPruneList) {
Traian Schiau9f978602015-04-10 15:51:39 +03001008 return EXIT_SUCCESS;
Mark Salyzync89839a2014-02-11 12:29:31 -08001009 }
Joe Onorato400da4a2010-03-01 09:11:54 -08001010 if (clearLog) {
Traian Schiau9f978602015-04-10 15:51:39 +03001011 return EXIT_SUCCESS;
Joe Onorato400da4a2010-03-01 09:11:54 -08001012 }
The Android Open Source Project190995d2009-03-03 19:32:55 -08001013
1014 //LOG_EVENT_INT(10, 12345);
1015 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
1016 //LOG_EVENT_STRING(0, "whassup, doc?");
1017
Mark Salyzyn1ff18092015-01-26 13:41:33 -08001018 dev = NULL;
Mark Salyzyna0443902015-02-27 13:41:34 -08001019 log_device_t unexpected("unexpected", false);
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001020 while (1) {
1021 struct log_msg log_msg;
Mark Salyzyn1ff18092015-01-26 13:41:33 -08001022 log_device_t* d;
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001023 int ret = android_logger_list_read(logger_list, &log_msg);
1024
1025 if (ret == 0) {
Traian Schiau9f978602015-04-10 15:51:39 +03001026 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001027 }
1028
1029 if (ret < 0) {
1030 if (ret == -EAGAIN) {
1031 break;
1032 }
1033
1034 if (ret == -EIO) {
Traian Schiau9f978602015-04-10 15:51:39 +03001035 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001036 }
1037 if (ret == -EINVAL) {
Traian Schiau9f978602015-04-10 15:51:39 +03001038 logcat_panic(false, "read: unexpected length.\n");
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001039 }
Traian Schiau9f978602015-04-10 15:51:39 +03001040 logcat_panic(false, "logcat read failure");
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001041 }
1042
Mark Salyzyn1ff18092015-01-26 13:41:33 -08001043 for(d = devices; d; d = d->next) {
1044 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001045 break;
1046 }
1047 }
Mark Salyzyn1ff18092015-01-26 13:41:33 -08001048 if (!d) {
Traian Schiau9f978602015-04-10 15:51:39 +03001049 g_devCount = 2; // set to Multiple
Mark Salyzyn784d64f2015-02-26 14:33:35 -08001050 d = &unexpected;
1051 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001052 }
1053
Mark Salyzyn1ff18092015-01-26 13:41:33 -08001054 if (dev != d) {
1055 dev = d;
Traian Schiau9f978602015-04-10 15:51:39 +03001056 maybePrintStart(dev, printDividers);
Mark Salyzyn1ff18092015-01-26 13:41:33 -08001057 }
Traian Schiau9f978602015-04-10 15:51:39 +03001058 if (g_printBinary) {
1059 printBinary(&log_msg);
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001060 } else {
Traian Schiau9f978602015-04-10 15:51:39 +03001061 processBuffer(dev, &log_msg);
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001062 }
1063 }
1064
1065 android_logger_list_free(logger_list);
The Android Open Source Project190995d2009-03-03 19:32:55 -08001066
Traian Schiau9f978602015-04-10 15:51:39 +03001067 return EXIT_SUCCESS;
The Android Open Source Project190995d2009-03-03 19:32:55 -08001068}