blob: ddbfb3e5d1fee2aa5a744509ba48ee6849f19edc [file] [log] [blame]
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001// Copyright 2006-2015 The Android Open Source Project
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08002
Mark Salyzyn3ef730c2015-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 Salyzynf3555d92015-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 Salyzyn3ef730c2015-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 Schiau59763032015-04-10 15:51:39 +030016#include <sys/cdefs.h>
Riley Andrewsaede9892015-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 Salyzynf3555d92015-05-27 07:39:56 -070020#include <sys/types.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070021#include <time.h>
22#include <unistd.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080023
Mark Salyzynf3555d92015-05-27 07:39:56 -070024#include <memory>
25#include <string>
26
27#include <base/file.h>
28#include <base/strings.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070029#include <cutils/sched_policy.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080030#include <cutils/sockets.h>
Mark Salyzyn3ef730c2015-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 Salyzyn3ef730c2015-06-02 07:57:16 -070035#include <log/logger.h>
Colin Cross9227bd32013-07-23 16:59:20 -070036#include <log/logprint.h>
Riley Andrewsaede9892015-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 Salyzyn5f6738a2015-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 Schiau59763032015-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 Schiau59763032015-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 Schiau59763032015-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 Salyzyn9421b0c2015-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 Schiau59763032015-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 Schiau59763032015-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 Schiau59763032015-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 Schiau59763032015-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 Salyzyn9421b0c2015-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 Salyzyn9421b0c2015-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 Schiau59763032015-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 Salyzyn7b30ff82015-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 Salyzyn7b30ff82015-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 Schiau59763032015-04-10 15:51:39 +0300202 logcat_panic(false, "output error");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800203 }
204 }
Mark Salyzyn7b30ff82015-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 Salyzyn3ef730c2015-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 Andrewsaede9892015-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 Schiau59763032015-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 Salyzyn3ef730c2015-06-02 07:57:16 -0700236 struct stat statbuf;
Traian Schiau59763032015-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 Salyzynbba894a2015-03-09 09:32:56 -0700257 " Like specifying filterspec '*:S'\n"
Mark Salyzynf3555d92015-05-27 07:39:56 -0700258 " -f <filename> Log to file. Default is stdout\n"
Traian Schiau59763032015-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 Salyzynb932b2f2015-05-15 09:01:58 -0700262 " brief color long printable process raw tag thread\n"
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700263 " threadtime time usec UTC year zone\n\n"
Mark Salyzyn7b30ff82015-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 Salyzynf28f6a92015-08-31 08:01:33 -0700271 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
272 " or 'YYYY-MM-DD hh:mm:ss.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 Salyzyn7c975ac2014-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 Salyzynbba894a2015-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 Salyzynbba894a2015-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 Salyzyne1f20042015-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 Schiau59763032015-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 Salyzyn77d7e812015-04-13 09:27:57 -0700369 va_list args;
370 va_start(args, fmt);
371 vfprintf(stderr, fmt, args);
372 va_end(args);
Traian Schiau59763032015-04-10 15:51:39 +0300373
374 if (showHelp) {
375 show_help(getprogname());
376 }
377
378 exit(EXIT_FAILURE);
379}
380
Mark Salyzynf28f6a92015-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 }
387 return t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
388}
Mark Salyzynf3555d92015-05-27 07:39:56 -0700389
390// Find last logged line in gestalt of all matching existing output files
391static log_time lastLogTime(char *outputFileName) {
392 log_time retval(log_time::EPOCH);
393 if (!outputFileName) {
394 return retval;
395 }
396
397 log_time now(CLOCK_REALTIME);
398
399 std::string directory;
400 char *file = strrchr(outputFileName, '/');
401 if (!file) {
402 directory = ".";
403 file = outputFileName;
404 } else {
405 *file = '\0';
406 directory = outputFileName;
407 *file = '/';
408 ++file;
409 }
410 size_t len = strlen(file);
411 log_time modulo(0, NS_PER_SEC);
412 std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(directory.c_str()), closedir);
413 struct dirent *dp;
414 while ((dp = readdir(dir.get())) != NULL) {
415 if ((dp->d_type != DT_REG)
416 || strncmp(dp->d_name, file, len)
417 || (dp->d_name[len]
418 && ((dp->d_name[len] != '.')
419 || !isdigit(dp->d_name[len+1])))) {
420 continue;
421 }
422
423 std::string file_name = directory;
424 file_name += "/";
425 file_name += dp->d_name;
426 std::string file;
427 if (!android::base::ReadFileToString(file_name, &file)) {
428 continue;
429 }
430
431 bool found = false;
432 for (const auto& line : android::base::Split(file, "\n")) {
433 log_time t(log_time::EPOCH);
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700434 char *ep = parseTime(t, line.c_str());
Mark Salyzynf3555d92015-05-27 07:39:56 -0700435 if (!ep || (*ep != ' ')) {
436 continue;
437 }
438 // determine the time precision of the logs (eg: msec or usec)
439 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
440 if (t.tv_nsec % (mod * 10)) {
441 modulo.tv_nsec = mod;
442 break;
443 }
444 }
445 // We filter any times later than current as we may not have the
446 // year stored with each log entry. Also, since it is possible for
447 // entries to be recorded out of order (very rare) we select the
448 // maximum we find just in case.
449 if ((t < now) && (t > retval)) {
450 retval = t;
451 found = true;
452 }
453 }
454 // We count on the basename file to be the definitive end, so stop here.
455 if (!dp->d_name[len] && found) {
456 break;
457 }
458 }
459 if (retval == log_time::EPOCH) {
460 return retval;
461 }
462 // tail_time prints matching or higher, round up by the modulo to prevent
463 // a replay of the last entry we have just checked.
464 retval += modulo;
465 return retval;
466}
467
Traian Schiau59763032015-04-10 15:51:39 +0300468} /* namespace android */
469
470
Joe Onorato6fa09a02010-02-26 10:04:23 -0800471int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472{
Traian Schiau59763032015-04-10 15:51:39 +0300473 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474 int err;
475 int hasSetLogFormat = 0;
476 int clearLog = 0;
477 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800478 unsigned long setLogSize = 0;
479 int getPruneList = 0;
480 char *setPruneList = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800481 int printStatistics = 0;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800482 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800484 log_device_t* devices = NULL;
485 log_device_t* dev;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800486 bool printDividers = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800487 struct logger_list *logger_list;
Traian Schiau59763032015-04-10 15:51:39 +0300488 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800489 log_time tail_time(log_time::EPOCH);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800491 signal(SIGPIPE, exit);
492
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493 g_logformat = android_log_format_new();
494
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
Traian Schiau59763032015-04-10 15:51:39 +0300496 show_help(argv[0]);
497 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800498 }
499
500 for (;;) {
501 int ret;
502
Traian Schiau59763032015-04-10 15:51:39 +0300503 ret = getopt(argc, argv, ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504
505 if (ret < 0) {
506 break;
507 }
508
509 switch(ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800510 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511 // default to all silent
512 android_log_addFilterRule(g_logformat, "*:s");
513 break;
514
515 case 'c':
516 clearLog = 1;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800517 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518 break;
519
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800520 case 'L':
521 mode |= ANDROID_LOG_PSTORE;
522 break;
523
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800524 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800525 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800526 break;
527
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800528 case 't':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800529 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800530 /* FALLTHRU */
531 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800532 if (strspn(optarg, "0123456789") != strlen(optarg)) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700533 char *cp = parseTime(tail_time, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800534 if (!cp) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700535 logcat_panic(false, "-%c \"%s\" not in time format\n",
536 ret, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800537 }
538 if (*cp) {
539 char c = *cp;
540 *cp = '\0';
541 fprintf(stderr,
542 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
543 ret, optarg, c, cp + 1);
544 *cp = c;
545 }
546 } else {
Traian Schiau59763032015-04-10 15:51:39 +0300547 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800548 fprintf(stderr,
549 "WARNING: -%c %s invalid, setting to 1\n",
550 ret, optarg);
551 tail_lines = 1;
552 }
553 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800554 break;
555
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800556 case 'D':
557 printDividers = true;
558 break;
559
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800560 case 'g':
561 getLogSize = 1;
562 break;
563
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800564 case 'G': {
Traian Schiau59763032015-04-10 15:51:39 +0300565 char *cp;
566 if (strtoll(optarg, &cp, 0) > 0) {
567 setLogSize = strtoll(optarg, &cp, 0);
568 } else {
569 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800570 }
571
572 switch(*cp) {
573 case 'g':
574 case 'G':
575 setLogSize *= 1024;
576 /* FALLTHRU */
577 case 'm':
578 case 'M':
579 setLogSize *= 1024;
580 /* FALLTHRU */
581 case 'k':
582 case 'K':
583 setLogSize *= 1024;
584 /* FALLTHRU */
585 case '\0':
586 break;
587
588 default:
589 setLogSize = 0;
590 }
591
592 if (!setLogSize) {
593 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
Traian Schiau59763032015-04-10 15:51:39 +0300594 return EXIT_FAILURE;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800595 }
596 }
597 break;
598
599 case 'p':
600 getPruneList = 1;
601 break;
602
603 case 'P':
604 setPruneList = optarg;
605 break;
606
Joe Onorato6fa09a02010-02-26 10:04:23 -0800607 case 'b': {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800608 if (strcmp(optarg, "all") == 0) {
609 while (devices) {
610 dev = devices;
611 devices = dev->next;
612 delete dev;
613 }
614
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700615 devices = dev = NULL;
Traian Schiau59763032015-04-10 15:51:39 +0300616 g_devCount = 0;
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700617 for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
618 const char *name = android_log_id_to_name((log_id_t)i);
619 log_id_t log_id = android_name_to_log_id(name);
620
621 if (log_id != (log_id_t)i) {
622 continue;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800623 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700624
625 bool binary = strcmp(name, "events") == 0;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800626 log_device_t* d = new log_device_t(name, binary);
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700627
628 if (dev) {
629 dev->next = d;
630 dev = d;
631 } else {
632 devices = dev = d;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800633 }
Traian Schiau59763032015-04-10 15:51:39 +0300634 g_devCount++;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800635 }
636 break;
637 }
638
Joe Onorato6fa09a02010-02-26 10:04:23 -0800639 bool binary = strcmp(optarg, "events") == 0;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800640
641 if (devices) {
642 dev = devices;
643 while (dev->next) {
644 dev = dev->next;
645 }
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800646 dev->next = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800647 } else {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800648 devices = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800649 }
Traian Schiau59763032015-04-10 15:51:39 +0300650 g_devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800651 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800652 break;
653
654 case 'B':
Traian Schiau59763032015-04-10 15:51:39 +0300655 g_printBinary = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800656 break;
657
658 case 'f':
Mark Salyzyn9812fc42015-10-06 08:59:02 -0700659 if ((tail_time == log_time::EPOCH) && (tail_lines == 0)) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700660 tail_time = lastLogTime(optarg);
661 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800662 // redirect output to a file
Traian Schiau59763032015-04-10 15:51:39 +0300663 g_outputFileName = optarg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800664 break;
665
666 case 'r':
Traian Schiau59763032015-04-10 15:51:39 +0300667 if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) {
668 logcat_panic(true, "Invalid parameter %s to -r\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800669 }
670 break;
671
672 case 'n':
Traian Schiau59763032015-04-10 15:51:39 +0300673 if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) {
674 logcat_panic(true, "Invalid parameter %s to -n\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800675 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800676 break;
677
678 case 'v':
679 err = setLogFormat (optarg);
680 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300681 logcat_panic(true, "Invalid parameter %s to -v\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800682 }
Mark Salyzyne1f20042015-05-06 08:40:40 -0700683 hasSetLogFormat |= err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800684 break;
685
686 case 'Q':
687 /* this is a *hidden* option used to start a version of logcat */
688 /* in an emulated device only. it basically looks for androidboot.logcat= */
689 /* on the kernel command line. If something is found, it extracts a log filter */
690 /* and uses it to run the program. If nothing is found, the program should */
691 /* quit immediately */
692#define KERNEL_OPTION "androidboot.logcat="
693#define CONSOLE_OPTION "androidboot.console="
694 {
695 int fd;
696 char* logcat;
697 char* console;
698 int force_exit = 1;
699 static char cmdline[1024];
700
701 fd = open("/proc/cmdline", O_RDONLY);
702 if (fd >= 0) {
703 int n = read(fd, cmdline, sizeof(cmdline)-1 );
704 if (n < 0) n = 0;
705 cmdline[n] = 0;
706 close(fd);
707 } else {
708 cmdline[0] = 0;
709 }
710
711 logcat = strstr( cmdline, KERNEL_OPTION );
712 console = strstr( cmdline, CONSOLE_OPTION );
713 if (logcat != NULL) {
714 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
715 char* q = strpbrk( p, " \t\n\r" );;
716
717 if (q != NULL)
718 *q = 0;
719
720 forceFilters = p;
721 force_exit = 0;
722 }
723 /* if nothing found or invalid filters, exit quietly */
Traian Schiau59763032015-04-10 15:51:39 +0300724 if (force_exit) {
725 return EXIT_SUCCESS;
726 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800727
728 /* redirect our output to the emulator console */
729 if (console) {
730 char* p = console + sizeof(CONSOLE_OPTION)-1;
731 char* q = strpbrk( p, " \t\n\r" );
732 char devname[64];
733 int len;
734
735 if (q != NULL) {
736 len = q - p;
737 } else
738 len = strlen(p);
739
740 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
741 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
742 if (len < (int)sizeof(devname)) {
743 fd = open( devname, O_WRONLY );
744 if (fd >= 0) {
745 dup2(fd, 1);
746 dup2(fd, 2);
747 close(fd);
748 }
749 }
750 }
751 }
752 break;
753
Mark Salyzyn34facab2014-02-06 14:48:50 -0800754 case 'S':
755 printStatistics = 1;
756 break;
757
Traian Schiau59763032015-04-10 15:51:39 +0300758 case ':':
759 logcat_panic(true, "Option -%c needs an argument\n", optopt);
760 break;
761
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800762 default:
Traian Schiau59763032015-04-10 15:51:39 +0300763 logcat_panic(true, "Unrecognized Option %c\n", optopt);
764 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800765 }
766 }
767
Joe Onorato6fa09a02010-02-26 10:04:23 -0800768 if (!devices) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800769 dev = devices = new log_device_t("main", false);
Traian Schiau59763032015-04-10 15:51:39 +0300770 g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800771 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800772 dev = dev->next = new log_device_t("system", false);
Traian Schiau59763032015-04-10 15:51:39 +0300773 g_devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800774 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700775 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800776 dev = dev->next = new log_device_t("crash", false);
Traian Schiau59763032015-04-10 15:51:39 +0300777 g_devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700778 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800779 }
780
Traian Schiau59763032015-04-10 15:51:39 +0300781 if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) {
782 logcat_panic(true, "-r requires -f as well\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800783 }
784
Traian Schiau59763032015-04-10 15:51:39 +0300785 setupOutput();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800786
787 if (hasSetLogFormat == 0) {
788 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
789
790 if (logFormat != NULL) {
791 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800792 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800793 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800794 logFormat);
795 }
Mark Salyzyn649fc602014-09-16 09:15:15 -0700796 } else {
797 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800798 }
799 }
800
801 if (forceFilters) {
802 err = android_log_addFilterString(g_logformat, forceFilters);
803 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300804 logcat_panic(false, "Invalid filter expression in logcat args\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800805 }
806 } else if (argc == optind) {
807 // Add from environment variable
808 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
809
810 if (env_tags_orig != NULL) {
811 err = android_log_addFilterString(g_logformat, env_tags_orig);
812
Mark Salyzyn95132e92013-11-22 10:55:48 -0800813 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300814 logcat_panic(true,
815 "Invalid filter expression in ANDROID_LOG_TAGS\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800816 }
817 }
818 } else {
819 // Add from commandline
820 for (int i = optind ; i < argc ; i++) {
821 err = android_log_addFilterString(g_logformat, argv[i]);
822
Mark Salyzyn95132e92013-11-22 10:55:48 -0800823 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300824 logcat_panic(true, "Invalid filter expression '%s'\n", argv[i]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800825 }
826 }
827 }
828
Joe Onorato6fa09a02010-02-26 10:04:23 -0800829 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800830 if (tail_time != log_time::EPOCH) {
831 logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
832 } else {
833 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
834 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700835 const char *openDeviceFail = NULL;
836 const char *clearFail = NULL;
837 const char *setSizeFail = NULL;
838 const char *getSizeFail = NULL;
839 // We have three orthogonal actions below to clear, set log size and
840 // get log size. All sharing the same iteration loop.
Joe Onorato6fa09a02010-02-26 10:04:23 -0800841 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800842 dev->logger_list = logger_list;
843 dev->logger = android_logger_open(logger_list,
844 android_name_to_log_id(dev->device));
845 if (!dev->logger) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700846 openDeviceFail = openDeviceFail ?: dev->device;
847 dev = dev->next;
848 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800849 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800850
851 if (clearLog) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700852 if (android_logger_clear(dev->logger)) {
853 clearFail = clearFail ?: dev->device;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800854 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800855 }
856
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700857 if (setLogSize) {
858 if (android_logger_set_log_size(dev->logger, setLogSize)) {
859 setSizeFail = setSizeFail ?: dev->device;
860 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800861 }
862
Joe Onorato6fa09a02010-02-26 10:04:23 -0800863 if (getLogSize) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700864 long size = android_logger_get_log_size(dev->logger);
865 long readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800866
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700867 if ((size < 0) || (readable < 0)) {
868 getSizeFail = getSizeFail ?: dev->device;
869 } else {
870 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
871 "max entry is %db, max payload is %db\n", dev->device,
872 value_of_size(size), multiplier_of_size(size),
873 value_of_size(readable), multiplier_of_size(readable),
874 (int) LOGGER_ENTRY_MAX_LEN,
875 (int) LOGGER_ENTRY_MAX_PAYLOAD);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800876 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800877 }
878
879 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800880 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700881 // report any errors in the above loop and exit
882 if (openDeviceFail) {
883 logcat_panic(false, "Unable to open log device '%s'\n", openDeviceFail);
884 }
885 if (clearFail) {
886 logcat_panic(false, "failed to clear the '%s' log\n", clearFail);
887 }
888 if (setSizeFail) {
889 logcat_panic(false, "failed to set the '%s' log size\n", setSizeFail);
890 }
891 if (getSizeFail) {
892 logcat_panic(false, "failed to get the readable '%s' log size",
893 getSizeFail);
894 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800895
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800896 if (setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +0300897 size_t len = strlen(setPruneList);
898 /*extra 32 bytes are needed by android_logger_set_prune_list */
899 size_t bLen = len + 32;
900 char *buf = NULL;
901 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
902 buf[len] = '\0';
903 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
904 logcat_panic(false, "failed to set the prune list");
905 }
906 free(buf);
907 } else {
908 logcat_panic(false, "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800909 }
910 }
911
Mark Salyzyn1c950472014-04-01 17:19:47 -0700912 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800913 size_t len = 8192;
914 char *buf;
915
916 for(int retry = 32;
917 (retry >= 0) && ((buf = new char [len]));
Traian Schiau59763032015-04-10 15:51:39 +0300918 delete [] buf, buf = NULL, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800919 if (getPruneList) {
920 android_logger_get_prune_list(logger_list, buf, len);
921 } else {
922 android_logger_get_statistics(logger_list, buf, len);
923 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800924 buf[len-1] = '\0';
Traian Schiau59763032015-04-10 15:51:39 +0300925 if (atol(buf) < 3) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800926 delete [] buf;
927 buf = NULL;
928 break;
929 }
Traian Schiau59763032015-04-10 15:51:39 +0300930 size_t ret = atol(buf) + 1;
931 if (ret <= len) {
932 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800933 break;
934 }
Traian Schiau59763032015-04-10 15:51:39 +0300935 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800936 }
937
938 if (!buf) {
Traian Schiau59763032015-04-10 15:51:39 +0300939 logcat_panic(false, "failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -0800940 }
941
942 // remove trailing FF
943 char *cp = buf + len - 1;
944 *cp = '\0';
945 bool truncated = *--cp != '\f';
946 if (!truncated) {
947 *cp = '\0';
948 }
949
950 // squash out the byte count
951 cp = buf;
952 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -0700953 while (isdigit(*cp)) {
954 ++cp;
955 }
956 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800957 ++cp;
958 }
959 }
960
961 printf("%s", cp);
962 delete [] buf;
Traian Schiau59763032015-04-10 15:51:39 +0300963 return EXIT_SUCCESS;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800964 }
965
966
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800967 if (getLogSize) {
Traian Schiau59763032015-04-10 15:51:39 +0300968 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800969 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800970 if (setLogSize || setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +0300971 return EXIT_SUCCESS;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800972 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800973 if (clearLog) {
Traian Schiau59763032015-04-10 15:51:39 +0300974 return EXIT_SUCCESS;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800975 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800976
977 //LOG_EVENT_INT(10, 12345);
978 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
979 //LOG_EVENT_STRING(0, "whassup, doc?");
980
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800981 dev = NULL;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800982 log_device_t unexpected("unexpected", false);
Mark Salyzyn95132e92013-11-22 10:55:48 -0800983 while (1) {
984 struct log_msg log_msg;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800985 log_device_t* d;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800986 int ret = android_logger_list_read(logger_list, &log_msg);
987
988 if (ret == 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300989 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800990 }
991
992 if (ret < 0) {
993 if (ret == -EAGAIN) {
994 break;
995 }
996
997 if (ret == -EIO) {
Traian Schiau59763032015-04-10 15:51:39 +0300998 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800999 }
1000 if (ret == -EINVAL) {
Traian Schiau59763032015-04-10 15:51:39 +03001001 logcat_panic(false, "read: unexpected length.\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001002 }
Traian Schiau59763032015-04-10 15:51:39 +03001003 logcat_panic(false, "logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001004 }
1005
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001006 for(d = devices; d; d = d->next) {
1007 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001008 break;
1009 }
1010 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001011 if (!d) {
Traian Schiau59763032015-04-10 15:51:39 +03001012 g_devCount = 2; // set to Multiple
Mark Salyzyn9421b0c2015-02-26 14:33:35 -08001013 d = &unexpected;
1014 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001015 }
1016
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001017 if (dev != d) {
1018 dev = d;
Traian Schiau59763032015-04-10 15:51:39 +03001019 maybePrintStart(dev, printDividers);
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001020 }
Traian Schiau59763032015-04-10 15:51:39 +03001021 if (g_printBinary) {
1022 printBinary(&log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001023 } else {
Traian Schiau59763032015-04-10 15:51:39 +03001024 processBuffer(dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001025 }
1026 }
1027
1028 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001029
Traian Schiau59763032015-04-10 15:51:39 +03001030 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001031}