blob: c204a16821e55a69d4e80aa95d886f5513f7bf7b [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;
Mark Salyzyn538bc122016-11-16 15:28:31 -080084static bool g_debug;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085
Mark Salyzyn4fd05072016-10-18 11:30:11 -070086enum helpType {
87 HELP_FALSE,
88 HELP_TRUE,
89 HELP_FORMAT
90};
91
Mark Salyzynaa730c12016-03-30 12:38:29 -070092// if showHelp is set, newline required in fmt statement to transition to usage
Mark Salyzyn4fd05072016-10-18 11:30:11 -070093__noreturn static void logcat_panic(enum helpType showHelp, const char *fmt, ...) __printflike(2,3);
Traian Schiau59763032015-04-10 15:51:39 +030094
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095static int openLogFile (const char *pathname)
96{
Edwin Vane80b221c2012-08-13 12:55:07 -040097 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098}
99
100static void rotateLogs()
101{
102 int err;
103
104 // Can't rotate logs if we're not outputting to a file
105 if (g_outputFileName == NULL) {
106 return;
107 }
108
109 close(g_outFD);
110
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700111 // Compute the maximum number of digits needed to count up to g_maxRotatedLogs in decimal.
112 // eg: g_maxRotatedLogs == 30 -> log10(30) == 1.477 -> maxRotationCountDigits == 2
113 int maxRotationCountDigits =
114 (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
115
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116 for (int i = g_maxRotatedLogs ; i > 0 ; i--) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700117 std::string file1 = android::base::StringPrintf(
118 "%s.%.*d", g_outputFileName, maxRotationCountDigits, i);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700120 std::string file0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121 if (i - 1 == 0) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700122 file0 = android::base::StringPrintf("%s", g_outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800123 } else {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700124 file0 = android::base::StringPrintf(
125 "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 }
127
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700128 if ((file0.length() == 0) || (file1.length() == 0)) {
Traian Schiau59763032015-04-10 15:51:39 +0300129 perror("while rotating log files");
130 break;
131 }
132
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700133 err = rename(file0.c_str(), file1.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134
135 if (err < 0 && errno != ENOENT) {
136 perror("while rotating log files");
137 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 }
139
Traian Schiau59763032015-04-10 15:51:39 +0300140 g_outFD = openLogFile(g_outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141
142 if (g_outFD < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700143 logcat_panic(HELP_FALSE, "couldn't open output file");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144 }
145
146 g_outByteCount = 0;
147
148}
149
Mark Salyzyn95132e92013-11-22 10:55:48 -0800150void printBinary(struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151{
Mark Salyzyn95132e92013-11-22 10:55:48 -0800152 size_t size = buf->len();
153
154 TEMP_FAILURE_RETRY(write(g_outFD, buf, size));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155}
156
Mark Salyzynaa730c12016-03-30 12:38:29 -0700157static bool regexOk(const AndroidLogEntry& entry)
Casey Dahlindc42a872016-03-17 16:18:55 -0700158{
Mark Salyzynaa730c12016-03-30 12:38:29 -0700159 if (!g_regex) {
Casey Dahlindc42a872016-03-17 16:18:55 -0700160 return true;
161 }
162
Casey Dahlindc42a872016-03-17 16:18:55 -0700163 std::string messageString(entry.message, entry.messageLen);
164
165 return g_regex->PartialMatch(messageString);
166}
167
Mark Salyzyn95132e92013-11-22 10:55:48 -0800168static void processBuffer(log_device_t* dev, struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169{
Mathias Agopian50844522010-03-17 16:10:26 -0700170 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171 int err;
172 AndroidLogEntry entry;
173 char binaryMsgBuf[1024];
174
Joe Onorato6fa09a02010-02-26 10:04:23 -0800175 if (dev->binary) {
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800176 static bool hasOpenedEventTagMap = false;
177 static EventTagMap *eventTagMap = NULL;
178
179 if (!eventTagMap && !hasOpenedEventTagMap) {
Mark Salyzyn1179eb82016-11-11 09:48:56 -0800180 eventTagMap = android_openEventTagMap(NULL);
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800181 hasOpenedEventTagMap = true;
182 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800183 err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry,
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800184 eventTagMap,
Mark Salyzyn95132e92013-11-22 10:55:48 -0800185 binaryMsgBuf,
186 sizeof(binaryMsgBuf));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 //printf(">>> pri=%d len=%d msg='%s'\n",
188 // entry.priority, entry.messageLen, entry.message);
189 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800190 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191 }
Mark Salyzyn538bc122016-11-16 15:28:31 -0800192 if ((err < 0) && !g_debug) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 goto error;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800194 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195
Mark Salyzyn9f53cac2016-10-24 13:11:46 -0700196 if (android_log_shouldPrintLine(g_logformat,
197 std::string(entry.tag, entry.tagLen).c_str(),
198 entry.priority)) {
Mark Salyzync9202772016-03-30 09:38:31 -0700199 bool match = regexOk(entry);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800200
Mark Salyzync9202772016-03-30 09:38:31 -0700201 g_printCount += match;
202 if (match || g_printItAnyways) {
203 bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry);
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700204
Mark Salyzync9202772016-03-30 09:38:31 -0700205 if (bytesWritten < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700206 logcat_panic(HELP_FALSE, "output error");
Mark Salyzync9202772016-03-30 09:38:31 -0700207 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800208 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 }
210
211 g_outByteCount += bytesWritten;
212
Mark Salyzyn95132e92013-11-22 10:55:48 -0800213 if (g_logRotateSizeKBytes > 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214 && (g_outByteCount / 1024) >= g_logRotateSizeKBytes
215 ) {
216 rotateLogs();
217 }
218
219error:
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 return;
221}
222
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800223static void maybePrintStart(log_device_t* dev, bool printDividers) {
224 if (!dev->printed || printDividers) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800225 if (g_devCount > 1 && !g_printBinary) {
226 char buf[1024];
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800227 snprintf(buf, sizeof(buf), "--------- %s %s\n",
228 dev->printed ? "switch to" : "beginning of",
Mark Salyzyn95132e92013-11-22 10:55:48 -0800229 dev->device);
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800230 if (write(g_outFD, buf, strlen(buf)) < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700231 logcat_panic(HELP_FALSE, "output error");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800232 }
233 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800234 dev->printed = true;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800235 }
236}
237
Mark Salyzynad5e4112016-08-04 07:53:52 -0700238static void setupOutputAndSchedulingPolicy(bool blocking) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 if (g_outputFileName == NULL) {
240 g_outFD = STDOUT_FILENO;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700241 return;
242 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243
Mark Salyzynad5e4112016-08-04 07:53:52 -0700244 if (blocking) {
245 // Lower priority and set to batch scheduling if we are saving
246 // the logs into files and taking continuous content.
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700247 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
248 fprintf(stderr, "failed to set background scheduling policy\n");
249 }
250
251 struct sched_param param;
252 memset(&param, 0, sizeof(param));
253 if (sched_setscheduler((pid_t) 0, SCHED_BATCH, &param) < 0) {
254 fprintf(stderr, "failed to set to batch scheduler\n");
255 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800256
Riley Andrewsaede9892015-06-08 23:36:34 -0700257 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
258 fprintf(stderr, "failed set to priority\n");
259 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800260 }
Mark Salyzynad5e4112016-08-04 07:53:52 -0700261
262 g_outFD = openLogFile (g_outputFileName);
263
264 if (g_outFD < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700265 logcat_panic(HELP_FALSE, "couldn't open output file");
Mark Salyzynad5e4112016-08-04 07:53:52 -0700266 }
267
268 struct stat statbuf;
269 if (fstat(g_outFD, &statbuf) == -1) {
270 close(g_outFD);
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700271 logcat_panic(HELP_FALSE, "couldn't get output file stat\n");
Mark Salyzynad5e4112016-08-04 07:53:52 -0700272 }
273
274 if ((size_t) statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
275 close(g_outFD);
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700276 logcat_panic(HELP_FALSE, "invalid output file stat\n");
Mark Salyzynad5e4112016-08-04 07:53:52 -0700277 }
278
279 g_outByteCount = statbuf.st_size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280}
281
282static void show_help(const char *cmd)
283{
284 fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd);
285
286 fprintf(stderr, "options include:\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700287 " -s Set default filter to silent. Equivalent to filterspec '*:S'\n"
288 " -f <file>, --file=<file> Log to file. Default is stdout\n"
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700289 " -r <kbytes>, --rotate-kbytes=<kbytes>\n"
290 " Rotate log every kbytes. Requires -f option\n"
291 " -n <count>, --rotate-count=<count>\n"
292 " Sets max number of rotated logs to <count>, default 4\n"
Mark Salyzyn02687e72016-08-03 14:20:41 -0700293 " --id=<id> If the signature id for logging to file changes, then clear\n"
294 " the fileset and continue\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700295 " -v <format>, --format=<format>\n"
Mark Salyzyn9cfd1c62016-07-06 11:12:14 -0700296 " Sets log print format verb and adverbs, where <format> is:\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700297 " brief help long process raw tag thread threadtime time\n"
Mark Salyzyne735a732016-07-06 13:30:40 -0700298 " and individually flagged modifying adverbs can be added:\n"
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700299 " color descriptive epoch monotonic printable uid\n"
300 " usec UTC year zone\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700301 " -D, --dividers Print dividers between each log buffer\n"
302 " -c, --clear Clear (flush) the entire log and exit\n"
Mark Salyzynb7d059b2016-06-06 14:56:00 -0700303 " if Log to File specified, clear fileset instead\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700304 " -d Dump the log and then exit (don't block)\n"
305 " -e <expr>, --regex=<expr>\n"
306 " Only print lines where the log message matches <expr>\n"
307 " where <expr> is a regular expression\n"
308 // Leave --head undocumented as alias for -m
309 " -m <count>, --max-count=<count>\n"
310 " Quit after printing <count> lines. This is meant to be\n"
311 " paired with --regex, but will work on its own.\n"
312 " --print Paired with --regex and --max-count to let content bypass\n"
Mark Salyzync9202772016-03-30 09:38:31 -0700313 " regex filter but still stop at number of matches.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700314 // Leave --tail undocumented as alias for -t
315 " -t <count> Print only the most recent <count> lines (implies -d)\n"
316 " -t '<time>' Print most recent lines since specified time (implies -d)\n"
317 " -T <count> Print only the most recent <count> lines (does not imply -d)\n"
318 " -T '<time>' Print most recent lines since specified time (not imply -d)\n"
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700319 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700320 " 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700321 " -g, --buffer-size Get the size of the ring buffer.\n"
322 " -G <size>, --buffer-size=<size>\n"
323 " Set size of log ring buffer, may suffix with K or M.\n"
Oleksiy Avramchenko39e2d222016-11-29 12:48:11 +0100324 " -L, --last Dump logs from prior to last reboot\n"
Mark Salyzyn083b0372015-12-04 10:59:45 -0800325 // Leave security (Device Owner only installations) and
326 // kernel (userdebug and eng) buffers undocumented.
Mark Salyzyn378f4742016-04-12 09:11:46 -0700327 " -b <buffer>, --buffer=<buffer> Request alternate ring buffer, 'main',\n"
328 " 'system', 'radio', 'events', 'crash', 'default' or 'all'.\n"
Mark Salyzyn45177732016-04-11 14:03:48 -0700329 " Multiple -b parameters or comma separated list of buffers are\n"
330 " allowed. Buffers interleaved. Default -b main,system,crash.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700331 " -B, --binary Output the log in binary.\n"
332 " -S, --statistics Output statistics.\n"
333 " -p, --prune Print prune white and ~black list. Service is specified as\n"
334 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700335 " with ~, otherwise weighed for longevity if unadorned. All\n"
336 " other pruning activity is oldest first. Special case ~!\n"
337 " represents an automatic quicker pruning for the noisiest\n"
338 " UID as determined by the current statistics.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700339 " -P '<list> ...', --prune='<list> ...'\n"
340 " Set prune white and ~black list, using same format as\n"
341 " listed above. Must be quoted.\n"
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800342 " --pid=<pid> Only prints logs from the given pid.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700343 // Check ANDROID_LOG_WRAP_DEFAULT_TIMEOUT value for match to 2 hours
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800344 " --wrap Sleep for 2 hours or when buffer about to wrap whichever\n"
345 " comes first. Improves efficiency of polling by providing\n"
346 " an about-to-wrap wakeup.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700348 fprintf(stderr,"\nfilterspecs are a series of \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349 " <tag>[:priority]\n\n"
350 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700351 " V Verbose (default for <tag>)\n"
352 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353 " I Info\n"
354 " W Warn\n"
355 " E Error\n"
356 " F Fatal\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700357 " S Silent (suppress all output)\n"
358 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
359 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
360 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
361 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
362 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700363 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364}
365
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700366static void show_format_help()
367{
368 fprintf(stderr,
369 "-v <format>, --format=<format> options:\n"
370 " Sets log print format verb and adverbs, where <format> is:\n"
371 " brief long process raw tag thread threadtime time\n"
372 " and individually flagged modifying adverbs can be added:\n"
373 " color descriptive epoch monotonic printable uid usec UTC year zone\n"
374 "\nSingle format verbs:\n"
375 " brief — Display priority/tag and PID of the process issuing the message.\n"
376 " long — Display all metadata fields, separate messages with blank lines.\n"
377 " process — Display PID only.\n"
378 " raw — Display the raw log message, with no other metadata fields.\n"
379 " tag — Display the priority/tag only.\n"
380 " threadtime — Display the date, invocation time, priority, tag, and the PID\n"
381 " and TID of the thread issuing the message. (the default format).\n"
382 " time — Display the date, invocation time, priority/tag, and PID of the\n"
383 " process issuing the message.\n"
384 "\nAdverb modifiers can be used in combination:\n"
385 " color — Display in highlighted color to match priority. i.e. \x1B[38;5;231mVERBOSE\n"
386 " \x1B[38;5;75mDEBUG \x1B[38;5;40mINFO \x1B[38;5;166mWARNING \x1B[38;5;196mERROR FATAL\x1B[0m\n"
387 " descriptive — events logs only, descriptions from event-log-tags database.\n"
388 " epoch — Display time as seconds since Jan 1 1970.\n"
389 " monotonic — Display time as cpu seconds since last boot.\n"
390 " printable — Ensure that any binary logging content is escaped.\n"
391 " uid — If permitted, display the UID or Android ID of logged process.\n"
392 " usec — Display time down the microsecond precision.\n"
393 " UTC — Display time as UTC.\n"
394 " year — Add the year to the displayed time.\n"
395 " zone — Add the local timezone to the displayed time.\n"
396 " \"<zone>\" — Print using this public named timezone (experimental).\n\n"
397 );
398}
399
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400static int setLogFormat(const char * formatString)
401{
402 static AndroidLogPrintFormat format;
403
404 format = android_log_formatFromString(formatString);
405
406 if (format == FORMAT_OFF) {
407 // FORMAT_OFF means invalid string
408 return -1;
409 }
410
Mark Salyzyne1f20042015-05-06 08:40:40 -0700411 return android_log_setPrintFormat(g_logformat, format);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412}
413
Mark Salyzyn671e3432014-05-06 07:34:59 -0700414static const char multipliers[][2] = {
415 { "" },
416 { "K" },
417 { "M" },
418 { "G" }
419};
420
421static unsigned long value_of_size(unsigned long value)
422{
423 for (unsigned i = 0;
424 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
425 value /= 1024, ++i) ;
426 return value;
427}
428
429static const char *multiplier_of_size(unsigned long value)
430{
431 unsigned i;
432 for (i = 0;
433 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
434 value /= 1024, ++i) ;
435 return multipliers[i];
436}
437
Traian Schiau59763032015-04-10 15:51:39 +0300438/*String to unsigned int, returns -1 if it fails*/
Mark Salyzyn33c26252016-04-12 09:11:46 -0700439static bool getSizeTArg(const char *ptr, size_t *val, size_t min = 0,
Traian Schiau59763032015-04-10 15:51:39 +0300440 size_t max = SIZE_MAX)
441{
Kristian Monsen562e5132015-06-05 14:10:12 -0700442 if (!ptr) {
Traian Schiau59763032015-04-10 15:51:39 +0300443 return false;
444 }
445
Kristian Monsen562e5132015-06-05 14:10:12 -0700446 char *endp;
447 errno = 0;
448 size_t ret = (size_t)strtoll(ptr, &endp, 0);
449
450 if (endp[0] || errno) {
451 return false;
452 }
453
454 if ((ret > max) || (ret < min)) {
Traian Schiau59763032015-04-10 15:51:39 +0300455 return false;
456 }
457
458 *val = ret;
459 return true;
460}
461
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700462static void logcat_panic(enum helpType showHelp, const char *fmt, ...)
Traian Schiau59763032015-04-10 15:51:39 +0300463{
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700464 va_list args;
465 va_start(args, fmt);
466 vfprintf(stderr, fmt, args);
467 va_end(args);
Traian Schiau59763032015-04-10 15:51:39 +0300468
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700469 switch (showHelp) {
470 case HELP_TRUE:
Traian Schiau59763032015-04-10 15:51:39 +0300471 show_help(getprogname());
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700472 break;
473 case HELP_FORMAT:
474 show_format_help();
475 break;
476 case HELP_FALSE:
477 default:
478 break;
Traian Schiau59763032015-04-10 15:51:39 +0300479 }
480
481 exit(EXIT_FAILURE);
482}
483
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700484static char *parseTime(log_time &t, const char *cp) {
485
486 char *ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
487 if (ep) {
488 return ep;
489 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700490 ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
491 if (ep) {
492 return ep;
493 }
494 return t.strptime(cp, "%s.%q");
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700495}
Mark Salyzynf3555d92015-05-27 07:39:56 -0700496
Mark Salyzyn31961062016-08-04 07:43:46 -0700497// Find last logged line in <outputFileName>, or <outputFileName>.1
Mark Salyzynf3555d92015-05-27 07:39:56 -0700498static log_time lastLogTime(char *outputFileName) {
499 log_time retval(log_time::EPOCH);
500 if (!outputFileName) {
501 return retval;
502 }
503
Mark Salyzynf3555d92015-05-27 07:39:56 -0700504 std::string directory;
505 char *file = strrchr(outputFileName, '/');
506 if (!file) {
507 directory = ".";
508 file = outputFileName;
509 } else {
510 *file = '\0';
511 directory = outputFileName;
512 *file = '/';
513 ++file;
514 }
Mark Salyzync18c2132016-04-01 07:52:20 -0700515
516 std::unique_ptr<DIR, int(*)(DIR*)>
517 dir(opendir(directory.c_str()), closedir);
518 if (!dir.get()) {
519 return retval;
520 }
521
Mark Salyzyn31961062016-08-04 07:43:46 -0700522 log_time now(android_log_clockid());
Mark Salyzync18c2132016-04-01 07:52:20 -0700523
Mark Salyzynf3555d92015-05-27 07:39:56 -0700524 size_t len = strlen(file);
525 log_time modulo(0, NS_PER_SEC);
Mark Salyzynf3555d92015-05-27 07:39:56 -0700526 struct dirent *dp;
Mark Salyzync18c2132016-04-01 07:52:20 -0700527
Mark Salyzynf3555d92015-05-27 07:39:56 -0700528 while ((dp = readdir(dir.get())) != NULL) {
Mark Salyzyn31961062016-08-04 07:43:46 -0700529 if ((dp->d_type != DT_REG) ||
530 (strncmp(dp->d_name, file, len) != 0) ||
531 (dp->d_name[len] &&
532 ((dp->d_name[len] != '.') ||
533 (strtoll(dp->d_name + 1, NULL, 10) != 1)))) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700534 continue;
535 }
536
537 std::string file_name = directory;
538 file_name += "/";
539 file_name += dp->d_name;
540 std::string file;
541 if (!android::base::ReadFileToString(file_name, &file)) {
542 continue;
543 }
544
545 bool found = false;
546 for (const auto& line : android::base::Split(file, "\n")) {
547 log_time t(log_time::EPOCH);
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700548 char *ep = parseTime(t, line.c_str());
Mark Salyzynf3555d92015-05-27 07:39:56 -0700549 if (!ep || (*ep != ' ')) {
550 continue;
551 }
552 // determine the time precision of the logs (eg: msec or usec)
553 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
554 if (t.tv_nsec % (mod * 10)) {
555 modulo.tv_nsec = mod;
556 break;
557 }
558 }
559 // We filter any times later than current as we may not have the
560 // year stored with each log entry. Also, since it is possible for
561 // entries to be recorded out of order (very rare) we select the
562 // maximum we find just in case.
563 if ((t < now) && (t > retval)) {
564 retval = t;
565 found = true;
566 }
567 }
568 // We count on the basename file to be the definitive end, so stop here.
569 if (!dp->d_name[len] && found) {
570 break;
571 }
572 }
573 if (retval == log_time::EPOCH) {
574 return retval;
575 }
576 // tail_time prints matching or higher, round up by the modulo to prevent
577 // a replay of the last entry we have just checked.
578 retval += modulo;
579 return retval;
580}
581
Traian Schiau59763032015-04-10 15:51:39 +0300582} /* namespace android */
583
584
Joe Onorato6fa09a02010-02-26 10:04:23 -0800585int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800586{
Traian Schiau59763032015-04-10 15:51:39 +0300587 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800588 int err;
589 int hasSetLogFormat = 0;
590 int clearLog = 0;
591 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800592 unsigned long setLogSize = 0;
593 int getPruneList = 0;
594 char *setPruneList = NULL;
Mark Salyzyn02687e72016-08-03 14:20:41 -0700595 char *setId = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800596 int printStatistics = 0;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800597 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800598 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800599 log_device_t* devices = NULL;
600 log_device_t* dev;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800601 bool printDividers = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800602 struct logger_list *logger_list;
Traian Schiau59763032015-04-10 15:51:39 +0300603 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800604 log_time tail_time(log_time::EPOCH);
Kristian Monsen562e5132015-06-05 14:10:12 -0700605 size_t pid = 0;
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700606 bool got_t = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800607
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800608 signal(SIGPIPE, exit);
609
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800610 g_logformat = android_log_format_new();
611
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800612 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
Traian Schiau59763032015-04-10 15:51:39 +0300613 show_help(argv[0]);
614 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800615 }
616
617 for (;;) {
618 int ret;
619
Kristian Monsen562e5132015-06-05 14:10:12 -0700620 int option_index = 0;
Mark Salyzync9202772016-03-30 09:38:31 -0700621 // list of long-argument only strings for later comparison
Kristian Monsen562e5132015-06-05 14:10:12 -0700622 static const char pid_str[] = "pid";
Mark Salyzyn538bc122016-11-16 15:28:31 -0800623 static const char debug_str[] = "debug";
Mark Salyzyn02687e72016-08-03 14:20:41 -0700624 static const char id_str[] = "id";
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800625 static const char wrap_str[] = "wrap";
Mark Salyzync9202772016-03-30 09:38:31 -0700626 static const char print_str[] = "print";
Kristian Monsen562e5132015-06-05 14:10:12 -0700627 static const struct option long_options[] = {
Mark Salyzynf8bff872015-11-30 12:57:56 -0800628 { "binary", no_argument, NULL, 'B' },
629 { "buffer", required_argument, NULL, 'b' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700630 { "buffer-size", optional_argument, NULL, 'g' },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800631 { "clear", no_argument, NULL, 'c' },
Mark Salyzyn538bc122016-11-16 15:28:31 -0800632 { debug_str, no_argument, NULL, 0 },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800633 { "dividers", no_argument, NULL, 'D' },
634 { "file", required_argument, NULL, 'f' },
635 { "format", required_argument, NULL, 'v' },
Mark Salyzync18c2132016-04-01 07:52:20 -0700636 // hidden and undocumented reserved alias for --regex
637 { "grep", required_argument, NULL, 'e' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700638 // hidden and undocumented reserved alias for --max-count
639 { "head", required_argument, NULL, 'm' },
Mark Salyzyn02687e72016-08-03 14:20:41 -0700640 { id_str, required_argument, NULL, 0 },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800641 { "last", no_argument, NULL, 'L' },
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700642 { "max-count", required_argument, NULL, 'm' },
Mark Salyzync18c2132016-04-01 07:52:20 -0700643 { pid_str, required_argument, NULL, 0 },
Mark Salyzync9202772016-03-30 09:38:31 -0700644 { print_str, no_argument, NULL, 0 },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800645 { "prune", optional_argument, NULL, 'p' },
Casey Dahlindc42a872016-03-17 16:18:55 -0700646 { "regex", required_argument, NULL, 'e' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700647 { "rotate-count", required_argument, NULL, 'n' },
648 { "rotate-kbytes", required_argument, NULL, 'r' },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800649 { "statistics", no_argument, NULL, 'S' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700650 // hidden and undocumented reserved alias for -t
651 { "tail", required_argument, NULL, 't' },
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800652 // support, but ignore and do not document, the optional argument
653 { wrap_str, optional_argument, NULL, 0 },
Kristian Monsen562e5132015-06-05 14:10:12 -0700654 { NULL, 0, NULL, 0 }
655 };
656
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700657 ret = getopt_long(argc, argv, ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:m:e:",
Kristian Monsen562e5132015-06-05 14:10:12 -0700658 long_options, &option_index);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800659
660 if (ret < 0) {
661 break;
662 }
663
Kristian Monsen562e5132015-06-05 14:10:12 -0700664 switch (ret) {
665 case 0:
Mark Salyzyn02687e72016-08-03 14:20:41 -0700666 // only long options
Kristian Monsen562e5132015-06-05 14:10:12 -0700667 if (long_options[option_index].name == pid_str) {
668 // ToDo: determine runtime PID_MAX?
669 if (!getSizeTArg(optarg, &pid, 1)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700670 logcat_panic(HELP_TRUE, "%s %s out of range\n",
Kristian Monsen562e5132015-06-05 14:10:12 -0700671 long_options[option_index].name, optarg);
672 }
673 break;
674 }
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800675 if (long_options[option_index].name == wrap_str) {
676 mode |= ANDROID_LOG_WRAP |
677 ANDROID_LOG_RDONLY |
678 ANDROID_LOG_NONBLOCK;
679 // ToDo: implement API that supports setting a wrap timeout
680 size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT;
681 if (optarg && !getSizeTArg(optarg, &dummy, 1)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700682 logcat_panic(HELP_TRUE, "%s %s out of range\n",
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800683 long_options[option_index].name, optarg);
684 }
685 if (dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) {
686 fprintf(stderr,
687 "WARNING: %s %u seconds, ignoring %zu\n",
688 long_options[option_index].name,
689 ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy);
690 }
691 break;
692 }
Mark Salyzync9202772016-03-30 09:38:31 -0700693 if (long_options[option_index].name == print_str) {
694 g_printItAnyways = true;
695 break;
696 }
Mark Salyzyn538bc122016-11-16 15:28:31 -0800697 if (long_options[option_index].name == debug_str) {
698 g_debug = true;
699 break;
700 }
Mark Salyzyn02687e72016-08-03 14:20:41 -0700701 if (long_options[option_index].name == id_str) {
702 setId = optarg && optarg[0] ? optarg : NULL;
703 break;
704 }
Kristian Monsen562e5132015-06-05 14:10:12 -0700705 break;
706
Mark Salyzyn95132e92013-11-22 10:55:48 -0800707 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800708 // default to all silent
709 android_log_addFilterRule(g_logformat, "*:s");
710 break;
711
712 case 'c':
713 clearLog = 1;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800714 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800715 break;
716
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800717 case 'L':
Mark Salyzynad5e4112016-08-04 07:53:52 -0700718 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK;
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800719 break;
720
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800721 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800722 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800723 break;
724
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800725 case 't':
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700726 got_t = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800727 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800728 /* FALLTHRU */
729 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800730 if (strspn(optarg, "0123456789") != strlen(optarg)) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700731 char *cp = parseTime(tail_time, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800732 if (!cp) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700733 logcat_panic(HELP_FALSE,
734 "-%c \"%s\" not in time format\n",
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700735 ret, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800736 }
737 if (*cp) {
738 char c = *cp;
739 *cp = '\0';
740 fprintf(stderr,
741 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
742 ret, optarg, c, cp + 1);
743 *cp = c;
744 }
745 } else {
Traian Schiau59763032015-04-10 15:51:39 +0300746 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800747 fprintf(stderr,
748 "WARNING: -%c %s invalid, setting to 1\n",
749 ret, optarg);
750 tail_lines = 1;
751 }
752 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800753 break;
754
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800755 case 'D':
756 printDividers = true;
757 break;
758
Casey Dahlindc42a872016-03-17 16:18:55 -0700759 case 'e':
760 g_regex = new pcrecpp::RE(optarg);
761 break;
762
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700763 case 'm': {
764 char *end = NULL;
765 if (!getSizeTArg(optarg, &g_maxCount)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700766 logcat_panic(HELP_FALSE, "-%c \"%s\" isn't an "
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700767 "integer greater than zero\n", ret, optarg);
768 }
769 }
770 break;
771
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800772 case 'g':
Mark Salyzynf8bff872015-11-30 12:57:56 -0800773 if (!optarg) {
774 getLogSize = 1;
775 break;
776 }
777 // FALLTHRU
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800778
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800779 case 'G': {
Traian Schiau59763032015-04-10 15:51:39 +0300780 char *cp;
781 if (strtoll(optarg, &cp, 0) > 0) {
782 setLogSize = strtoll(optarg, &cp, 0);
783 } else {
784 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800785 }
786
787 switch(*cp) {
788 case 'g':
789 case 'G':
790 setLogSize *= 1024;
791 /* FALLTHRU */
792 case 'm':
793 case 'M':
794 setLogSize *= 1024;
795 /* FALLTHRU */
796 case 'k':
797 case 'K':
798 setLogSize *= 1024;
799 /* FALLTHRU */
800 case '\0':
801 break;
802
803 default:
804 setLogSize = 0;
805 }
806
807 if (!setLogSize) {
808 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
Traian Schiau59763032015-04-10 15:51:39 +0300809 return EXIT_FAILURE;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800810 }
811 }
812 break;
813
814 case 'p':
Mark Salyzynf8bff872015-11-30 12:57:56 -0800815 if (!optarg) {
816 getPruneList = 1;
817 break;
818 }
819 // FALLTHRU
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800820
821 case 'P':
822 setPruneList = optarg;
823 break;
824
Joe Onorato6fa09a02010-02-26 10:04:23 -0800825 case 'b': {
Mark Salyzyn45177732016-04-11 14:03:48 -0700826 unsigned idMask = 0;
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700827 while ((optarg = strtok(optarg, ",:; \t\n\r\f")) != NULL) {
828 if (strcmp(optarg, "default") == 0) {
Mark Salyzyn45177732016-04-11 14:03:48 -0700829 idMask |= (1 << LOG_ID_MAIN) |
830 (1 << LOG_ID_SYSTEM) |
831 (1 << LOG_ID_CRASH);
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700832 } else if (strcmp(optarg, "all") == 0) {
Mark Salyzyn45177732016-04-11 14:03:48 -0700833 idMask = (unsigned)-1;
834 } else {
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700835 log_id_t log_id = android_name_to_log_id(optarg);
Mark Salyzyn45177732016-04-11 14:03:48 -0700836 const char *name = android_log_id_to_name(log_id);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800837
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700838 if (strcmp(name, optarg) != 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700839 logcat_panic(HELP_TRUE,
840 "unknown buffer %s\n", optarg);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800841 }
Mark Salyzyn45177732016-04-11 14:03:48 -0700842 idMask |= (1 << log_id);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800843 }
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700844 optarg = NULL;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800845 }
846
Mark Salyzyn45177732016-04-11 14:03:48 -0700847 for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
848 const char *name = android_log_id_to_name((log_id_t)i);
849 log_id_t log_id = android_name_to_log_id(name);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800850
Mark Salyzyn45177732016-04-11 14:03:48 -0700851 if (log_id != (log_id_t)i) {
852 continue;
853 }
854 if ((idMask & (1 << i)) == 0) {
855 continue;
856 }
Mark Salyzyn083b0372015-12-04 10:59:45 -0800857
Mark Salyzyn45177732016-04-11 14:03:48 -0700858 bool found = false;
859 for (dev = devices; dev; dev = dev->next) {
860 if (!strcmp(name, dev->device)) {
861 found = true;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800862 break;
863 }
Mark Salyzyn45177732016-04-11 14:03:48 -0700864 if (!dev->next) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800865 break;
866 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800867 }
Mark Salyzyn45177732016-04-11 14:03:48 -0700868 if (found) {
869 continue;
870 }
871
872 bool binary = !strcmp(name, "events") ||
873 !strcmp(name, "security");
874 log_device_t* d = new log_device_t(name, binary);
875
Mark Salyzyn083b0372015-12-04 10:59:45 -0800876 if (dev) {
Mark Salyzyn45177732016-04-11 14:03:48 -0700877 dev->next = d;
878 dev = d;
879 } else {
880 devices = dev = d;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800881 }
Mark Salyzyn45177732016-04-11 14:03:48 -0700882 g_devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800883 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800884 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800885 break;
886
887 case 'B':
Traian Schiau59763032015-04-10 15:51:39 +0300888 g_printBinary = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800889 break;
890
891 case 'f':
Mark Salyzyn9812fc42015-10-06 08:59:02 -0700892 if ((tail_time == log_time::EPOCH) && (tail_lines == 0)) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700893 tail_time = lastLogTime(optarg);
894 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800895 // redirect output to a file
Traian Schiau59763032015-04-10 15:51:39 +0300896 g_outputFileName = optarg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800897 break;
898
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700899 case 'r':
900 if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700901 logcat_panic(HELP_TRUE,
902 "Invalid parameter \"%s\" to -r\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800903 }
904 break;
905
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700906 case 'n':
907 if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700908 logcat_panic(HELP_TRUE,
909 "Invalid parameter \"%s\" to -n\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800910 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800911 break;
912
913 case 'v':
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700914 if (!strcmp(optarg, "help") || !strcmp(optarg, "--help")) {
915 show_format_help();
916 exit(0);
917 }
918 err = setLogFormat(optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800919 if (err < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700920 logcat_panic(HELP_FORMAT,
921 "Invalid parameter \"%s\" to -v\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800922 }
Mark Salyzyne1f20042015-05-06 08:40:40 -0700923 hasSetLogFormat |= err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800924 break;
925
926 case 'Q':
927 /* this is a *hidden* option used to start a version of logcat */
928 /* in an emulated device only. it basically looks for androidboot.logcat= */
929 /* on the kernel command line. If something is found, it extracts a log filter */
930 /* and uses it to run the program. If nothing is found, the program should */
931 /* quit immediately */
932#define KERNEL_OPTION "androidboot.logcat="
933#define CONSOLE_OPTION "androidboot.console="
934 {
935 int fd;
936 char* logcat;
937 char* console;
938 int force_exit = 1;
939 static char cmdline[1024];
940
941 fd = open("/proc/cmdline", O_RDONLY);
942 if (fd >= 0) {
943 int n = read(fd, cmdline, sizeof(cmdline)-1 );
944 if (n < 0) n = 0;
945 cmdline[n] = 0;
946 close(fd);
947 } else {
948 cmdline[0] = 0;
949 }
950
951 logcat = strstr( cmdline, KERNEL_OPTION );
952 console = strstr( cmdline, CONSOLE_OPTION );
953 if (logcat != NULL) {
954 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
955 char* q = strpbrk( p, " \t\n\r" );;
956
957 if (q != NULL)
958 *q = 0;
959
960 forceFilters = p;
961 force_exit = 0;
962 }
963 /* if nothing found or invalid filters, exit quietly */
Traian Schiau59763032015-04-10 15:51:39 +0300964 if (force_exit) {
965 return EXIT_SUCCESS;
966 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800967
968 /* redirect our output to the emulator console */
969 if (console) {
970 char* p = console + sizeof(CONSOLE_OPTION)-1;
971 char* q = strpbrk( p, " \t\n\r" );
972 char devname[64];
973 int len;
974
975 if (q != NULL) {
976 len = q - p;
977 } else
978 len = strlen(p);
979
980 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
981 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
982 if (len < (int)sizeof(devname)) {
983 fd = open( devname, O_WRONLY );
984 if (fd >= 0) {
985 dup2(fd, 1);
986 dup2(fd, 2);
987 close(fd);
988 }
989 }
990 }
991 }
992 break;
993
Mark Salyzyn34facab2014-02-06 14:48:50 -0800994 case 'S':
995 printStatistics = 1;
996 break;
997
Traian Schiau59763032015-04-10 15:51:39 +0300998 case ':':
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700999 logcat_panic(HELP_TRUE,
1000 "Option -%c needs an argument\n", optopt);
Traian Schiau59763032015-04-10 15:51:39 +03001001 break;
1002
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001003 default:
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001004 logcat_panic(HELP_TRUE,
1005 "Unrecognized Option %c\n", optopt);
Traian Schiau59763032015-04-10 15:51:39 +03001006 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001007 }
1008 }
1009
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001010 if (g_maxCount && got_t) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001011 logcat_panic(HELP_TRUE,
1012 "Cannot use -m (--max-count) and -t together\n");
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001013 }
Mark Salyzync9202772016-03-30 09:38:31 -07001014 if (g_printItAnyways && (!g_regex || !g_maxCount)) {
1015 // One day it would be nice if --print -v color and --regex <expr>
1016 // could play with each other and show regex highlighted content.
1017 fprintf(stderr, "WARNING: "
1018 "--print ignored, to be used in combination with\n"
1019 " "
1020 "--regex <expr> and --max-count <N>\n");
1021 g_printItAnyways = false;
1022 }
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001023
Joe Onorato6fa09a02010-02-26 10:04:23 -08001024 if (!devices) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001025 dev = devices = new log_device_t("main", false);
Traian Schiau59763032015-04-10 15:51:39 +03001026 g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001027 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001028 dev = dev->next = new log_device_t("system", false);
Traian Schiau59763032015-04-10 15:51:39 +03001029 g_devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001030 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001031 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001032 dev = dev->next = new log_device_t("crash", false);
Traian Schiau59763032015-04-10 15:51:39 +03001033 g_devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -07001034 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001035 }
1036
Traian Schiau59763032015-04-10 15:51:39 +03001037 if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001038 logcat_panic(HELP_TRUE, "-r requires -f as well\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001039 }
1040
Mark Salyzyn02687e72016-08-03 14:20:41 -07001041 if (setId != NULL) {
1042 if (g_outputFileName == NULL) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001043 logcat_panic(HELP_TRUE, "--id='%s' requires -f as well\n", setId);
Mark Salyzyn02687e72016-08-03 14:20:41 -07001044 }
1045
1046 std::string file_name = android::base::StringPrintf("%s.id", g_outputFileName);
1047 std::string file;
1048 bool file_ok = android::base::ReadFileToString(file_name, &file);
1049 android::base::WriteStringToFile(setId, file_name,
1050 S_IRUSR | S_IWUSR, getuid(), getgid());
1051 if (!file_ok || (file.compare(setId) == 0)) {
1052 setId = NULL;
1053 }
1054 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001055
1056 if (hasSetLogFormat == 0) {
1057 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
1058
1059 if (logFormat != NULL) {
1060 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001061 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001062 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001063 logFormat);
1064 }
Mark Salyzyn649fc602014-09-16 09:15:15 -07001065 } else {
1066 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001067 }
1068 }
1069
1070 if (forceFilters) {
1071 err = android_log_addFilterString(g_logformat, forceFilters);
1072 if (err < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001073 logcat_panic(HELP_FALSE,
1074 "Invalid filter expression in logcat args\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001075 }
1076 } else if (argc == optind) {
1077 // Add from environment variable
1078 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
1079
1080 if (env_tags_orig != NULL) {
1081 err = android_log_addFilterString(g_logformat, env_tags_orig);
1082
Mark Salyzyn95132e92013-11-22 10:55:48 -08001083 if (err < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001084 logcat_panic(HELP_TRUE,
Traian Schiau59763032015-04-10 15:51:39 +03001085 "Invalid filter expression in ANDROID_LOG_TAGS\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001086 }
1087 }
1088 } else {
1089 // Add from commandline
1090 for (int i = optind ; i < argc ; i++) {
1091 err = android_log_addFilterString(g_logformat, argv[i]);
1092
Mark Salyzyn95132e92013-11-22 10:55:48 -08001093 if (err < 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001094 logcat_panic(HELP_TRUE,
1095 "Invalid filter expression '%s'\n", argv[i]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001096 }
1097 }
1098 }
1099
Joe Onorato6fa09a02010-02-26 10:04:23 -08001100 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001101 if (tail_time != log_time::EPOCH) {
Kristian Monsen562e5132015-06-05 14:10:12 -07001102 logger_list = android_logger_list_alloc_time(mode, tail_time, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001103 } else {
Kristian Monsen562e5132015-06-05 14:10:12 -07001104 logger_list = android_logger_list_alloc(mode, tail_lines, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001105 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001106 const char *openDeviceFail = NULL;
1107 const char *clearFail = NULL;
1108 const char *setSizeFail = NULL;
1109 const char *getSizeFail = NULL;
1110 // We have three orthogonal actions below to clear, set log size and
1111 // get log size. All sharing the same iteration loop.
Joe Onorato6fa09a02010-02-26 10:04:23 -08001112 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001113 dev->logger_list = logger_list;
1114 dev->logger = android_logger_open(logger_list,
1115 android_name_to_log_id(dev->device));
1116 if (!dev->logger) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001117 openDeviceFail = openDeviceFail ?: dev->device;
1118 dev = dev->next;
1119 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001120 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001121
Mark Salyzyn02687e72016-08-03 14:20:41 -07001122 if (clearLog || setId) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001123 if (g_outputFileName) {
1124 int maxRotationCountDigits =
1125 (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
1126
1127 for (int i = g_maxRotatedLogs ; i >= 0 ; --i) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001128 std::string file;
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001129
1130 if (i == 0) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001131 file = android::base::StringPrintf("%s", g_outputFileName);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001132 } else {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001133 file = android::base::StringPrintf("%s.%.*d",
1134 g_outputFileName, maxRotationCountDigits, i);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001135 }
1136
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001137 if (file.length() == 0) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001138 perror("while clearing log files");
1139 clearFail = clearFail ?: dev->device;
1140 break;
1141 }
1142
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001143 err = unlink(file.c_str());
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001144
1145 if (err < 0 && errno != ENOENT && clearFail == NULL) {
1146 perror("while clearing log files");
1147 clearFail = dev->device;
1148 }
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001149 }
1150 } else if (android_logger_clear(dev->logger)) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001151 clearFail = clearFail ?: dev->device;
Joe Onorato6fa09a02010-02-26 10:04:23 -08001152 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001153 }
1154
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001155 if (setLogSize) {
1156 if (android_logger_set_log_size(dev->logger, setLogSize)) {
1157 setSizeFail = setSizeFail ?: dev->device;
1158 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001159 }
1160
Joe Onorato6fa09a02010-02-26 10:04:23 -08001161 if (getLogSize) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001162 long size = android_logger_get_log_size(dev->logger);
1163 long readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001164
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001165 if ((size < 0) || (readable < 0)) {
1166 getSizeFail = getSizeFail ?: dev->device;
1167 } else {
1168 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
1169 "max entry is %db, max payload is %db\n", dev->device,
1170 value_of_size(size), multiplier_of_size(size),
1171 value_of_size(readable), multiplier_of_size(readable),
1172 (int) LOGGER_ENTRY_MAX_LEN,
1173 (int) LOGGER_ENTRY_MAX_PAYLOAD);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001174 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001175 }
1176
1177 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001178 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001179 // report any errors in the above loop and exit
1180 if (openDeviceFail) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001181 logcat_panic(HELP_FALSE,
1182 "Unable to open log device '%s'\n", openDeviceFail);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001183 }
1184 if (clearFail) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001185 logcat_panic(HELP_FALSE,
1186 "failed to clear the '%s' log\n", clearFail);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001187 }
1188 if (setSizeFail) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001189 logcat_panic(HELP_FALSE,
1190 "failed to set the '%s' log size\n", setSizeFail);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001191 }
1192 if (getSizeFail) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001193 logcat_panic(HELP_FALSE,
1194 "failed to get the readable '%s' log size", getSizeFail);
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001195 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001196
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001197 if (setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001198 size_t len = strlen(setPruneList);
1199 /*extra 32 bytes are needed by android_logger_set_prune_list */
1200 size_t bLen = len + 32;
1201 char *buf = NULL;
1202 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
1203 buf[len] = '\0';
1204 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001205 logcat_panic(HELP_FALSE, "failed to set the prune list");
Traian Schiau59763032015-04-10 15:51:39 +03001206 }
1207 free(buf);
1208 } else {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001209 logcat_panic(HELP_FALSE, "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001210 }
1211 }
1212
Mark Salyzyn1c950472014-04-01 17:19:47 -07001213 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001214 size_t len = 8192;
1215 char *buf;
1216
Mark Salyzyn083b0372015-12-04 10:59:45 -08001217 for (int retry = 32;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001218 (retry >= 0) && ((buf = new char [len]));
Traian Schiau59763032015-04-10 15:51:39 +03001219 delete [] buf, buf = NULL, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001220 if (getPruneList) {
1221 android_logger_get_prune_list(logger_list, buf, len);
1222 } else {
1223 android_logger_get_statistics(logger_list, buf, len);
1224 }
Mark Salyzyn34facab2014-02-06 14:48:50 -08001225 buf[len-1] = '\0';
Traian Schiau59763032015-04-10 15:51:39 +03001226 if (atol(buf) < 3) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001227 delete [] buf;
1228 buf = NULL;
1229 break;
1230 }
Traian Schiau59763032015-04-10 15:51:39 +03001231 size_t ret = atol(buf) + 1;
1232 if (ret <= len) {
1233 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001234 break;
1235 }
Traian Schiau59763032015-04-10 15:51:39 +03001236 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001237 }
1238
1239 if (!buf) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001240 logcat_panic(HELP_FALSE, "failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -08001241 }
1242
1243 // remove trailing FF
1244 char *cp = buf + len - 1;
1245 *cp = '\0';
1246 bool truncated = *--cp != '\f';
1247 if (!truncated) {
1248 *cp = '\0';
1249 }
1250
1251 // squash out the byte count
1252 cp = buf;
1253 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -07001254 while (isdigit(*cp)) {
1255 ++cp;
1256 }
1257 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001258 ++cp;
1259 }
1260 }
1261
1262 printf("%s", cp);
1263 delete [] buf;
Traian Schiau59763032015-04-10 15:51:39 +03001264 return EXIT_SUCCESS;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001265 }
1266
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001267 if (getLogSize) {
Traian Schiau59763032015-04-10 15:51:39 +03001268 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001269 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001270 if (setLogSize || setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001271 return EXIT_SUCCESS;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001272 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001273 if (clearLog) {
Traian Schiau59763032015-04-10 15:51:39 +03001274 return EXIT_SUCCESS;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001275 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001276
Mark Salyzynad5e4112016-08-04 07:53:52 -07001277 setupOutputAndSchedulingPolicy((mode & ANDROID_LOG_NONBLOCK) == 0);
Mark Salyzyn02687e72016-08-03 14:20:41 -07001278
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001279 //LOG_EVENT_INT(10, 12345);
1280 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
1281 //LOG_EVENT_STRING(0, "whassup, doc?");
1282
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001283 dev = NULL;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001284 log_device_t unexpected("unexpected", false);
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001285
Mark Salyzynaa730c12016-03-30 12:38:29 -07001286 while (!g_maxCount || (g_printCount < g_maxCount)) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001287 struct log_msg log_msg;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001288 log_device_t* d;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001289 int ret = android_logger_list_read(logger_list, &log_msg);
1290
1291 if (ret == 0) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001292 logcat_panic(HELP_FALSE, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001293 }
1294
1295 if (ret < 0) {
1296 if (ret == -EAGAIN) {
1297 break;
1298 }
1299
1300 if (ret == -EIO) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001301 logcat_panic(HELP_FALSE, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001302 }
1303 if (ret == -EINVAL) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001304 logcat_panic(HELP_FALSE, "read: unexpected length.\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001305 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001306 logcat_panic(HELP_FALSE, "logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001307 }
1308
Mark Salyzyn083b0372015-12-04 10:59:45 -08001309 for (d = devices; d; d = d->next) {
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001310 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001311 break;
1312 }
1313 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001314 if (!d) {
Traian Schiau59763032015-04-10 15:51:39 +03001315 g_devCount = 2; // set to Multiple
Mark Salyzyn9421b0c2015-02-26 14:33:35 -08001316 d = &unexpected;
1317 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001318 }
1319
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001320 if (dev != d) {
1321 dev = d;
Traian Schiau59763032015-04-10 15:51:39 +03001322 maybePrintStart(dev, printDividers);
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001323 }
Traian Schiau59763032015-04-10 15:51:39 +03001324 if (g_printBinary) {
1325 printBinary(&log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001326 } else {
Traian Schiau59763032015-04-10 15:51:39 +03001327 processBuffer(dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001328 }
1329 }
1330
1331 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001332
Traian Schiau59763032015-04-10 15:51:39 +03001333 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001334}