blob: cb9a85bff836ea6b6dce3a56bf9d905959b05451 [file] [log] [blame]
Mark Salyzyn0a890f92015-01-26 13:41:33 -08001// Copyright 2006-2015 The Android Open Source Project
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08002
Mark Salyzynffe87bf2015-06-02 07:57:16 -07003#include <arpa/inet.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -08004#include <assert.h>
5#include <ctype.h>
Mark Salyzyn0acc3ef2015-05-27 07:39:56 -07006#include <dirent.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -08007#include <errno.h>
8#include <fcntl.h>
Aristidis Papaioannoueba73442014-10-16 22:19:55 -07009#include <math.h>
Mark Salyzynffe87bf2015-06-02 07:57:16 -070010#include <sched.h>
11#include <signal.h>
12#include <stdarg.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080013#include <stdio.h>
14#include <stdlib.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080015#include <string.h>
Traian Schiaua4808162015-04-10 15:51:39 +030016#include <sys/cdefs.h>
Riley Andrews99186a42015-06-08 23:36:34 -070017#include <sys/resource.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080018#include <sys/socket.h>
19#include <sys/stat.h>
Mark Salyzyn0acc3ef2015-05-27 07:39:56 -070020#include <sys/types.h>
Mark Salyzynffe87bf2015-06-02 07:57:16 -070021#include <time.h>
22#include <unistd.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080023
Mark Salyzyn0acc3ef2015-05-27 07:39:56 -070024#include <memory>
25#include <string>
26
27#include <base/file.h>
28#include <base/strings.h>
Mark Salyzynffe87bf2015-06-02 07:57:16 -070029#include <cutils/sched_policy.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080030#include <cutils/sockets.h>
Mark Salyzynffe87bf2015-06-02 07:57:16 -070031#include <log/event_tag_map.h>
Mark Salyzyn95132e92013-11-22 10:55:48 -080032#include <log/log.h>
Mark Salyzynfa3716b2014-02-14 16:05:05 -080033#include <log/log_read.h>
Colin Cross9227bd32013-07-23 16:59:20 -070034#include <log/logd.h>
Mark Salyzynffe87bf2015-06-02 07:57:16 -070035#include <log/logger.h>
Colin Cross9227bd32013-07-23 16:59:20 -070036#include <log/logprint.h>
Riley Andrews99186a42015-06-08 23:36:34 -070037#include <utils/threads.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038
The Android Open Source Projectdd7bc332009-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 Onorato6fa09a02010-02-26 10:04:23 -080046struct log_device_t {
Mark Salyzyn95132e92013-11-22 10:55:48 -080047 const char* device;
Joe Onorato6fa09a02010-02-26 10:04:23 -080048 bool binary;
Mark Salyzyn95132e92013-11-22 10:55:48 -080049 struct logger *logger;
50 struct logger_list *logger_list;
Joe Onorato6fa09a02010-02-26 10:04:23 -080051 bool printed;
Joe Onorato6fa09a02010-02-26 10:04:23 -080052
Joe Onorato6fa09a02010-02-26 10:04:23 -080053 log_device_t* next;
54
Mark Salyzyn7ae3cf12015-02-27 13:41:34 -080055 log_device_t(const char* d, bool b) {
Joe Onorato6fa09a02010-02-26 10:04:23 -080056 device = d;
57 binary = b;
Joe Onorato6fa09a02010-02-26 10:04:23 -080058 next = NULL;
59 printed = false;
Traian Schiaua4808162015-04-10 15:51:39 +030060 logger = NULL;
61 logger_list = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -080062 }
Joe Onorato6fa09a02010-02-26 10:04:23 -080063};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064
65namespace android {
66
67/* Global Variables */
68
69static const char * g_outputFileName = NULL;
Traian Schiaua4808162015-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 Projectdd7bc332009-03-03 19:32:55 -080074static int g_outFD = -1;
Traian Schiaua4808162015-04-10 15:51:39 +030075static size_t g_outByteCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076static int g_printBinary = 0;
Mark Salyzyn51a71422015-02-26 14:33:35 -080077static int g_devCount = 0; // >1 means multiple
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078
Traian Schiaua4808162015-04-10 15:51:39 +030079__noreturn static void logcat_panic(bool showHelp, const char *fmt, ...) __printflike(2,3);
80
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081static int openLogFile (const char *pathname)
82{
Edwin Vane80b221c2012-08-13 12:55:07 -040083 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Projectdd7bc332009-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 Papaioannoueba73442014-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 Projectdd7bc332009-03-03 19:32:55 -0800102 for (int i = g_maxRotatedLogs ; i > 0 ; i--) {
103 char *file0, *file1;
104
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700105 asprintf(&file1, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106
107 if (i - 1 == 0) {
108 asprintf(&file0, "%s", g_outputFileName);
109 } else {
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700110 asprintf(&file0, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111 }
112
Traian Schiaua4808162015-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 Projectdd7bc332009-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 Schiaua4808162015-04-10 15:51:39 +0300128 g_outFD = openLogFile(g_outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129
130 if (g_outFD < 0) {
Traian Schiaua4808162015-04-10 15:51:39 +0300131 logcat_panic(false, "couldn't open output file");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 }
133
134 g_outByteCount = 0;
135
136}
137
Mark Salyzyn95132e92013-11-22 10:55:48 -0800138void printBinary(struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139{
Mark Salyzyn95132e92013-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 Projectdd7bc332009-03-03 19:32:55 -0800143}
144
Mark Salyzyn95132e92013-11-22 10:55:48 -0800145static void processBuffer(log_device_t* dev, struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146{
Mathias Agopian50844522010-03-17 16:10:26 -0700147 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148 int err;
149 AndroidLogEntry entry;
150 char binaryMsgBuf[1024];
151
Joe Onorato6fa09a02010-02-26 10:04:23 -0800152 if (dev->binary) {
Mark Salyzyn51a71422015-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 Salyzyn95132e92013-11-22 10:55:48 -0800160 err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry,
Mark Salyzyn51a71422015-02-26 14:33:35 -0800161 eventTagMap,
Mark Salyzyn95132e92013-11-22 10:55:48 -0800162 binaryMsgBuf,
163 sizeof(binaryMsgBuf));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164 //printf(">>> pri=%d len=%d msg='%s'\n",
165 // entry.priority, entry.messageLen, entry.message);
166 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800167 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800169 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170 goto error;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800171 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800173 if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800174 bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry);
175
176 if (bytesWritten < 0) {
Traian Schiaua4808162015-04-10 15:51:39 +0300177 logcat_panic(false, "output error");
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800178 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179 }
180
181 g_outByteCount += bytesWritten;
182
Mark Salyzyn95132e92013-11-22 10:55:48 -0800183 if (g_logRotateSizeKBytes > 0
The Android Open Source Projectdd7bc332009-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 Salyzyn0a890f92015-01-26 13:41:33 -0800194static void maybePrintStart(log_device_t* dev, bool printDividers) {
195 if (!dev->printed || printDividers) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800196 if (g_devCount > 1 && !g_printBinary) {
197 char buf[1024];
Mark Salyzyn0a890f92015-01-26 13:41:33 -0800198 snprintf(buf, sizeof(buf), "--------- %s %s\n",
199 dev->printed ? "switch to" : "beginning of",
Mark Salyzyn95132e92013-11-22 10:55:48 -0800200 dev->device);
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800201 if (write(g_outFD, buf, strlen(buf)) < 0) {
Traian Schiaua4808162015-04-10 15:51:39 +0300202 logcat_panic(false, "output error");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800203 }
204 }
Mark Salyzyn0a890f92015-01-26 13:41:33 -0800205 dev->printed = true;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800206 }
207}
208
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209static void setupOutput()
210{
211
212 if (g_outputFileName == NULL) {
213 g_outFD = STDOUT_FILENO;
214
215 } else {
Mark Salyzynffe87bf2015-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 Projectdd7bc332009-03-03 19:32:55 -0800225
Riley Andrews99186a42015-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 Projectdd7bc332009-03-03 19:32:55 -0800230 g_outFD = openLogFile (g_outputFileName);
231
232 if (g_outFD < 0) {
Traian Schiaua4808162015-04-10 15:51:39 +0300233 logcat_panic(false, "couldn't open output file");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 }
235
Mark Salyzynffe87bf2015-06-02 07:57:16 -0700236 struct stat statbuf;
Traian Schiaua4808162015-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 Projectdd7bc332009-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 Salyzyna23cd1c2015-03-09 09:32:56 -0700257 " Like specifying filterspec '*:S'\n"
Mark Salyzyn0acc3ef2015-05-27 07:39:56 -0700258 " -f <filename> Log to file. Default is stdout\n"
Traian Schiaua4808162015-04-10 15:51:39 +0300259 " -r <kbytes> Rotate log every kbytes. Requires -f\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800260 " -n <count> Sets max number of rotated logs to <count>, default 4\n"
Pierre Zurekead88fc2010-10-17 22:39:37 +0200261 " -v <format> Sets the log print format, where <format> is:\n\n"
Mark Salyzyn46d758e2015-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 Salyzyn0a890f92015-01-26 13:41:33 -0800264 " -D print dividers between each log buffer\n"
The Android Open Source Projectdd7bc332009-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 Egnord1d3b6d2010-03-11 20:32:17 -0800267 " -t <count> print only the most recent <count> lines (implies -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700268 " -t '<time>' print most recent lines since specified time (implies -d)\n"
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800269 " -T <count> print only the most recent <count> lines (does not imply -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700270 " -T '<time>' print most recent lines since specified time (not imply -d)\n"
Mark Salyzyn8c680ac2015-08-31 08:01:33 -0700271 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
Mark Salyzyn46d758e2015-08-31 15:53:41 -0700272 " 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273 " -g get the size of the log's ring buffer and exit\n"
Mark Salyzyna8e998e2014-12-15 10:01:31 -0800274 " -L dump logs from prior to last reboot\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800275 " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n"
Mark Salyzyn99f47a92014-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 Salyzyn34facab2014-02-06 14:48:50 -0800279 " -B output the log in binary.\n"
Mark Salyzynbbbe14f2014-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 Projectdd7bc332009-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 Salyzyna23cd1c2015-03-09 09:32:56 -0700294 " V Verbose (default for <tag>)\n"
295 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 " I Info\n"
297 " W Warn\n"
298 " E Error\n"
299 " F Fatal\n"
Mark Salyzyna23cd1c2015-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 Salyzyn649fc602014-09-16 09:15:15 -0700306 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307}
308
The Android Open Source Projectdd7bc332009-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 Salyzyndbc49232015-05-06 08:40:40 -0700320 return android_log_setPrintFormat(g_logformat, format);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321}
322
Mark Salyzyn671e3432014-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 Schiaua4808162015-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 Salyzyn2e4282a2015-04-13 09:27:57 -0700369 va_list args;
370 va_start(args, fmt);
371 vfprintf(stderr, fmt, args);
372 va_end(args);
Traian Schiaua4808162015-04-10 15:51:39 +0300373
374 if (showHelp) {
375 show_help(getprogname());
376 }
377
378 exit(EXIT_FAILURE);
379}
380
Mark Salyzyn8c680ac2015-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 Salyzyn46d758e2015-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 Salyzyn8c680ac2015-08-31 08:01:33 -0700392}
Mark Salyzyn0acc3ef2015-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);
402
403 std::string directory;
404 char *file = strrchr(outputFileName, '/');
405 if (!file) {
406 directory = ".";
407 file = outputFileName;
408 } else {
409 *file = '\0';
410 directory = outputFileName;
411 *file = '/';
412 ++file;
413 }
414 size_t len = strlen(file);
415 log_time modulo(0, NS_PER_SEC);
416 std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(directory.c_str()), closedir);
417 struct dirent *dp;
418 while ((dp = readdir(dir.get())) != NULL) {
419 if ((dp->d_type != DT_REG)
420 || strncmp(dp->d_name, file, len)
421 || (dp->d_name[len]
422 && ((dp->d_name[len] != '.')
423 || !isdigit(dp->d_name[len+1])))) {
424 continue;
425 }
426
427 std::string file_name = directory;
428 file_name += "/";
429 file_name += dp->d_name;
430 std::string file;
431 if (!android::base::ReadFileToString(file_name, &file)) {
432 continue;
433 }
434
435 bool found = false;
436 for (const auto& line : android::base::Split(file, "\n")) {
437 log_time t(log_time::EPOCH);
Mark Salyzyn8c680ac2015-08-31 08:01:33 -0700438 char *ep = parseTime(t, line.c_str());
Mark Salyzyn0acc3ef2015-05-27 07:39:56 -0700439 if (!ep || (*ep != ' ')) {
440 continue;
441 }
442 // determine the time precision of the logs (eg: msec or usec)
443 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
444 if (t.tv_nsec % (mod * 10)) {
445 modulo.tv_nsec = mod;
446 break;
447 }
448 }
449 // We filter any times later than current as we may not have the
450 // year stored with each log entry. Also, since it is possible for
451 // entries to be recorded out of order (very rare) we select the
452 // maximum we find just in case.
453 if ((t < now) && (t > retval)) {
454 retval = t;
455 found = true;
456 }
457 }
458 // We count on the basename file to be the definitive end, so stop here.
459 if (!dp->d_name[len] && found) {
460 break;
461 }
462 }
463 if (retval == log_time::EPOCH) {
464 return retval;
465 }
466 // tail_time prints matching or higher, round up by the modulo to prevent
467 // a replay of the last entry we have just checked.
468 retval += modulo;
469 return retval;
470}
471
Traian Schiaua4808162015-04-10 15:51:39 +0300472} /* namespace android */
473
474
Joe Onorato6fa09a02010-02-26 10:04:23 -0800475int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476{
Traian Schiaua4808162015-04-10 15:51:39 +0300477 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478 int err;
479 int hasSetLogFormat = 0;
480 int clearLog = 0;
481 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800482 unsigned long setLogSize = 0;
483 int getPruneList = 0;
484 char *setPruneList = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800485 int printStatistics = 0;
Mark Salyzyneae52fb2015-01-26 10:46:44 -0800486 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800488 log_device_t* devices = NULL;
489 log_device_t* dev;
Mark Salyzyn0a890f92015-01-26 13:41:33 -0800490 bool printDividers = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800491 struct logger_list *logger_list;
Traian Schiaua4808162015-04-10 15:51:39 +0300492 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800493 log_time tail_time(log_time::EPOCH);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800495 signal(SIGPIPE, exit);
496
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497 g_logformat = android_log_format_new();
498
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
Traian Schiaua4808162015-04-10 15:51:39 +0300500 show_help(argv[0]);
501 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502 }
503
504 for (;;) {
505 int ret;
506
Traian Schiaua4808162015-04-10 15:51:39 +0300507 ret = getopt(argc, argv, ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800508
509 if (ret < 0) {
510 break;
511 }
512
513 switch(ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800514 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515 // default to all silent
516 android_log_addFilterRule(g_logformat, "*:s");
517 break;
518
519 case 'c':
520 clearLog = 1;
Mark Salyzyneae52fb2015-01-26 10:46:44 -0800521 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522 break;
523
Mark Salyzyna8e998e2014-12-15 10:01:31 -0800524 case 'L':
525 mode |= ANDROID_LOG_PSTORE;
526 break;
527
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800528 case 'd':
Mark Salyzyneae52fb2015-01-26 10:46:44 -0800529 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530 break;
531
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800532 case 't':
Mark Salyzyneae52fb2015-01-26 10:46:44 -0800533 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800534 /* FALLTHRU */
535 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800536 if (strspn(optarg, "0123456789") != strlen(optarg)) {
Mark Salyzyn8c680ac2015-08-31 08:01:33 -0700537 char *cp = parseTime(tail_time, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800538 if (!cp) {
Mark Salyzyn8c680ac2015-08-31 08:01:33 -0700539 logcat_panic(false, "-%c \"%s\" not in time format\n",
540 ret, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800541 }
542 if (*cp) {
543 char c = *cp;
544 *cp = '\0';
545 fprintf(stderr,
546 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
547 ret, optarg, c, cp + 1);
548 *cp = c;
549 }
550 } else {
Traian Schiaua4808162015-04-10 15:51:39 +0300551 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800552 fprintf(stderr,
553 "WARNING: -%c %s invalid, setting to 1\n",
554 ret, optarg);
555 tail_lines = 1;
556 }
557 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800558 break;
559
Mark Salyzyn0a890f92015-01-26 13:41:33 -0800560 case 'D':
561 printDividers = true;
562 break;
563
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800564 case 'g':
565 getLogSize = 1;
566 break;
567
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800568 case 'G': {
Traian Schiaua4808162015-04-10 15:51:39 +0300569 char *cp;
570 if (strtoll(optarg, &cp, 0) > 0) {
571 setLogSize = strtoll(optarg, &cp, 0);
572 } else {
573 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800574 }
575
576 switch(*cp) {
577 case 'g':
578 case 'G':
579 setLogSize *= 1024;
580 /* FALLTHRU */
581 case 'm':
582 case 'M':
583 setLogSize *= 1024;
584 /* FALLTHRU */
585 case 'k':
586 case 'K':
587 setLogSize *= 1024;
588 /* FALLTHRU */
589 case '\0':
590 break;
591
592 default:
593 setLogSize = 0;
594 }
595
596 if (!setLogSize) {
597 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
Traian Schiaua4808162015-04-10 15:51:39 +0300598 return EXIT_FAILURE;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800599 }
600 }
601 break;
602
603 case 'p':
604 getPruneList = 1;
605 break;
606
607 case 'P':
608 setPruneList = optarg;
609 break;
610
Joe Onorato6fa09a02010-02-26 10:04:23 -0800611 case 'b': {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800612 if (strcmp(optarg, "all") == 0) {
613 while (devices) {
614 dev = devices;
615 devices = dev->next;
616 delete dev;
617 }
618
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700619 devices = dev = NULL;
Traian Schiaua4808162015-04-10 15:51:39 +0300620 g_devCount = 0;
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700621 for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
622 const char *name = android_log_id_to_name((log_id_t)i);
623 log_id_t log_id = android_name_to_log_id(name);
624
625 if (log_id != (log_id_t)i) {
626 continue;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800627 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700628
629 bool binary = strcmp(name, "events") == 0;
Mark Salyzyn7ae3cf12015-02-27 13:41:34 -0800630 log_device_t* d = new log_device_t(name, binary);
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700631
632 if (dev) {
633 dev->next = d;
634 dev = d;
635 } else {
636 devices = dev = d;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800637 }
Traian Schiaua4808162015-04-10 15:51:39 +0300638 g_devCount++;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800639 }
640 break;
641 }
642
Joe Onorato6fa09a02010-02-26 10:04:23 -0800643 bool binary = strcmp(optarg, "events") == 0;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800644
645 if (devices) {
646 dev = devices;
647 while (dev->next) {
648 dev = dev->next;
649 }
Mark Salyzyn7ae3cf12015-02-27 13:41:34 -0800650 dev->next = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800651 } else {
Mark Salyzyn7ae3cf12015-02-27 13:41:34 -0800652 devices = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800653 }
Traian Schiaua4808162015-04-10 15:51:39 +0300654 g_devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800655 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800656 break;
657
658 case 'B':
Traian Schiaua4808162015-04-10 15:51:39 +0300659 g_printBinary = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800660 break;
661
662 case 'f':
Mark Salyzyn3fe8f1c2015-10-06 08:59:02 -0700663 if ((tail_time == log_time::EPOCH) && (tail_lines == 0)) {
Mark Salyzyn0acc3ef2015-05-27 07:39:56 -0700664 tail_time = lastLogTime(optarg);
665 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800666 // redirect output to a file
Traian Schiaua4808162015-04-10 15:51:39 +0300667 g_outputFileName = optarg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800668 break;
669
670 case 'r':
Traian Schiaua4808162015-04-10 15:51:39 +0300671 if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) {
672 logcat_panic(true, "Invalid parameter %s to -r\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800673 }
674 break;
675
676 case 'n':
Traian Schiaua4808162015-04-10 15:51:39 +0300677 if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) {
678 logcat_panic(true, "Invalid parameter %s to -n\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800679 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800680 break;
681
682 case 'v':
683 err = setLogFormat (optarg);
684 if (err < 0) {
Traian Schiaua4808162015-04-10 15:51:39 +0300685 logcat_panic(true, "Invalid parameter %s to -v\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686 }
Mark Salyzyndbc49232015-05-06 08:40:40 -0700687 hasSetLogFormat |= err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800688 break;
689
690 case 'Q':
691 /* this is a *hidden* option used to start a version of logcat */
692 /* in an emulated device only. it basically looks for androidboot.logcat= */
693 /* on the kernel command line. If something is found, it extracts a log filter */
694 /* and uses it to run the program. If nothing is found, the program should */
695 /* quit immediately */
696#define KERNEL_OPTION "androidboot.logcat="
697#define CONSOLE_OPTION "androidboot.console="
698 {
699 int fd;
700 char* logcat;
701 char* console;
702 int force_exit = 1;
703 static char cmdline[1024];
704
705 fd = open("/proc/cmdline", O_RDONLY);
706 if (fd >= 0) {
707 int n = read(fd, cmdline, sizeof(cmdline)-1 );
708 if (n < 0) n = 0;
709 cmdline[n] = 0;
710 close(fd);
711 } else {
712 cmdline[0] = 0;
713 }
714
715 logcat = strstr( cmdline, KERNEL_OPTION );
716 console = strstr( cmdline, CONSOLE_OPTION );
717 if (logcat != NULL) {
718 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
719 char* q = strpbrk( p, " \t\n\r" );;
720
721 if (q != NULL)
722 *q = 0;
723
724 forceFilters = p;
725 force_exit = 0;
726 }
727 /* if nothing found or invalid filters, exit quietly */
Traian Schiaua4808162015-04-10 15:51:39 +0300728 if (force_exit) {
729 return EXIT_SUCCESS;
730 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800731
732 /* redirect our output to the emulator console */
733 if (console) {
734 char* p = console + sizeof(CONSOLE_OPTION)-1;
735 char* q = strpbrk( p, " \t\n\r" );
736 char devname[64];
737 int len;
738
739 if (q != NULL) {
740 len = q - p;
741 } else
742 len = strlen(p);
743
744 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
745 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
746 if (len < (int)sizeof(devname)) {
747 fd = open( devname, O_WRONLY );
748 if (fd >= 0) {
749 dup2(fd, 1);
750 dup2(fd, 2);
751 close(fd);
752 }
753 }
754 }
755 }
756 break;
757
Mark Salyzyn34facab2014-02-06 14:48:50 -0800758 case 'S':
759 printStatistics = 1;
760 break;
761
Traian Schiaua4808162015-04-10 15:51:39 +0300762 case ':':
763 logcat_panic(true, "Option -%c needs an argument\n", optopt);
764 break;
765
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800766 default:
Traian Schiaua4808162015-04-10 15:51:39 +0300767 logcat_panic(true, "Unrecognized Option %c\n", optopt);
768 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800769 }
770 }
771
Joe Onorato6fa09a02010-02-26 10:04:23 -0800772 if (!devices) {
Mark Salyzyn7ae3cf12015-02-27 13:41:34 -0800773 dev = devices = new log_device_t("main", false);
Traian Schiaua4808162015-04-10 15:51:39 +0300774 g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800775 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn7ae3cf12015-02-27 13:41:34 -0800776 dev = dev->next = new log_device_t("system", false);
Traian Schiaua4808162015-04-10 15:51:39 +0300777 g_devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800778 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700779 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn7ae3cf12015-02-27 13:41:34 -0800780 dev = dev->next = new log_device_t("crash", false);
Traian Schiaua4808162015-04-10 15:51:39 +0300781 g_devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700782 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800783 }
784
Traian Schiaua4808162015-04-10 15:51:39 +0300785 if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) {
786 logcat_panic(true, "-r requires -f as well\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800787 }
788
Traian Schiaua4808162015-04-10 15:51:39 +0300789 setupOutput();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800790
791 if (hasSetLogFormat == 0) {
792 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
793
794 if (logFormat != NULL) {
795 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800796 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800797 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800798 logFormat);
799 }
Mark Salyzyn649fc602014-09-16 09:15:15 -0700800 } else {
801 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800802 }
803 }
804
805 if (forceFilters) {
806 err = android_log_addFilterString(g_logformat, forceFilters);
807 if (err < 0) {
Traian Schiaua4808162015-04-10 15:51:39 +0300808 logcat_panic(false, "Invalid filter expression in logcat args\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800809 }
810 } else if (argc == optind) {
811 // Add from environment variable
812 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
813
814 if (env_tags_orig != NULL) {
815 err = android_log_addFilterString(g_logformat, env_tags_orig);
816
Mark Salyzyn95132e92013-11-22 10:55:48 -0800817 if (err < 0) {
Traian Schiaua4808162015-04-10 15:51:39 +0300818 logcat_panic(true,
819 "Invalid filter expression in ANDROID_LOG_TAGS\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800820 }
821 }
822 } else {
823 // Add from commandline
824 for (int i = optind ; i < argc ; i++) {
825 err = android_log_addFilterString(g_logformat, argv[i]);
826
Mark Salyzyn95132e92013-11-22 10:55:48 -0800827 if (err < 0) {
Traian Schiaua4808162015-04-10 15:51:39 +0300828 logcat_panic(true, "Invalid filter expression '%s'\n", argv[i]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800829 }
830 }
831 }
832
Joe Onorato6fa09a02010-02-26 10:04:23 -0800833 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800834 if (tail_time != log_time::EPOCH) {
835 logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
836 } else {
837 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
838 }
Mark Salyzynfef00ae2015-09-16 15:34:00 -0700839 const char *openDeviceFail = NULL;
840 const char *clearFail = NULL;
841 const char *setSizeFail = NULL;
842 const char *getSizeFail = NULL;
843 // We have three orthogonal actions below to clear, set log size and
844 // get log size. All sharing the same iteration loop.
Joe Onorato6fa09a02010-02-26 10:04:23 -0800845 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800846 dev->logger_list = logger_list;
847 dev->logger = android_logger_open(logger_list,
848 android_name_to_log_id(dev->device));
849 if (!dev->logger) {
Mark Salyzynfef00ae2015-09-16 15:34:00 -0700850 openDeviceFail = openDeviceFail ?: dev->device;
851 dev = dev->next;
852 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800853 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800854
855 if (clearLog) {
Mark Salyzynfef00ae2015-09-16 15:34:00 -0700856 if (android_logger_clear(dev->logger)) {
857 clearFail = clearFail ?: dev->device;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800858 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800859 }
860
Mark Salyzynfef00ae2015-09-16 15:34:00 -0700861 if (setLogSize) {
862 if (android_logger_set_log_size(dev->logger, setLogSize)) {
863 setSizeFail = setSizeFail ?: dev->device;
864 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800865 }
866
Joe Onorato6fa09a02010-02-26 10:04:23 -0800867 if (getLogSize) {
Mark Salyzynfef00ae2015-09-16 15:34:00 -0700868 long size = android_logger_get_log_size(dev->logger);
869 long readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800870
Mark Salyzynfef00ae2015-09-16 15:34:00 -0700871 if ((size < 0) || (readable < 0)) {
872 getSizeFail = getSizeFail ?: dev->device;
873 } else {
874 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
875 "max entry is %db, max payload is %db\n", dev->device,
876 value_of_size(size), multiplier_of_size(size),
877 value_of_size(readable), multiplier_of_size(readable),
878 (int) LOGGER_ENTRY_MAX_LEN,
879 (int) LOGGER_ENTRY_MAX_PAYLOAD);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800880 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800881 }
882
883 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800884 }
Mark Salyzynfef00ae2015-09-16 15:34:00 -0700885 // report any errors in the above loop and exit
886 if (openDeviceFail) {
887 logcat_panic(false, "Unable to open log device '%s'\n", openDeviceFail);
888 }
889 if (clearFail) {
890 logcat_panic(false, "failed to clear the '%s' log\n", clearFail);
891 }
892 if (setSizeFail) {
893 logcat_panic(false, "failed to set the '%s' log size\n", setSizeFail);
894 }
895 if (getSizeFail) {
896 logcat_panic(false, "failed to get the readable '%s' log size",
897 getSizeFail);
898 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800899
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800900 if (setPruneList) {
Traian Schiaua4808162015-04-10 15:51:39 +0300901 size_t len = strlen(setPruneList);
902 /*extra 32 bytes are needed by android_logger_set_prune_list */
903 size_t bLen = len + 32;
904 char *buf = NULL;
905 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
906 buf[len] = '\0';
907 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
908 logcat_panic(false, "failed to set the prune list");
909 }
910 free(buf);
911 } else {
912 logcat_panic(false, "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800913 }
914 }
915
Mark Salyzyn1c950472014-04-01 17:19:47 -0700916 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800917 size_t len = 8192;
918 char *buf;
919
920 for(int retry = 32;
921 (retry >= 0) && ((buf = new char [len]));
Traian Schiaua4808162015-04-10 15:51:39 +0300922 delete [] buf, buf = NULL, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800923 if (getPruneList) {
924 android_logger_get_prune_list(logger_list, buf, len);
925 } else {
926 android_logger_get_statistics(logger_list, buf, len);
927 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800928 buf[len-1] = '\0';
Traian Schiaua4808162015-04-10 15:51:39 +0300929 if (atol(buf) < 3) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800930 delete [] buf;
931 buf = NULL;
932 break;
933 }
Traian Schiaua4808162015-04-10 15:51:39 +0300934 size_t ret = atol(buf) + 1;
935 if (ret <= len) {
936 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800937 break;
938 }
Traian Schiaua4808162015-04-10 15:51:39 +0300939 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800940 }
941
942 if (!buf) {
Traian Schiaua4808162015-04-10 15:51:39 +0300943 logcat_panic(false, "failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -0800944 }
945
946 // remove trailing FF
947 char *cp = buf + len - 1;
948 *cp = '\0';
949 bool truncated = *--cp != '\f';
950 if (!truncated) {
951 *cp = '\0';
952 }
953
954 // squash out the byte count
955 cp = buf;
956 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -0700957 while (isdigit(*cp)) {
958 ++cp;
959 }
960 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800961 ++cp;
962 }
963 }
964
965 printf("%s", cp);
966 delete [] buf;
Traian Schiaua4808162015-04-10 15:51:39 +0300967 return EXIT_SUCCESS;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800968 }
969
970
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800971 if (getLogSize) {
Traian Schiaua4808162015-04-10 15:51:39 +0300972 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800973 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800974 if (setLogSize || setPruneList) {
Traian Schiaua4808162015-04-10 15:51:39 +0300975 return EXIT_SUCCESS;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800976 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800977 if (clearLog) {
Traian Schiaua4808162015-04-10 15:51:39 +0300978 return EXIT_SUCCESS;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800979 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800980
981 //LOG_EVENT_INT(10, 12345);
982 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
983 //LOG_EVENT_STRING(0, "whassup, doc?");
984
Mark Salyzyn0a890f92015-01-26 13:41:33 -0800985 dev = NULL;
Mark Salyzyn7ae3cf12015-02-27 13:41:34 -0800986 log_device_t unexpected("unexpected", false);
Mark Salyzyn95132e92013-11-22 10:55:48 -0800987 while (1) {
988 struct log_msg log_msg;
Mark Salyzyn0a890f92015-01-26 13:41:33 -0800989 log_device_t* d;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800990 int ret = android_logger_list_read(logger_list, &log_msg);
991
992 if (ret == 0) {
Traian Schiaua4808162015-04-10 15:51:39 +0300993 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800994 }
995
996 if (ret < 0) {
997 if (ret == -EAGAIN) {
998 break;
999 }
1000
1001 if (ret == -EIO) {
Traian Schiaua4808162015-04-10 15:51:39 +03001002 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001003 }
1004 if (ret == -EINVAL) {
Traian Schiaua4808162015-04-10 15:51:39 +03001005 logcat_panic(false, "read: unexpected length.\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001006 }
Traian Schiaua4808162015-04-10 15:51:39 +03001007 logcat_panic(false, "logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001008 }
1009
Mark Salyzyn0a890f92015-01-26 13:41:33 -08001010 for(d = devices; d; d = d->next) {
1011 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001012 break;
1013 }
1014 }
Mark Salyzyn0a890f92015-01-26 13:41:33 -08001015 if (!d) {
Traian Schiaua4808162015-04-10 15:51:39 +03001016 g_devCount = 2; // set to Multiple
Mark Salyzyn51a71422015-02-26 14:33:35 -08001017 d = &unexpected;
1018 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001019 }
1020
Mark Salyzyn0a890f92015-01-26 13:41:33 -08001021 if (dev != d) {
1022 dev = d;
Traian Schiaua4808162015-04-10 15:51:39 +03001023 maybePrintStart(dev, printDividers);
Mark Salyzyn0a890f92015-01-26 13:41:33 -08001024 }
Traian Schiaua4808162015-04-10 15:51:39 +03001025 if (g_printBinary) {
1026 printBinary(&log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001027 } else {
Traian Schiaua4808162015-04-10 15:51:39 +03001028 processBuffer(dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001029 }
1030 }
1031
1032 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001033
Traian Schiaua4808162015-04-10 15:51:39 +03001034 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001035}