blob: 6d7740eaaf94d1afe5234922ebae9b871b21f2e1 [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>
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 Salyzyn3ef730c2015-06-02 07:57:16 -070020#include <time.h>
21#include <unistd.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080022
Mark Salyzynf3555d92015-05-27 07:39:56 -070023#include <memory>
24#include <string>
25
26#include <base/file.h>
27#include <base/strings.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070028#include <cutils/sched_policy.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080029#include <cutils/sockets.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070030#include <log/event_tag_map.h>
Mark Salyzyn95132e92013-11-22 10:55:48 -080031#include <log/log.h>
Mark Salyzynfa3716b2014-02-14 16:05:05 -080032#include <log/log_read.h>
Colin Cross9227bd32013-07-23 16:59:20 -070033#include <log/logd.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070034#include <log/logger.h>
Colin Cross9227bd32013-07-23 16:59:20 -070035#include <log/logprint.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037#define DEFAULT_MAX_ROTATED_LOGS 4
38
39static AndroidLogFormat * g_logformat;
40
41/* logd prefixes records with a length field */
42#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
43
Joe Onorato6fa09a02010-02-26 10:04:23 -080044struct log_device_t {
Mark Salyzyn95132e92013-11-22 10:55:48 -080045 const char* device;
Joe Onorato6fa09a02010-02-26 10:04:23 -080046 bool binary;
Mark Salyzyn95132e92013-11-22 10:55:48 -080047 struct logger *logger;
48 struct logger_list *logger_list;
Joe Onorato6fa09a02010-02-26 10:04:23 -080049 bool printed;
Joe Onorato6fa09a02010-02-26 10:04:23 -080050
Joe Onorato6fa09a02010-02-26 10:04:23 -080051 log_device_t* next;
52
Mark Salyzyn5f6738a2015-02-27 13:41:34 -080053 log_device_t(const char* d, bool b) {
Joe Onorato6fa09a02010-02-26 10:04:23 -080054 device = d;
55 binary = b;
Joe Onorato6fa09a02010-02-26 10:04:23 -080056 next = NULL;
57 printed = false;
Traian Schiau59763032015-04-10 15:51:39 +030058 logger = NULL;
59 logger_list = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -080060 }
Joe Onorato6fa09a02010-02-26 10:04:23 -080061};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062
63namespace android {
64
65/* Global Variables */
66
67static const char * g_outputFileName = NULL;
Traian Schiau59763032015-04-10 15:51:39 +030068// 0 means "no log rotation"
69static size_t g_logRotateSizeKBytes = 0;
70// 0 means "unbounded"
71static size_t g_maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072static int g_outFD = -1;
Traian Schiau59763032015-04-10 15:51:39 +030073static size_t g_outByteCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074static int g_printBinary = 0;
Mark Salyzyn9421b0c2015-02-26 14:33:35 -080075static int g_devCount = 0; // >1 means multiple
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076
Traian Schiau59763032015-04-10 15:51:39 +030077__noreturn static void logcat_panic(bool showHelp, const char *fmt, ...) __printflike(2,3);
78
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079static int openLogFile (const char *pathname)
80{
Edwin Vane80b221c2012-08-13 12:55:07 -040081 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082}
83
84static void rotateLogs()
85{
86 int err;
87
88 // Can't rotate logs if we're not outputting to a file
89 if (g_outputFileName == NULL) {
90 return;
91 }
92
93 close(g_outFD);
94
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070095 // Compute the maximum number of digits needed to count up to g_maxRotatedLogs in decimal.
96 // eg: g_maxRotatedLogs == 30 -> log10(30) == 1.477 -> maxRotationCountDigits == 2
97 int maxRotationCountDigits =
98 (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
99
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100 for (int i = g_maxRotatedLogs ; i > 0 ; i--) {
101 char *file0, *file1;
102
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700103 asprintf(&file1, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104
105 if (i - 1 == 0) {
106 asprintf(&file0, "%s", g_outputFileName);
107 } else {
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700108 asprintf(&file0, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109 }
110
Traian Schiau59763032015-04-10 15:51:39 +0300111 if (!file0 || !file1) {
112 perror("while rotating log files");
113 break;
114 }
115
116 err = rename(file0, file1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117
118 if (err < 0 && errno != ENOENT) {
119 perror("while rotating log files");
120 }
121
122 free(file1);
123 free(file0);
124 }
125
Traian Schiau59763032015-04-10 15:51:39 +0300126 g_outFD = openLogFile(g_outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127
128 if (g_outFD < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300129 logcat_panic(false, "couldn't open output file");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 }
131
132 g_outByteCount = 0;
133
134}
135
Mark Salyzyn95132e92013-11-22 10:55:48 -0800136void printBinary(struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137{
Mark Salyzyn95132e92013-11-22 10:55:48 -0800138 size_t size = buf->len();
139
140 TEMP_FAILURE_RETRY(write(g_outFD, buf, size));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141}
142
Mark Salyzyn95132e92013-11-22 10:55:48 -0800143static void processBuffer(log_device_t* dev, struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144{
Mathias Agopian50844522010-03-17 16:10:26 -0700145 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 int err;
147 AndroidLogEntry entry;
148 char binaryMsgBuf[1024];
149
Joe Onorato6fa09a02010-02-26 10:04:23 -0800150 if (dev->binary) {
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800151 static bool hasOpenedEventTagMap = false;
152 static EventTagMap *eventTagMap = NULL;
153
154 if (!eventTagMap && !hasOpenedEventTagMap) {
155 eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
156 hasOpenedEventTagMap = true;
157 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800158 err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry,
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800159 eventTagMap,
Mark Salyzyn95132e92013-11-22 10:55:48 -0800160 binaryMsgBuf,
161 sizeof(binaryMsgBuf));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162 //printf(">>> pri=%d len=%d msg='%s'\n",
163 // entry.priority, entry.messageLen, entry.message);
164 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800165 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800167 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 goto error;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800169 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800171 if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800172 bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry);
173
174 if (bytesWritten < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300175 logcat_panic(false, "output error");
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800176 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177 }
178
179 g_outByteCount += bytesWritten;
180
Mark Salyzyn95132e92013-11-22 10:55:48 -0800181 if (g_logRotateSizeKBytes > 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182 && (g_outByteCount / 1024) >= g_logRotateSizeKBytes
183 ) {
184 rotateLogs();
185 }
186
187error:
188 //fprintf (stderr, "Error processing record\n");
189 return;
190}
191
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800192static void maybePrintStart(log_device_t* dev, bool printDividers) {
193 if (!dev->printed || printDividers) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800194 if (g_devCount > 1 && !g_printBinary) {
195 char buf[1024];
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800196 snprintf(buf, sizeof(buf), "--------- %s %s\n",
197 dev->printed ? "switch to" : "beginning of",
Mark Salyzyn95132e92013-11-22 10:55:48 -0800198 dev->device);
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800199 if (write(g_outFD, buf, strlen(buf)) < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300200 logcat_panic(false, "output error");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800201 }
202 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800203 dev->printed = true;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800204 }
205}
206
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207static void setupOutput()
208{
209
210 if (g_outputFileName == NULL) {
211 g_outFD = STDOUT_FILENO;
212
213 } else {
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700214 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
215 fprintf(stderr, "failed to set background scheduling policy\n");
216 }
217
218 struct sched_param param;
219 memset(&param, 0, sizeof(param));
220 if (sched_setscheduler((pid_t) 0, SCHED_BATCH, &param) < 0) {
221 fprintf(stderr, "failed to set to batch scheduler\n");
222 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223
224 g_outFD = openLogFile (g_outputFileName);
225
226 if (g_outFD < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300227 logcat_panic(false, "couldn't open output file");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228 }
229
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700230 struct stat statbuf;
Traian Schiau59763032015-04-10 15:51:39 +0300231 if (fstat(g_outFD, &statbuf) == -1) {
232 close(g_outFD);
233 logcat_panic(false, "couldn't get output file stat\n");
234 }
235
236 if ((size_t) statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
237 close(g_outFD);
238 logcat_panic(false, "invalid output file stat\n");
239 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240
241 g_outByteCount = statbuf.st_size;
242 }
243}
244
245static void show_help(const char *cmd)
246{
247 fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd);
248
249 fprintf(stderr, "options include:\n"
250 " -s Set default filter to silent.\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700251 " Like specifying filterspec '*:S'\n"
Mark Salyzynf3555d92015-05-27 07:39:56 -0700252 " -f <filename> Log to file. Default is stdout\n"
Traian Schiau59763032015-04-10 15:51:39 +0300253 " -r <kbytes> Rotate log every kbytes. Requires -f\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254 " -n <count> Sets max number of rotated logs to <count>, default 4\n"
Pierre Zurekead88fc2010-10-17 22:39:37 +0200255 " -v <format> Sets the log print format, where <format> is:\n\n"
Mark Salyzyne1f20042015-05-06 08:40:40 -0700256 " brief color long process raw tag thread threadtime time usec\n\n"
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800257 " -D print dividers between each log buffer\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258 " -c clear (flush) the entire log and exit\n"
259 " -d dump the log and then exit (don't block)\n"
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800260 " -t <count> print only the most recent <count> lines (implies -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700261 " -t '<time>' print most recent lines since specified time (implies -d)\n"
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800262 " -T <count> print only the most recent <count> lines (does not imply -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700263 " -T '<time>' print most recent lines since specified time (not imply -d)\n"
264 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm'\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265 " -g get the size of the log's ring buffer and exit\n"
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800266 " -L dump logs from prior to last reboot\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800267 " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n"
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700268 " 'events', 'crash' or 'all'. Multiple -b parameters are\n"
269 " allowed and results are interleaved. The default is\n"
270 " -b main -b system -b crash.\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800271 " -B output the log in binary.\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700272 " -S output statistics.\n"
273 " -G <size> set size of log ring buffer, may suffix with K or M.\n"
274 " -p print prune white and ~black list. Service is specified as\n"
275 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
276 " with ~, otherwise weighed for longevity if unadorned. All\n"
277 " other pruning activity is oldest first. Special case ~!\n"
278 " represents an automatic quicker pruning for the noisiest\n"
279 " UID as determined by the current statistics.\n"
280 " -P '<list> ...' set prune white and ~black list, using same format as\n"
281 " printed above. Must be quoted.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282
283 fprintf(stderr,"\nfilterspecs are a series of \n"
284 " <tag>[:priority]\n\n"
285 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700286 " V Verbose (default for <tag>)\n"
287 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288 " I Info\n"
289 " W Warn\n"
290 " E Error\n"
291 " F Fatal\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700292 " S Silent (suppress all output)\n"
293 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
294 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
295 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
296 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
297 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700298 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299}
300
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301static int setLogFormat(const char * formatString)
302{
303 static AndroidLogPrintFormat format;
304
305 format = android_log_formatFromString(formatString);
306
307 if (format == FORMAT_OFF) {
308 // FORMAT_OFF means invalid string
309 return -1;
310 }
311
Mark Salyzyne1f20042015-05-06 08:40:40 -0700312 return android_log_setPrintFormat(g_logformat, format);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313}
314
Mark Salyzyn671e3432014-05-06 07:34:59 -0700315static const char multipliers[][2] = {
316 { "" },
317 { "K" },
318 { "M" },
319 { "G" }
320};
321
322static unsigned long value_of_size(unsigned long value)
323{
324 for (unsigned i = 0;
325 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
326 value /= 1024, ++i) ;
327 return value;
328}
329
330static const char *multiplier_of_size(unsigned long value)
331{
332 unsigned i;
333 for (i = 0;
334 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
335 value /= 1024, ++i) ;
336 return multipliers[i];
337}
338
Traian Schiau59763032015-04-10 15:51:39 +0300339/*String to unsigned int, returns -1 if it fails*/
340static bool getSizeTArg(char *ptr, size_t *val, size_t min = 0,
341 size_t max = SIZE_MAX)
342{
343 char *endp;
344 errno = 0;
345 size_t ret = (size_t) strtoll(ptr, &endp, 0);
346
347 if (endp[0] != '\0' || errno != 0 ) {
348 return false;
349 }
350
351 if (ret > max || ret < min) {
352 return false;
353 }
354
355 *val = ret;
356 return true;
357}
358
359static void logcat_panic(bool showHelp, const char *fmt, ...)
360{
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700361 va_list args;
362 va_start(args, fmt);
363 vfprintf(stderr, fmt, args);
364 va_end(args);
Traian Schiau59763032015-04-10 15:51:39 +0300365
366 if (showHelp) {
367 show_help(getprogname());
368 }
369
370 exit(EXIT_FAILURE);
371}
372
Mark Salyzynf3555d92015-05-27 07:39:56 -0700373static const char g_defaultTimeFormat[] = "%m-%d %H:%M:%S.%q";
374
375// Find last logged line in gestalt of all matching existing output files
376static log_time lastLogTime(char *outputFileName) {
377 log_time retval(log_time::EPOCH);
378 if (!outputFileName) {
379 return retval;
380 }
381
382 log_time now(CLOCK_REALTIME);
383
384 std::string directory;
385 char *file = strrchr(outputFileName, '/');
386 if (!file) {
387 directory = ".";
388 file = outputFileName;
389 } else {
390 *file = '\0';
391 directory = outputFileName;
392 *file = '/';
393 ++file;
394 }
395 size_t len = strlen(file);
396 log_time modulo(0, NS_PER_SEC);
397 std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(directory.c_str()), closedir);
398 struct dirent *dp;
399 while ((dp = readdir(dir.get())) != NULL) {
400 if ((dp->d_type != DT_REG)
401 || strncmp(dp->d_name, file, len)
402 || (dp->d_name[len]
403 && ((dp->d_name[len] != '.')
404 || !isdigit(dp->d_name[len+1])))) {
405 continue;
406 }
407
408 std::string file_name = directory;
409 file_name += "/";
410 file_name += dp->d_name;
411 std::string file;
412 if (!android::base::ReadFileToString(file_name, &file)) {
413 continue;
414 }
415
416 bool found = false;
417 for (const auto& line : android::base::Split(file, "\n")) {
418 log_time t(log_time::EPOCH);
419 char *ep = t.strptime(line.c_str(), g_defaultTimeFormat);
420 if (!ep || (*ep != ' ')) {
421 continue;
422 }
423 // determine the time precision of the logs (eg: msec or usec)
424 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
425 if (t.tv_nsec % (mod * 10)) {
426 modulo.tv_nsec = mod;
427 break;
428 }
429 }
430 // We filter any times later than current as we may not have the
431 // year stored with each log entry. Also, since it is possible for
432 // entries to be recorded out of order (very rare) we select the
433 // maximum we find just in case.
434 if ((t < now) && (t > retval)) {
435 retval = t;
436 found = true;
437 }
438 }
439 // We count on the basename file to be the definitive end, so stop here.
440 if (!dp->d_name[len] && found) {
441 break;
442 }
443 }
444 if (retval == log_time::EPOCH) {
445 return retval;
446 }
447 // tail_time prints matching or higher, round up by the modulo to prevent
448 // a replay of the last entry we have just checked.
449 retval += modulo;
450 return retval;
451}
452
Traian Schiau59763032015-04-10 15:51:39 +0300453} /* namespace android */
454
455
Joe Onorato6fa09a02010-02-26 10:04:23 -0800456int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800457{
Traian Schiau59763032015-04-10 15:51:39 +0300458 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459 int err;
460 int hasSetLogFormat = 0;
461 int clearLog = 0;
462 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800463 unsigned long setLogSize = 0;
464 int getPruneList = 0;
465 char *setPruneList = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800466 int printStatistics = 0;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800467 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800469 log_device_t* devices = NULL;
470 log_device_t* dev;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800471 bool printDividers = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800472 struct logger_list *logger_list;
Traian Schiau59763032015-04-10 15:51:39 +0300473 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800474 log_time tail_time(log_time::EPOCH);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800476 signal(SIGPIPE, exit);
477
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478 g_logformat = android_log_format_new();
479
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
Traian Schiau59763032015-04-10 15:51:39 +0300481 show_help(argv[0]);
482 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483 }
484
485 for (;;) {
486 int ret;
487
Traian Schiau59763032015-04-10 15:51:39 +0300488 ret = getopt(argc, argv, ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800489
490 if (ret < 0) {
491 break;
492 }
493
494 switch(ret) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800495 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800496 // default to all silent
497 android_log_addFilterRule(g_logformat, "*:s");
498 break;
499
500 case 'c':
501 clearLog = 1;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800502 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503 break;
504
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800505 case 'L':
506 mode |= ANDROID_LOG_PSTORE;
507 break;
508
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800510 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511 break;
512
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800513 case 't':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800514 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800515 /* FALLTHRU */
516 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800517 if (strspn(optarg, "0123456789") != strlen(optarg)) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700518 char *cp = tail_time.strptime(optarg, g_defaultTimeFormat);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800519 if (!cp) {
Traian Schiau59763032015-04-10 15:51:39 +0300520 logcat_panic(false,
521 "-%c \"%s\" not in \"%s\" time format\n",
Mark Salyzynf3555d92015-05-27 07:39:56 -0700522 ret, optarg, g_defaultTimeFormat);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800523 }
524 if (*cp) {
525 char c = *cp;
526 *cp = '\0';
527 fprintf(stderr,
528 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
529 ret, optarg, c, cp + 1);
530 *cp = c;
531 }
532 } else {
Traian Schiau59763032015-04-10 15:51:39 +0300533 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800534 fprintf(stderr,
535 "WARNING: -%c %s invalid, setting to 1\n",
536 ret, optarg);
537 tail_lines = 1;
538 }
539 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800540 break;
541
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800542 case 'D':
543 printDividers = true;
544 break;
545
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800546 case 'g':
547 getLogSize = 1;
548 break;
549
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800550 case 'G': {
Traian Schiau59763032015-04-10 15:51:39 +0300551 char *cp;
552 if (strtoll(optarg, &cp, 0) > 0) {
553 setLogSize = strtoll(optarg, &cp, 0);
554 } else {
555 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800556 }
557
558 switch(*cp) {
559 case 'g':
560 case 'G':
561 setLogSize *= 1024;
562 /* FALLTHRU */
563 case 'm':
564 case 'M':
565 setLogSize *= 1024;
566 /* FALLTHRU */
567 case 'k':
568 case 'K':
569 setLogSize *= 1024;
570 /* FALLTHRU */
571 case '\0':
572 break;
573
574 default:
575 setLogSize = 0;
576 }
577
578 if (!setLogSize) {
579 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
Traian Schiau59763032015-04-10 15:51:39 +0300580 return EXIT_FAILURE;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800581 }
582 }
583 break;
584
585 case 'p':
586 getPruneList = 1;
587 break;
588
589 case 'P':
590 setPruneList = optarg;
591 break;
592
Joe Onorato6fa09a02010-02-26 10:04:23 -0800593 case 'b': {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800594 if (strcmp(optarg, "all") == 0) {
595 while (devices) {
596 dev = devices;
597 devices = dev->next;
598 delete dev;
599 }
600
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700601 devices = dev = NULL;
Traian Schiau59763032015-04-10 15:51:39 +0300602 g_devCount = 0;
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700603 for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
604 const char *name = android_log_id_to_name((log_id_t)i);
605 log_id_t log_id = android_name_to_log_id(name);
606
607 if (log_id != (log_id_t)i) {
608 continue;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800609 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700610
611 bool binary = strcmp(name, "events") == 0;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800612 log_device_t* d = new log_device_t(name, binary);
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700613
614 if (dev) {
615 dev->next = d;
616 dev = d;
617 } else {
618 devices = dev = d;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800619 }
Traian Schiau59763032015-04-10 15:51:39 +0300620 g_devCount++;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800621 }
622 break;
623 }
624
Joe Onorato6fa09a02010-02-26 10:04:23 -0800625 bool binary = strcmp(optarg, "events") == 0;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800626
627 if (devices) {
628 dev = devices;
629 while (dev->next) {
630 dev = dev->next;
631 }
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800632 dev->next = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800633 } else {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800634 devices = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800635 }
Traian Schiau59763032015-04-10 15:51:39 +0300636 g_devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800637 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800638 break;
639
640 case 'B':
Traian Schiau59763032015-04-10 15:51:39 +0300641 g_printBinary = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800642 break;
643
644 case 'f':
Mark Salyzynf3555d92015-05-27 07:39:56 -0700645 if ((tail_time == log_time::EPOCH) && (tail_lines != 0)) {
646 tail_time = lastLogTime(optarg);
647 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800648 // redirect output to a file
Traian Schiau59763032015-04-10 15:51:39 +0300649 g_outputFileName = optarg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800650 break;
651
652 case 'r':
Traian Schiau59763032015-04-10 15:51:39 +0300653 if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) {
654 logcat_panic(true, "Invalid parameter %s to -r\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800655 }
656 break;
657
658 case 'n':
Traian Schiau59763032015-04-10 15:51:39 +0300659 if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) {
660 logcat_panic(true, "Invalid parameter %s to -n\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800661 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800662 break;
663
664 case 'v':
665 err = setLogFormat (optarg);
666 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300667 logcat_panic(true, "Invalid parameter %s to -v\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800668 }
Mark Salyzyne1f20042015-05-06 08:40:40 -0700669 hasSetLogFormat |= err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800670 break;
671
672 case 'Q':
673 /* this is a *hidden* option used to start a version of logcat */
674 /* in an emulated device only. it basically looks for androidboot.logcat= */
675 /* on the kernel command line. If something is found, it extracts a log filter */
676 /* and uses it to run the program. If nothing is found, the program should */
677 /* quit immediately */
678#define KERNEL_OPTION "androidboot.logcat="
679#define CONSOLE_OPTION "androidboot.console="
680 {
681 int fd;
682 char* logcat;
683 char* console;
684 int force_exit = 1;
685 static char cmdline[1024];
686
687 fd = open("/proc/cmdline", O_RDONLY);
688 if (fd >= 0) {
689 int n = read(fd, cmdline, sizeof(cmdline)-1 );
690 if (n < 0) n = 0;
691 cmdline[n] = 0;
692 close(fd);
693 } else {
694 cmdline[0] = 0;
695 }
696
697 logcat = strstr( cmdline, KERNEL_OPTION );
698 console = strstr( cmdline, CONSOLE_OPTION );
699 if (logcat != NULL) {
700 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
701 char* q = strpbrk( p, " \t\n\r" );;
702
703 if (q != NULL)
704 *q = 0;
705
706 forceFilters = p;
707 force_exit = 0;
708 }
709 /* if nothing found or invalid filters, exit quietly */
Traian Schiau59763032015-04-10 15:51:39 +0300710 if (force_exit) {
711 return EXIT_SUCCESS;
712 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800713
714 /* redirect our output to the emulator console */
715 if (console) {
716 char* p = console + sizeof(CONSOLE_OPTION)-1;
717 char* q = strpbrk( p, " \t\n\r" );
718 char devname[64];
719 int len;
720
721 if (q != NULL) {
722 len = q - p;
723 } else
724 len = strlen(p);
725
726 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
727 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
728 if (len < (int)sizeof(devname)) {
729 fd = open( devname, O_WRONLY );
730 if (fd >= 0) {
731 dup2(fd, 1);
732 dup2(fd, 2);
733 close(fd);
734 }
735 }
736 }
737 }
738 break;
739
Mark Salyzyn34facab2014-02-06 14:48:50 -0800740 case 'S':
741 printStatistics = 1;
742 break;
743
Traian Schiau59763032015-04-10 15:51:39 +0300744 case ':':
745 logcat_panic(true, "Option -%c needs an argument\n", optopt);
746 break;
747
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800748 default:
Traian Schiau59763032015-04-10 15:51:39 +0300749 logcat_panic(true, "Unrecognized Option %c\n", optopt);
750 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800751 }
752 }
753
Joe Onorato6fa09a02010-02-26 10:04:23 -0800754 if (!devices) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800755 dev = devices = new log_device_t("main", false);
Traian Schiau59763032015-04-10 15:51:39 +0300756 g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800757 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800758 dev = dev->next = new log_device_t("system", false);
Traian Schiau59763032015-04-10 15:51:39 +0300759 g_devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800760 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700761 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800762 dev = dev->next = new log_device_t("crash", false);
Traian Schiau59763032015-04-10 15:51:39 +0300763 g_devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700764 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800765 }
766
Traian Schiau59763032015-04-10 15:51:39 +0300767 if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) {
768 logcat_panic(true, "-r requires -f as well\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800769 }
770
Traian Schiau59763032015-04-10 15:51:39 +0300771 setupOutput();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800772
773 if (hasSetLogFormat == 0) {
774 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
775
776 if (logFormat != NULL) {
777 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800778 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800779 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800780 logFormat);
781 }
Mark Salyzyn649fc602014-09-16 09:15:15 -0700782 } else {
783 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800784 }
785 }
786
787 if (forceFilters) {
788 err = android_log_addFilterString(g_logformat, forceFilters);
789 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300790 logcat_panic(false, "Invalid filter expression in logcat args\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800791 }
792 } else if (argc == optind) {
793 // Add from environment variable
794 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
795
796 if (env_tags_orig != NULL) {
797 err = android_log_addFilterString(g_logformat, env_tags_orig);
798
Mark Salyzyn95132e92013-11-22 10:55:48 -0800799 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300800 logcat_panic(true,
801 "Invalid filter expression in ANDROID_LOG_TAGS\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800802 }
803 }
804 } else {
805 // Add from commandline
806 for (int i = optind ; i < argc ; i++) {
807 err = android_log_addFilterString(g_logformat, argv[i]);
808
Mark Salyzyn95132e92013-11-22 10:55:48 -0800809 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300810 logcat_panic(true, "Invalid filter expression '%s'\n", argv[i]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800811 }
812 }
813 }
814
Joe Onorato6fa09a02010-02-26 10:04:23 -0800815 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800816 if (tail_time != log_time::EPOCH) {
817 logger_list = android_logger_list_alloc_time(mode, tail_time, 0);
818 } else {
819 logger_list = android_logger_list_alloc(mode, tail_lines, 0);
820 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800821 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800822 dev->logger_list = logger_list;
823 dev->logger = android_logger_open(logger_list,
824 android_name_to_log_id(dev->device));
825 if (!dev->logger) {
Traian Schiau59763032015-04-10 15:51:39 +0300826 logcat_panic(false, "Unable to open log device '%s'\n",
827 dev->device);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800828 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800829
830 if (clearLog) {
831 int ret;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800832 ret = android_logger_clear(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800833 if (ret) {
Traian Schiau59763032015-04-10 15:51:39 +0300834 logcat_panic(false, "failed to clear the log");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800835 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800836 }
837
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800838 if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) {
Traian Schiau59763032015-04-10 15:51:39 +0300839 logcat_panic(false, "failed to set the log size");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800840 }
841
Joe Onorato6fa09a02010-02-26 10:04:23 -0800842 if (getLogSize) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800843 long size, readable;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800844
Mark Salyzyn95132e92013-11-22 10:55:48 -0800845 size = android_logger_get_log_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800846 if (size < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300847 logcat_panic(false, "failed to get the log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800848 }
849
Mark Salyzyn95132e92013-11-22 10:55:48 -0800850 readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800851 if (readable < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300852 logcat_panic(false, "failed to get the readable log size");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800853 }
854
Mark Salyzyn671e3432014-05-06 07:34:59 -0700855 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
Joe Onorato6fa09a02010-02-26 10:04:23 -0800856 "max entry is %db, max payload is %db\n", dev->device,
Mark Salyzyn671e3432014-05-06 07:34:59 -0700857 value_of_size(size), multiplier_of_size(size),
858 value_of_size(readable), multiplier_of_size(readable),
Joe Onorato6fa09a02010-02-26 10:04:23 -0800859 (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD);
860 }
861
862 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800863 }
864
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800865 if (setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +0300866 size_t len = strlen(setPruneList);
867 /*extra 32 bytes are needed by android_logger_set_prune_list */
868 size_t bLen = len + 32;
869 char *buf = NULL;
870 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
871 buf[len] = '\0';
872 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
873 logcat_panic(false, "failed to set the prune list");
874 }
875 free(buf);
876 } else {
877 logcat_panic(false, "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800878 }
879 }
880
Mark Salyzyn1c950472014-04-01 17:19:47 -0700881 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800882 size_t len = 8192;
883 char *buf;
884
885 for(int retry = 32;
886 (retry >= 0) && ((buf = new char [len]));
Traian Schiau59763032015-04-10 15:51:39 +0300887 delete [] buf, buf = NULL, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800888 if (getPruneList) {
889 android_logger_get_prune_list(logger_list, buf, len);
890 } else {
891 android_logger_get_statistics(logger_list, buf, len);
892 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800893 buf[len-1] = '\0';
Traian Schiau59763032015-04-10 15:51:39 +0300894 if (atol(buf) < 3) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800895 delete [] buf;
896 buf = NULL;
897 break;
898 }
Traian Schiau59763032015-04-10 15:51:39 +0300899 size_t ret = atol(buf) + 1;
900 if (ret <= len) {
901 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800902 break;
903 }
Traian Schiau59763032015-04-10 15:51:39 +0300904 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800905 }
906
907 if (!buf) {
Traian Schiau59763032015-04-10 15:51:39 +0300908 logcat_panic(false, "failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -0800909 }
910
911 // remove trailing FF
912 char *cp = buf + len - 1;
913 *cp = '\0';
914 bool truncated = *--cp != '\f';
915 if (!truncated) {
916 *cp = '\0';
917 }
918
919 // squash out the byte count
920 cp = buf;
921 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -0700922 while (isdigit(*cp)) {
923 ++cp;
924 }
925 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800926 ++cp;
927 }
928 }
929
930 printf("%s", cp);
931 delete [] buf;
Traian Schiau59763032015-04-10 15:51:39 +0300932 return EXIT_SUCCESS;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800933 }
934
935
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800936 if (getLogSize) {
Traian Schiau59763032015-04-10 15:51:39 +0300937 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800938 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800939 if (setLogSize || setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +0300940 return EXIT_SUCCESS;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800941 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800942 if (clearLog) {
Traian Schiau59763032015-04-10 15:51:39 +0300943 return EXIT_SUCCESS;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800944 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800945
946 //LOG_EVENT_INT(10, 12345);
947 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
948 //LOG_EVENT_STRING(0, "whassup, doc?");
949
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800950 dev = NULL;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800951 log_device_t unexpected("unexpected", false);
Mark Salyzyn95132e92013-11-22 10:55:48 -0800952 while (1) {
953 struct log_msg log_msg;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800954 log_device_t* d;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800955 int ret = android_logger_list_read(logger_list, &log_msg);
956
957 if (ret == 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300958 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800959 }
960
961 if (ret < 0) {
962 if (ret == -EAGAIN) {
963 break;
964 }
965
966 if (ret == -EIO) {
Traian Schiau59763032015-04-10 15:51:39 +0300967 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800968 }
969 if (ret == -EINVAL) {
Traian Schiau59763032015-04-10 15:51:39 +0300970 logcat_panic(false, "read: unexpected length.\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800971 }
Traian Schiau59763032015-04-10 15:51:39 +0300972 logcat_panic(false, "logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -0800973 }
974
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800975 for(d = devices; d; d = d->next) {
976 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800977 break;
978 }
979 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800980 if (!d) {
Traian Schiau59763032015-04-10 15:51:39 +0300981 g_devCount = 2; // set to Multiple
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800982 d = &unexpected;
983 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800984 }
985
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800986 if (dev != d) {
987 dev = d;
Traian Schiau59763032015-04-10 15:51:39 +0300988 maybePrintStart(dev, printDividers);
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800989 }
Traian Schiau59763032015-04-10 15:51:39 +0300990 if (g_printBinary) {
991 printBinary(&log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -0800992 } else {
Traian Schiau59763032015-04-10 15:51:39 +0300993 processBuffer(dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -0800994 }
995 }
996
997 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800998
Traian Schiau59763032015-04-10 15:51:39 +0300999 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001000}