blob: 4d7adf13997f8e842eae6fc4061559f98417cf07 [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>
Aristidis Papaioannouf7cc6622014-10-16 22:19:55 -07009#include <math.h>
Mark Salyzyn38fc72c2015-06-02 07:57:16 -070010#include <sched.h>
11#include <signal.h>
12#include <stdarg.h>
Mark Salyzyn12af9652013-12-13 11:10:11 -080013#include <stdio.h>
14#include <stdlib.h>
Mark Salyzyn12af9652013-12-13 11:10:11 -080015#include <string.h>
Traian Schiau9f978602015-04-10 15:51:39 +030016#include <sys/cdefs.h>
Riley Andrews53fe1b02015-06-08 23:36:34 -070017#include <sys/resource.h>
Mark Salyzyn12af9652013-12-13 11:10:11 -080018#include <sys/socket.h>
19#include <sys/stat.h>
Mark Salyzyn3fa657e2015-05-27 07:39:56 -070020#include <sys/types.h>
Mark Salyzyn38fc72c2015-06-02 07:57:16 -070021#include <time.h>
22#include <unistd.h>
Mark Salyzyn12af9652013-12-13 11:10:11 -080023
Mark Salyzyn3fa657e2015-05-27 07:39:56 -070024#include <memory>
25#include <string>
26
27#include <base/file.h>
28#include <base/strings.h>
Mark Salyzyn38fc72c2015-06-02 07:57:16 -070029#include <cutils/sched_policy.h>
Mark Salyzyn12af9652013-12-13 11:10:11 -080030#include <cutils/sockets.h>
Mark Salyzyn38fc72c2015-06-02 07:57:16 -070031#include <log/event_tag_map.h>
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -080032#include <log/log.h>
Mark Salyzyn9addf592014-02-14 16:05:05 -080033#include <log/log_read.h>
Colin Crosse355ded2013-07-23 16:59:20 -070034#include <log/logd.h>
Mark Salyzyn38fc72c2015-06-02 07:57:16 -070035#include <log/logger.h>
Colin Crosse355ded2013-07-23 16:59:20 -070036#include <log/logprint.h>
Riley Andrews53fe1b02015-06-08 23:36:34 -070037#include <utils/threads.h>
The Android Open Source Project190995d2009-03-03 19:32:55 -080038
The Android Open Source Project190995d2009-03-03 19:32:55 -080039#define DEFAULT_MAX_ROTATED_LOGS 4
40
41static AndroidLogFormat * g_logformat;
42
43/* logd prefixes records with a length field */
44#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
45
Joe Onoratod23b9cf2010-02-26 10:04:23 -080046struct log_device_t {
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -080047 const char* device;
Joe Onoratod23b9cf2010-02-26 10:04:23 -080048 bool binary;
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -080049 struct logger *logger;
50 struct logger_list *logger_list;
Joe Onoratod23b9cf2010-02-26 10:04:23 -080051 bool printed;
Joe Onoratod23b9cf2010-02-26 10:04:23 -080052
Joe Onoratod23b9cf2010-02-26 10:04:23 -080053 log_device_t* next;
54
Mark Salyzyna0443902015-02-27 13:41:34 -080055 log_device_t(const char* d, bool b) {
Joe Onoratod23b9cf2010-02-26 10:04:23 -080056 device = d;
57 binary = b;
Joe Onoratod23b9cf2010-02-26 10:04:23 -080058 next = NULL;
59 printed = false;
Traian Schiau9f978602015-04-10 15:51:39 +030060 logger = NULL;
61 logger_list = NULL;
Joe Onoratod23b9cf2010-02-26 10:04:23 -080062 }
Joe Onoratod23b9cf2010-02-26 10:04:23 -080063};
The Android Open Source Project190995d2009-03-03 19:32:55 -080064
65namespace android {
66
67/* Global Variables */
68
69static const char * g_outputFileName = NULL;
Traian Schiau9f978602015-04-10 15:51:39 +030070// 0 means "no log rotation"
71static size_t g_logRotateSizeKBytes = 0;
72// 0 means "unbounded"
73static size_t g_maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS;
The Android Open Source Project190995d2009-03-03 19:32:55 -080074static int g_outFD = -1;
Traian Schiau9f978602015-04-10 15:51:39 +030075static size_t g_outByteCount = 0;
The Android Open Source Project190995d2009-03-03 19:32:55 -080076static int g_printBinary = 0;
Mark Salyzyn784d64f2015-02-26 14:33:35 -080077static int g_devCount = 0; // >1 means multiple
The Android Open Source Project190995d2009-03-03 19:32:55 -080078
Traian Schiau9f978602015-04-10 15:51:39 +030079__noreturn static void logcat_panic(bool showHelp, const char *fmt, ...) __printflike(2,3);
80
The Android Open Source Project190995d2009-03-03 19:32:55 -080081static int openLogFile (const char *pathname)
82{
Edwin Vaneff5c94d2012-08-13 12:55:07 -040083 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Project190995d2009-03-03 19:32:55 -080084}
85
86static void rotateLogs()
87{
88 int err;
89
90 // Can't rotate logs if we're not outputting to a file
91 if (g_outputFileName == NULL) {
92 return;
93 }
94
95 close(g_outFD);
96
Aristidis Papaioannouf7cc6622014-10-16 22:19:55 -070097 // Compute the maximum number of digits needed to count up to g_maxRotatedLogs in decimal.
98 // eg: g_maxRotatedLogs == 30 -> log10(30) == 1.477 -> maxRotationCountDigits == 2
99 int maxRotationCountDigits =
100 (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
101
The Android Open Source Project190995d2009-03-03 19:32:55 -0800102 for (int i = g_maxRotatedLogs ; i > 0 ; i--) {
103 char *file0, *file1;
104
Aristidis Papaioannouf7cc6622014-10-16 22:19:55 -0700105 asprintf(&file1, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800106
107 if (i - 1 == 0) {
108 asprintf(&file0, "%s", g_outputFileName);
109 } else {
Aristidis Papaioannouf7cc6622014-10-16 22:19:55 -0700110 asprintf(&file0, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800111 }
112
Traian Schiau9f978602015-04-10 15:51:39 +0300113 if (!file0 || !file1) {
114 perror("while rotating log files");
115 break;
116 }
117
118 err = rename(file0, file1);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800119
120 if (err < 0 && errno != ENOENT) {
121 perror("while rotating log files");
122 }
123
124 free(file1);
125 free(file0);
126 }
127
Traian Schiau9f978602015-04-10 15:51:39 +0300128 g_outFD = openLogFile(g_outputFileName);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800129
130 if (g_outFD < 0) {
Traian Schiau9f978602015-04-10 15:51:39 +0300131 logcat_panic(false, "couldn't open output file");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800132 }
133
134 g_outByteCount = 0;
135
136}
137
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800138void printBinary(struct log_msg *buf)
The Android Open Source Project190995d2009-03-03 19:32:55 -0800139{
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800140 size_t size = buf->len();
141
142 TEMP_FAILURE_RETRY(write(g_outFD, buf, size));
The Android Open Source Project190995d2009-03-03 19:32:55 -0800143}
144
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800145static void processBuffer(log_device_t* dev, struct log_msg *buf)
The Android Open Source Project190995d2009-03-03 19:32:55 -0800146{
Mathias Agopianb9218fb2010-03-17 16:10:26 -0700147 int bytesWritten = 0;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800148 int err;
149 AndroidLogEntry entry;
150 char binaryMsgBuf[1024];
151
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800152 if (dev->binary) {
Mark Salyzyn784d64f2015-02-26 14:33:35 -0800153 static bool hasOpenedEventTagMap = false;
154 static EventTagMap *eventTagMap = NULL;
155
156 if (!eventTagMap && !hasOpenedEventTagMap) {
157 eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
158 hasOpenedEventTagMap = true;
159 }
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800160 err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry,
Mark Salyzyn784d64f2015-02-26 14:33:35 -0800161 eventTagMap,
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800162 binaryMsgBuf,
163 sizeof(binaryMsgBuf));
The Android Open Source Project190995d2009-03-03 19:32:55 -0800164 //printf(">>> pri=%d len=%d msg='%s'\n",
165 // entry.priority, entry.messageLen, entry.message);
166 } else {
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800167 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800168 }
Joe Onorato400da4a2010-03-01 09:11:54 -0800169 if (err < 0) {
The Android Open Source Project190995d2009-03-03 19:32:55 -0800170 goto error;
Joe Onorato400da4a2010-03-01 09:11:54 -0800171 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800172
Joe Onorato400da4a2010-03-01 09:11:54 -0800173 if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) {
Joe Onorato400da4a2010-03-01 09:11:54 -0800174 bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry);
175
176 if (bytesWritten < 0) {
Traian Schiau9f978602015-04-10 15:51:39 +0300177 logcat_panic(false, "output error");
Joe Onorato400da4a2010-03-01 09:11:54 -0800178 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800179 }
180
181 g_outByteCount += bytesWritten;
182
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800183 if (g_logRotateSizeKBytes > 0
The Android Open Source Project190995d2009-03-03 19:32:55 -0800184 && (g_outByteCount / 1024) >= g_logRotateSizeKBytes
185 ) {
186 rotateLogs();
187 }
188
189error:
190 //fprintf (stderr, "Error processing record\n");
191 return;
192}
193
Mark Salyzyn1ff18092015-01-26 13:41:33 -0800194static void maybePrintStart(log_device_t* dev, bool printDividers) {
195 if (!dev->printed || printDividers) {
Dan Egnorcce2b082010-03-11 20:32:17 -0800196 if (g_devCount > 1 && !g_printBinary) {
197 char buf[1024];
Mark Salyzyn1ff18092015-01-26 13:41:33 -0800198 snprintf(buf, sizeof(buf), "--------- %s %s\n",
199 dev->printed ? "switch to" : "beginning of",
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800200 dev->device);
Dan Egnorcce2b082010-03-11 20:32:17 -0800201 if (write(g_outFD, buf, strlen(buf)) < 0) {
Traian Schiau9f978602015-04-10 15:51:39 +0300202 logcat_panic(false, "output error");
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800203 }
204 }
Mark Salyzyn1ff18092015-01-26 13:41:33 -0800205 dev->printed = true;
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800206 }
207}
208
The Android Open Source Project190995d2009-03-03 19:32:55 -0800209static void setupOutput()
210{
211
212 if (g_outputFileName == NULL) {
213 g_outFD = STDOUT_FILENO;
214
215 } else {
Mark Salyzyn38fc72c2015-06-02 07:57:16 -0700216 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
217 fprintf(stderr, "failed to set background scheduling policy\n");
218 }
219
220 struct sched_param param;
221 memset(&param, 0, sizeof(param));
222 if (sched_setscheduler((pid_t) 0, SCHED_BATCH, &param) < 0) {
223 fprintf(stderr, "failed to set to batch scheduler\n");
224 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800225
Riley Andrews53fe1b02015-06-08 23:36:34 -0700226 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
227 fprintf(stderr, "failed set to priority\n");
228 }
229
The Android Open Source Project190995d2009-03-03 19:32:55 -0800230 g_outFD = openLogFile (g_outputFileName);
231
232 if (g_outFD < 0) {
Traian Schiau9f978602015-04-10 15:51:39 +0300233 logcat_panic(false, "couldn't open output file");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800234 }
235
Mark Salyzyn38fc72c2015-06-02 07:57:16 -0700236 struct stat statbuf;
Traian Schiau9f978602015-04-10 15:51:39 +0300237 if (fstat(g_outFD, &statbuf) == -1) {
238 close(g_outFD);
239 logcat_panic(false, "couldn't get output file stat\n");
240 }
241
242 if ((size_t) statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
243 close(g_outFD);
244 logcat_panic(false, "invalid output file stat\n");
245 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800246
247 g_outByteCount = statbuf.st_size;
248 }
249}
250
251static void show_help(const char *cmd)
252{
253 fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd);
254
255 fprintf(stderr, "options include:\n"
256 " -s Set default filter to silent.\n"
Mark Salyzyn438b8222015-03-09 09:32:56 -0700257 " Like specifying filterspec '*:S'\n"
Mark Salyzyn3fa657e2015-05-27 07:39:56 -0700258 " -f <filename> Log to file. Default is stdout\n"
Traian Schiau9f978602015-04-10 15:51:39 +0300259 " -r <kbytes> Rotate log every kbytes. Requires -f\n"
The Android Open Source Project190995d2009-03-03 19:32:55 -0800260 " -n <count> Sets max number of rotated logs to <count>, default 4\n"
Pierre Zurek507ea562010-10-17 22:39:37 +0200261 " -v <format> Sets the log print format, where <format> is:\n\n"
Mark Salyzyn4d0473f2015-08-31 15:53:41 -0700262 " brief color epoch long monotonic printable process raw\n"
263 " tag thread threadtime time usec UTC year zone\n\n"
Mark Salyzyn1ff18092015-01-26 13:41:33 -0800264 " -D print dividers between each log buffer\n"
The Android Open Source Project190995d2009-03-03 19:32:55 -0800265 " -c clear (flush) the entire log and exit\n"
266 " -d dump the log and then exit (don't block)\n"
Dan Egnorcce2b082010-03-11 20:32:17 -0800267 " -t <count> print only the most recent <count> lines (implies -d)\n"
Mark Salyzyn1ef48182014-09-01 10:41:33 -0700268 " -t '<time>' print most recent lines since specified time (implies -d)\n"
Mark Salyzyn2256e2f2013-12-09 13:47:00 -0800269 " -T <count> print only the most recent <count> lines (does not imply -d)\n"
Mark Salyzyn1ef48182014-09-01 10:41:33 -0700270 " -T '<time>' print most recent lines since specified time (not imply -d)\n"
Mark Salyzyneb0456d2015-08-31 08:01:33 -0700271 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
Mark Salyzyn4d0473f2015-08-31 15:53:41 -0700272 " 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n"
The Android Open Source Project190995d2009-03-03 19:32:55 -0800273 " -g get the size of the log's ring buffer and exit\n"
Mark Salyzyn66460fc2014-12-15 10:01:31 -0800274 " -L dump logs from prior to last reboot\n"
Mark Salyzynd774bce2014-02-06 14:48:50 -0800275 " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n"
Mark Salyzyn855c7302014-04-07 14:58:08 -0700276 " 'events', 'crash' or 'all'. Multiple -b parameters are\n"
277 " allowed and results are interleaved. The default is\n"
278 " -b main -b system -b crash.\n"
Mark Salyzynd774bce2014-02-06 14:48:50 -0800279 " -B output the log in binary.\n"
Mark Salyzyne00fbe52014-04-11 13:49:43 -0700280 " -S output statistics.\n"
281 " -G <size> set size of log ring buffer, may suffix with K or M.\n"
282 " -p print prune white and ~black list. Service is specified as\n"
283 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
284 " with ~, otherwise weighed for longevity if unadorned. All\n"
285 " other pruning activity is oldest first. Special case ~!\n"
286 " represents an automatic quicker pruning for the noisiest\n"
287 " UID as determined by the current statistics.\n"
288 " -P '<list> ...' set prune white and ~black list, using same format as\n"
289 " printed above. Must be quoted.\n");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800290
291 fprintf(stderr,"\nfilterspecs are a series of \n"
292 " <tag>[:priority]\n\n"
293 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzyn438b8222015-03-09 09:32:56 -0700294 " V Verbose (default for <tag>)\n"
295 " D Debug (default for '*')\n"
The Android Open Source Project190995d2009-03-03 19:32:55 -0800296 " I Info\n"
297 " W Warn\n"
298 " E Error\n"
299 " F Fatal\n"
Mark Salyzyn438b8222015-03-09 09:32:56 -0700300 " S Silent (suppress all output)\n"
301 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
302 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
303 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
304 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
305 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyndd569af2014-09-16 09:15:15 -0700306 "or defaults to \"threadtime\"\n\n");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800307}
308
The Android Open Source Project190995d2009-03-03 19:32:55 -0800309static int setLogFormat(const char * formatString)
310{
311 static AndroidLogPrintFormat format;
312
313 format = android_log_formatFromString(formatString);
314
315 if (format == FORMAT_OFF) {
316 // FORMAT_OFF means invalid string
317 return -1;
318 }
319
Mark Salyzyn7661bad2015-05-06 08:40:40 -0700320 return android_log_setPrintFormat(g_logformat, format);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800321}
322
Mark Salyzyn4e756fb2014-05-06 07:34:59 -0700323static const char multipliers[][2] = {
324 { "" },
325 { "K" },
326 { "M" },
327 { "G" }
328};
329
330static unsigned long value_of_size(unsigned long value)
331{
332 for (unsigned i = 0;
333 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
334 value /= 1024, ++i) ;
335 return value;
336}
337
338static const char *multiplier_of_size(unsigned long value)
339{
340 unsigned i;
341 for (i = 0;
342 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
343 value /= 1024, ++i) ;
344 return multipliers[i];
345}
346
Traian Schiau9f978602015-04-10 15:51:39 +0300347/*String to unsigned int, returns -1 if it fails*/
348static bool getSizeTArg(char *ptr, size_t *val, size_t min = 0,
349 size_t max = SIZE_MAX)
350{
351 char *endp;
352 errno = 0;
353 size_t ret = (size_t) strtoll(ptr, &endp, 0);
354
355 if (endp[0] != '\0' || errno != 0 ) {
356 return false;
357 }
358
359 if (ret > max || ret < min) {
360 return false;
361 }
362
363 *val = ret;
364 return true;
365}
366
367static void logcat_panic(bool showHelp, const char *fmt, ...)
368{
Mark Salyzynf4fbfad2015-04-13 09:27:57 -0700369 va_list args;
370 va_start(args, fmt);
371 vfprintf(stderr, fmt, args);
372 va_end(args);
Traian Schiau9f978602015-04-10 15:51:39 +0300373
374 if (showHelp) {
375 show_help(getprogname());
376 }
377
378 exit(EXIT_FAILURE);
379}
380
Mark Salyzyneb0456d2015-08-31 08:01:33 -0700381static char *parseTime(log_time &t, const char *cp) {
382
383 char *ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
384 if (ep) {
385 return ep;
386 }
Mark Salyzyn4d0473f2015-08-31 15:53:41 -0700387 ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
388 if (ep) {
389 return ep;
390 }
391 return t.strptime(cp, "%s.%q");
Mark Salyzyneb0456d2015-08-31 08:01:33 -0700392}
Mark Salyzyn3fa657e2015-05-27 07:39:56 -0700393
394// Find last logged line in gestalt of all matching existing output files
395static log_time lastLogTime(char *outputFileName) {
396 log_time retval(log_time::EPOCH);
397 if (!outputFileName) {
398 return retval;
399 }
400
401 log_time now(CLOCK_REALTIME);
Mark Salyzyn0e2fe422015-09-08 08:56:32 -0700402 bool monotonic = android_log_timestamp() == 'm';
403 if (monotonic) {
404 now = log_time(CLOCK_MONOTONIC);
405 }
Mark Salyzyn3fa657e2015-05-27 07:39:56 -0700406
407 std::string directory;
408 char *file = strrchr(outputFileName, '/');
409 if (!file) {
410 directory = ".";
411 file = outputFileName;
412 } else {
413 *file = '\0';
414 directory = outputFileName;
415 *file = '/';
416 ++file;
417 }
418 size_t len = strlen(file);
419 log_time modulo(0, NS_PER_SEC);
420 std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(directory.c_str()), closedir);
421 struct dirent *dp;
422 while ((dp = readdir(dir.get())) != NULL) {
423 if ((dp->d_type != DT_REG)
Mark Salyzyn0e2fe422015-09-08 08:56:32 -0700424 // If we are using realtime, check all files that match the
425 // basename for latest time. If we are using monotonic time
426 // then only check the main file because time cycles on
427 // every reboot.
428 || strncmp(dp->d_name, file, len + monotonic)
Mark Salyzyn3fa657e2015-05-27 07:39:56 -0700429 || (dp->d_name[len]
430 && ((dp->d_name[len] != '.')
431 || !isdigit(dp->d_name[len+1])))) {
432 continue;
433 }
434
435 std::string file_name = directory;
436 file_name += "/";
437 file_name += dp->d_name;
438 std::string file;
439 if (!android::base::ReadFileToString(file_name, &file)) {
440 continue;
441 }
442
443 bool found = false;
444 for (const auto& line : android::base::Split(file, "\n")) {
445 log_time t(log_time::EPOCH);
Mark Salyzyneb0456d2015-08-31 08:01:33 -0700446 char *ep = parseTime(t, line.c_str());
Mark Salyzyn3fa657e2015-05-27 07:39:56 -0700447 if (!ep || (*ep != ' ')) {
448 continue;
449 }
450 // determine the time precision of the logs (eg: msec or usec)
451 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
452 if (t.tv_nsec % (mod * 10)) {
453 modulo.tv_nsec = mod;
454 break;
455 }
456 }
457 // We filter any times later than current as we may not have the
458 // year stored with each log entry. Also, since it is possible for
459 // entries to be recorded out of order (very rare) we select the
460 // maximum we find just in case.
461 if ((t < now) && (t > retval)) {
462 retval = t;
463 found = true;
464 }
465 }
466 // We count on the basename file to be the definitive end, so stop here.
467 if (!dp->d_name[len] && found) {
468 break;
469 }
470 }
471 if (retval == log_time::EPOCH) {
472 return retval;
473 }
474 // tail_time prints matching or higher, round up by the modulo to prevent
475 // a replay of the last entry we have just checked.
476 retval += modulo;
477 return retval;
478}
479
Traian Schiau9f978602015-04-10 15:51:39 +0300480} /* namespace android */
481
482
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800483int main(int argc, char **argv)
The Android Open Source Project190995d2009-03-03 19:32:55 -0800484{
Traian Schiau9f978602015-04-10 15:51:39 +0300485 using namespace android;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800486 int err;
487 int hasSetLogFormat = 0;
488 int clearLog = 0;
489 int getLogSize = 0;
Mark Salyzync89839a2014-02-11 12:29:31 -0800490 unsigned long setLogSize = 0;
491 int getPruneList = 0;
492 char *setPruneList = NULL;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800493 int printStatistics = 0;
Mark Salyzyn4bdf2532015-01-26 10:46:44 -0800494 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800495 const char *forceFilters = NULL;
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800496 log_device_t* devices = NULL;
497 log_device_t* dev;
Mark Salyzyn1ff18092015-01-26 13:41:33 -0800498 bool printDividers = false;
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800499 struct logger_list *logger_list;
Traian Schiau9f978602015-04-10 15:51:39 +0300500 size_t tail_lines = 0;
Mark Salyzyn9addf592014-02-14 16:05:05 -0800501 log_time tail_time(log_time::EPOCH);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800502
Mark Salyzyn12af9652013-12-13 11:10:11 -0800503 signal(SIGPIPE, exit);
504
The Android Open Source Project190995d2009-03-03 19:32:55 -0800505 g_logformat = android_log_format_new();
506
The Android Open Source Project190995d2009-03-03 19:32:55 -0800507 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
Traian Schiau9f978602015-04-10 15:51:39 +0300508 show_help(argv[0]);
509 return EXIT_SUCCESS;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800510 }
511
512 for (;;) {
513 int ret;
514
Traian Schiau9f978602015-04-10 15:51:39 +0300515 ret = getopt(argc, argv, ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800516
517 if (ret < 0) {
518 break;
519 }
520
521 switch(ret) {
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800522 case 's':
The Android Open Source Project190995d2009-03-03 19:32:55 -0800523 // default to all silent
524 android_log_addFilterRule(g_logformat, "*:s");
525 break;
526
527 case 'c':
528 clearLog = 1;
Mark Salyzyn4bdf2532015-01-26 10:46:44 -0800529 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800530 break;
531
Mark Salyzyn66460fc2014-12-15 10:01:31 -0800532 case 'L':
533 mode |= ANDROID_LOG_PSTORE;
534 break;
535
The Android Open Source Project190995d2009-03-03 19:32:55 -0800536 case 'd':
Mark Salyzyn4bdf2532015-01-26 10:46:44 -0800537 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800538 break;
539
Dan Egnorcce2b082010-03-11 20:32:17 -0800540 case 't':
Mark Salyzyn4bdf2532015-01-26 10:46:44 -0800541 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn2256e2f2013-12-09 13:47:00 -0800542 /* FALLTHRU */
543 case 'T':
Mark Salyzyn9addf592014-02-14 16:05:05 -0800544 if (strspn(optarg, "0123456789") != strlen(optarg)) {
Mark Salyzyneb0456d2015-08-31 08:01:33 -0700545 char *cp = parseTime(tail_time, optarg);
Mark Salyzyn9addf592014-02-14 16:05:05 -0800546 if (!cp) {
Mark Salyzyneb0456d2015-08-31 08:01:33 -0700547 logcat_panic(false, "-%c \"%s\" not in time format\n",
548 ret, optarg);
Mark Salyzyn9addf592014-02-14 16:05:05 -0800549 }
550 if (*cp) {
551 char c = *cp;
552 *cp = '\0';
553 fprintf(stderr,
554 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
555 ret, optarg, c, cp + 1);
556 *cp = c;
557 }
558 } else {
Traian Schiau9f978602015-04-10 15:51:39 +0300559 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzyn9addf592014-02-14 16:05:05 -0800560 fprintf(stderr,
561 "WARNING: -%c %s invalid, setting to 1\n",
562 ret, optarg);
563 tail_lines = 1;
564 }
565 }
Dan Egnorcce2b082010-03-11 20:32:17 -0800566 break;
567
Mark Salyzyn1ff18092015-01-26 13:41:33 -0800568 case 'D':
569 printDividers = true;
570 break;
571
The Android Open Source Project190995d2009-03-03 19:32:55 -0800572 case 'g':
573 getLogSize = 1;
574 break;
575
Mark Salyzync89839a2014-02-11 12:29:31 -0800576 case 'G': {
Traian Schiau9f978602015-04-10 15:51:39 +0300577 char *cp;
578 if (strtoll(optarg, &cp, 0) > 0) {
579 setLogSize = strtoll(optarg, &cp, 0);
580 } else {
581 setLogSize = 0;
Mark Salyzync89839a2014-02-11 12:29:31 -0800582 }
583
584 switch(*cp) {
585 case 'g':
586 case 'G':
587 setLogSize *= 1024;
588 /* FALLTHRU */
589 case 'm':
590 case 'M':
591 setLogSize *= 1024;
592 /* FALLTHRU */
593 case 'k':
594 case 'K':
595 setLogSize *= 1024;
596 /* FALLTHRU */
597 case '\0':
598 break;
599
600 default:
601 setLogSize = 0;
602 }
603
604 if (!setLogSize) {
605 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
Traian Schiau9f978602015-04-10 15:51:39 +0300606 return EXIT_FAILURE;
Mark Salyzync89839a2014-02-11 12:29:31 -0800607 }
608 }
609 break;
610
611 case 'p':
612 getPruneList = 1;
613 break;
614
615 case 'P':
616 setPruneList = optarg;
617 break;
618
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800619 case 'b': {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800620 if (strcmp(optarg, "all") == 0) {
621 while (devices) {
622 dev = devices;
623 devices = dev->next;
624 delete dev;
625 }
626
Mark Salyzyn65777692014-10-10 15:25:38 -0700627 devices = dev = NULL;
Traian Schiau9f978602015-04-10 15:51:39 +0300628 g_devCount = 0;
Mark Salyzyn65777692014-10-10 15:25:38 -0700629 for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
630 const char *name = android_log_id_to_name((log_id_t)i);
631 log_id_t log_id = android_name_to_log_id(name);
632
633 if (log_id != (log_id_t)i) {
634 continue;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800635 }
Mark Salyzyn65777692014-10-10 15:25:38 -0700636
637 bool binary = strcmp(name, "events") == 0;
Mark Salyzyna0443902015-02-27 13:41:34 -0800638 log_device_t* d = new log_device_t(name, binary);
Mark Salyzyn65777692014-10-10 15:25:38 -0700639
640 if (dev) {
641 dev->next = d;
642 dev = d;
643 } else {
644 devices = dev = d;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800645 }
Traian Schiau9f978602015-04-10 15:51:39 +0300646 g_devCount++;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800647 }
648 break;
649 }
650
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800651 bool binary = strcmp(optarg, "events") == 0;
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800652
653 if (devices) {
654 dev = devices;
655 while (dev->next) {
656 dev = dev->next;
657 }
Mark Salyzyna0443902015-02-27 13:41:34 -0800658 dev->next = new log_device_t(optarg, binary);
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800659 } else {
Mark Salyzyna0443902015-02-27 13:41:34 -0800660 devices = new log_device_t(optarg, binary);
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800661 }
Traian Schiau9f978602015-04-10 15:51:39 +0300662 g_devCount++;
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800663 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800664 break;
665
666 case 'B':
Traian Schiau9f978602015-04-10 15:51:39 +0300667 g_printBinary = 1;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800668 break;
669
670 case 'f':
Mark Salyzyn518c3b72015-10-06 08:59:02 -0700671 if ((tail_time == log_time::EPOCH) && (tail_lines == 0)) {
Mark Salyzyn3fa657e2015-05-27 07:39:56 -0700672 tail_time = lastLogTime(optarg);
673 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800674 // redirect output to a file
Traian Schiau9f978602015-04-10 15:51:39 +0300675 g_outputFileName = optarg;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800676 break;
677
678 case 'r':
Traian Schiau9f978602015-04-10 15:51:39 +0300679 if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) {
680 logcat_panic(true, "Invalid parameter %s to -r\n", optarg);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800681 }
682 break;
683
684 case 'n':
Traian Schiau9f978602015-04-10 15:51:39 +0300685 if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) {
686 logcat_panic(true, "Invalid parameter %s to -n\n", optarg);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800687 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800688 break;
689
690 case 'v':
691 err = setLogFormat (optarg);
692 if (err < 0) {
Traian Schiau9f978602015-04-10 15:51:39 +0300693 logcat_panic(true, "Invalid parameter %s to -v\n", optarg);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800694 }
Mark Salyzyn7661bad2015-05-06 08:40:40 -0700695 hasSetLogFormat |= err;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800696 break;
697
698 case 'Q':
699 /* this is a *hidden* option used to start a version of logcat */
700 /* in an emulated device only. it basically looks for androidboot.logcat= */
701 /* on the kernel command line. If something is found, it extracts a log filter */
702 /* and uses it to run the program. If nothing is found, the program should */
703 /* quit immediately */
704#define KERNEL_OPTION "androidboot.logcat="
705#define CONSOLE_OPTION "androidboot.console="
706 {
707 int fd;
708 char* logcat;
709 char* console;
710 int force_exit = 1;
711 static char cmdline[1024];
712
713 fd = open("/proc/cmdline", O_RDONLY);
714 if (fd >= 0) {
715 int n = read(fd, cmdline, sizeof(cmdline)-1 );
716 if (n < 0) n = 0;
717 cmdline[n] = 0;
718 close(fd);
719 } else {
720 cmdline[0] = 0;
721 }
722
723 logcat = strstr( cmdline, KERNEL_OPTION );
724 console = strstr( cmdline, CONSOLE_OPTION );
725 if (logcat != NULL) {
726 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
727 char* q = strpbrk( p, " \t\n\r" );;
728
729 if (q != NULL)
730 *q = 0;
731
732 forceFilters = p;
733 force_exit = 0;
734 }
735 /* if nothing found or invalid filters, exit quietly */
Traian Schiau9f978602015-04-10 15:51:39 +0300736 if (force_exit) {
737 return EXIT_SUCCESS;
738 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800739
740 /* redirect our output to the emulator console */
741 if (console) {
742 char* p = console + sizeof(CONSOLE_OPTION)-1;
743 char* q = strpbrk( p, " \t\n\r" );
744 char devname[64];
745 int len;
746
747 if (q != NULL) {
748 len = q - p;
749 } else
750 len = strlen(p);
751
752 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
753 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
754 if (len < (int)sizeof(devname)) {
755 fd = open( devname, O_WRONLY );
756 if (fd >= 0) {
757 dup2(fd, 1);
758 dup2(fd, 2);
759 close(fd);
760 }
761 }
762 }
763 }
764 break;
765
Mark Salyzynd774bce2014-02-06 14:48:50 -0800766 case 'S':
767 printStatistics = 1;
768 break;
769
Traian Schiau9f978602015-04-10 15:51:39 +0300770 case ':':
771 logcat_panic(true, "Option -%c needs an argument\n", optopt);
772 break;
773
The Android Open Source Project190995d2009-03-03 19:32:55 -0800774 default:
Traian Schiau9f978602015-04-10 15:51:39 +0300775 logcat_panic(true, "Unrecognized Option %c\n", optopt);
776 break;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800777 }
778 }
779
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800780 if (!devices) {
Mark Salyzyna0443902015-02-27 13:41:34 -0800781 dev = devices = new log_device_t("main", false);
Traian Schiau9f978602015-04-10 15:51:39 +0300782 g_devCount = 1;
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800783 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyna0443902015-02-27 13:41:34 -0800784 dev = dev->next = new log_device_t("system", false);
Traian Schiau9f978602015-04-10 15:51:39 +0300785 g_devCount++;
Joe Onorato400da4a2010-03-01 09:11:54 -0800786 }
Mark Salyzyn855c7302014-04-07 14:58:08 -0700787 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyna0443902015-02-27 13:41:34 -0800788 dev = dev->next = new log_device_t("crash", false);
Traian Schiau9f978602015-04-10 15:51:39 +0300789 g_devCount++;
Mark Salyzyn855c7302014-04-07 14:58:08 -0700790 }
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800791 }
792
Traian Schiau9f978602015-04-10 15:51:39 +0300793 if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) {
794 logcat_panic(true, "-r requires -f as well\n");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800795 }
796
Traian Schiau9f978602015-04-10 15:51:39 +0300797 setupOutput();
The Android Open Source Project190995d2009-03-03 19:32:55 -0800798
799 if (hasSetLogFormat == 0) {
800 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
801
802 if (logFormat != NULL) {
803 err = setLogFormat(logFormat);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800804 if (err < 0) {
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800805 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Project190995d2009-03-03 19:32:55 -0800806 logFormat);
807 }
Mark Salyzyndd569af2014-09-16 09:15:15 -0700808 } else {
809 setLogFormat("threadtime");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800810 }
811 }
812
813 if (forceFilters) {
814 err = android_log_addFilterString(g_logformat, forceFilters);
815 if (err < 0) {
Traian Schiau9f978602015-04-10 15:51:39 +0300816 logcat_panic(false, "Invalid filter expression in logcat args\n");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800817 }
818 } else if (argc == optind) {
819 // Add from environment variable
820 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
821
822 if (env_tags_orig != NULL) {
823 err = android_log_addFilterString(g_logformat, env_tags_orig);
824
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800825 if (err < 0) {
Traian Schiau9f978602015-04-10 15:51:39 +0300826 logcat_panic(true,
827 "Invalid filter expression in ANDROID_LOG_TAGS\n");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800828 }
829 }
830 } else {
831 // Add from commandline
832 for (int i = optind ; i < argc ; i++) {
833 err = android_log_addFilterString(g_logformat, argv[i]);
834
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800835 if (err < 0) {
Traian Schiau9f978602015-04-10 15:51:39 +0300836 logcat_panic(true, "Invalid filter expression '%s'\n", argv[i]);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800837 }
838 }
839 }
840
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800841 dev = devices;
Mark Salyzyn9addf592014-02-14 16:05:05 -0800842 if (tail_time != log_time::EPOCH) {
843 logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
844 } else {
845 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
846 }
Mark Salyzyn7ccdb902015-09-16 15:34:00 -0700847 const char *openDeviceFail = NULL;
848 const char *clearFail = NULL;
849 const char *setSizeFail = NULL;
850 const char *getSizeFail = NULL;
851 // We have three orthogonal actions below to clear, set log size and
852 // get log size. All sharing the same iteration loop.
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800853 while (dev) {
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800854 dev->logger_list = logger_list;
855 dev->logger = android_logger_open(logger_list,
856 android_name_to_log_id(dev->device));
857 if (!dev->logger) {
Mark Salyzyn7ccdb902015-09-16 15:34:00 -0700858 openDeviceFail = openDeviceFail ?: dev->device;
859 dev = dev->next;
860 continue;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800861 }
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800862
863 if (clearLog) {
Mark Salyzyn7ccdb902015-09-16 15:34:00 -0700864 if (android_logger_clear(dev->logger)) {
865 clearFail = clearFail ?: dev->device;
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800866 }
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800867 }
868
Mark Salyzyn7ccdb902015-09-16 15:34:00 -0700869 if (setLogSize) {
870 if (android_logger_set_log_size(dev->logger, setLogSize)) {
871 setSizeFail = setSizeFail ?: dev->device;
872 }
Mark Salyzync89839a2014-02-11 12:29:31 -0800873 }
874
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800875 if (getLogSize) {
Mark Salyzyn7ccdb902015-09-16 15:34:00 -0700876 long size = android_logger_get_log_size(dev->logger);
877 long readable = android_logger_get_log_readable_size(dev->logger);
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800878
Mark Salyzyn7ccdb902015-09-16 15:34:00 -0700879 if ((size < 0) || (readable < 0)) {
880 getSizeFail = getSizeFail ?: dev->device;
881 } else {
882 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
883 "max entry is %db, max payload is %db\n", dev->device,
884 value_of_size(size), multiplier_of_size(size),
885 value_of_size(readable), multiplier_of_size(readable),
886 (int) LOGGER_ENTRY_MAX_LEN,
887 (int) LOGGER_ENTRY_MAX_PAYLOAD);
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800888 }
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800889 }
890
891 dev = dev->next;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800892 }
Mark Salyzyn7ccdb902015-09-16 15:34:00 -0700893 // report any errors in the above loop and exit
894 if (openDeviceFail) {
895 logcat_panic(false, "Unable to open log device '%s'\n", openDeviceFail);
896 }
897 if (clearFail) {
898 logcat_panic(false, "failed to clear the '%s' log\n", clearFail);
899 }
900 if (setSizeFail) {
901 logcat_panic(false, "failed to set the '%s' log size\n", setSizeFail);
902 }
903 if (getSizeFail) {
904 logcat_panic(false, "failed to get the readable '%s' log size",
905 getSizeFail);
906 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800907
Mark Salyzync89839a2014-02-11 12:29:31 -0800908 if (setPruneList) {
Traian Schiau9f978602015-04-10 15:51:39 +0300909 size_t len = strlen(setPruneList);
910 /*extra 32 bytes are needed by android_logger_set_prune_list */
911 size_t bLen = len + 32;
912 char *buf = NULL;
913 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
914 buf[len] = '\0';
915 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
916 logcat_panic(false, "failed to set the prune list");
917 }
918 free(buf);
919 } else {
920 logcat_panic(false, "failed to set the prune list (alloc)");
Mark Salyzync89839a2014-02-11 12:29:31 -0800921 }
922 }
923
Mark Salyzync402bc72014-04-01 17:19:47 -0700924 if (printStatistics || getPruneList) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800925 size_t len = 8192;
926 char *buf;
927
928 for(int retry = 32;
929 (retry >= 0) && ((buf = new char [len]));
Traian Schiau9f978602015-04-10 15:51:39 +0300930 delete [] buf, buf = NULL, --retry) {
Mark Salyzync89839a2014-02-11 12:29:31 -0800931 if (getPruneList) {
932 android_logger_get_prune_list(logger_list, buf, len);
933 } else {
934 android_logger_get_statistics(logger_list, buf, len);
935 }
Mark Salyzynd774bce2014-02-06 14:48:50 -0800936 buf[len-1] = '\0';
Traian Schiau9f978602015-04-10 15:51:39 +0300937 if (atol(buf) < 3) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800938 delete [] buf;
939 buf = NULL;
940 break;
941 }
Traian Schiau9f978602015-04-10 15:51:39 +0300942 size_t ret = atol(buf) + 1;
943 if (ret <= len) {
944 len = ret;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800945 break;
946 }
Traian Schiau9f978602015-04-10 15:51:39 +0300947 len = ret;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800948 }
949
950 if (!buf) {
Traian Schiau9f978602015-04-10 15:51:39 +0300951 logcat_panic(false, "failed to read data");
Mark Salyzynd774bce2014-02-06 14:48:50 -0800952 }
953
954 // remove trailing FF
955 char *cp = buf + len - 1;
956 *cp = '\0';
957 bool truncated = *--cp != '\f';
958 if (!truncated) {
959 *cp = '\0';
960 }
961
962 // squash out the byte count
963 cp = buf;
964 if (!truncated) {
Mark Salyzyn63a4ad52014-03-21 13:12:16 -0700965 while (isdigit(*cp)) {
966 ++cp;
967 }
968 if (*cp == '\n') {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800969 ++cp;
970 }
971 }
972
973 printf("%s", cp);
974 delete [] buf;
Traian Schiau9f978602015-04-10 15:51:39 +0300975 return EXIT_SUCCESS;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800976 }
977
978
The Android Open Source Project190995d2009-03-03 19:32:55 -0800979 if (getLogSize) {
Traian Schiau9f978602015-04-10 15:51:39 +0300980 return EXIT_SUCCESS;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800981 }
Mark Salyzync89839a2014-02-11 12:29:31 -0800982 if (setLogSize || setPruneList) {
Traian Schiau9f978602015-04-10 15:51:39 +0300983 return EXIT_SUCCESS;
Mark Salyzync89839a2014-02-11 12:29:31 -0800984 }
Joe Onorato400da4a2010-03-01 09:11:54 -0800985 if (clearLog) {
Traian Schiau9f978602015-04-10 15:51:39 +0300986 return EXIT_SUCCESS;
Joe Onorato400da4a2010-03-01 09:11:54 -0800987 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800988
989 //LOG_EVENT_INT(10, 12345);
990 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
991 //LOG_EVENT_STRING(0, "whassup, doc?");
992
Mark Salyzyn1ff18092015-01-26 13:41:33 -0800993 dev = NULL;
Mark Salyzyna0443902015-02-27 13:41:34 -0800994 log_device_t unexpected("unexpected", false);
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800995 while (1) {
996 struct log_msg log_msg;
Mark Salyzyn1ff18092015-01-26 13:41:33 -0800997 log_device_t* d;
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800998 int ret = android_logger_list_read(logger_list, &log_msg);
999
1000 if (ret == 0) {
Traian Schiau9f978602015-04-10 15:51:39 +03001001 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001002 }
1003
1004 if (ret < 0) {
1005 if (ret == -EAGAIN) {
1006 break;
1007 }
1008
1009 if (ret == -EIO) {
Traian Schiau9f978602015-04-10 15:51:39 +03001010 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001011 }
1012 if (ret == -EINVAL) {
Traian Schiau9f978602015-04-10 15:51:39 +03001013 logcat_panic(false, "read: unexpected length.\n");
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001014 }
Traian Schiau9f978602015-04-10 15:51:39 +03001015 logcat_panic(false, "logcat read failure");
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001016 }
1017
Mark Salyzyn1ff18092015-01-26 13:41:33 -08001018 for(d = devices; d; d = d->next) {
1019 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001020 break;
1021 }
1022 }
Mark Salyzyn1ff18092015-01-26 13:41:33 -08001023 if (!d) {
Traian Schiau9f978602015-04-10 15:51:39 +03001024 g_devCount = 2; // set to Multiple
Mark Salyzyn784d64f2015-02-26 14:33:35 -08001025 d = &unexpected;
1026 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001027 }
1028
Mark Salyzyn1ff18092015-01-26 13:41:33 -08001029 if (dev != d) {
1030 dev = d;
Traian Schiau9f978602015-04-10 15:51:39 +03001031 maybePrintStart(dev, printDividers);
Mark Salyzyn1ff18092015-01-26 13:41:33 -08001032 }
Traian Schiau9f978602015-04-10 15:51:39 +03001033 if (g_printBinary) {
1034 printBinary(&log_msg);
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001035 } else {
Traian Schiau9f978602015-04-10 15:51:39 +03001036 processBuffer(dev, &log_msg);
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001037 }
1038 }
1039
1040 android_logger_list_free(logger_list);
The Android Open Source Project190995d2009-03-03 19:32:55 -08001041
Traian Schiau9f978602015-04-10 15:51:39 +03001042 return EXIT_SUCCESS;
The Android Open Source Project190995d2009-03-03 19:32:55 -08001043}