blob: 0c59f23073c53dc6be56185f00699964edf9864d [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
Elliott Hughes0a7adf02015-12-04 22:00:26 -080027#include <android-base/file.h>
28#include <android-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
Mark Salyzyn11a3e702015-12-01 15:57:25 -0800401 clockid_t clock_type = android_log_clockid();
402 log_time now(clock_type);
403 bool monotonic = clock_type == CLOCK_MONOTONIC;
Mark Salyzyn3fa657e2015-05-27 07:39:56 -0700404
405 std::string directory;
406 char *file = strrchr(outputFileName, '/');
407 if (!file) {
408 directory = ".";
409 file = outputFileName;
410 } else {
411 *file = '\0';
412 directory = outputFileName;
413 *file = '/';
414 ++file;
415 }
416 size_t len = strlen(file);
417 log_time modulo(0, NS_PER_SEC);
418 std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(directory.c_str()), closedir);
419 struct dirent *dp;
420 while ((dp = readdir(dir.get())) != NULL) {
421 if ((dp->d_type != DT_REG)
Mark Salyzyn0e2fe422015-09-08 08:56:32 -0700422 // If we are using realtime, check all files that match the
423 // basename for latest time. If we are using monotonic time
424 // then only check the main file because time cycles on
425 // every reboot.
426 || strncmp(dp->d_name, file, len + monotonic)
Mark Salyzyn3fa657e2015-05-27 07:39:56 -0700427 || (dp->d_name[len]
428 && ((dp->d_name[len] != '.')
429 || !isdigit(dp->d_name[len+1])))) {
430 continue;
431 }
432
433 std::string file_name = directory;
434 file_name += "/";
435 file_name += dp->d_name;
436 std::string file;
437 if (!android::base::ReadFileToString(file_name, &file)) {
438 continue;
439 }
440
441 bool found = false;
442 for (const auto& line : android::base::Split(file, "\n")) {
443 log_time t(log_time::EPOCH);
Mark Salyzyneb0456d2015-08-31 08:01:33 -0700444 char *ep = parseTime(t, line.c_str());
Mark Salyzyn3fa657e2015-05-27 07:39:56 -0700445 if (!ep || (*ep != ' ')) {
446 continue;
447 }
448 // determine the time precision of the logs (eg: msec or usec)
449 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
450 if (t.tv_nsec % (mod * 10)) {
451 modulo.tv_nsec = mod;
452 break;
453 }
454 }
455 // We filter any times later than current as we may not have the
456 // year stored with each log entry. Also, since it is possible for
457 // entries to be recorded out of order (very rare) we select the
458 // maximum we find just in case.
459 if ((t < now) && (t > retval)) {
460 retval = t;
461 found = true;
462 }
463 }
464 // We count on the basename file to be the definitive end, so stop here.
465 if (!dp->d_name[len] && found) {
466 break;
467 }
468 }
469 if (retval == log_time::EPOCH) {
470 return retval;
471 }
472 // tail_time prints matching or higher, round up by the modulo to prevent
473 // a replay of the last entry we have just checked.
474 retval += modulo;
475 return retval;
476}
477
Traian Schiau9f978602015-04-10 15:51:39 +0300478} /* namespace android */
479
480
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800481int main(int argc, char **argv)
The Android Open Source Project190995d2009-03-03 19:32:55 -0800482{
Traian Schiau9f978602015-04-10 15:51:39 +0300483 using namespace android;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800484 int err;
485 int hasSetLogFormat = 0;
486 int clearLog = 0;
487 int getLogSize = 0;
Mark Salyzync89839a2014-02-11 12:29:31 -0800488 unsigned long setLogSize = 0;
489 int getPruneList = 0;
490 char *setPruneList = NULL;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800491 int printStatistics = 0;
Mark Salyzyn4bdf2532015-01-26 10:46:44 -0800492 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800493 const char *forceFilters = NULL;
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800494 log_device_t* devices = NULL;
495 log_device_t* dev;
Mark Salyzyn1ff18092015-01-26 13:41:33 -0800496 bool printDividers = false;
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800497 struct logger_list *logger_list;
Traian Schiau9f978602015-04-10 15:51:39 +0300498 size_t tail_lines = 0;
Mark Salyzyn9addf592014-02-14 16:05:05 -0800499 log_time tail_time(log_time::EPOCH);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800500
Mark Salyzyn12af9652013-12-13 11:10:11 -0800501 signal(SIGPIPE, exit);
502
The Android Open Source Project190995d2009-03-03 19:32:55 -0800503 g_logformat = android_log_format_new();
504
The Android Open Source Project190995d2009-03-03 19:32:55 -0800505 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
Traian Schiau9f978602015-04-10 15:51:39 +0300506 show_help(argv[0]);
507 return EXIT_SUCCESS;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800508 }
509
510 for (;;) {
511 int ret;
512
Traian Schiau9f978602015-04-10 15:51:39 +0300513 ret = getopt(argc, argv, ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800514
515 if (ret < 0) {
516 break;
517 }
518
519 switch(ret) {
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800520 case 's':
The Android Open Source Project190995d2009-03-03 19:32:55 -0800521 // default to all silent
522 android_log_addFilterRule(g_logformat, "*:s");
523 break;
524
525 case 'c':
526 clearLog = 1;
Mark Salyzyn4bdf2532015-01-26 10:46:44 -0800527 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800528 break;
529
Mark Salyzyn66460fc2014-12-15 10:01:31 -0800530 case 'L':
531 mode |= ANDROID_LOG_PSTORE;
532 break;
533
The Android Open Source Project190995d2009-03-03 19:32:55 -0800534 case 'd':
Mark Salyzyn4bdf2532015-01-26 10:46:44 -0800535 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800536 break;
537
Dan Egnorcce2b082010-03-11 20:32:17 -0800538 case 't':
Mark Salyzyn4bdf2532015-01-26 10:46:44 -0800539 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn2256e2f2013-12-09 13:47:00 -0800540 /* FALLTHRU */
541 case 'T':
Mark Salyzyn9addf592014-02-14 16:05:05 -0800542 if (strspn(optarg, "0123456789") != strlen(optarg)) {
Mark Salyzyneb0456d2015-08-31 08:01:33 -0700543 char *cp = parseTime(tail_time, optarg);
Mark Salyzyn9addf592014-02-14 16:05:05 -0800544 if (!cp) {
Mark Salyzyneb0456d2015-08-31 08:01:33 -0700545 logcat_panic(false, "-%c \"%s\" not in time format\n",
546 ret, optarg);
Mark Salyzyn9addf592014-02-14 16:05:05 -0800547 }
548 if (*cp) {
549 char c = *cp;
550 *cp = '\0';
551 fprintf(stderr,
552 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
553 ret, optarg, c, cp + 1);
554 *cp = c;
555 }
556 } else {
Traian Schiau9f978602015-04-10 15:51:39 +0300557 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzyn9addf592014-02-14 16:05:05 -0800558 fprintf(stderr,
559 "WARNING: -%c %s invalid, setting to 1\n",
560 ret, optarg);
561 tail_lines = 1;
562 }
563 }
Dan Egnorcce2b082010-03-11 20:32:17 -0800564 break;
565
Mark Salyzyn1ff18092015-01-26 13:41:33 -0800566 case 'D':
567 printDividers = true;
568 break;
569
The Android Open Source Project190995d2009-03-03 19:32:55 -0800570 case 'g':
571 getLogSize = 1;
572 break;
573
Mark Salyzync89839a2014-02-11 12:29:31 -0800574 case 'G': {
Traian Schiau9f978602015-04-10 15:51:39 +0300575 char *cp;
576 if (strtoll(optarg, &cp, 0) > 0) {
577 setLogSize = strtoll(optarg, &cp, 0);
578 } else {
579 setLogSize = 0;
Mark Salyzync89839a2014-02-11 12:29:31 -0800580 }
581
582 switch(*cp) {
583 case 'g':
584 case 'G':
585 setLogSize *= 1024;
586 /* FALLTHRU */
587 case 'm':
588 case 'M':
589 setLogSize *= 1024;
590 /* FALLTHRU */
591 case 'k':
592 case 'K':
593 setLogSize *= 1024;
594 /* FALLTHRU */
595 case '\0':
596 break;
597
598 default:
599 setLogSize = 0;
600 }
601
602 if (!setLogSize) {
603 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
Traian Schiau9f978602015-04-10 15:51:39 +0300604 return EXIT_FAILURE;
Mark Salyzync89839a2014-02-11 12:29:31 -0800605 }
606 }
607 break;
608
609 case 'p':
610 getPruneList = 1;
611 break;
612
613 case 'P':
614 setPruneList = optarg;
615 break;
616
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800617 case 'b': {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800618 if (strcmp(optarg, "all") == 0) {
619 while (devices) {
620 dev = devices;
621 devices = dev->next;
622 delete dev;
623 }
624
Mark Salyzyn65777692014-10-10 15:25:38 -0700625 devices = dev = NULL;
Traian Schiau9f978602015-04-10 15:51:39 +0300626 g_devCount = 0;
Mark Salyzyn65777692014-10-10 15:25:38 -0700627 for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
628 const char *name = android_log_id_to_name((log_id_t)i);
629 log_id_t log_id = android_name_to_log_id(name);
630
631 if (log_id != (log_id_t)i) {
632 continue;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800633 }
Mark Salyzyn65777692014-10-10 15:25:38 -0700634
635 bool binary = strcmp(name, "events") == 0;
Mark Salyzyna0443902015-02-27 13:41:34 -0800636 log_device_t* d = new log_device_t(name, binary);
Mark Salyzyn65777692014-10-10 15:25:38 -0700637
638 if (dev) {
639 dev->next = d;
640 dev = d;
641 } else {
642 devices = dev = d;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800643 }
Traian Schiau9f978602015-04-10 15:51:39 +0300644 g_devCount++;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800645 }
646 break;
647 }
648
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800649 bool binary = strcmp(optarg, "events") == 0;
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800650
651 if (devices) {
652 dev = devices;
653 while (dev->next) {
654 dev = dev->next;
655 }
Mark Salyzyna0443902015-02-27 13:41:34 -0800656 dev->next = new log_device_t(optarg, binary);
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800657 } else {
Mark Salyzyna0443902015-02-27 13:41:34 -0800658 devices = new log_device_t(optarg, binary);
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800659 }
Traian Schiau9f978602015-04-10 15:51:39 +0300660 g_devCount++;
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800661 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800662 break;
663
664 case 'B':
Traian Schiau9f978602015-04-10 15:51:39 +0300665 g_printBinary = 1;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800666 break;
667
668 case 'f':
Mark Salyzyn518c3b72015-10-06 08:59:02 -0700669 if ((tail_time == log_time::EPOCH) && (tail_lines == 0)) {
Mark Salyzyn3fa657e2015-05-27 07:39:56 -0700670 tail_time = lastLogTime(optarg);
671 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800672 // redirect output to a file
Traian Schiau9f978602015-04-10 15:51:39 +0300673 g_outputFileName = optarg;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800674 break;
675
676 case 'r':
Traian Schiau9f978602015-04-10 15:51:39 +0300677 if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) {
678 logcat_panic(true, "Invalid parameter %s to -r\n", optarg);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800679 }
680 break;
681
682 case 'n':
Traian Schiau9f978602015-04-10 15:51:39 +0300683 if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) {
684 logcat_panic(true, "Invalid parameter %s to -n\n", optarg);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800685 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800686 break;
687
688 case 'v':
689 err = setLogFormat (optarg);
690 if (err < 0) {
Traian Schiau9f978602015-04-10 15:51:39 +0300691 logcat_panic(true, "Invalid parameter %s to -v\n", optarg);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800692 }
Mark Salyzyn7661bad2015-05-06 08:40:40 -0700693 hasSetLogFormat |= err;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800694 break;
695
696 case 'Q':
697 /* this is a *hidden* option used to start a version of logcat */
698 /* in an emulated device only. it basically looks for androidboot.logcat= */
699 /* on the kernel command line. If something is found, it extracts a log filter */
700 /* and uses it to run the program. If nothing is found, the program should */
701 /* quit immediately */
702#define KERNEL_OPTION "androidboot.logcat="
703#define CONSOLE_OPTION "androidboot.console="
704 {
705 int fd;
706 char* logcat;
707 char* console;
708 int force_exit = 1;
709 static char cmdline[1024];
710
711 fd = open("/proc/cmdline", O_RDONLY);
712 if (fd >= 0) {
713 int n = read(fd, cmdline, sizeof(cmdline)-1 );
714 if (n < 0) n = 0;
715 cmdline[n] = 0;
716 close(fd);
717 } else {
718 cmdline[0] = 0;
719 }
720
721 logcat = strstr( cmdline, KERNEL_OPTION );
722 console = strstr( cmdline, CONSOLE_OPTION );
723 if (logcat != NULL) {
724 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
725 char* q = strpbrk( p, " \t\n\r" );;
726
727 if (q != NULL)
728 *q = 0;
729
730 forceFilters = p;
731 force_exit = 0;
732 }
733 /* if nothing found or invalid filters, exit quietly */
Traian Schiau9f978602015-04-10 15:51:39 +0300734 if (force_exit) {
735 return EXIT_SUCCESS;
736 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800737
738 /* redirect our output to the emulator console */
739 if (console) {
740 char* p = console + sizeof(CONSOLE_OPTION)-1;
741 char* q = strpbrk( p, " \t\n\r" );
742 char devname[64];
743 int len;
744
745 if (q != NULL) {
746 len = q - p;
747 } else
748 len = strlen(p);
749
750 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
751 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
752 if (len < (int)sizeof(devname)) {
753 fd = open( devname, O_WRONLY );
754 if (fd >= 0) {
755 dup2(fd, 1);
756 dup2(fd, 2);
757 close(fd);
758 }
759 }
760 }
761 }
762 break;
763
Mark Salyzynd774bce2014-02-06 14:48:50 -0800764 case 'S':
765 printStatistics = 1;
766 break;
767
Traian Schiau9f978602015-04-10 15:51:39 +0300768 case ':':
769 logcat_panic(true, "Option -%c needs an argument\n", optopt);
770 break;
771
The Android Open Source Project190995d2009-03-03 19:32:55 -0800772 default:
Traian Schiau9f978602015-04-10 15:51:39 +0300773 logcat_panic(true, "Unrecognized Option %c\n", optopt);
774 break;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800775 }
776 }
777
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800778 if (!devices) {
Mark Salyzyna0443902015-02-27 13:41:34 -0800779 dev = devices = new log_device_t("main", false);
Traian Schiau9f978602015-04-10 15:51:39 +0300780 g_devCount = 1;
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800781 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyna0443902015-02-27 13:41:34 -0800782 dev = dev->next = new log_device_t("system", false);
Traian Schiau9f978602015-04-10 15:51:39 +0300783 g_devCount++;
Joe Onorato400da4a2010-03-01 09:11:54 -0800784 }
Mark Salyzyn855c7302014-04-07 14:58:08 -0700785 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyna0443902015-02-27 13:41:34 -0800786 dev = dev->next = new log_device_t("crash", false);
Traian Schiau9f978602015-04-10 15:51:39 +0300787 g_devCount++;
Mark Salyzyn855c7302014-04-07 14:58:08 -0700788 }
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800789 }
790
Traian Schiau9f978602015-04-10 15:51:39 +0300791 if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) {
792 logcat_panic(true, "-r requires -f as well\n");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800793 }
794
Traian Schiau9f978602015-04-10 15:51:39 +0300795 setupOutput();
The Android Open Source Project190995d2009-03-03 19:32:55 -0800796
797 if (hasSetLogFormat == 0) {
798 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
799
800 if (logFormat != NULL) {
801 err = setLogFormat(logFormat);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800802 if (err < 0) {
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800803 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Project190995d2009-03-03 19:32:55 -0800804 logFormat);
805 }
Mark Salyzyndd569af2014-09-16 09:15:15 -0700806 } else {
807 setLogFormat("threadtime");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800808 }
809 }
810
811 if (forceFilters) {
812 err = android_log_addFilterString(g_logformat, forceFilters);
813 if (err < 0) {
Traian Schiau9f978602015-04-10 15:51:39 +0300814 logcat_panic(false, "Invalid filter expression in logcat args\n");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800815 }
816 } else if (argc == optind) {
817 // Add from environment variable
818 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
819
820 if (env_tags_orig != NULL) {
821 err = android_log_addFilterString(g_logformat, env_tags_orig);
822
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800823 if (err < 0) {
Traian Schiau9f978602015-04-10 15:51:39 +0300824 logcat_panic(true,
825 "Invalid filter expression in ANDROID_LOG_TAGS\n");
The Android Open Source Project190995d2009-03-03 19:32:55 -0800826 }
827 }
828 } else {
829 // Add from commandline
830 for (int i = optind ; i < argc ; i++) {
831 err = android_log_addFilterString(g_logformat, argv[i]);
832
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800833 if (err < 0) {
Traian Schiau9f978602015-04-10 15:51:39 +0300834 logcat_panic(true, "Invalid filter expression '%s'\n", argv[i]);
The Android Open Source Project190995d2009-03-03 19:32:55 -0800835 }
836 }
837 }
838
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800839 dev = devices;
Mark Salyzyn9addf592014-02-14 16:05:05 -0800840 if (tail_time != log_time::EPOCH) {
841 logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
842 } else {
843 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
844 }
Mark Salyzyn7ccdb902015-09-16 15:34:00 -0700845 const char *openDeviceFail = NULL;
846 const char *clearFail = NULL;
847 const char *setSizeFail = NULL;
848 const char *getSizeFail = NULL;
849 // We have three orthogonal actions below to clear, set log size and
850 // get log size. All sharing the same iteration loop.
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800851 while (dev) {
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800852 dev->logger_list = logger_list;
853 dev->logger = android_logger_open(logger_list,
854 android_name_to_log_id(dev->device));
855 if (!dev->logger) {
Mark Salyzyn7ccdb902015-09-16 15:34:00 -0700856 openDeviceFail = openDeviceFail ?: dev->device;
857 dev = dev->next;
858 continue;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800859 }
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800860
861 if (clearLog) {
Mark Salyzyn7ccdb902015-09-16 15:34:00 -0700862 if (android_logger_clear(dev->logger)) {
863 clearFail = clearFail ?: dev->device;
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800864 }
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800865 }
866
Mark Salyzyn7ccdb902015-09-16 15:34:00 -0700867 if (setLogSize) {
868 if (android_logger_set_log_size(dev->logger, setLogSize)) {
869 setSizeFail = setSizeFail ?: dev->device;
870 }
Mark Salyzync89839a2014-02-11 12:29:31 -0800871 }
872
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800873 if (getLogSize) {
Mark Salyzyn7ccdb902015-09-16 15:34:00 -0700874 long size = android_logger_get_log_size(dev->logger);
875 long readable = android_logger_get_log_readable_size(dev->logger);
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800876
Mark Salyzyn7ccdb902015-09-16 15:34:00 -0700877 if ((size < 0) || (readable < 0)) {
878 getSizeFail = getSizeFail ?: dev->device;
879 } else {
880 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
881 "max entry is %db, max payload is %db\n", dev->device,
882 value_of_size(size), multiplier_of_size(size),
883 value_of_size(readable), multiplier_of_size(readable),
884 (int) LOGGER_ENTRY_MAX_LEN,
885 (int) LOGGER_ENTRY_MAX_PAYLOAD);
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800886 }
Joe Onoratod23b9cf2010-02-26 10:04:23 -0800887 }
888
889 dev = dev->next;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800890 }
Mark Salyzyn7ccdb902015-09-16 15:34:00 -0700891 // report any errors in the above loop and exit
892 if (openDeviceFail) {
893 logcat_panic(false, "Unable to open log device '%s'\n", openDeviceFail);
894 }
895 if (clearFail) {
896 logcat_panic(false, "failed to clear the '%s' log\n", clearFail);
897 }
898 if (setSizeFail) {
899 logcat_panic(false, "failed to set the '%s' log size\n", setSizeFail);
900 }
901 if (getSizeFail) {
902 logcat_panic(false, "failed to get the readable '%s' log size",
903 getSizeFail);
904 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800905
Mark Salyzync89839a2014-02-11 12:29:31 -0800906 if (setPruneList) {
Traian Schiau9f978602015-04-10 15:51:39 +0300907 size_t len = strlen(setPruneList);
908 /*extra 32 bytes are needed by android_logger_set_prune_list */
909 size_t bLen = len + 32;
910 char *buf = NULL;
911 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
912 buf[len] = '\0';
913 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
914 logcat_panic(false, "failed to set the prune list");
915 }
916 free(buf);
917 } else {
918 logcat_panic(false, "failed to set the prune list (alloc)");
Mark Salyzync89839a2014-02-11 12:29:31 -0800919 }
920 }
921
Mark Salyzync402bc72014-04-01 17:19:47 -0700922 if (printStatistics || getPruneList) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800923 size_t len = 8192;
924 char *buf;
925
926 for(int retry = 32;
927 (retry >= 0) && ((buf = new char [len]));
Traian Schiau9f978602015-04-10 15:51:39 +0300928 delete [] buf, buf = NULL, --retry) {
Mark Salyzync89839a2014-02-11 12:29:31 -0800929 if (getPruneList) {
930 android_logger_get_prune_list(logger_list, buf, len);
931 } else {
932 android_logger_get_statistics(logger_list, buf, len);
933 }
Mark Salyzynd774bce2014-02-06 14:48:50 -0800934 buf[len-1] = '\0';
Traian Schiau9f978602015-04-10 15:51:39 +0300935 if (atol(buf) < 3) {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800936 delete [] buf;
937 buf = NULL;
938 break;
939 }
Traian Schiau9f978602015-04-10 15:51:39 +0300940 size_t ret = atol(buf) + 1;
941 if (ret <= len) {
942 len = ret;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800943 break;
944 }
Traian Schiau9f978602015-04-10 15:51:39 +0300945 len = ret;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800946 }
947
948 if (!buf) {
Traian Schiau9f978602015-04-10 15:51:39 +0300949 logcat_panic(false, "failed to read data");
Mark Salyzynd774bce2014-02-06 14:48:50 -0800950 }
951
952 // remove trailing FF
953 char *cp = buf + len - 1;
954 *cp = '\0';
955 bool truncated = *--cp != '\f';
956 if (!truncated) {
957 *cp = '\0';
958 }
959
960 // squash out the byte count
961 cp = buf;
962 if (!truncated) {
Mark Salyzyn63a4ad52014-03-21 13:12:16 -0700963 while (isdigit(*cp)) {
964 ++cp;
965 }
966 if (*cp == '\n') {
Mark Salyzynd774bce2014-02-06 14:48:50 -0800967 ++cp;
968 }
969 }
970
971 printf("%s", cp);
972 delete [] buf;
Traian Schiau9f978602015-04-10 15:51:39 +0300973 return EXIT_SUCCESS;
Mark Salyzynd774bce2014-02-06 14:48:50 -0800974 }
975
976
The Android Open Source Project190995d2009-03-03 19:32:55 -0800977 if (getLogSize) {
Traian Schiau9f978602015-04-10 15:51:39 +0300978 return EXIT_SUCCESS;
The Android Open Source Project190995d2009-03-03 19:32:55 -0800979 }
Mark Salyzync89839a2014-02-11 12:29:31 -0800980 if (setLogSize || setPruneList) {
Traian Schiau9f978602015-04-10 15:51:39 +0300981 return EXIT_SUCCESS;
Mark Salyzync89839a2014-02-11 12:29:31 -0800982 }
Joe Onorato400da4a2010-03-01 09:11:54 -0800983 if (clearLog) {
Traian Schiau9f978602015-04-10 15:51:39 +0300984 return EXIT_SUCCESS;
Joe Onorato400da4a2010-03-01 09:11:54 -0800985 }
The Android Open Source Project190995d2009-03-03 19:32:55 -0800986
987 //LOG_EVENT_INT(10, 12345);
988 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
989 //LOG_EVENT_STRING(0, "whassup, doc?");
990
Mark Salyzyn1ff18092015-01-26 13:41:33 -0800991 dev = NULL;
Mark Salyzyna0443902015-02-27 13:41:34 -0800992 log_device_t unexpected("unexpected", false);
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800993 while (1) {
994 struct log_msg log_msg;
Mark Salyzyn1ff18092015-01-26 13:41:33 -0800995 log_device_t* d;
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -0800996 int ret = android_logger_list_read(logger_list, &log_msg);
997
998 if (ret == 0) {
Traian Schiau9f978602015-04-10 15:51:39 +0300999 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001000 }
1001
1002 if (ret < 0) {
1003 if (ret == -EAGAIN) {
1004 break;
1005 }
1006
1007 if (ret == -EIO) {
Traian Schiau9f978602015-04-10 15:51:39 +03001008 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001009 }
1010 if (ret == -EINVAL) {
Traian Schiau9f978602015-04-10 15:51:39 +03001011 logcat_panic(false, "read: unexpected length.\n");
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001012 }
Traian Schiau9f978602015-04-10 15:51:39 +03001013 logcat_panic(false, "logcat read failure");
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001014 }
1015
Mark Salyzyn1ff18092015-01-26 13:41:33 -08001016 for(d = devices; d; d = d->next) {
1017 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001018 break;
1019 }
1020 }
Mark Salyzyn1ff18092015-01-26 13:41:33 -08001021 if (!d) {
Traian Schiau9f978602015-04-10 15:51:39 +03001022 g_devCount = 2; // set to Multiple
Mark Salyzyn784d64f2015-02-26 14:33:35 -08001023 d = &unexpected;
1024 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001025 }
1026
Mark Salyzyn1ff18092015-01-26 13:41:33 -08001027 if (dev != d) {
1028 dev = d;
Traian Schiau9f978602015-04-10 15:51:39 +03001029 maybePrintStart(dev, printDividers);
Mark Salyzyn1ff18092015-01-26 13:41:33 -08001030 }
Traian Schiau9f978602015-04-10 15:51:39 +03001031 if (g_printBinary) {
1032 printBinary(&log_msg);
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001033 } else {
Traian Schiau9f978602015-04-10 15:51:39 +03001034 processBuffer(dev, &log_msg);
Mark Salyzyn2a8a6aa2013-11-22 10:55:48 -08001035 }
1036 }
1037
1038 android_logger_list_free(logger_list);
The Android Open Source Project190995d2009-03-03 19:32:55 -08001039
Traian Schiau9f978602015-04-10 15:51:39 +03001040 return EXIT_SUCCESS;
The Android Open Source Project190995d2009-03-03 19:32:55 -08001041}