blob: afcc20ae4b3ca6b084950e41823cd3e3c56042d0 [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 Salyzyn65772ca2013-12-13 11:10:11 -08003#include <assert.h>
4#include <ctype.h>
Mark Salyzynf3555d92015-05-27 07:39:56 -07005#include <dirent.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -08006#include <errno.h>
7#include <fcntl.h>
Aristidis Papaioannoueba73442014-10-16 22:19:55 -07008#include <math.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -08009#include <stdio.h>
10#include <stdlib.h>
11#include <stdarg.h>
12#include <string.h>
13#include <signal.h>
14#include <time.h>
15#include <unistd.h>
Traian Schiau59763032015-04-10 15:51:39 +030016#include <sys/cdefs.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080017#include <sys/socket.h>
18#include <sys/stat.h>
Mark Salyzynf3555d92015-05-27 07:39:56 -070019#include <sys/types.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080020#include <arpa/inet.h>
21
Mark Salyzynf3555d92015-05-27 07:39:56 -070022#include <memory>
23#include <string>
24
25#include <base/file.h>
26#include <base/strings.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080027#include <cutils/sockets.h>
Mark Salyzyn95132e92013-11-22 10:55:48 -080028#include <log/log.h>
Mark Salyzynfa3716b2014-02-14 16:05:05 -080029#include <log/log_read.h>
Colin Cross9227bd32013-07-23 16:59:20 -070030#include <log/logger.h>
31#include <log/logd.h>
32#include <log/logprint.h>
33#include <log/event_tag_map.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035#define DEFAULT_MAX_ROTATED_LOGS 4
36
37static AndroidLogFormat * g_logformat;
38
39/* logd prefixes records with a length field */
40#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
41
Joe Onorato6fa09a02010-02-26 10:04:23 -080042struct log_device_t {
Mark Salyzyn95132e92013-11-22 10:55:48 -080043 const char* device;
Joe Onorato6fa09a02010-02-26 10:04:23 -080044 bool binary;
Mark Salyzyn95132e92013-11-22 10:55:48 -080045 struct logger *logger;
46 struct logger_list *logger_list;
Joe Onorato6fa09a02010-02-26 10:04:23 -080047 bool printed;
Joe Onorato6fa09a02010-02-26 10:04:23 -080048
Joe Onorato6fa09a02010-02-26 10:04:23 -080049 log_device_t* next;
50
Mark Salyzyn5f6738a2015-02-27 13:41:34 -080051 log_device_t(const char* d, bool b) {
Joe Onorato6fa09a02010-02-26 10:04:23 -080052 device = d;
53 binary = b;
Joe Onorato6fa09a02010-02-26 10:04:23 -080054 next = NULL;
55 printed = false;
Traian Schiau59763032015-04-10 15:51:39 +030056 logger = NULL;
57 logger_list = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -080058 }
Joe Onorato6fa09a02010-02-26 10:04:23 -080059};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
61namespace android {
62
63/* Global Variables */
64
65static const char * g_outputFileName = NULL;
Traian Schiau59763032015-04-10 15:51:39 +030066// 0 means "no log rotation"
67static size_t g_logRotateSizeKBytes = 0;
68// 0 means "unbounded"
69static size_t g_maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070static int g_outFD = -1;
Traian Schiau59763032015-04-10 15:51:39 +030071static size_t g_outByteCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072static int g_printBinary = 0;
Mark Salyzyn9421b0c2015-02-26 14:33:35 -080073static int g_devCount = 0; // >1 means multiple
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074
Traian Schiau59763032015-04-10 15:51:39 +030075__noreturn static void logcat_panic(bool showHelp, const char *fmt, ...) __printflike(2,3);
76
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077static int openLogFile (const char *pathname)
78{
Edwin Vane80b221c2012-08-13 12:55:07 -040079 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080}
81
82static void rotateLogs()
83{
84 int err;
85
86 // Can't rotate logs if we're not outputting to a file
87 if (g_outputFileName == NULL) {
88 return;
89 }
90
91 close(g_outFD);
92
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070093 // Compute the maximum number of digits needed to count up to g_maxRotatedLogs in decimal.
94 // eg: g_maxRotatedLogs == 30 -> log10(30) == 1.477 -> maxRotationCountDigits == 2
95 int maxRotationCountDigits =
96 (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
97
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098 for (int i = g_maxRotatedLogs ; i > 0 ; i--) {
99 char *file0, *file1;
100
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700101 asprintf(&file1, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102
103 if (i - 1 == 0) {
104 asprintf(&file0, "%s", g_outputFileName);
105 } else {
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700106 asprintf(&file0, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107 }
108
Traian Schiau59763032015-04-10 15:51:39 +0300109 if (!file0 || !file1) {
110 perror("while rotating log files");
111 break;
112 }
113
114 err = rename(file0, file1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115
116 if (err < 0 && errno != ENOENT) {
117 perror("while rotating log files");
118 }
119
120 free(file1);
121 free(file0);
122 }
123
Traian Schiau59763032015-04-10 15:51:39 +0300124 g_outFD = openLogFile(g_outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125
126 if (g_outFD < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300127 logcat_panic(false, "couldn't open output file");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128 }
129
130 g_outByteCount = 0;
131
132}
133
Mark Salyzyn95132e92013-11-22 10:55:48 -0800134void printBinary(struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135{
Mark Salyzyn95132e92013-11-22 10:55:48 -0800136 size_t size = buf->len();
137
138 TEMP_FAILURE_RETRY(write(g_outFD, buf, size));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139}
140
Mark Salyzyn95132e92013-11-22 10:55:48 -0800141static void processBuffer(log_device_t* dev, struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142{
Mathias Agopian50844522010-03-17 16:10:26 -0700143 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144 int err;
145 AndroidLogEntry entry;
146 char binaryMsgBuf[1024];
147
Joe Onorato6fa09a02010-02-26 10:04:23 -0800148 if (dev->binary) {
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800149 static bool hasOpenedEventTagMap = false;
150 static EventTagMap *eventTagMap = NULL;
151
152 if (!eventTagMap && !hasOpenedEventTagMap) {
153 eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
154 hasOpenedEventTagMap = true;
155 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800156 err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry,
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800157 eventTagMap,
Mark Salyzyn95132e92013-11-22 10:55:48 -0800158 binaryMsgBuf,
159 sizeof(binaryMsgBuf));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160 //printf(">>> pri=%d len=%d msg='%s'\n",
161 // entry.priority, entry.messageLen, entry.message);
162 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800163 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800165 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166 goto error;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800167 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800169 if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800170 bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry);
171
172 if (bytesWritten < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300173 logcat_panic(false, "output error");
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800174 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175 }
176
177 g_outByteCount += bytesWritten;
178
Mark Salyzyn95132e92013-11-22 10:55:48 -0800179 if (g_logRotateSizeKBytes > 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180 && (g_outByteCount / 1024) >= g_logRotateSizeKBytes
181 ) {
182 rotateLogs();
183 }
184
185error:
186 //fprintf (stderr, "Error processing record\n");
187 return;
188}
189
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800190static void maybePrintStart(log_device_t* dev, bool printDividers) {
191 if (!dev->printed || printDividers) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800192 if (g_devCount > 1 && !g_printBinary) {
193 char buf[1024];
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800194 snprintf(buf, sizeof(buf), "--------- %s %s\n",
195 dev->printed ? "switch to" : "beginning of",
Mark Salyzyn95132e92013-11-22 10:55:48 -0800196 dev->device);
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800197 if (write(g_outFD, buf, strlen(buf)) < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300198 logcat_panic(false, "output error");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800199 }
200 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800201 dev->printed = true;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800202 }
203}
204
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205static void setupOutput()
206{
207
208 if (g_outputFileName == NULL) {
209 g_outFD = STDOUT_FILENO;
210
211 } else {
212 struct stat statbuf;
213
214 g_outFD = openLogFile (g_outputFileName);
215
216 if (g_outFD < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300217 logcat_panic(false, "couldn't open output file");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218 }
219
Traian Schiau59763032015-04-10 15:51:39 +0300220 if (fstat(g_outFD, &statbuf) == -1) {
221 close(g_outFD);
222 logcat_panic(false, "couldn't get output file stat\n");
223 }
224
225 if ((size_t) statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
226 close(g_outFD);
227 logcat_panic(false, "invalid output file stat\n");
228 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229
230 g_outByteCount = statbuf.st_size;
231 }
232}
233
234static void show_help(const char *cmd)
235{
236 fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd);
237
238 fprintf(stderr, "options include:\n"
239 " -s Set default filter to silent.\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700240 " Like specifying filterspec '*:S'\n"
Mark Salyzynf3555d92015-05-27 07:39:56 -0700241 " -f <filename> Log to file. Default is stdout\n"
Traian Schiau59763032015-04-10 15:51:39 +0300242 " -r <kbytes> Rotate log every kbytes. Requires -f\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 " -n <count> Sets max number of rotated logs to <count>, default 4\n"
Pierre Zurekead88fc2010-10-17 22:39:37 +0200244 " -v <format> Sets the log print format, where <format> is:\n\n"
Mark Salyzyne1f20042015-05-06 08:40:40 -0700245 " brief color long process raw tag thread threadtime time usec\n\n"
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800246 " -D print dividers between each log buffer\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247 " -c clear (flush) the entire log and exit\n"
248 " -d dump the log and then exit (don't block)\n"
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800249 " -t <count> print only the most recent <count> lines (implies -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700250 " -t '<time>' print most recent lines since specified time (implies -d)\n"
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800251 " -T <count> print only the most recent <count> lines (does not imply -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700252 " -T '<time>' print most recent lines since specified time (not imply -d)\n"
253 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm'\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254 " -g get the size of the log's ring buffer and exit\n"
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800255 " -L dump logs from prior to last reboot\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800256 " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n"
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700257 " 'events', 'crash' or 'all'. Multiple -b parameters are\n"
258 " allowed and results are interleaved. The default is\n"
259 " -b main -b system -b crash.\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800260 " -B output the log in binary.\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700261 " -S output statistics.\n"
262 " -G <size> set size of log ring buffer, may suffix with K or M.\n"
263 " -p print prune white and ~black list. Service is specified as\n"
264 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
265 " with ~, otherwise weighed for longevity if unadorned. All\n"
266 " other pruning activity is oldest first. Special case ~!\n"
267 " represents an automatic quicker pruning for the noisiest\n"
268 " UID as determined by the current statistics.\n"
269 " -P '<list> ...' set prune white and ~black list, using same format as\n"
270 " printed above. Must be quoted.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271
272 fprintf(stderr,"\nfilterspecs are a series of \n"
273 " <tag>[:priority]\n\n"
274 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700275 " V Verbose (default for <tag>)\n"
276 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277 " I Info\n"
278 " W Warn\n"
279 " E Error\n"
280 " F Fatal\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700281 " S Silent (suppress all output)\n"
282 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
283 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
284 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
285 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
286 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700287 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288}
289
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800290static int setLogFormat(const char * formatString)
291{
292 static AndroidLogPrintFormat format;
293
294 format = android_log_formatFromString(formatString);
295
296 if (format == FORMAT_OFF) {
297 // FORMAT_OFF means invalid string
298 return -1;
299 }
300
Mark Salyzyne1f20042015-05-06 08:40:40 -0700301 return android_log_setPrintFormat(g_logformat, format);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302}
303
Mark Salyzyn671e3432014-05-06 07:34:59 -0700304static const char multipliers[][2] = {
305 { "" },
306 { "K" },
307 { "M" },
308 { "G" }
309};
310
311static unsigned long value_of_size(unsigned long value)
312{
313 for (unsigned i = 0;
314 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
315 value /= 1024, ++i) ;
316 return value;
317}
318
319static const char *multiplier_of_size(unsigned long value)
320{
321 unsigned i;
322 for (i = 0;
323 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
324 value /= 1024, ++i) ;
325 return multipliers[i];
326}
327
Traian Schiau59763032015-04-10 15:51:39 +0300328/*String to unsigned int, returns -1 if it fails*/
329static bool getSizeTArg(char *ptr, size_t *val, size_t min = 0,
330 size_t max = SIZE_MAX)
331{
332 char *endp;
333 errno = 0;
334 size_t ret = (size_t) strtoll(ptr, &endp, 0);
335
336 if (endp[0] != '\0' || errno != 0 ) {
337 return false;
338 }
339
340 if (ret > max || ret < min) {
341 return false;
342 }
343
344 *val = ret;
345 return true;
346}
347
348static void logcat_panic(bool showHelp, const char *fmt, ...)
349{
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700350 va_list args;
351 va_start(args, fmt);
352 vfprintf(stderr, fmt, args);
353 va_end(args);
Traian Schiau59763032015-04-10 15:51:39 +0300354
355 if (showHelp) {
356 show_help(getprogname());
357 }
358
359 exit(EXIT_FAILURE);
360}
361
Mark Salyzynf3555d92015-05-27 07:39:56 -0700362static const char g_defaultTimeFormat[] = "%m-%d %H:%M:%S.%q";
363
364// Find last logged line in gestalt of all matching existing output files
365static log_time lastLogTime(char *outputFileName) {
366 log_time retval(log_time::EPOCH);
367 if (!outputFileName) {
368 return retval;
369 }
370
371 log_time now(CLOCK_REALTIME);
372
373 std::string directory;
374 char *file = strrchr(outputFileName, '/');
375 if (!file) {
376 directory = ".";
377 file = outputFileName;
378 } else {
379 *file = '\0';
380 directory = outputFileName;
381 *file = '/';
382 ++file;
383 }
384 size_t len = strlen(file);
385 log_time modulo(0, NS_PER_SEC);
386 std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(directory.c_str()), closedir);
387 struct dirent *dp;
388 while ((dp = readdir(dir.get())) != NULL) {
389 if ((dp->d_type != DT_REG)
390 || strncmp(dp->d_name, file, len)
391 || (dp->d_name[len]
392 && ((dp->d_name[len] != '.')
393 || !isdigit(dp->d_name[len+1])))) {
394 continue;
395 }
396
397 std::string file_name = directory;
398 file_name += "/";
399 file_name += dp->d_name;
400 std::string file;
401 if (!android::base::ReadFileToString(file_name, &file)) {
402 continue;
403 }
404
405 bool found = false;
406 for (const auto& line : android::base::Split(file, "\n")) {
407 log_time t(log_time::EPOCH);
408 char *ep = t.strptime(line.c_str(), g_defaultTimeFormat);
409 if (!ep || (*ep != ' ')) {
410 continue;
411 }
412 // determine the time precision of the logs (eg: msec or usec)
413 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
414 if (t.tv_nsec % (mod * 10)) {
415 modulo.tv_nsec = mod;
416 break;
417 }
418 }
419 // We filter any times later than current as we may not have the
420 // year stored with each log entry. Also, since it is possible for
421 // entries to be recorded out of order (very rare) we select the
422 // maximum we find just in case.
423 if ((t < now) && (t > retval)) {
424 retval = t;
425 found = true;
426 }
427 }
428 // We count on the basename file to be the definitive end, so stop here.
429 if (!dp->d_name[len] && found) {
430 break;
431 }
432 }
433 if (retval == log_time::EPOCH) {
434 return retval;
435 }
436 // tail_time prints matching or higher, round up by the modulo to prevent
437 // a replay of the last entry we have just checked.
438 retval += modulo;
439 return retval;
440}
441
Traian Schiau59763032015-04-10 15:51:39 +0300442} /* namespace android */
443
444
Joe Onorato6fa09a02010-02-26 10:04:23 -0800445int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800446{
Traian Schiau59763032015-04-10 15:51:39 +0300447 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448 int err;
449 int hasSetLogFormat = 0;
450 int clearLog = 0;
451 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800452 unsigned long setLogSize = 0;
453 int getPruneList = 0;
454 char *setPruneList = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800455 int printStatistics = 0;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800456 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800457 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800458 log_device_t* devices = NULL;
459 log_device_t* dev;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800460 bool printDividers = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800461 struct logger_list *logger_list;
Traian Schiau59763032015-04-10 15:51:39 +0300462 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800463 log_time tail_time(log_time::EPOCH);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800465 signal(SIGPIPE, exit);
466
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467 g_logformat = android_log_format_new();
468
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
Traian Schiau59763032015-04-10 15:51:39 +0300470 show_help(argv[0]);
471 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472 }
473
474 for (;;) {
475 int ret;
476
Traian Schiau59763032015-04-10 15:51:39 +0300477 ret = getopt(argc, argv, ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478
479 if (ret < 0) {
480 break;
481 }
482
483 switch(ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800484 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485 // default to all silent
486 android_log_addFilterRule(g_logformat, "*:s");
487 break;
488
489 case 'c':
490 clearLog = 1;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800491 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800492 break;
493
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800494 case 'L':
495 mode |= ANDROID_LOG_PSTORE;
496 break;
497
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800498 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800499 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500 break;
501
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800502 case 't':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800503 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800504 /* FALLTHRU */
505 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800506 if (strspn(optarg, "0123456789") != strlen(optarg)) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700507 char *cp = tail_time.strptime(optarg, g_defaultTimeFormat);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800508 if (!cp) {
Traian Schiau59763032015-04-10 15:51:39 +0300509 logcat_panic(false,
510 "-%c \"%s\" not in \"%s\" time format\n",
Mark Salyzynf3555d92015-05-27 07:39:56 -0700511 ret, optarg, g_defaultTimeFormat);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800512 }
513 if (*cp) {
514 char c = *cp;
515 *cp = '\0';
516 fprintf(stderr,
517 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
518 ret, optarg, c, cp + 1);
519 *cp = c;
520 }
521 } else {
Traian Schiau59763032015-04-10 15:51:39 +0300522 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800523 fprintf(stderr,
524 "WARNING: -%c %s invalid, setting to 1\n",
525 ret, optarg);
526 tail_lines = 1;
527 }
528 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800529 break;
530
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800531 case 'D':
532 printDividers = true;
533 break;
534
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535 case 'g':
536 getLogSize = 1;
537 break;
538
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800539 case 'G': {
Traian Schiau59763032015-04-10 15:51:39 +0300540 char *cp;
541 if (strtoll(optarg, &cp, 0) > 0) {
542 setLogSize = strtoll(optarg, &cp, 0);
543 } else {
544 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800545 }
546
547 switch(*cp) {
548 case 'g':
549 case 'G':
550 setLogSize *= 1024;
551 /* FALLTHRU */
552 case 'm':
553 case 'M':
554 setLogSize *= 1024;
555 /* FALLTHRU */
556 case 'k':
557 case 'K':
558 setLogSize *= 1024;
559 /* FALLTHRU */
560 case '\0':
561 break;
562
563 default:
564 setLogSize = 0;
565 }
566
567 if (!setLogSize) {
568 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
Traian Schiau59763032015-04-10 15:51:39 +0300569 return EXIT_FAILURE;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800570 }
571 }
572 break;
573
574 case 'p':
575 getPruneList = 1;
576 break;
577
578 case 'P':
579 setPruneList = optarg;
580 break;
581
Joe Onorato6fa09a02010-02-26 10:04:23 -0800582 case 'b': {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800583 if (strcmp(optarg, "all") == 0) {
584 while (devices) {
585 dev = devices;
586 devices = dev->next;
587 delete dev;
588 }
589
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700590 devices = dev = NULL;
Traian Schiau59763032015-04-10 15:51:39 +0300591 g_devCount = 0;
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700592 for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
593 const char *name = android_log_id_to_name((log_id_t)i);
594 log_id_t log_id = android_name_to_log_id(name);
595
596 if (log_id != (log_id_t)i) {
597 continue;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800598 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700599
600 bool binary = strcmp(name, "events") == 0;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800601 log_device_t* d = new log_device_t(name, binary);
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700602
603 if (dev) {
604 dev->next = d;
605 dev = d;
606 } else {
607 devices = dev = d;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800608 }
Traian Schiau59763032015-04-10 15:51:39 +0300609 g_devCount++;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800610 }
611 break;
612 }
613
Joe Onorato6fa09a02010-02-26 10:04:23 -0800614 bool binary = strcmp(optarg, "events") == 0;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800615
616 if (devices) {
617 dev = devices;
618 while (dev->next) {
619 dev = dev->next;
620 }
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800621 dev->next = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800622 } else {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800623 devices = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800624 }
Traian Schiau59763032015-04-10 15:51:39 +0300625 g_devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800626 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800627 break;
628
629 case 'B':
Traian Schiau59763032015-04-10 15:51:39 +0300630 g_printBinary = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800631 break;
632
633 case 'f':
Mark Salyzynf3555d92015-05-27 07:39:56 -0700634 if ((tail_time == log_time::EPOCH) && (tail_lines != 0)) {
635 tail_time = lastLogTime(optarg);
636 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800637 // redirect output to a file
Traian Schiau59763032015-04-10 15:51:39 +0300638 g_outputFileName = optarg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800639 break;
640
641 case 'r':
Traian Schiau59763032015-04-10 15:51:39 +0300642 if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) {
643 logcat_panic(true, "Invalid parameter %s to -r\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800644 }
645 break;
646
647 case 'n':
Traian Schiau59763032015-04-10 15:51:39 +0300648 if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) {
649 logcat_panic(true, "Invalid parameter %s to -n\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800650 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800651 break;
652
653 case 'v':
654 err = setLogFormat (optarg);
655 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300656 logcat_panic(true, "Invalid parameter %s to -v\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800657 }
Mark Salyzyne1f20042015-05-06 08:40:40 -0700658 hasSetLogFormat |= err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800659 break;
660
661 case 'Q':
662 /* this is a *hidden* option used to start a version of logcat */
663 /* in an emulated device only. it basically looks for androidboot.logcat= */
664 /* on the kernel command line. If something is found, it extracts a log filter */
665 /* and uses it to run the program. If nothing is found, the program should */
666 /* quit immediately */
667#define KERNEL_OPTION "androidboot.logcat="
668#define CONSOLE_OPTION "androidboot.console="
669 {
670 int fd;
671 char* logcat;
672 char* console;
673 int force_exit = 1;
674 static char cmdline[1024];
675
676 fd = open("/proc/cmdline", O_RDONLY);
677 if (fd >= 0) {
678 int n = read(fd, cmdline, sizeof(cmdline)-1 );
679 if (n < 0) n = 0;
680 cmdline[n] = 0;
681 close(fd);
682 } else {
683 cmdline[0] = 0;
684 }
685
686 logcat = strstr( cmdline, KERNEL_OPTION );
687 console = strstr( cmdline, CONSOLE_OPTION );
688 if (logcat != NULL) {
689 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
690 char* q = strpbrk( p, " \t\n\r" );;
691
692 if (q != NULL)
693 *q = 0;
694
695 forceFilters = p;
696 force_exit = 0;
697 }
698 /* if nothing found or invalid filters, exit quietly */
Traian Schiau59763032015-04-10 15:51:39 +0300699 if (force_exit) {
700 return EXIT_SUCCESS;
701 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800702
703 /* redirect our output to the emulator console */
704 if (console) {
705 char* p = console + sizeof(CONSOLE_OPTION)-1;
706 char* q = strpbrk( p, " \t\n\r" );
707 char devname[64];
708 int len;
709
710 if (q != NULL) {
711 len = q - p;
712 } else
713 len = strlen(p);
714
715 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
716 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
717 if (len < (int)sizeof(devname)) {
718 fd = open( devname, O_WRONLY );
719 if (fd >= 0) {
720 dup2(fd, 1);
721 dup2(fd, 2);
722 close(fd);
723 }
724 }
725 }
726 }
727 break;
728
Mark Salyzyn34facab2014-02-06 14:48:50 -0800729 case 'S':
730 printStatistics = 1;
731 break;
732
Traian Schiau59763032015-04-10 15:51:39 +0300733 case ':':
734 logcat_panic(true, "Option -%c needs an argument\n", optopt);
735 break;
736
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800737 default:
Traian Schiau59763032015-04-10 15:51:39 +0300738 logcat_panic(true, "Unrecognized Option %c\n", optopt);
739 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800740 }
741 }
742
Joe Onorato6fa09a02010-02-26 10:04:23 -0800743 if (!devices) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800744 dev = devices = new log_device_t("main", false);
Traian Schiau59763032015-04-10 15:51:39 +0300745 g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800746 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800747 dev = dev->next = new log_device_t("system", false);
Traian Schiau59763032015-04-10 15:51:39 +0300748 g_devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800749 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700750 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800751 dev = dev->next = new log_device_t("crash", false);
Traian Schiau59763032015-04-10 15:51:39 +0300752 g_devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700753 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800754 }
755
Traian Schiau59763032015-04-10 15:51:39 +0300756 if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) {
757 logcat_panic(true, "-r requires -f as well\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800758 }
759
Traian Schiau59763032015-04-10 15:51:39 +0300760 setupOutput();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800761
762 if (hasSetLogFormat == 0) {
763 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
764
765 if (logFormat != NULL) {
766 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800767 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800768 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800769 logFormat);
770 }
Mark Salyzyn649fc602014-09-16 09:15:15 -0700771 } else {
772 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800773 }
774 }
775
776 if (forceFilters) {
777 err = android_log_addFilterString(g_logformat, forceFilters);
778 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300779 logcat_panic(false, "Invalid filter expression in logcat args\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800780 }
781 } else if (argc == optind) {
782 // Add from environment variable
783 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
784
785 if (env_tags_orig != NULL) {
786 err = android_log_addFilterString(g_logformat, env_tags_orig);
787
Mark Salyzyn95132e92013-11-22 10:55:48 -0800788 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300789 logcat_panic(true,
790 "Invalid filter expression in ANDROID_LOG_TAGS\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800791 }
792 }
793 } else {
794 // Add from commandline
795 for (int i = optind ; i < argc ; i++) {
796 err = android_log_addFilterString(g_logformat, argv[i]);
797
Mark Salyzyn95132e92013-11-22 10:55:48 -0800798 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300799 logcat_panic(true, "Invalid filter expression '%s'\n", argv[i]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800800 }
801 }
802 }
803
Joe Onorato6fa09a02010-02-26 10:04:23 -0800804 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800805 if (tail_time != log_time::EPOCH) {
806 logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
807 } else {
808 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
809 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800810 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800811 dev->logger_list = logger_list;
812 dev->logger = android_logger_open(logger_list,
813 android_name_to_log_id(dev->device));
814 if (!dev->logger) {
Traian Schiau59763032015-04-10 15:51:39 +0300815 logcat_panic(false, "Unable to open log device '%s'\n",
816 dev->device);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800817 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800818
819 if (clearLog) {
820 int ret;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800821 ret = android_logger_clear(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800822 if (ret) {
Traian Schiau59763032015-04-10 15:51:39 +0300823 logcat_panic(false, "failed to clear the log");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800824 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800825 }
826
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800827 if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) {
Traian Schiau59763032015-04-10 15:51:39 +0300828 logcat_panic(false, "failed to set the log size");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800829 }
830
Joe Onorato6fa09a02010-02-26 10:04:23 -0800831 if (getLogSize) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800832 long size, readable;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800833
Mark Salyzyn95132e92013-11-22 10:55:48 -0800834 size = android_logger_get_log_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800835 if (size < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300836 logcat_panic(false, "failed to get the log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800837 }
838
Mark Salyzyn95132e92013-11-22 10:55:48 -0800839 readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800840 if (readable < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300841 logcat_panic(false, "failed to get the readable log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800842 }
843
Mark Salyzyn671e3432014-05-06 07:34:59 -0700844 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
Joe Onorato6fa09a02010-02-26 10:04:23 -0800845 "max entry is %db, max payload is %db\n", dev->device,
Mark Salyzyn671e3432014-05-06 07:34:59 -0700846 value_of_size(size), multiplier_of_size(size),
847 value_of_size(readable), multiplier_of_size(readable),
Joe Onorato6fa09a02010-02-26 10:04:23 -0800848 (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
849 }
850
851 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800852 }
853
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800854 if (setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +0300855 size_t len = strlen(setPruneList);
856 /*extra 32 bytes are needed by android_logger_set_prune_list */
857 size_t bLen = len + 32;
858 char *buf = NULL;
859 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
860 buf[len] = '\0';
861 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
862 logcat_panic(false, "failed to set the prune list");
863 }
864 free(buf);
865 } else {
866 logcat_panic(false, "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800867 }
868 }
869
Mark Salyzyn1c950472014-04-01 17:19:47 -0700870 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800871 size_t len = 8192;
872 char *buf;
873
874 for(int retry = 32;
875 (retry >= 0) && ((buf = new char [len]));
Traian Schiau59763032015-04-10 15:51:39 +0300876 delete [] buf, buf = NULL, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800877 if (getPruneList) {
878 android_logger_get_prune_list(logger_list, buf, len);
879 } else {
880 android_logger_get_statistics(logger_list, buf, len);
881 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800882 buf[len-1] = '\0';
Traian Schiau59763032015-04-10 15:51:39 +0300883 if (atol(buf) < 3) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800884 delete [] buf;
885 buf = NULL;
886 break;
887 }
Traian Schiau59763032015-04-10 15:51:39 +0300888 size_t ret = atol(buf) + 1;
889 if (ret <= len) {
890 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800891 break;
892 }
Traian Schiau59763032015-04-10 15:51:39 +0300893 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800894 }
895
896 if (!buf) {
Traian Schiau59763032015-04-10 15:51:39 +0300897 logcat_panic(false, "failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -0800898 }
899
900 // remove trailing FF
901 char *cp = buf + len - 1;
902 *cp = '\0';
903 bool truncated = *--cp != '\f';
904 if (!truncated) {
905 *cp = '\0';
906 }
907
908 // squash out the byte count
909 cp = buf;
910 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -0700911 while (isdigit(*cp)) {
912 ++cp;
913 }
914 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800915 ++cp;
916 }
917 }
918
919 printf("%s", cp);
920 delete [] buf;
Traian Schiau59763032015-04-10 15:51:39 +0300921 return EXIT_SUCCESS;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800922 }
923
924
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800925 if (getLogSize) {
Traian Schiau59763032015-04-10 15:51:39 +0300926 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800927 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800928 if (setLogSize || setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +0300929 return EXIT_SUCCESS;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800930 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800931 if (clearLog) {
Traian Schiau59763032015-04-10 15:51:39 +0300932 return EXIT_SUCCESS;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800933 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800934
935 //LOG_EVENT_INT(10, 12345);
936 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
937 //LOG_EVENT_STRING(0, "whassup, doc?");
938
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800939 dev = NULL;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800940 log_device_t unexpected("unexpected", false);
Mark Salyzyn95132e92013-11-22 10:55:48 -0800941 while (1) {
942 struct log_msg log_msg;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800943 log_device_t* d;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800944 int ret = android_logger_list_read(logger_list, &log_msg);
945
946 if (ret == 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300947 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800948 }
949
950 if (ret < 0) {
951 if (ret == -EAGAIN) {
952 break;
953 }
954
955 if (ret == -EIO) {
Traian Schiau59763032015-04-10 15:51:39 +0300956 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800957 }
958 if (ret == -EINVAL) {
Traian Schiau59763032015-04-10 15:51:39 +0300959 logcat_panic(false, "read: unexpected length.\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800960 }
Traian Schiau59763032015-04-10 15:51:39 +0300961 logcat_panic(false, "logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800962 }
963
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800964 for(d = devices; d; d = d->next) {
965 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800966 break;
967 }
968 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800969 if (!d) {
Traian Schiau59763032015-04-10 15:51:39 +0300970 g_devCount = 2; // set to Multiple
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800971 d = &unexpected;
972 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800973 }
974
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800975 if (dev != d) {
976 dev = d;
Traian Schiau59763032015-04-10 15:51:39 +0300977 maybePrintStart(dev, printDividers);
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800978 }
Traian Schiau59763032015-04-10 15:51:39 +0300979 if (g_printBinary) {
980 printBinary(&log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -0800981 } else {
Traian Schiau59763032015-04-10 15:51:39 +0300982 processBuffer(dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -0800983 }
984 }
985
986 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800987
Traian Schiau59763032015-04-10 15:51:39 +0300988 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800989}