blob: d0c693d31665d50a32c39a45db9fbd3682cb1d1a [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>
Kristian Monsen562e5132015-06-05 14:10:12 -07009#include <getopt.h>
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070010#include <math.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070011#include <sched.h>
12#include <signal.h>
13#include <stdarg.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080014#include <stdio.h>
15#include <stdlib.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080016#include <string.h>
Traian Schiau59763032015-04-10 15:51:39 +030017#include <sys/cdefs.h>
Riley Andrewsaede9892015-06-08 23:36:34 -070018#include <sys/resource.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080019#include <sys/socket.h>
20#include <sys/stat.h>
Mark Salyzynf3555d92015-05-27 07:39:56 -070021#include <sys/types.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070022#include <time.h>
23#include <unistd.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080024
Mark Salyzynf3555d92015-05-27 07:39:56 -070025#include <memory>
26#include <string>
27
Elliott Hughes4f713192015-12-04 22:00:26 -080028#include <android-base/file.h>
Mark Salyzyn5b1a5382016-08-03 14:20:41 -070029#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080030#include <android-base/strings.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070031#include <cutils/sched_policy.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080032#include <cutils/sockets.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070033#include <log/event_tag_map.h>
Colin Cross9227bd32013-07-23 16:59:20 -070034#include <log/logprint.h>
Mark Salyzynaeaaf812016-09-30 13:30:33 -070035#include <private/android_logger.h>
Elliott Hughesb9e53b42016-02-17 11:58:01 -080036#include <system/thread_defs.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037
Casey Dahlindc42a872016-03-17 16:18:55 -070038#include <pcrecpp.h>
39
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#define DEFAULT_MAX_ROTATED_LOGS 4
41
42static AndroidLogFormat * g_logformat;
43
44/* logd prefixes records with a length field */
45#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
46
Joe Onorato6fa09a02010-02-26 10:04:23 -080047struct log_device_t {
Mark Salyzyn95132e92013-11-22 10:55:48 -080048 const char* device;
Joe Onorato6fa09a02010-02-26 10:04:23 -080049 bool binary;
Mark Salyzyn95132e92013-11-22 10:55:48 -080050 struct logger *logger;
51 struct logger_list *logger_list;
Joe Onorato6fa09a02010-02-26 10:04:23 -080052 bool printed;
Joe Onorato6fa09a02010-02-26 10:04:23 -080053
Joe Onorato6fa09a02010-02-26 10:04:23 -080054 log_device_t* next;
55
Mark Salyzyn5f6738a2015-02-27 13:41:34 -080056 log_device_t(const char* d, bool b) {
Joe Onorato6fa09a02010-02-26 10:04:23 -080057 device = d;
58 binary = b;
Joe Onorato6fa09a02010-02-26 10:04:23 -080059 next = NULL;
60 printed = false;
Traian Schiau59763032015-04-10 15:51:39 +030061 logger = NULL;
62 logger_list = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -080063 }
Joe Onorato6fa09a02010-02-26 10:04:23 -080064};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065
66namespace android {
67
68/* Global Variables */
69
Mark Salyzync6d66522016-03-30 12:38:29 -070070static const char * g_outputFileName;
Traian Schiau59763032015-04-10 15:51:39 +030071// 0 means "no log rotation"
Mark Salyzync6d66522016-03-30 12:38:29 -070072static size_t g_logRotateSizeKBytes;
Traian Schiau59763032015-04-10 15:51:39 +030073// 0 means "unbounded"
74static size_t g_maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075static int g_outFD = -1;
Mark Salyzync6d66522016-03-30 12:38:29 -070076static size_t g_outByteCount;
77static int g_printBinary;
78static int g_devCount; // >1 means multiple
Casey Dahlindc42a872016-03-17 16:18:55 -070079static pcrecpp::RE* g_regex;
Casey Dahlin6ac498d2016-03-17 14:04:52 -070080// 0 means "infinite"
Mark Salyzync6d66522016-03-30 12:38:29 -070081static size_t g_maxCount;
82static size_t g_printCount;
Mark Salyzync9202772016-03-30 09:38:31 -070083static bool g_printItAnyways;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084
Mark Salyzyn4fd05072016-10-18 11:30:11 -070085enum helpType {
86 HELP_FALSE,
87 HELP_TRUE,
88 HELP_FORMAT
89};
90
Mark Salyzynaa730c12016-03-30 12:38:29 -070091// if showHelp is set, newline required in fmt statement to transition to usage
Mark Salyzyn4fd05072016-10-18 11:30:11 -070092__noreturn static void logcat_panic(enum helpType showHelp, const char *fmt, ...) __printflike(2,3);
Traian Schiau59763032015-04-10 15:51:39 +030093
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094static int openLogFile (const char *pathname)
95{
Edwin Vane80b221c2012-08-13 12:55:07 -040096 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097}
98
99static void rotateLogs()
100{
101 int err;
102
103 // Can't rotate logs if we're not outputting to a file
104 if (g_outputFileName == NULL) {
105 return;
106 }
107
108 close(g_outFD);
109
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700110 // Compute the maximum number of digits needed to count up to g_maxRotatedLogs in decimal.
111 // eg: g_maxRotatedLogs == 30 -> log10(30) == 1.477 -> maxRotationCountDigits == 2
112 int maxRotationCountDigits =
113 (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
114
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115 for (int i = g_maxRotatedLogs ; i > 0 ; i--) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700116 std::string file1 = android::base::StringPrintf(
117 "%s.%.*d", g_outputFileName, maxRotationCountDigits, i);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700119 std::string file0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120 if (i - 1 == 0) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700121 file0 = android::base::StringPrintf("%s", g_outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122 } else {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700123 file0 = android::base::StringPrintf(
124 "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125 }
126
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700127 if ((file0.length() == 0) || (file1.length() == 0)) {
Traian Schiau59763032015-04-10 15:51:39 +0300128 perror("while rotating log files");
129 break;
130 }
131
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700132 err = rename(file0.c_str(), file1.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133
134 if (err < 0 && errno != ENOENT) {
135 perror("while rotating log files");
136 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 }
138
Traian Schiau59763032015-04-10 15:51:39 +0300139 g_outFD = openLogFile(g_outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140
141 if (g_outFD < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700142 logcat_panic(HELP_FALSE, "couldn't open output file");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 }
144
145 g_outByteCount = 0;
146
147}
148
Mark Salyzyn95132e92013-11-22 10:55:48 -0800149void printBinary(struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150{
Mark Salyzyn95132e92013-11-22 10:55:48 -0800151 size_t size = buf->len();
152
153 TEMP_FAILURE_RETRY(write(g_outFD, buf, size));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154}
155
Mark Salyzynaa730c12016-03-30 12:38:29 -0700156static bool regexOk(const AndroidLogEntry& entry)
Casey Dahlindc42a872016-03-17 16:18:55 -0700157{
Mark Salyzynaa730c12016-03-30 12:38:29 -0700158 if (!g_regex) {
Casey Dahlindc42a872016-03-17 16:18:55 -0700159 return true;
160 }
161
Casey Dahlindc42a872016-03-17 16:18:55 -0700162 std::string messageString(entry.message, entry.messageLen);
163
164 return g_regex->PartialMatch(messageString);
165}
166
Mark Salyzyn95132e92013-11-22 10:55:48 -0800167static void processBuffer(log_device_t* dev, struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168{
Mathias Agopian50844522010-03-17 16:10:26 -0700169 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170 int err;
171 AndroidLogEntry entry;
172 char binaryMsgBuf[1024];
173
Joe Onorato6fa09a02010-02-26 10:04:23 -0800174 if (dev->binary) {
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800175 static bool hasOpenedEventTagMap = false;
176 static EventTagMap *eventTagMap = NULL;
177
178 if (!eventTagMap && !hasOpenedEventTagMap) {
179 eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
180 hasOpenedEventTagMap = true;
181 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800182 err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry,
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800183 eventTagMap,
Mark Salyzyn95132e92013-11-22 10:55:48 -0800184 binaryMsgBuf,
185 sizeof(binaryMsgBuf));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 //printf(">>> pri=%d len=%d msg='%s'\n",
187 // entry.priority, entry.messageLen, entry.message);
188 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800189 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800191 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 goto error;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800193 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194
Mark Salyzyn9f53cac2016-10-24 13:11:46 -0700195 if (android_log_shouldPrintLine(g_logformat,
196 std::string(entry.tag, entry.tagLen).c_str(),
197 entry.priority)) {
Mark Salyzync9202772016-03-30 09:38:31 -0700198 bool match = regexOk(entry);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800199
Mark Salyzync9202772016-03-30 09:38:31 -0700200 g_printCount += match;
201 if (match || g_printItAnyways) {
202 bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry);
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700203
Mark Salyzync9202772016-03-30 09:38:31 -0700204 if (bytesWritten < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700205 logcat_panic(HELP_FALSE, "output error");
Mark Salyzync9202772016-03-30 09:38:31 -0700206 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800207 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 }
209
210 g_outByteCount += bytesWritten;
211
Mark Salyzyn95132e92013-11-22 10:55:48 -0800212 if (g_logRotateSizeKBytes > 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213 && (g_outByteCount / 1024) >= g_logRotateSizeKBytes
214 ) {
215 rotateLogs();
216 }
217
218error:
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219 return;
220}
221
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800222static void maybePrintStart(log_device_t* dev, bool printDividers) {
223 if (!dev->printed || printDividers) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800224 if (g_devCount > 1 && !g_printBinary) {
225 char buf[1024];
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800226 snprintf(buf, sizeof(buf), "--------- %s %s\n",
227 dev->printed ? "switch to" : "beginning of",
Mark Salyzyn95132e92013-11-22 10:55:48 -0800228 dev->device);
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800229 if (write(g_outFD, buf, strlen(buf)) < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700230 logcat_panic(HELP_FALSE, "output error");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800231 }
232 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800233 dev->printed = true;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800234 }
235}
236
Mark Salyzynad5e4112016-08-04 07:53:52 -0700237static void setupOutputAndSchedulingPolicy(bool blocking) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 if (g_outputFileName == NULL) {
239 g_outFD = STDOUT_FILENO;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700240 return;
241 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242
Mark Salyzynad5e4112016-08-04 07:53:52 -0700243 if (blocking) {
244 // Lower priority and set to batch scheduling if we are saving
245 // the logs into files and taking continuous content.
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700246 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
247 fprintf(stderr, "failed to set background scheduling policy\n");
248 }
249
250 struct sched_param param;
251 memset(&param, 0, sizeof(param));
252 if (sched_setscheduler((pid_t) 0, SCHED_BATCH, &param) < 0) {
253 fprintf(stderr, "failed to set to batch scheduler\n");
254 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255
Riley Andrewsaede9892015-06-08 23:36:34 -0700256 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
257 fprintf(stderr, "failed set to priority\n");
258 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259 }
Mark Salyzynad5e4112016-08-04 07:53:52 -0700260
261 g_outFD = openLogFile (g_outputFileName);
262
263 if (g_outFD < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700264 logcat_panic(HELP_FALSE, "couldn't open output file");
Mark Salyzynad5e4112016-08-04 07:53:52 -0700265 }
266
267 struct stat statbuf;
268 if (fstat(g_outFD, &statbuf) == -1) {
269 close(g_outFD);
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700270 logcat_panic(HELP_FALSE, "couldn't get output file stat\n");
Mark Salyzynad5e4112016-08-04 07:53:52 -0700271 }
272
273 if ((size_t) statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
274 close(g_outFD);
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700275 logcat_panic(HELP_FALSE, "invalid output file stat\n");
Mark Salyzynad5e4112016-08-04 07:53:52 -0700276 }
277
278 g_outByteCount = statbuf.st_size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800279}
280
281static void show_help(const char *cmd)
282{
283 fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd);
284
285 fprintf(stderr, "options include:\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700286 " -s Set default filter to silent. Equivalent to filterspec '*:S'\n"
287 " -f <file>, --file=<file> Log to file. Default is stdout\n"
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700288 " -r <kbytes>, --rotate-kbytes=<kbytes>\n"
289 " Rotate log every kbytes. Requires -f option\n"
290 " -n <count>, --rotate-count=<count>\n"
291 " Sets max number of rotated logs to <count>, default 4\n"
Mark Salyzyn02687e72016-08-03 14:20:41 -0700292 " --id=<id> If the signature id for logging to file changes, then clear\n"
293 " the fileset and continue\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700294 " -v <format>, --format=<format>\n"
Mark Salyzyn9cfd1c62016-07-06 11:12:14 -0700295 " Sets log print format verb and adverbs, where <format> is:\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700296 " brief help long process raw tag thread threadtime time\n"
Mark Salyzyne735a732016-07-06 13:30:40 -0700297 " and individually flagged modifying adverbs can be added:\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700298 " color descriptive epoch monotonic printable uid\n"
299 " usec UTC year zone\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700300 " -D, --dividers Print dividers between each log buffer\n"
301 " -c, --clear Clear (flush) the entire log and exit\n"
Mark Salyzynb7d059b2016-06-06 14:56:00 -0700302 " if Log to File specified, clear fileset instead\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700303 " -d Dump the log and then exit (don't block)\n"
304 " -e <expr>, --regex=<expr>\n"
305 " Only print lines where the log message matches <expr>\n"
306 " where <expr> is a regular expression\n"
307 // Leave --head undocumented as alias for -m
308 " -m <count>, --max-count=<count>\n"
309 " Quit after printing <count> lines. This is meant to be\n"
310 " paired with --regex, but will work on its own.\n"
311 " --print Paired with --regex and --max-count to let content bypass\n"
Mark Salyzync9202772016-03-30 09:38:31 -0700312 " regex filter but still stop at number of matches.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700313 // Leave --tail undocumented as alias for -t
314 " -t <count> Print only the most recent <count> lines (implies -d)\n"
315 " -t '<time>' Print most recent lines since specified time (implies -d)\n"
316 " -T <count> Print only the most recent <count> lines (does not imply -d)\n"
317 " -T '<time>' Print most recent lines since specified time (not imply -d)\n"
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700318 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700319 " 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700320 " -g, --buffer-size Get the size of the ring buffer.\n"
321 " -G <size>, --buffer-size=<size>\n"
322 " Set size of log ring buffer, may suffix with K or M.\n"
323 " -L, -last Dump logs from prior to last reboot\n"
Mark Salyzyn083b0372015-12-04 10:59:45 -0800324 // Leave security (Device Owner only installations) and
325 // kernel (userdebug and eng) buffers undocumented.
Mark Salyzyn378f4742016-04-12 09:11:46 -0700326 " -b <buffer>, --buffer=<buffer> Request alternate ring buffer, 'main',\n"
327 " 'system', 'radio', 'events', 'crash', 'default' or 'all'.\n"
Mark Salyzyn45177732016-04-11 14:03:48 -0700328 " Multiple -b parameters or comma separated list of buffers are\n"
329 " allowed. Buffers interleaved. Default -b main,system,crash.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700330 " -B, --binary Output the log in binary.\n"
331 " -S, --statistics Output statistics.\n"
332 " -p, --prune Print prune white and ~black list. Service is specified as\n"
333 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700334 " with ~, otherwise weighed for longevity if unadorned. All\n"
335 " other pruning activity is oldest first. Special case ~!\n"
336 " represents an automatic quicker pruning for the noisiest\n"
337 " UID as determined by the current statistics.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700338 " -P '<list> ...', --prune='<list> ...'\n"
339 " Set prune white and ~black list, using same format as\n"
340 " listed above. Must be quoted.\n"
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800341 " --pid=<pid> Only prints logs from the given pid.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700342 // Check ANDROID_LOG_WRAP_DEFAULT_TIMEOUT value for match to 2 hours
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800343 " --wrap Sleep for 2 hours or when buffer about to wrap whichever\n"
344 " comes first. Improves efficiency of polling by providing\n"
345 " an about-to-wrap wakeup.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700347 fprintf(stderr,"\nfilterspecs are a series of \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348 " <tag>[:priority]\n\n"
349 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700350 " V Verbose (default for <tag>)\n"
351 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352 " I Info\n"
353 " W Warn\n"
354 " E Error\n"
355 " F Fatal\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700356 " S Silent (suppress all output)\n"
357 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
358 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
359 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
360 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
361 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700362 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363}
364
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700365static void show_format_help()
366{
367 fprintf(stderr,
368 "-v <format>, --format=<format> options:\n"
369 " Sets log print format verb and adverbs, where <format> is:\n"
370 " brief long process raw tag thread threadtime time\n"
371 " and individually flagged modifying adverbs can be added:\n"
372 " color descriptive epoch monotonic printable uid usec UTC year zone\n"
373 "\nSingle format verbs:\n"
374 " brief — Display priority/tag and PID of the process issuing the message.\n"
375 " long — Display all metadata fields, separate messages with blank lines.\n"
376 " process — Display PID only.\n"
377 " raw — Display the raw log message, with no other metadata fields.\n"
378 " tag — Display the priority/tag only.\n"
379 " threadtime — Display the date, invocation time, priority, tag, and the PID\n"
380 " and TID of the thread issuing the message. (the default format).\n"
381 " time — Display the date, invocation time, priority/tag, and PID of the\n"
382 " process issuing the message.\n"
383 "\nAdverb modifiers can be used in combination:\n"
384 " color — Display in highlighted color to match priority. i.e. \x1B[38;5;231mVERBOSE\n"
385 " \x1B[38;5;75mDEBUG \x1B[38;5;40mINFO \x1B[38;5;166mWARNING \x1B[38;5;196mERROR FATAL\x1B[0m\n"
386 " descriptive — events logs only, descriptions from event-log-tags database.\n"
387 " epoch — Display time as seconds since Jan 1 1970.\n"
388 " monotonic — Display time as cpu seconds since last boot.\n"
389 " printable — Ensure that any binary logging content is escaped.\n"
390 " uid — If permitted, display the UID or Android ID of logged process.\n"
391 " usec — Display time down the microsecond precision.\n"
392 " UTC — Display time as UTC.\n"
393 " year — Add the year to the displayed time.\n"
394 " zone — Add the local timezone to the displayed time.\n"
395 " \"<zone>\" — Print using this public named timezone (experimental).\n\n"
396 );
397}
398
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399static int setLogFormat(const char * formatString)
400{
401 static AndroidLogPrintFormat format;
402
403 format = android_log_formatFromString(formatString);
404
405 if (format == FORMAT_OFF) {
406 // FORMAT_OFF means invalid string
407 return -1;
408 }
409
Mark Salyzyne1f20042015-05-06 08:40:40 -0700410 return android_log_setPrintFormat(g_logformat, format);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800411}
412
Mark Salyzyn671e3432014-05-06 07:34:59 -0700413static const char multipliers[][2] = {
414 { "" },
415 { "K" },
416 { "M" },
417 { "G" }
418};
419
420static unsigned long value_of_size(unsigned long value)
421{
422 for (unsigned i = 0;
423 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
424 value /= 1024, ++i) ;
425 return value;
426}
427
428static const char *multiplier_of_size(unsigned long value)
429{
430 unsigned i;
431 for (i = 0;
432 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
433 value /= 1024, ++i) ;
434 return multipliers[i];
435}
436
Traian Schiau59763032015-04-10 15:51:39 +0300437/*String to unsigned int, returns -1 if it fails*/
Mark Salyzyn33c26252016-04-12 09:11:46 -0700438static bool getSizeTArg(const char *ptr, size_t *val, size_t min = 0,
Traian Schiau59763032015-04-10 15:51:39 +0300439 size_t max = SIZE_MAX)
440{
Kristian Monsen562e5132015-06-05 14:10:12 -0700441 if (!ptr) {
Traian Schiau59763032015-04-10 15:51:39 +0300442 return false;
443 }
444
Kristian Monsen562e5132015-06-05 14:10:12 -0700445 char *endp;
446 errno = 0;
447 size_t ret = (size_t)strtoll(ptr, &endp, 0);
448
449 if (endp[0] || errno) {
450 return false;
451 }
452
453 if ((ret > max) || (ret < min)) {
Traian Schiau59763032015-04-10 15:51:39 +0300454 return false;
455 }
456
457 *val = ret;
458 return true;
459}
460
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700461static void logcat_panic(enum helpType showHelp, const char *fmt, ...)
Traian Schiau59763032015-04-10 15:51:39 +0300462{
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700463 va_list args;
464 va_start(args, fmt);
465 vfprintf(stderr, fmt, args);
466 va_end(args);
Traian Schiau59763032015-04-10 15:51:39 +0300467
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700468 switch (showHelp) {
469 case HELP_TRUE:
Traian Schiau59763032015-04-10 15:51:39 +0300470 show_help(getprogname());
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700471 break;
472 case HELP_FORMAT:
473 show_format_help();
474 break;
475 case HELP_FALSE:
476 default:
477 break;
Traian Schiau59763032015-04-10 15:51:39 +0300478 }
479
480 exit(EXIT_FAILURE);
481}
482
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700483static char *parseTime(log_time &t, const char *cp) {
484
485 char *ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
486 if (ep) {
487 return ep;
488 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700489 ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
490 if (ep) {
491 return ep;
492 }
493 return t.strptime(cp, "%s.%q");
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700494}
Mark Salyzynf3555d92015-05-27 07:39:56 -0700495
Mark Salyzyn31961062016-08-04 07:43:46 -0700496// Find last logged line in <outputFileName>, or <outputFileName>.1
Mark Salyzynf3555d92015-05-27 07:39:56 -0700497static log_time lastLogTime(char *outputFileName) {
498 log_time retval(log_time::EPOCH);
499 if (!outputFileName) {
500 return retval;
501 }
502
Mark Salyzynf3555d92015-05-27 07:39:56 -0700503 std::string directory;
504 char *file = strrchr(outputFileName, '/');
505 if (!file) {
506 directory = ".";
507 file = outputFileName;
508 } else {
509 *file = '\0';
510 directory = outputFileName;
511 *file = '/';
512 ++file;
513 }
Mark Salyzync18c2132016-04-01 07:52:20 -0700514
515 std::unique_ptr<DIR, int(*)(DIR*)>
516 dir(opendir(directory.c_str()), closedir);
517 if (!dir.get()) {
518 return retval;
519 }
520
Mark Salyzyn31961062016-08-04 07:43:46 -0700521 log_time now(android_log_clockid());
Mark Salyzync18c2132016-04-01 07:52:20 -0700522
Mark Salyzynf3555d92015-05-27 07:39:56 -0700523 size_t len = strlen(file);
524 log_time modulo(0, NS_PER_SEC);
Mark Salyzynf3555d92015-05-27 07:39:56 -0700525 struct dirent *dp;
Mark Salyzync18c2132016-04-01 07:52:20 -0700526
Mark Salyzynf3555d92015-05-27 07:39:56 -0700527 while ((dp = readdir(dir.get())) != NULL) {
Mark Salyzyn31961062016-08-04 07:43:46 -0700528 if ((dp->d_type != DT_REG) ||
529 (strncmp(dp->d_name, file, len) != 0) ||
530 (dp->d_name[len] &&
531 ((dp->d_name[len] != '.') ||
532 (strtoll(dp->d_name + 1, NULL, 10) != 1)))) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700533 continue;
534 }
535
536 std::string file_name = directory;
537 file_name += "/";
538 file_name += dp->d_name;
539 std::string file;
540 if (!android::base::ReadFileToString(file_name, &file)) {
541 continue;
542 }
543
544 bool found = false;
545 for (const auto& line : android::base::Split(file, "\n")) {
546 log_time t(log_time::EPOCH);
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700547 char *ep = parseTime(t, line.c_str());
Mark Salyzynf3555d92015-05-27 07:39:56 -0700548 if (!ep || (*ep != ' ')) {
549 continue;
550 }
551 // determine the time precision of the logs (eg: msec or usec)
552 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
553 if (t.tv_nsec % (mod * 10)) {
554 modulo.tv_nsec = mod;
555 break;
556 }
557 }
558 // We filter any times later than current as we may not have the
559 // year stored with each log entry. Also, since it is possible for
560 // entries to be recorded out of order (very rare) we select the
561 // maximum we find just in case.
562 if ((t < now) && (t > retval)) {
563 retval = t;
564 found = true;
565 }
566 }
567 // We count on the basename file to be the definitive end, so stop here.
568 if (!dp->d_name[len] && found) {
569 break;
570 }
571 }
572 if (retval == log_time::EPOCH) {
573 return retval;
574 }
575 // tail_time prints matching or higher, round up by the modulo to prevent
576 // a replay of the last entry we have just checked.
577 retval += modulo;
578 return retval;
579}
580
Traian Schiau59763032015-04-10 15:51:39 +0300581} /* namespace android */
582
583
Joe Onorato6fa09a02010-02-26 10:04:23 -0800584int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800585{
Traian Schiau59763032015-04-10 15:51:39 +0300586 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800587 int err;
588 int hasSetLogFormat = 0;
589 int clearLog = 0;
590 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800591 unsigned long setLogSize = 0;
592 int getPruneList = 0;
593 char *setPruneList = NULL;
Mark Salyzyn02687e72016-08-03 14:20:41 -0700594 char *setId = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800595 int printStatistics = 0;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800596 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800597 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800598 log_device_t* devices = NULL;
599 log_device_t* dev;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800600 bool printDividers = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800601 struct logger_list *logger_list;
Traian Schiau59763032015-04-10 15:51:39 +0300602 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800603 log_time tail_time(log_time::EPOCH);
Kristian Monsen562e5132015-06-05 14:10:12 -0700604 size_t pid = 0;
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700605 bool got_t = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800606
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800607 signal(SIGPIPE, exit);
608
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800609 g_logformat = android_log_format_new();
610
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800611 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
Traian Schiau59763032015-04-10 15:51:39 +0300612 show_help(argv[0]);
613 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800614 }
615
616 for (;;) {
617 int ret;
618
Kristian Monsen562e5132015-06-05 14:10:12 -0700619 int option_index = 0;
Mark Salyzync9202772016-03-30 09:38:31 -0700620 // list of long-argument only strings for later comparison
Kristian Monsen562e5132015-06-05 14:10:12 -0700621 static const char pid_str[] = "pid";
Mark Salyzyn02687e72016-08-03 14:20:41 -0700622 static const char id_str[] = "id";
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800623 static const char wrap_str[] = "wrap";
Mark Salyzync9202772016-03-30 09:38:31 -0700624 static const char print_str[] = "print";
Kristian Monsen562e5132015-06-05 14:10:12 -0700625 static const struct option long_options[] = {
Mark Salyzynf8bff872015-11-30 12:57:56 -0800626 { "binary", no_argument, NULL, 'B' },
627 { "buffer", required_argument, NULL, 'b' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700628 { "buffer-size", optional_argument, NULL, 'g' },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800629 { "clear", no_argument, NULL, 'c' },
630 { "dividers", no_argument, NULL, 'D' },
631 { "file", required_argument, NULL, 'f' },
632 { "format", required_argument, NULL, 'v' },
Mark Salyzync18c2132016-04-01 07:52:20 -0700633 // hidden and undocumented reserved alias for --regex
634 { "grep", required_argument, NULL, 'e' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700635 // hidden and undocumented reserved alias for --max-count
636 { "head", required_argument, NULL, 'm' },
Mark Salyzyn02687e72016-08-03 14:20:41 -0700637 { id_str, required_argument, NULL, 0 },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800638 { "last", no_argument, NULL, 'L' },
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700639 { "max-count", required_argument, NULL, 'm' },
Mark Salyzync18c2132016-04-01 07:52:20 -0700640 { pid_str, required_argument, NULL, 0 },
Mark Salyzync9202772016-03-30 09:38:31 -0700641 { print_str, no_argument, NULL, 0 },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800642 { "prune", optional_argument, NULL, 'p' },
Casey Dahlindc42a872016-03-17 16:18:55 -0700643 { "regex", required_argument, NULL, 'e' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700644 { "rotate-count", required_argument, NULL, 'n' },
645 { "rotate-kbytes", required_argument, NULL, 'r' },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800646 { "statistics", no_argument, NULL, 'S' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700647 // hidden and undocumented reserved alias for -t
648 { "tail", required_argument, NULL, 't' },
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800649 // support, but ignore and do not document, the optional argument
650 { wrap_str, optional_argument, NULL, 0 },
Kristian Monsen562e5132015-06-05 14:10:12 -0700651 { NULL, 0, NULL, 0 }
652 };
653
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700654 ret = getopt_long(argc, argv, ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:m:e:",
Kristian Monsen562e5132015-06-05 14:10:12 -0700655 long_options, &option_index);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800656
657 if (ret < 0) {
658 break;
659 }
660
Kristian Monsen562e5132015-06-05 14:10:12 -0700661 switch (ret) {
662 case 0:
Mark Salyzyn02687e72016-08-03 14:20:41 -0700663 // only long options
Kristian Monsen562e5132015-06-05 14:10:12 -0700664 if (long_options[option_index].name == pid_str) {
665 // ToDo: determine runtime PID_MAX?
666 if (!getSizeTArg(optarg, &pid, 1)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700667 logcat_panic(HELP_TRUE, "%s %s out of range\n",
Kristian Monsen562e5132015-06-05 14:10:12 -0700668 long_options[option_index].name, optarg);
669 }
670 break;
671 }
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800672 if (long_options[option_index].name == wrap_str) {
673 mode |= ANDROID_LOG_WRAP |
674 ANDROID_LOG_RDONLY |
675 ANDROID_LOG_NONBLOCK;
676 // ToDo: implement API that supports setting a wrap timeout
677 size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT;
678 if (optarg && !getSizeTArg(optarg, &dummy, 1)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700679 logcat_panic(HELP_TRUE, "%s %s out of range\n",
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800680 long_options[option_index].name, optarg);
681 }
682 if (dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) {
683 fprintf(stderr,
684 "WARNING: %s %u seconds, ignoring %zu\n",
685 long_options[option_index].name,
686 ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy);
687 }
688 break;
689 }
Mark Salyzync9202772016-03-30 09:38:31 -0700690 if (long_options[option_index].name == print_str) {
691 g_printItAnyways = true;
692 break;
693 }
Mark Salyzyn02687e72016-08-03 14:20:41 -0700694 if (long_options[option_index].name == id_str) {
695 setId = optarg && optarg[0] ? optarg : NULL;
696 break;
697 }
Kristian Monsen562e5132015-06-05 14:10:12 -0700698 break;
699
Mark Salyzyn95132e92013-11-22 10:55:48 -0800700 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800701 // default to all silent
702 android_log_addFilterRule(g_logformat, "*:s");
703 break;
704
705 case 'c':
706 clearLog = 1;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800707 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800708 break;
709
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800710 case 'L':
Mark Salyzynad5e4112016-08-04 07:53:52 -0700711 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK;
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800712 break;
713
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800714 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800715 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800716 break;
717
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800718 case 't':
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700719 got_t = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800720 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800721 /* FALLTHRU */
722 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800723 if (strspn(optarg, "0123456789") != strlen(optarg)) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700724 char *cp = parseTime(tail_time, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800725 if (!cp) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700726 logcat_panic(HELP_FALSE,
727 "-%c \"%s\" not in time format\n",
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700728 ret, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800729 }
730 if (*cp) {
731 char c = *cp;
732 *cp = '\0';
733 fprintf(stderr,
734 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
735 ret, optarg, c, cp + 1);
736 *cp = c;
737 }
738 } else {
Traian Schiau59763032015-04-10 15:51:39 +0300739 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800740 fprintf(stderr,
741 "WARNING: -%c %s invalid, setting to 1\n",
742 ret, optarg);
743 tail_lines = 1;
744 }
745 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800746 break;
747
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800748 case 'D':
749 printDividers = true;
750 break;
751
Casey Dahlindc42a872016-03-17 16:18:55 -0700752 case 'e':
753 g_regex = new pcrecpp::RE(optarg);
754 break;
755
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700756 case 'm': {
757 char *end = NULL;
758 if (!getSizeTArg(optarg, &g_maxCount)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700759 logcat_panic(HELP_FALSE, "-%c \"%s\" isn't an "
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700760 "integer greater than zero\n", ret, optarg);
761 }
762 }
763 break;
764
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800765 case 'g':
Mark Salyzynf8bff872015-11-30 12:57:56 -0800766 if (!optarg) {
767 getLogSize = 1;
768 break;
769 }
770 // FALLTHRU
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800771
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800772 case 'G': {
Traian Schiau59763032015-04-10 15:51:39 +0300773 char *cp;
774 if (strtoll(optarg, &cp, 0) > 0) {
775 setLogSize = strtoll(optarg, &cp, 0);
776 } else {
777 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800778 }
779
780 switch(*cp) {
781 case 'g':
782 case 'G':
783 setLogSize *= 1024;
784 /* FALLTHRU */
785 case 'm':
786 case 'M':
787 setLogSize *= 1024;
788 /* FALLTHRU */
789 case 'k':
790 case 'K':
791 setLogSize *= 1024;
792 /* FALLTHRU */
793 case '\0':
794 break;
795
796 default:
797 setLogSize = 0;
798 }
799
800 if (!setLogSize) {
801 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
Traian Schiau59763032015-04-10 15:51:39 +0300802 return EXIT_FAILURE;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800803 }
804 }
805 break;
806
807 case 'p':
Mark Salyzynf8bff872015-11-30 12:57:56 -0800808 if (!optarg) {
809 getPruneList = 1;
810 break;
811 }
812 // FALLTHRU
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800813
814 case 'P':
815 setPruneList = optarg;
816 break;
817
Joe Onorato6fa09a02010-02-26 10:04:23 -0800818 case 'b': {
Mark Salyzyn45177732016-04-11 14:03:48 -0700819 unsigned idMask = 0;
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700820 while ((optarg = strtok(optarg, ",:; \t\n\r\f")) != NULL) {
821 if (strcmp(optarg, "default") == 0) {
Mark Salyzyn45177732016-04-11 14:03:48 -0700822 idMask |= (1 << LOG_ID_MAIN) |
823 (1 << LOG_ID_SYSTEM) |
824 (1 << LOG_ID_CRASH);
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700825 } else if (strcmp(optarg, "all") == 0) {
Mark Salyzyn45177732016-04-11 14:03:48 -0700826 idMask = (unsigned)-1;
827 } else {
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700828 log_id_t log_id = android_name_to_log_id(optarg);
Mark Salyzyn45177732016-04-11 14:03:48 -0700829 const char *name = android_log_id_to_name(log_id);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800830
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700831 if (strcmp(name, optarg) != 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700832 logcat_panic(HELP_TRUE,
833 "unknown buffer %s\n", optarg);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800834 }
Mark Salyzyn45177732016-04-11 14:03:48 -0700835 idMask |= (1 << log_id);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800836 }
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700837 optarg = NULL;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800838 }
839
Mark Salyzyn45177732016-04-11 14:03:48 -0700840 for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
841 const char *name = android_log_id_to_name((log_id_t)i);
842 log_id_t log_id = android_name_to_log_id(name);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800843
Mark Salyzyn45177732016-04-11 14:03:48 -0700844 if (log_id != (log_id_t)i) {
845 continue;
846 }
847 if ((idMask & (1 << i)) == 0) {
848 continue;
849 }
Mark Salyzyn083b0372015-12-04 10:59:45 -0800850
Mark Salyzyn45177732016-04-11 14:03:48 -0700851 bool found = false;
852 for (dev = devices; dev; dev = dev->next) {
853 if (!strcmp(name, dev->device)) {
854 found = true;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800855 break;
856 }
Mark Salyzyn45177732016-04-11 14:03:48 -0700857 if (!dev->next) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800858 break;
859 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800860 }
Mark Salyzyn45177732016-04-11 14:03:48 -0700861 if (found) {
862 continue;
863 }
864
865 bool binary = !strcmp(name, "events") ||
866 !strcmp(name, "security");
867 log_device_t* d = new log_device_t(name, binary);
868
Mark Salyzyn083b0372015-12-04 10:59:45 -0800869 if (dev) {
Mark Salyzyn45177732016-04-11 14:03:48 -0700870 dev->next = d;
871 dev = d;
872 } else {
873 devices = dev = d;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800874 }
Mark Salyzyn45177732016-04-11 14:03:48 -0700875 g_devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800876 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800877 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800878 break;
879
880 case 'B':
Traian Schiau59763032015-04-10 15:51:39 +0300881 g_printBinary = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800882 break;
883
884 case 'f':
Mark Salyzyn9812fc42015-10-06 08:59:02 -0700885 if ((tail_time == log_time::EPOCH) && (tail_lines == 0)) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700886 tail_time = lastLogTime(optarg);
887 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800888 // redirect output to a file
Traian Schiau59763032015-04-10 15:51:39 +0300889 g_outputFileName = optarg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800890 break;
891
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700892 case 'r':
893 if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700894 logcat_panic(HELP_TRUE,
895 "Invalid parameter \"%s\" to -r\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800896 }
897 break;
898
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700899 case 'n':
900 if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700901 logcat_panic(HELP_TRUE,
902 "Invalid parameter \"%s\" to -n\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800903 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800904 break;
905
906 case 'v':
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700907 if (!strcmp(optarg, "help") || !strcmp(optarg, "--help")) {
908 show_format_help();
909 exit(0);
910 }
911 err = setLogFormat(optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800912 if (err < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700913 logcat_panic(HELP_FORMAT,
914 "Invalid parameter \"%s\" to -v\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800915 }
Mark Salyzyne1f20042015-05-06 08:40:40 -0700916 hasSetLogFormat |= err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800917 break;
918
919 case 'Q':
920 /* this is a *hidden* option used to start a version of logcat */
921 /* in an emulated device only. it basically looks for androidboot.logcat= */
922 /* on the kernel command line. If something is found, it extracts a log filter */
923 /* and uses it to run the program. If nothing is found, the program should */
924 /* quit immediately */
925#define KERNEL_OPTION "androidboot.logcat="
926#define CONSOLE_OPTION "androidboot.console="
927 {
928 int fd;
929 char* logcat;
930 char* console;
931 int force_exit = 1;
932 static char cmdline[1024];
933
934 fd = open("/proc/cmdline", O_RDONLY);
935 if (fd >= 0) {
936 int n = read(fd, cmdline, sizeof(cmdline)-1 );
937 if (n < 0) n = 0;
938 cmdline[n] = 0;
939 close(fd);
940 } else {
941 cmdline[0] = 0;
942 }
943
944 logcat = strstr( cmdline, KERNEL_OPTION );
945 console = strstr( cmdline, CONSOLE_OPTION );
946 if (logcat != NULL) {
947 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
948 char* q = strpbrk( p, " \t\n\r" );;
949
950 if (q != NULL)
951 *q = 0;
952
953 forceFilters = p;
954 force_exit = 0;
955 }
956 /* if nothing found or invalid filters, exit quietly */
Traian Schiau59763032015-04-10 15:51:39 +0300957 if (force_exit) {
958 return EXIT_SUCCESS;
959 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800960
961 /* redirect our output to the emulator console */
962 if (console) {
963 char* p = console + sizeof(CONSOLE_OPTION)-1;
964 char* q = strpbrk( p, " \t\n\r" );
965 char devname[64];
966 int len;
967
968 if (q != NULL) {
969 len = q - p;
970 } else
971 len = strlen(p);
972
973 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
974 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
975 if (len < (int)sizeof(devname)) {
976 fd = open( devname, O_WRONLY );
977 if (fd >= 0) {
978 dup2(fd, 1);
979 dup2(fd, 2);
980 close(fd);
981 }
982 }
983 }
984 }
985 break;
986
Mark Salyzyn34facab2014-02-06 14:48:50 -0800987 case 'S':
988 printStatistics = 1;
989 break;
990
Traian Schiau59763032015-04-10 15:51:39 +0300991 case ':':
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700992 logcat_panic(HELP_TRUE,
993 "Option -%c needs an argument\n", optopt);
Traian Schiau59763032015-04-10 15:51:39 +0300994 break;
995
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800996 default:
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700997 logcat_panic(HELP_TRUE,
998 "Unrecognized Option %c\n", optopt);
Traian Schiau59763032015-04-10 15:51:39 +0300999 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001000 }
1001 }
1002
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001003 if (g_maxCount && got_t) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001004 logcat_panic(HELP_TRUE,
1005 "Cannot use -m (--max-count) and -t together\n");
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001006 }
Mark Salyzync9202772016-03-30 09:38:31 -07001007 if (g_printItAnyways && (!g_regex || !g_maxCount)) {
1008 // One day it would be nice if --print -v color and --regex <expr>
1009 // could play with each other and show regex highlighted content.
1010 fprintf(stderr, "WARNING: "
1011 "--print ignored, to be used in combination with\n"
1012 " "
1013 "--regex <expr> and --max-count <N>\n");
1014 g_printItAnyways = false;
1015 }
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001016
Joe Onorato6fa09a02010-02-26 10:04:23 -08001017 if (!devices) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001018 dev = devices = new log_device_t("main", false);
Traian Schiau59763032015-04-10 15:51:39 +03001019 g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001020 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001021 dev = dev->next = new log_device_t("system", false);
Traian Schiau59763032015-04-10 15:51:39 +03001022 g_devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001023 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001024 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001025 dev = dev->next = new log_device_t("crash", false);
Traian Schiau59763032015-04-10 15:51:39 +03001026 g_devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001027 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001028 }
1029
Traian Schiau59763032015-04-10 15:51:39 +03001030 if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001031 logcat_panic(HELP_TRUE, "-r requires -f as well\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001032 }
1033
Mark Salyzyn02687e72016-08-03 14:20:41 -07001034 if (setId != NULL) {
1035 if (g_outputFileName == NULL) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001036 logcat_panic(HELP_TRUE, "--id='%s' requires -f as well\n", setId);
Mark Salyzyn02687e72016-08-03 14:20:41 -07001037 }
1038
1039 std::string file_name = android::base::StringPrintf("%s.id", g_outputFileName);
1040 std::string file;
1041 bool file_ok = android::base::ReadFileToString(file_name, &file);
1042 android::base::WriteStringToFile(setId, file_name,
1043 S_IRUSR | S_IWUSR, getuid(), getgid());
1044 if (!file_ok || (file.compare(setId) == 0)) {
1045 setId = NULL;
1046 }
1047 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001048
1049 if (hasSetLogFormat == 0) {
1050 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
1051
1052 if (logFormat != NULL) {
1053 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001054 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001055 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001056 logFormat);
1057 }
Mark Salyzyn649fc602014-09-16 09:15:15 -07001058 } else {
1059 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001060 }
1061 }
1062
1063 if (forceFilters) {
1064 err = android_log_addFilterString(g_logformat, forceFilters);
1065 if (err < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001066 logcat_panic(HELP_FALSE,
1067 "Invalid filter expression in logcat args\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001068 }
1069 } else if (argc == optind) {
1070 // Add from environment variable
1071 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
1072
1073 if (env_tags_orig != NULL) {
1074 err = android_log_addFilterString(g_logformat, env_tags_orig);
1075
Mark Salyzyn95132e92013-11-22 10:55:48 -08001076 if (err < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001077 logcat_panic(HELP_TRUE,
Traian Schiau59763032015-04-10 15:51:39 +03001078 "Invalid filter expression in ANDROID_LOG_TAGS\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001079 }
1080 }
1081 } else {
1082 // Add from commandline
1083 for (int i = optind ; i < argc ; i++) {
1084 err = android_log_addFilterString(g_logformat, argv[i]);
1085
Mark Salyzyn95132e92013-11-22 10:55:48 -08001086 if (err < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001087 logcat_panic(HELP_TRUE,
1088 "Invalid filter expression '%s'\n", argv[i]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001089 }
1090 }
1091 }
1092
Joe Onorato6fa09a02010-02-26 10:04:23 -08001093 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001094 if (tail_time != log_time::EPOCH) {
Kristian Monsen562e5132015-06-05 14:10:12 -07001095 logger_list = android_logger_list_alloc_time(mode, tail_time, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001096 } else {
Kristian Monsen562e5132015-06-05 14:10:12 -07001097 logger_list = android_logger_list_alloc(mode, tail_lines, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001098 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001099 const char *openDeviceFail = NULL;
1100 const char *clearFail = NULL;
1101 const char *setSizeFail = NULL;
1102 const char *getSizeFail = NULL;
1103 // We have three orthogonal actions below to clear, set log size and
1104 // get log size. All sharing the same iteration loop.
Joe Onorato6fa09a02010-02-26 10:04:23 -08001105 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001106 dev->logger_list = logger_list;
1107 dev->logger = android_logger_open(logger_list,
1108 android_name_to_log_id(dev->device));
1109 if (!dev->logger) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001110 openDeviceFail = openDeviceFail ?: dev->device;
1111 dev = dev->next;
1112 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001113 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001114
Mark Salyzyn02687e72016-08-03 14:20:41 -07001115 if (clearLog || setId) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001116 if (g_outputFileName) {
1117 int maxRotationCountDigits =
1118 (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
1119
1120 for (int i = g_maxRotatedLogs ; i >= 0 ; --i) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001121 std::string file;
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001122
1123 if (i == 0) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001124 file = android::base::StringPrintf("%s", g_outputFileName);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001125 } else {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001126 file = android::base::StringPrintf("%s.%.*d",
1127 g_outputFileName, maxRotationCountDigits, i);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001128 }
1129
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001130 if (file.length() == 0) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001131 perror("while clearing log files");
1132 clearFail = clearFail ?: dev->device;
1133 break;
1134 }
1135
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001136 err = unlink(file.c_str());
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001137
1138 if (err < 0 && errno != ENOENT && clearFail == NULL) {
1139 perror("while clearing log files");
1140 clearFail = dev->device;
1141 }
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001142 }
1143 } else if (android_logger_clear(dev->logger)) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001144 clearFail = clearFail ?: dev->device;
Joe Onorato6fa09a02010-02-26 10:04:23 -08001145 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001146 }
1147
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001148 if (setLogSize) {
1149 if (android_logger_set_log_size(dev->logger, setLogSize)) {
1150 setSizeFail = setSizeFail ?: dev->device;
1151 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001152 }
1153
Joe Onorato6fa09a02010-02-26 10:04:23 -08001154 if (getLogSize) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001155 long size = android_logger_get_log_size(dev->logger);
1156 long readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001157
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001158 if ((size < 0) || (readable < 0)) {
1159 getSizeFail = getSizeFail ?: dev->device;
1160 } else {
1161 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
1162 "max entry is %db, max payload is %db\n", dev->device,
1163 value_of_size(size), multiplier_of_size(size),
1164 value_of_size(readable), multiplier_of_size(readable),
1165 (int) LOGGER_ENTRY_MAX_LEN,
1166 (int) LOGGER_ENTRY_MAX_PAYLOAD);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001167 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001168 }
1169
1170 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001171 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001172 // report any errors in the above loop and exit
1173 if (openDeviceFail) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001174 logcat_panic(HELP_FALSE,
1175 "Unable to open log device '%s'\n", openDeviceFail);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001176 }
1177 if (clearFail) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001178 logcat_panic(HELP_FALSE,
1179 "failed to clear the '%s' log\n", clearFail);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001180 }
1181 if (setSizeFail) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001182 logcat_panic(HELP_FALSE,
1183 "failed to set the '%s' log size\n", setSizeFail);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001184 }
1185 if (getSizeFail) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001186 logcat_panic(HELP_FALSE,
1187 "failed to get the readable '%s' log size", getSizeFail);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001188 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001189
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001190 if (setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001191 size_t len = strlen(setPruneList);
1192 /*extra 32 bytes are needed by android_logger_set_prune_list */
1193 size_t bLen = len + 32;
1194 char *buf = NULL;
1195 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
1196 buf[len] = '\0';
1197 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001198 logcat_panic(HELP_FALSE, "failed to set the prune list");
Traian Schiau59763032015-04-10 15:51:39 +03001199 }
1200 free(buf);
1201 } else {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001202 logcat_panic(HELP_FALSE, "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001203 }
1204 }
1205
Mark Salyzyn1c950472014-04-01 17:19:47 -07001206 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001207 size_t len = 8192;
1208 char *buf;
1209
Mark Salyzyn083b0372015-12-04 10:59:45 -08001210 for (int retry = 32;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001211 (retry >= 0) && ((buf = new char [len]));
Traian Schiau59763032015-04-10 15:51:39 +03001212 delete [] buf, buf = NULL, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001213 if (getPruneList) {
1214 android_logger_get_prune_list(logger_list, buf, len);
1215 } else {
1216 android_logger_get_statistics(logger_list, buf, len);
1217 }
Mark Salyzyn34facab2014-02-06 14:48:50 -08001218 buf[len-1] = '\0';
Traian Schiau59763032015-04-10 15:51:39 +03001219 if (atol(buf) < 3) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001220 delete [] buf;
1221 buf = NULL;
1222 break;
1223 }
Traian Schiau59763032015-04-10 15:51:39 +03001224 size_t ret = atol(buf) + 1;
1225 if (ret <= len) {
1226 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001227 break;
1228 }
Traian Schiau59763032015-04-10 15:51:39 +03001229 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001230 }
1231
1232 if (!buf) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001233 logcat_panic(HELP_FALSE, "failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -08001234 }
1235
1236 // remove trailing FF
1237 char *cp = buf + len - 1;
1238 *cp = '\0';
1239 bool truncated = *--cp != '\f';
1240 if (!truncated) {
1241 *cp = '\0';
1242 }
1243
1244 // squash out the byte count
1245 cp = buf;
1246 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -07001247 while (isdigit(*cp)) {
1248 ++cp;
1249 }
1250 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001251 ++cp;
1252 }
1253 }
1254
1255 printf("%s", cp);
1256 delete [] buf;
Traian Schiau59763032015-04-10 15:51:39 +03001257 return EXIT_SUCCESS;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001258 }
1259
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001260 if (getLogSize) {
Traian Schiau59763032015-04-10 15:51:39 +03001261 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001262 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001263 if (setLogSize || setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001264 return EXIT_SUCCESS;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001265 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001266 if (clearLog) {
Traian Schiau59763032015-04-10 15:51:39 +03001267 return EXIT_SUCCESS;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001268 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001269
Mark Salyzynad5e4112016-08-04 07:53:52 -07001270 setupOutputAndSchedulingPolicy((mode & ANDROID_LOG_NONBLOCK) == 0);
Mark Salyzyn02687e72016-08-03 14:20:41 -07001271
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001272 //LOG_EVENT_INT(10, 12345);
1273 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
1274 //LOG_EVENT_STRING(0, "whassup, doc?");
1275
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001276 dev = NULL;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001277 log_device_t unexpected("unexpected", false);
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001278
Mark Salyzynaa730c12016-03-30 12:38:29 -07001279 while (!g_maxCount || (g_printCount < g_maxCount)) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001280 struct log_msg log_msg;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001281 log_device_t* d;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001282 int ret = android_logger_list_read(logger_list, &log_msg);
1283
1284 if (ret == 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001285 logcat_panic(HELP_FALSE, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001286 }
1287
1288 if (ret < 0) {
1289 if (ret == -EAGAIN) {
1290 break;
1291 }
1292
1293 if (ret == -EIO) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001294 logcat_panic(HELP_FALSE, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001295 }
1296 if (ret == -EINVAL) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001297 logcat_panic(HELP_FALSE, "read: unexpected length.\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001298 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001299 logcat_panic(HELP_FALSE, "logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001300 }
1301
Mark Salyzyn083b0372015-12-04 10:59:45 -08001302 for (d = devices; d; d = d->next) {
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001303 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001304 break;
1305 }
1306 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001307 if (!d) {
Traian Schiau59763032015-04-10 15:51:39 +03001308 g_devCount = 2; // set to Multiple
Mark Salyzyn9421b0c2015-02-26 14:33:35 -08001309 d = &unexpected;
1310 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001311 }
1312
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001313 if (dev != d) {
1314 dev = d;
Traian Schiau59763032015-04-10 15:51:39 +03001315 maybePrintStart(dev, printDividers);
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001316 }
Traian Schiau59763032015-04-10 15:51:39 +03001317 if (g_printBinary) {
1318 printBinary(&log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001319 } else {
Traian Schiau59763032015-04-10 15:51:39 +03001320 processBuffer(dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001321 }
1322 }
1323
1324 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001325
Traian Schiau59763032015-04-10 15:51:39 +03001326 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001327}