blob: e598bb81048d3a921cd133fac92275f5d44f50a9 [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"
263 " threadtime time usec\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"
271 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm'\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272 " -g get the size of the log's ring buffer and exit\n"
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800273 " -L dump logs from prior to last reboot\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800274 " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n"
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700275 " 'events', 'crash' or 'all'. Multiple -b parameters are\n"
276 " allowed and results are interleaved. The default is\n"
277 " -b main -b system -b crash.\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800278 " -B output the log in binary.\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700279 " -S output statistics.\n"
280 " -G <size> set size of log ring buffer, may suffix with K or M.\n"
281 " -p print prune white and ~black list. Service is specified as\n"
282 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
283 " with ~, otherwise weighed for longevity if unadorned. All\n"
284 " other pruning activity is oldest first. Special case ~!\n"
285 " represents an automatic quicker pruning for the noisiest\n"
286 " UID as determined by the current statistics.\n"
287 " -P '<list> ...' set prune white and ~black list, using same format as\n"
288 " printed above. Must be quoted.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289
290 fprintf(stderr,"\nfilterspecs are a series of \n"
291 " <tag>[:priority]\n\n"
292 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700293 " V Verbose (default for <tag>)\n"
294 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295 " I Info\n"
296 " W Warn\n"
297 " E Error\n"
298 " F Fatal\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700299 " S Silent (suppress all output)\n"
300 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
301 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
302 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
303 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
304 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700305 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306}
307
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308static int setLogFormat(const char * formatString)
309{
310 static AndroidLogPrintFormat format;
311
312 format = android_log_formatFromString(formatString);
313
314 if (format == FORMAT_OFF) {
315 // FORMAT_OFF means invalid string
316 return -1;
317 }
318
Mark Salyzyne1f20042015-05-06 08:40:40 -0700319 return android_log_setPrintFormat(g_logformat, format);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320}
321
Mark Salyzyn671e3432014-05-06 07:34:59 -0700322static const char multipliers[][2] = {
323 { "" },
324 { "K" },
325 { "M" },
326 { "G" }
327};
328
329static unsigned long value_of_size(unsigned long value)
330{
331 for (unsigned i = 0;
332 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
333 value /= 1024, ++i) ;
334 return value;
335}
336
337static const char *multiplier_of_size(unsigned long value)
338{
339 unsigned i;
340 for (i = 0;
341 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
342 value /= 1024, ++i) ;
343 return multipliers[i];
344}
345
Traian Schiau59763032015-04-10 15:51:39 +0300346/*String to unsigned int, returns -1 if it fails*/
347static bool getSizeTArg(char *ptr, size_t *val, size_t min = 0,
348 size_t max = SIZE_MAX)
349{
350 char *endp;
351 errno = 0;
352 size_t ret = (size_t) strtoll(ptr, &endp, 0);
353
354 if (endp[0] != '\0' || errno != 0 ) {
355 return false;
356 }
357
358 if (ret > max || ret < min) {
359 return false;
360 }
361
362 *val = ret;
363 return true;
364}
365
366static void logcat_panic(bool showHelp, const char *fmt, ...)
367{
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700368 va_list args;
369 va_start(args, fmt);
370 vfprintf(stderr, fmt, args);
371 va_end(args);
Traian Schiau59763032015-04-10 15:51:39 +0300372
373 if (showHelp) {
374 show_help(getprogname());
375 }
376
377 exit(EXIT_FAILURE);
378}
379
Mark Salyzynf3555d92015-05-27 07:39:56 -0700380static const char g_defaultTimeFormat[] = "%m-%d %H:%M:%S.%q";
381
382// Find last logged line in gestalt of all matching existing output files
383static log_time lastLogTime(char *outputFileName) {
384 log_time retval(log_time::EPOCH);
385 if (!outputFileName) {
386 return retval;
387 }
388
389 log_time now(CLOCK_REALTIME);
390
391 std::string directory;
392 char *file = strrchr(outputFileName, '/');
393 if (!file) {
394 directory = ".";
395 file = outputFileName;
396 } else {
397 *file = '\0';
398 directory = outputFileName;
399 *file = '/';
400 ++file;
401 }
402 size_t len = strlen(file);
403 log_time modulo(0, NS_PER_SEC);
404 std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(directory.c_str()), closedir);
405 struct dirent *dp;
406 while ((dp = readdir(dir.get())) != NULL) {
407 if ((dp->d_type != DT_REG)
408 || strncmp(dp->d_name, file, len)
409 || (dp->d_name[len]
410 && ((dp->d_name[len] != '.')
411 || !isdigit(dp->d_name[len+1])))) {
412 continue;
413 }
414
415 std::string file_name = directory;
416 file_name += "/";
417 file_name += dp->d_name;
418 std::string file;
419 if (!android::base::ReadFileToString(file_name, &file)) {
420 continue;
421 }
422
423 bool found = false;
424 for (const auto& line : android::base::Split(file, "\n")) {
425 log_time t(log_time::EPOCH);
426 char *ep = t.strptime(line.c_str(), g_defaultTimeFormat);
427 if (!ep || (*ep != ' ')) {
428 continue;
429 }
430 // determine the time precision of the logs (eg: msec or usec)
431 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
432 if (t.tv_nsec % (mod * 10)) {
433 modulo.tv_nsec = mod;
434 break;
435 }
436 }
437 // We filter any times later than current as we may not have the
438 // year stored with each log entry. Also, since it is possible for
439 // entries to be recorded out of order (very rare) we select the
440 // maximum we find just in case.
441 if ((t < now) && (t > retval)) {
442 retval = t;
443 found = true;
444 }
445 }
446 // We count on the basename file to be the definitive end, so stop here.
447 if (!dp->d_name[len] && found) {
448 break;
449 }
450 }
451 if (retval == log_time::EPOCH) {
452 return retval;
453 }
454 // tail_time prints matching or higher, round up by the modulo to prevent
455 // a replay of the last entry we have just checked.
456 retval += modulo;
457 return retval;
458}
459
Traian Schiau59763032015-04-10 15:51:39 +0300460} /* namespace android */
461
462
Joe Onorato6fa09a02010-02-26 10:04:23 -0800463int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464{
Traian Schiau59763032015-04-10 15:51:39 +0300465 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466 int err;
467 int hasSetLogFormat = 0;
468 int clearLog = 0;
469 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800470 unsigned long setLogSize = 0;
471 int getPruneList = 0;
472 char *setPruneList = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800473 int printStatistics = 0;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800474 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800476 log_device_t* devices = NULL;
477 log_device_t* dev;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800478 bool printDividers = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800479 struct logger_list *logger_list;
Traian Schiau59763032015-04-10 15:51:39 +0300480 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800481 log_time tail_time(log_time::EPOCH);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800483 signal(SIGPIPE, exit);
484
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485 g_logformat = android_log_format_new();
486
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
Traian Schiau59763032015-04-10 15:51:39 +0300488 show_help(argv[0]);
489 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490 }
491
492 for (;;) {
493 int ret;
494
Traian Schiau59763032015-04-10 15:51:39 +0300495 ret = getopt(argc, argv, ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800496
497 if (ret < 0) {
498 break;
499 }
500
501 switch(ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800502 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503 // default to all silent
504 android_log_addFilterRule(g_logformat, "*:s");
505 break;
506
507 case 'c':
508 clearLog = 1;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800509 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510 break;
511
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800512 case 'L':
513 mode |= ANDROID_LOG_PSTORE;
514 break;
515
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800517 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518 break;
519
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800520 case 't':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800521 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800522 /* FALLTHRU */
523 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800524 if (strspn(optarg, "0123456789") != strlen(optarg)) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700525 char *cp = tail_time.strptime(optarg, g_defaultTimeFormat);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800526 if (!cp) {
Traian Schiau59763032015-04-10 15:51:39 +0300527 logcat_panic(false,
528 "-%c \"%s\" not in \"%s\" time format\n",
Mark Salyzynf3555d92015-05-27 07:39:56 -0700529 ret, optarg, g_defaultTimeFormat);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800530 }
531 if (*cp) {
532 char c = *cp;
533 *cp = '\0';
534 fprintf(stderr,
535 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
536 ret, optarg, c, cp + 1);
537 *cp = c;
538 }
539 } else {
Traian Schiau59763032015-04-10 15:51:39 +0300540 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800541 fprintf(stderr,
542 "WARNING: -%c %s invalid, setting to 1\n",
543 ret, optarg);
544 tail_lines = 1;
545 }
546 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800547 break;
548
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800549 case 'D':
550 printDividers = true;
551 break;
552
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800553 case 'g':
554 getLogSize = 1;
555 break;
556
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800557 case 'G': {
Traian Schiau59763032015-04-10 15:51:39 +0300558 char *cp;
559 if (strtoll(optarg, &cp, 0) > 0) {
560 setLogSize = strtoll(optarg, &cp, 0);
561 } else {
562 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800563 }
564
565 switch(*cp) {
566 case 'g':
567 case 'G':
568 setLogSize *= 1024;
569 /* FALLTHRU */
570 case 'm':
571 case 'M':
572 setLogSize *= 1024;
573 /* FALLTHRU */
574 case 'k':
575 case 'K':
576 setLogSize *= 1024;
577 /* FALLTHRU */
578 case '\0':
579 break;
580
581 default:
582 setLogSize = 0;
583 }
584
585 if (!setLogSize) {
586 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
Traian Schiau59763032015-04-10 15:51:39 +0300587 return EXIT_FAILURE;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800588 }
589 }
590 break;
591
592 case 'p':
593 getPruneList = 1;
594 break;
595
596 case 'P':
597 setPruneList = optarg;
598 break;
599
Joe Onorato6fa09a02010-02-26 10:04:23 -0800600 case 'b': {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800601 if (strcmp(optarg, "all") == 0) {
602 while (devices) {
603 dev = devices;
604 devices = dev->next;
605 delete dev;
606 }
607
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700608 devices = dev = NULL;
Traian Schiau59763032015-04-10 15:51:39 +0300609 g_devCount = 0;
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700610 for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
611 const char *name = android_log_id_to_name((log_id_t)i);
612 log_id_t log_id = android_name_to_log_id(name);
613
614 if (log_id != (log_id_t)i) {
615 continue;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800616 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700617
618 bool binary = strcmp(name, "events") == 0;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800619 log_device_t* d = new log_device_t(name, binary);
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700620
621 if (dev) {
622 dev->next = d;
623 dev = d;
624 } else {
625 devices = dev = d;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800626 }
Traian Schiau59763032015-04-10 15:51:39 +0300627 g_devCount++;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800628 }
629 break;
630 }
631
Joe Onorato6fa09a02010-02-26 10:04:23 -0800632 bool binary = strcmp(optarg, "events") == 0;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800633
634 if (devices) {
635 dev = devices;
636 while (dev->next) {
637 dev = dev->next;
638 }
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800639 dev->next = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800640 } else {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800641 devices = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800642 }
Traian Schiau59763032015-04-10 15:51:39 +0300643 g_devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800644 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800645 break;
646
647 case 'B':
Traian Schiau59763032015-04-10 15:51:39 +0300648 g_printBinary = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800649 break;
650
651 case 'f':
Mark Salyzynf3555d92015-05-27 07:39:56 -0700652 if ((tail_time == log_time::EPOCH) && (tail_lines != 0)) {
653 tail_time = lastLogTime(optarg);
654 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800655 // redirect output to a file
Traian Schiau59763032015-04-10 15:51:39 +0300656 g_outputFileName = optarg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800657 break;
658
659 case 'r':
Traian Schiau59763032015-04-10 15:51:39 +0300660 if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) {
661 logcat_panic(true, "Invalid parameter %s to -r\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800662 }
663 break;
664
665 case 'n':
Traian Schiau59763032015-04-10 15:51:39 +0300666 if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) {
667 logcat_panic(true, "Invalid parameter %s to -n\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800668 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800669 break;
670
671 case 'v':
672 err = setLogFormat (optarg);
673 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300674 logcat_panic(true, "Invalid parameter %s to -v\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800675 }
Mark Salyzyne1f20042015-05-06 08:40:40 -0700676 hasSetLogFormat |= err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800677 break;
678
679 case 'Q':
680 /* this is a *hidden* option used to start a version of logcat */
681 /* in an emulated device only. it basically looks for androidboot.logcat= */
682 /* on the kernel command line. If something is found, it extracts a log filter */
683 /* and uses it to run the program. If nothing is found, the program should */
684 /* quit immediately */
685#define KERNEL_OPTION "androidboot.logcat="
686#define CONSOLE_OPTION "androidboot.console="
687 {
688 int fd;
689 char* logcat;
690 char* console;
691 int force_exit = 1;
692 static char cmdline[1024];
693
694 fd = open("/proc/cmdline", O_RDONLY);
695 if (fd >= 0) {
696 int n = read(fd, cmdline, sizeof(cmdline)-1 );
697 if (n < 0) n = 0;
698 cmdline[n] = 0;
699 close(fd);
700 } else {
701 cmdline[0] = 0;
702 }
703
704 logcat = strstr( cmdline, KERNEL_OPTION );
705 console = strstr( cmdline, CONSOLE_OPTION );
706 if (logcat != NULL) {
707 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
708 char* q = strpbrk( p, " \t\n\r" );;
709
710 if (q != NULL)
711 *q = 0;
712
713 forceFilters = p;
714 force_exit = 0;
715 }
716 /* if nothing found or invalid filters, exit quietly */
Traian Schiau59763032015-04-10 15:51:39 +0300717 if (force_exit) {
718 return EXIT_SUCCESS;
719 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800720
721 /* redirect our output to the emulator console */
722 if (console) {
723 char* p = console + sizeof(CONSOLE_OPTION)-1;
724 char* q = strpbrk( p, " \t\n\r" );
725 char devname[64];
726 int len;
727
728 if (q != NULL) {
729 len = q - p;
730 } else
731 len = strlen(p);
732
733 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
734 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
735 if (len < (int)sizeof(devname)) {
736 fd = open( devname, O_WRONLY );
737 if (fd >= 0) {
738 dup2(fd, 1);
739 dup2(fd, 2);
740 close(fd);
741 }
742 }
743 }
744 }
745 break;
746
Mark Salyzyn34facab2014-02-06 14:48:50 -0800747 case 'S':
748 printStatistics = 1;
749 break;
750
Traian Schiau59763032015-04-10 15:51:39 +0300751 case ':':
752 logcat_panic(true, "Option -%c needs an argument\n", optopt);
753 break;
754
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800755 default:
Traian Schiau59763032015-04-10 15:51:39 +0300756 logcat_panic(true, "Unrecognized Option %c\n", optopt);
757 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800758 }
759 }
760
Joe Onorato6fa09a02010-02-26 10:04:23 -0800761 if (!devices) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800762 dev = devices = new log_device_t("main", false);
Traian Schiau59763032015-04-10 15:51:39 +0300763 g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800764 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800765 dev = dev->next = new log_device_t("system", false);
Traian Schiau59763032015-04-10 15:51:39 +0300766 g_devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800767 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700768 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800769 dev = dev->next = new log_device_t("crash", false);
Traian Schiau59763032015-04-10 15:51:39 +0300770 g_devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700771 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800772 }
773
Traian Schiau59763032015-04-10 15:51:39 +0300774 if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) {
775 logcat_panic(true, "-r requires -f as well\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800776 }
777
Traian Schiau59763032015-04-10 15:51:39 +0300778 setupOutput();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800779
780 if (hasSetLogFormat == 0) {
781 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
782
783 if (logFormat != NULL) {
784 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800785 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800786 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800787 logFormat);
788 }
Mark Salyzyn649fc602014-09-16 09:15:15 -0700789 } else {
790 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800791 }
792 }
793
794 if (forceFilters) {
795 err = android_log_addFilterString(g_logformat, forceFilters);
796 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300797 logcat_panic(false, "Invalid filter expression in logcat args\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800798 }
799 } else if (argc == optind) {
800 // Add from environment variable
801 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
802
803 if (env_tags_orig != NULL) {
804 err = android_log_addFilterString(g_logformat, env_tags_orig);
805
Mark Salyzyn95132e92013-11-22 10:55:48 -0800806 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300807 logcat_panic(true,
808 "Invalid filter expression in ANDROID_LOG_TAGS\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800809 }
810 }
811 } else {
812 // Add from commandline
813 for (int i = optind ; i < argc ; i++) {
814 err = android_log_addFilterString(g_logformat, argv[i]);
815
Mark Salyzyn95132e92013-11-22 10:55:48 -0800816 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300817 logcat_panic(true, "Invalid filter expression '%s'\n", argv[i]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800818 }
819 }
820 }
821
Joe Onorato6fa09a02010-02-26 10:04:23 -0800822 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800823 if (tail_time != log_time::EPOCH) {
824 logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
825 } else {
826 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
827 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800828 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800829 dev->logger_list = logger_list;
830 dev->logger = android_logger_open(logger_list,
831 android_name_to_log_id(dev->device));
832 if (!dev->logger) {
Traian Schiau59763032015-04-10 15:51:39 +0300833 logcat_panic(false, "Unable to open log device '%s'\n",
834 dev->device);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800835 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800836
837 if (clearLog) {
838 int ret;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800839 ret = android_logger_clear(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800840 if (ret) {
Traian Schiau59763032015-04-10 15:51:39 +0300841 logcat_panic(false, "failed to clear the log");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800842 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800843 }
844
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800845 if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) {
Traian Schiau59763032015-04-10 15:51:39 +0300846 logcat_panic(false, "failed to set the log size");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800847 }
848
Joe Onorato6fa09a02010-02-26 10:04:23 -0800849 if (getLogSize) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800850 long size, readable;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800851
Mark Salyzyn95132e92013-11-22 10:55:48 -0800852 size = android_logger_get_log_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800853 if (size < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300854 logcat_panic(false, "failed to get the log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800855 }
856
Mark Salyzyn95132e92013-11-22 10:55:48 -0800857 readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800858 if (readable < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300859 logcat_panic(false, "failed to get the readable log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800860 }
861
Mark Salyzyn671e3432014-05-06 07:34:59 -0700862 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
Joe Onorato6fa09a02010-02-26 10:04:23 -0800863 "max entry is %db, max payload is %db\n", dev->device,
Mark Salyzyn671e3432014-05-06 07:34:59 -0700864 value_of_size(size), multiplier_of_size(size),
865 value_of_size(readable), multiplier_of_size(readable),
Joe Onorato6fa09a02010-02-26 10:04:23 -0800866 (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
867 }
868
869 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800870 }
871
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800872 if (setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +0300873 size_t len = strlen(setPruneList);
874 /*extra 32 bytes are needed by android_logger_set_prune_list */
875 size_t bLen = len + 32;
876 char *buf = NULL;
877 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
878 buf[len] = '\0';
879 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
880 logcat_panic(false, "failed to set the prune list");
881 }
882 free(buf);
883 } else {
884 logcat_panic(false, "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800885 }
886 }
887
Mark Salyzyn1c950472014-04-01 17:19:47 -0700888 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800889 size_t len = 8192;
890 char *buf;
891
892 for(int retry = 32;
893 (retry >= 0) && ((buf = new char [len]));
Traian Schiau59763032015-04-10 15:51:39 +0300894 delete [] buf, buf = NULL, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800895 if (getPruneList) {
896 android_logger_get_prune_list(logger_list, buf, len);
897 } else {
898 android_logger_get_statistics(logger_list, buf, len);
899 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800900 buf[len-1] = '\0';
Traian Schiau59763032015-04-10 15:51:39 +0300901 if (atol(buf) < 3) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800902 delete [] buf;
903 buf = NULL;
904 break;
905 }
Traian Schiau59763032015-04-10 15:51:39 +0300906 size_t ret = atol(buf) + 1;
907 if (ret <= len) {
908 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800909 break;
910 }
Traian Schiau59763032015-04-10 15:51:39 +0300911 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800912 }
913
914 if (!buf) {
Traian Schiau59763032015-04-10 15:51:39 +0300915 logcat_panic(false, "failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -0800916 }
917
918 // remove trailing FF
919 char *cp = buf + len - 1;
920 *cp = '\0';
921 bool truncated = *--cp != '\f';
922 if (!truncated) {
923 *cp = '\0';
924 }
925
926 // squash out the byte count
927 cp = buf;
928 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -0700929 while (isdigit(*cp)) {
930 ++cp;
931 }
932 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800933 ++cp;
934 }
935 }
936
937 printf("%s", cp);
938 delete [] buf;
Traian Schiau59763032015-04-10 15:51:39 +0300939 return EXIT_SUCCESS;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800940 }
941
942
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800943 if (getLogSize) {
Traian Schiau59763032015-04-10 15:51:39 +0300944 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800945 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800946 if (setLogSize || setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +0300947 return EXIT_SUCCESS;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800948 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800949 if (clearLog) {
Traian Schiau59763032015-04-10 15:51:39 +0300950 return EXIT_SUCCESS;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800951 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800952
953 //LOG_EVENT_INT(10, 12345);
954 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
955 //LOG_EVENT_STRING(0, "whassup, doc?");
956
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800957 dev = NULL;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800958 log_device_t unexpected("unexpected", false);
Mark Salyzyn95132e92013-11-22 10:55:48 -0800959 while (1) {
960 struct log_msg log_msg;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800961 log_device_t* d;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800962 int ret = android_logger_list_read(logger_list, &log_msg);
963
964 if (ret == 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300965 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800966 }
967
968 if (ret < 0) {
969 if (ret == -EAGAIN) {
970 break;
971 }
972
973 if (ret == -EIO) {
Traian Schiau59763032015-04-10 15:51:39 +0300974 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800975 }
976 if (ret == -EINVAL) {
Traian Schiau59763032015-04-10 15:51:39 +0300977 logcat_panic(false, "read: unexpected length.\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800978 }
Traian Schiau59763032015-04-10 15:51:39 +0300979 logcat_panic(false, "logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800980 }
981
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800982 for(d = devices; d; d = d->next) {
983 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800984 break;
985 }
986 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800987 if (!d) {
Traian Schiau59763032015-04-10 15:51:39 +0300988 g_devCount = 2; // set to Multiple
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800989 d = &unexpected;
990 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800991 }
992
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800993 if (dev != d) {
994 dev = d;
Traian Schiau59763032015-04-10 15:51:39 +0300995 maybePrintStart(dev, printDividers);
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800996 }
Traian Schiau59763032015-04-10 15:51:39 +0300997 if (g_printBinary) {
998 printBinary(&log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -0800999 } else {
Traian Schiau59763032015-04-10 15:51:39 +03001000 processBuffer(dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001001 }
1002 }
1003
1004 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001005
Traian Schiau59763032015-04-10 15:51:39 +03001006 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001007}