blob: 7e2bac779d04a9c58fcc1268f880ee296fbe5d5c [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>
Mark Salyzyn95132e92013-11-22 10:55:48 -080034#include <log/log.h>
Mark Salyzynfa3716b2014-02-14 16:05:05 -080035#include <log/log_read.h>
Colin Cross9227bd32013-07-23 16:59:20 -070036#include <log/logd.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070037#include <log/logger.h>
Colin Cross9227bd32013-07-23 16:59:20 -070038#include <log/logprint.h>
Elliott Hughesb9e53b42016-02-17 11:58:01 -080039#include <system/thread_defs.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040
Casey Dahlindc42a872016-03-17 16:18:55 -070041#include <pcrecpp.h>
42
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043#define DEFAULT_MAX_ROTATED_LOGS 4
44
45static AndroidLogFormat * g_logformat;
46
47/* logd prefixes records with a length field */
48#define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
49
Joe Onorato6fa09a02010-02-26 10:04:23 -080050struct log_device_t {
Mark Salyzyn95132e92013-11-22 10:55:48 -080051 const char* device;
Joe Onorato6fa09a02010-02-26 10:04:23 -080052 bool binary;
Mark Salyzyn95132e92013-11-22 10:55:48 -080053 struct logger *logger;
54 struct logger_list *logger_list;
Joe Onorato6fa09a02010-02-26 10:04:23 -080055 bool printed;
Joe Onorato6fa09a02010-02-26 10:04:23 -080056
Joe Onorato6fa09a02010-02-26 10:04:23 -080057 log_device_t* next;
58
Mark Salyzyn5f6738a2015-02-27 13:41:34 -080059 log_device_t(const char* d, bool b) {
Joe Onorato6fa09a02010-02-26 10:04:23 -080060 device = d;
61 binary = b;
Joe Onorato6fa09a02010-02-26 10:04:23 -080062 next = NULL;
63 printed = false;
Traian Schiau59763032015-04-10 15:51:39 +030064 logger = NULL;
65 logger_list = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -080066 }
Joe Onorato6fa09a02010-02-26 10:04:23 -080067};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068
69namespace android {
70
71/* Global Variables */
72
Mark Salyzync6d66522016-03-30 12:38:29 -070073static const char * g_outputFileName;
Traian Schiau59763032015-04-10 15:51:39 +030074// 0 means "no log rotation"
Mark Salyzync6d66522016-03-30 12:38:29 -070075static size_t g_logRotateSizeKBytes;
Traian Schiau59763032015-04-10 15:51:39 +030076// 0 means "unbounded"
77static size_t g_maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078static int g_outFD = -1;
Mark Salyzync6d66522016-03-30 12:38:29 -070079static size_t g_outByteCount;
80static int g_printBinary;
81static int g_devCount; // >1 means multiple
Casey Dahlindc42a872016-03-17 16:18:55 -070082static pcrecpp::RE* g_regex;
Casey Dahlin6ac498d2016-03-17 14:04:52 -070083// 0 means "infinite"
Mark Salyzync6d66522016-03-30 12:38:29 -070084static size_t g_maxCount;
85static size_t g_printCount;
Mark Salyzync9202772016-03-30 09:38:31 -070086static bool g_printItAnyways;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087
Mark Salyzynaa730c12016-03-30 12:38:29 -070088// if showHelp is set, newline required in fmt statement to transition to usage
Traian Schiau59763032015-04-10 15:51:39 +030089__noreturn static void logcat_panic(bool showHelp, const char *fmt, ...) __printflike(2,3);
90
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091static int openLogFile (const char *pathname)
92{
Edwin Vane80b221c2012-08-13 12:55:07 -040093 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094}
95
96static void rotateLogs()
97{
98 int err;
99
100 // Can't rotate logs if we're not outputting to a file
101 if (g_outputFileName == NULL) {
102 return;
103 }
104
105 close(g_outFD);
106
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700107 // Compute the maximum number of digits needed to count up to g_maxRotatedLogs in decimal.
108 // eg: g_maxRotatedLogs == 30 -> log10(30) == 1.477 -> maxRotationCountDigits == 2
109 int maxRotationCountDigits =
110 (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
111
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112 for (int i = g_maxRotatedLogs ; i > 0 ; i--) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700113 std::string file1 = android::base::StringPrintf(
114 "%s.%.*d", g_outputFileName, maxRotationCountDigits, i);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700116 std::string file0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117 if (i - 1 == 0) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700118 file0 = android::base::StringPrintf("%s", g_outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119 } else {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700120 file0 = android::base::StringPrintf(
121 "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122 }
123
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700124 if ((file0.length() == 0) || (file1.length() == 0)) {
Traian Schiau59763032015-04-10 15:51:39 +0300125 perror("while rotating log files");
126 break;
127 }
128
Mark Salyzyn5b1a5382016-08-03 14:20:41 -0700129 err = rename(file0.c_str(), file1.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130
131 if (err < 0 && errno != ENOENT) {
132 perror("while rotating log files");
133 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134 }
135
Traian Schiau59763032015-04-10 15:51:39 +0300136 g_outFD = openLogFile(g_outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137
138 if (g_outFD < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300139 logcat_panic(false, "couldn't open output file");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140 }
141
142 g_outByteCount = 0;
143
144}
145
Mark Salyzyn95132e92013-11-22 10:55:48 -0800146void printBinary(struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147{
Mark Salyzyn95132e92013-11-22 10:55:48 -0800148 size_t size = buf->len();
149
150 TEMP_FAILURE_RETRY(write(g_outFD, buf, size));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151}
152
Mark Salyzynaa730c12016-03-30 12:38:29 -0700153static bool regexOk(const AndroidLogEntry& entry)
Casey Dahlindc42a872016-03-17 16:18:55 -0700154{
Mark Salyzynaa730c12016-03-30 12:38:29 -0700155 if (!g_regex) {
Casey Dahlindc42a872016-03-17 16:18:55 -0700156 return true;
157 }
158
Casey Dahlindc42a872016-03-17 16:18:55 -0700159 std::string messageString(entry.message, entry.messageLen);
160
161 return g_regex->PartialMatch(messageString);
162}
163
Mark Salyzyn95132e92013-11-22 10:55:48 -0800164static void processBuffer(log_device_t* dev, struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165{
Mathias Agopian50844522010-03-17 16:10:26 -0700166 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 int err;
168 AndroidLogEntry entry;
169 char binaryMsgBuf[1024];
170
Joe Onorato6fa09a02010-02-26 10:04:23 -0800171 if (dev->binary) {
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800172 static bool hasOpenedEventTagMap = false;
173 static EventTagMap *eventTagMap = NULL;
174
175 if (!eventTagMap && !hasOpenedEventTagMap) {
176 eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
177 hasOpenedEventTagMap = true;
178 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800179 err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry,
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800180 eventTagMap,
Mark Salyzyn95132e92013-11-22 10:55:48 -0800181 binaryMsgBuf,
182 sizeof(binaryMsgBuf));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183 //printf(">>> pri=%d len=%d msg='%s'\n",
184 // entry.priority, entry.messageLen, entry.message);
185 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800186 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800188 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189 goto error;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800190 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191
Mark Salyzync9202772016-03-30 09:38:31 -0700192 if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) {
193 bool match = regexOk(entry);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800194
Mark Salyzync9202772016-03-30 09:38:31 -0700195 g_printCount += match;
196 if (match || g_printItAnyways) {
197 bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry);
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700198
Mark Salyzync9202772016-03-30 09:38:31 -0700199 if (bytesWritten < 0) {
200 logcat_panic(false, "output error");
201 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800202 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 }
204
205 g_outByteCount += bytesWritten;
206
Mark Salyzyn95132e92013-11-22 10:55:48 -0800207 if (g_logRotateSizeKBytes > 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 && (g_outByteCount / 1024) >= g_logRotateSizeKBytes
209 ) {
210 rotateLogs();
211 }
212
213error:
214 //fprintf (stderr, "Error processing record\n");
215 return;
216}
217
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800218static void maybePrintStart(log_device_t* dev, bool printDividers) {
219 if (!dev->printed || printDividers) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800220 if (g_devCount > 1 && !g_printBinary) {
221 char buf[1024];
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800222 snprintf(buf, sizeof(buf), "--------- %s %s\n",
223 dev->printed ? "switch to" : "beginning of",
Mark Salyzyn95132e92013-11-22 10:55:48 -0800224 dev->device);
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800225 if (write(g_outFD, buf, strlen(buf)) < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300226 logcat_panic(false, "output error");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800227 }
228 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800229 dev->printed = true;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800230 }
231}
232
Mark Salyzynad5e4112016-08-04 07:53:52 -0700233static void setupOutputAndSchedulingPolicy(bool blocking) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 if (g_outputFileName == NULL) {
235 g_outFD = STDOUT_FILENO;
Mark Salyzynad5e4112016-08-04 07:53:52 -0700236 return;
237 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238
Mark Salyzynad5e4112016-08-04 07:53:52 -0700239 if (blocking) {
240 // Lower priority and set to batch scheduling if we are saving
241 // the logs into files and taking continuous content.
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700242 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
243 fprintf(stderr, "failed to set background scheduling policy\n");
244 }
245
246 struct sched_param param;
247 memset(&param, 0, sizeof(param));
248 if (sched_setscheduler((pid_t) 0, SCHED_BATCH, &param) < 0) {
249 fprintf(stderr, "failed to set to batch scheduler\n");
250 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251
Riley Andrewsaede9892015-06-08 23:36:34 -0700252 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
253 fprintf(stderr, "failed set to priority\n");
254 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255 }
Mark Salyzynad5e4112016-08-04 07:53:52 -0700256
257 g_outFD = openLogFile (g_outputFileName);
258
259 if (g_outFD < 0) {
260 logcat_panic(false, "couldn't open output file");
261 }
262
263 struct stat statbuf;
264 if (fstat(g_outFD, &statbuf) == -1) {
265 close(g_outFD);
266 logcat_panic(false, "couldn't get output file stat\n");
267 }
268
269 if ((size_t) statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
270 close(g_outFD);
271 logcat_panic(false, "invalid output file stat\n");
272 }
273
274 g_outByteCount = statbuf.st_size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800275}
276
277static void show_help(const char *cmd)
278{
279 fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd);
280
281 fprintf(stderr, "options include:\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700282 " -s Set default filter to silent. Equivalent to filterspec '*:S'\n"
283 " -f <file>, --file=<file> Log to file. Default is stdout\n"
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700284 " -r <kbytes>, --rotate-kbytes=<kbytes>\n"
285 " Rotate log every kbytes. Requires -f option\n"
286 " -n <count>, --rotate-count=<count>\n"
287 " Sets max number of rotated logs to <count>, default 4\n"
Mark Salyzyn02687e72016-08-03 14:20:41 -0700288 " --id=<id> If the signature id for logging to file changes, then clear\n"
289 " the fileset and continue\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700290 " -v <format>, --format=<format>\n"
Mark Salyzyn9cfd1c62016-07-06 11:12:14 -0700291 " Sets log print format verb and adverbs, where <format> is:\n"
Mark Salyzyne735a732016-07-06 13:30:40 -0700292 " brief long process raw tag thread threadtime time\n"
293 " and individually flagged modifying adverbs can be added:\n"
Mark Salyzyn9cfd1c62016-07-06 11:12:14 -0700294 " color epoch monotonic printable uid usec UTC year zone\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700295 " -D, --dividers Print dividers between each log buffer\n"
296 " -c, --clear Clear (flush) the entire log and exit\n"
Mark Salyzynb7d059b2016-06-06 14:56:00 -0700297 " if Log to File specified, clear fileset instead\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700298 " -d Dump the log and then exit (don't block)\n"
299 " -e <expr>, --regex=<expr>\n"
300 " Only print lines where the log message matches <expr>\n"
301 " where <expr> is a regular expression\n"
302 // Leave --head undocumented as alias for -m
303 " -m <count>, --max-count=<count>\n"
304 " Quit after printing <count> lines. This is meant to be\n"
305 " paired with --regex, but will work on its own.\n"
306 " --print Paired with --regex and --max-count to let content bypass\n"
Mark Salyzync9202772016-03-30 09:38:31 -0700307 " regex filter but still stop at number of matches.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700308 // Leave --tail undocumented as alias for -t
309 " -t <count> Print only the most recent <count> lines (implies -d)\n"
310 " -t '<time>' Print most recent lines since specified time (implies -d)\n"
311 " -T <count> Print only the most recent <count> lines (does not imply -d)\n"
312 " -T '<time>' Print most recent lines since specified time (not imply -d)\n"
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700313 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700314 " 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700315 " -g, --buffer-size Get the size of the ring buffer.\n"
316 " -G <size>, --buffer-size=<size>\n"
317 " Set size of log ring buffer, may suffix with K or M.\n"
318 " -L, -last Dump logs from prior to last reboot\n"
Mark Salyzyn083b0372015-12-04 10:59:45 -0800319 // Leave security (Device Owner only installations) and
320 // kernel (userdebug and eng) buffers undocumented.
Mark Salyzyn378f4742016-04-12 09:11:46 -0700321 " -b <buffer>, --buffer=<buffer> Request alternate ring buffer, 'main',\n"
322 " 'system', 'radio', 'events', 'crash', 'default' or 'all'.\n"
Mark Salyzyn45177732016-04-11 14:03:48 -0700323 " Multiple -b parameters or comma separated list of buffers are\n"
324 " allowed. Buffers interleaved. Default -b main,system,crash.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700325 " -B, --binary Output the log in binary.\n"
326 " -S, --statistics Output statistics.\n"
327 " -p, --prune Print prune white and ~black list. Service is specified as\n"
328 " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700329 " with ~, otherwise weighed for longevity if unadorned. All\n"
330 " other pruning activity is oldest first. Special case ~!\n"
331 " represents an automatic quicker pruning for the noisiest\n"
332 " UID as determined by the current statistics.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700333 " -P '<list> ...', --prune='<list> ...'\n"
334 " Set prune white and ~black list, using same format as\n"
335 " listed above. Must be quoted.\n"
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800336 " --pid=<pid> Only prints logs from the given pid.\n"
Mark Salyzyn378f4742016-04-12 09:11:46 -0700337 // Check ANDROID_LOG_WRAP_DEFAULT_TIMEOUT value for match to 2 hours
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800338 " --wrap Sleep for 2 hours or when buffer about to wrap whichever\n"
339 " comes first. Improves efficiency of polling by providing\n"
340 " an about-to-wrap wakeup.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700342 fprintf(stderr,"\nfilterspecs are a series of \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343 " <tag>[:priority]\n\n"
344 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700345 " V Verbose (default for <tag>)\n"
346 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347 " I Info\n"
348 " W Warn\n"
349 " E Error\n"
350 " F Fatal\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700351 " S Silent (suppress all output)\n"
352 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
353 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
354 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
355 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
356 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700357 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358}
359
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360static int setLogFormat(const char * formatString)
361{
362 static AndroidLogPrintFormat format;
363
364 format = android_log_formatFromString(formatString);
365
366 if (format == FORMAT_OFF) {
367 // FORMAT_OFF means invalid string
368 return -1;
369 }
370
Mark Salyzyne1f20042015-05-06 08:40:40 -0700371 return android_log_setPrintFormat(g_logformat, format);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372}
373
Mark Salyzyn671e3432014-05-06 07:34:59 -0700374static const char multipliers[][2] = {
375 { "" },
376 { "K" },
377 { "M" },
378 { "G" }
379};
380
381static unsigned long value_of_size(unsigned long value)
382{
383 for (unsigned i = 0;
384 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
385 value /= 1024, ++i) ;
386 return value;
387}
388
389static const char *multiplier_of_size(unsigned long value)
390{
391 unsigned i;
392 for (i = 0;
393 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
394 value /= 1024, ++i) ;
395 return multipliers[i];
396}
397
Traian Schiau59763032015-04-10 15:51:39 +0300398/*String to unsigned int, returns -1 if it fails*/
Mark Salyzyn33c26252016-04-12 09:11:46 -0700399static bool getSizeTArg(const char *ptr, size_t *val, size_t min = 0,
Traian Schiau59763032015-04-10 15:51:39 +0300400 size_t max = SIZE_MAX)
401{
Kristian Monsen562e5132015-06-05 14:10:12 -0700402 if (!ptr) {
Traian Schiau59763032015-04-10 15:51:39 +0300403 return false;
404 }
405
Kristian Monsen562e5132015-06-05 14:10:12 -0700406 char *endp;
407 errno = 0;
408 size_t ret = (size_t)strtoll(ptr, &endp, 0);
409
410 if (endp[0] || errno) {
411 return false;
412 }
413
414 if ((ret > max) || (ret < min)) {
Traian Schiau59763032015-04-10 15:51:39 +0300415 return false;
416 }
417
418 *val = ret;
419 return true;
420}
421
422static void logcat_panic(bool showHelp, const char *fmt, ...)
423{
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700424 va_list args;
425 va_start(args, fmt);
426 vfprintf(stderr, fmt, args);
427 va_end(args);
Traian Schiau59763032015-04-10 15:51:39 +0300428
429 if (showHelp) {
430 show_help(getprogname());
431 }
432
433 exit(EXIT_FAILURE);
434}
435
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700436static char *parseTime(log_time &t, const char *cp) {
437
438 char *ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
439 if (ep) {
440 return ep;
441 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700442 ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
443 if (ep) {
444 return ep;
445 }
446 return t.strptime(cp, "%s.%q");
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700447}
Mark Salyzynf3555d92015-05-27 07:39:56 -0700448
Mark Salyzyn31961062016-08-04 07:43:46 -0700449// Find last logged line in <outputFileName>, or <outputFileName>.1
Mark Salyzynf3555d92015-05-27 07:39:56 -0700450static log_time lastLogTime(char *outputFileName) {
451 log_time retval(log_time::EPOCH);
452 if (!outputFileName) {
453 return retval;
454 }
455
Mark Salyzynf3555d92015-05-27 07:39:56 -0700456 std::string directory;
457 char *file = strrchr(outputFileName, '/');
458 if (!file) {
459 directory = ".";
460 file = outputFileName;
461 } else {
462 *file = '\0';
463 directory = outputFileName;
464 *file = '/';
465 ++file;
466 }
Mark Salyzync18c2132016-04-01 07:52:20 -0700467
468 std::unique_ptr<DIR, int(*)(DIR*)>
469 dir(opendir(directory.c_str()), closedir);
470 if (!dir.get()) {
471 return retval;
472 }
473
Mark Salyzyn31961062016-08-04 07:43:46 -0700474 log_time now(android_log_clockid());
Mark Salyzync18c2132016-04-01 07:52:20 -0700475
Mark Salyzynf3555d92015-05-27 07:39:56 -0700476 size_t len = strlen(file);
477 log_time modulo(0, NS_PER_SEC);
Mark Salyzynf3555d92015-05-27 07:39:56 -0700478 struct dirent *dp;
Mark Salyzync18c2132016-04-01 07:52:20 -0700479
Mark Salyzynf3555d92015-05-27 07:39:56 -0700480 while ((dp = readdir(dir.get())) != NULL) {
Mark Salyzyn31961062016-08-04 07:43:46 -0700481 if ((dp->d_type != DT_REG) ||
482 (strncmp(dp->d_name, file, len) != 0) ||
483 (dp->d_name[len] &&
484 ((dp->d_name[len] != '.') ||
485 (strtoll(dp->d_name + 1, NULL, 10) != 1)))) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700486 continue;
487 }
488
489 std::string file_name = directory;
490 file_name += "/";
491 file_name += dp->d_name;
492 std::string file;
493 if (!android::base::ReadFileToString(file_name, &file)) {
494 continue;
495 }
496
497 bool found = false;
498 for (const auto& line : android::base::Split(file, "\n")) {
499 log_time t(log_time::EPOCH);
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700500 char *ep = parseTime(t, line.c_str());
Mark Salyzynf3555d92015-05-27 07:39:56 -0700501 if (!ep || (*ep != ' ')) {
502 continue;
503 }
504 // determine the time precision of the logs (eg: msec or usec)
505 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
506 if (t.tv_nsec % (mod * 10)) {
507 modulo.tv_nsec = mod;
508 break;
509 }
510 }
511 // We filter any times later than current as we may not have the
512 // year stored with each log entry. Also, since it is possible for
513 // entries to be recorded out of order (very rare) we select the
514 // maximum we find just in case.
515 if ((t < now) && (t > retval)) {
516 retval = t;
517 found = true;
518 }
519 }
520 // We count on the basename file to be the definitive end, so stop here.
521 if (!dp->d_name[len] && found) {
522 break;
523 }
524 }
525 if (retval == log_time::EPOCH) {
526 return retval;
527 }
528 // tail_time prints matching or higher, round up by the modulo to prevent
529 // a replay of the last entry we have just checked.
530 retval += modulo;
531 return retval;
532}
533
Traian Schiau59763032015-04-10 15:51:39 +0300534} /* namespace android */
535
536
Joe Onorato6fa09a02010-02-26 10:04:23 -0800537int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800538{
Traian Schiau59763032015-04-10 15:51:39 +0300539 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800540 int err;
541 int hasSetLogFormat = 0;
542 int clearLog = 0;
543 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800544 unsigned long setLogSize = 0;
545 int getPruneList = 0;
546 char *setPruneList = NULL;
Mark Salyzyn02687e72016-08-03 14:20:41 -0700547 char *setId = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800548 int printStatistics = 0;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800549 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800550 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800551 log_device_t* devices = NULL;
552 log_device_t* dev;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800553 bool printDividers = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800554 struct logger_list *logger_list;
Traian Schiau59763032015-04-10 15:51:39 +0300555 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800556 log_time tail_time(log_time::EPOCH);
Kristian Monsen562e5132015-06-05 14:10:12 -0700557 size_t pid = 0;
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700558 bool got_t = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800559
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800560 signal(SIGPIPE, exit);
561
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800562 g_logformat = android_log_format_new();
563
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800564 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
Traian Schiau59763032015-04-10 15:51:39 +0300565 show_help(argv[0]);
566 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800567 }
568
569 for (;;) {
570 int ret;
571
Kristian Monsen562e5132015-06-05 14:10:12 -0700572 int option_index = 0;
Mark Salyzync9202772016-03-30 09:38:31 -0700573 // list of long-argument only strings for later comparison
Kristian Monsen562e5132015-06-05 14:10:12 -0700574 static const char pid_str[] = "pid";
Mark Salyzyn02687e72016-08-03 14:20:41 -0700575 static const char id_str[] = "id";
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800576 static const char wrap_str[] = "wrap";
Mark Salyzync9202772016-03-30 09:38:31 -0700577 static const char print_str[] = "print";
Kristian Monsen562e5132015-06-05 14:10:12 -0700578 static const struct option long_options[] = {
Mark Salyzynf8bff872015-11-30 12:57:56 -0800579 { "binary", no_argument, NULL, 'B' },
580 { "buffer", required_argument, NULL, 'b' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700581 { "buffer-size", optional_argument, NULL, 'g' },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800582 { "clear", no_argument, NULL, 'c' },
583 { "dividers", no_argument, NULL, 'D' },
584 { "file", required_argument, NULL, 'f' },
585 { "format", required_argument, NULL, 'v' },
Mark Salyzync18c2132016-04-01 07:52:20 -0700586 // hidden and undocumented reserved alias for --regex
587 { "grep", required_argument, NULL, 'e' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700588 // hidden and undocumented reserved alias for --max-count
589 { "head", required_argument, NULL, 'm' },
Mark Salyzyn02687e72016-08-03 14:20:41 -0700590 { id_str, required_argument, NULL, 0 },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800591 { "last", no_argument, NULL, 'L' },
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700592 { "max-count", required_argument, NULL, 'm' },
Mark Salyzync18c2132016-04-01 07:52:20 -0700593 { pid_str, required_argument, NULL, 0 },
Mark Salyzync9202772016-03-30 09:38:31 -0700594 { print_str, no_argument, NULL, 0 },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800595 { "prune", optional_argument, NULL, 'p' },
Casey Dahlindc42a872016-03-17 16:18:55 -0700596 { "regex", required_argument, NULL, 'e' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700597 { "rotate-count", required_argument, NULL, 'n' },
598 { "rotate-kbytes", required_argument, NULL, 'r' },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800599 { "statistics", no_argument, NULL, 'S' },
Mark Salyzynd85f6462016-03-30 09:15:09 -0700600 // hidden and undocumented reserved alias for -t
601 { "tail", required_argument, NULL, 't' },
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800602 // support, but ignore and do not document, the optional argument
603 { wrap_str, optional_argument, NULL, 0 },
Kristian Monsen562e5132015-06-05 14:10:12 -0700604 { NULL, 0, NULL, 0 }
605 };
606
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700607 ret = getopt_long(argc, argv, ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:m:e:",
Kristian Monsen562e5132015-06-05 14:10:12 -0700608 long_options, &option_index);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800609
610 if (ret < 0) {
611 break;
612 }
613
Kristian Monsen562e5132015-06-05 14:10:12 -0700614 switch (ret) {
615 case 0:
Mark Salyzyn02687e72016-08-03 14:20:41 -0700616 // only long options
Kristian Monsen562e5132015-06-05 14:10:12 -0700617 if (long_options[option_index].name == pid_str) {
618 // ToDo: determine runtime PID_MAX?
619 if (!getSizeTArg(optarg, &pid, 1)) {
620 logcat_panic(true, "%s %s out of range\n",
621 long_options[option_index].name, optarg);
622 }
623 break;
624 }
Mark Salyzyn41ba25f2015-11-30 13:48:56 -0800625 if (long_options[option_index].name == wrap_str) {
626 mode |= ANDROID_LOG_WRAP |
627 ANDROID_LOG_RDONLY |
628 ANDROID_LOG_NONBLOCK;
629 // ToDo: implement API that supports setting a wrap timeout
630 size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT;
631 if (optarg && !getSizeTArg(optarg, &dummy, 1)) {
632 logcat_panic(true, "%s %s out of range\n",
633 long_options[option_index].name, optarg);
634 }
635 if (dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) {
636 fprintf(stderr,
637 "WARNING: %s %u seconds, ignoring %zu\n",
638 long_options[option_index].name,
639 ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy);
640 }
641 break;
642 }
Mark Salyzync9202772016-03-30 09:38:31 -0700643 if (long_options[option_index].name == print_str) {
644 g_printItAnyways = true;
645 break;
646 }
Mark Salyzyn02687e72016-08-03 14:20:41 -0700647 if (long_options[option_index].name == id_str) {
648 setId = optarg && optarg[0] ? optarg : NULL;
649 break;
650 }
Kristian Monsen562e5132015-06-05 14:10:12 -0700651 break;
652
Mark Salyzyn95132e92013-11-22 10:55:48 -0800653 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800654 // default to all silent
655 android_log_addFilterRule(g_logformat, "*:s");
656 break;
657
658 case 'c':
659 clearLog = 1;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800660 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800661 break;
662
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800663 case 'L':
Mark Salyzynad5e4112016-08-04 07:53:52 -0700664 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK;
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800665 break;
666
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800667 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800668 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800669 break;
670
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800671 case 't':
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700672 got_t = true;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800673 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800674 /* FALLTHRU */
675 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800676 if (strspn(optarg, "0123456789") != strlen(optarg)) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700677 char *cp = parseTime(tail_time, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800678 if (!cp) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700679 logcat_panic(false, "-%c \"%s\" not in time format\n",
680 ret, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800681 }
682 if (*cp) {
683 char c = *cp;
684 *cp = '\0';
685 fprintf(stderr,
686 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
687 ret, optarg, c, cp + 1);
688 *cp = c;
689 }
690 } else {
Traian Schiau59763032015-04-10 15:51:39 +0300691 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800692 fprintf(stderr,
693 "WARNING: -%c %s invalid, setting to 1\n",
694 ret, optarg);
695 tail_lines = 1;
696 }
697 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800698 break;
699
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800700 case 'D':
701 printDividers = true;
702 break;
703
Casey Dahlindc42a872016-03-17 16:18:55 -0700704 case 'e':
705 g_regex = new pcrecpp::RE(optarg);
706 break;
707
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700708 case 'm': {
709 char *end = NULL;
710 if (!getSizeTArg(optarg, &g_maxCount)) {
711 logcat_panic(false, "-%c \"%s\" isn't an "
712 "integer greater than zero\n", ret, optarg);
713 }
714 }
715 break;
716
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800717 case 'g':
Mark Salyzynf8bff872015-11-30 12:57:56 -0800718 if (!optarg) {
719 getLogSize = 1;
720 break;
721 }
722 // FALLTHRU
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800723
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800724 case 'G': {
Traian Schiau59763032015-04-10 15:51:39 +0300725 char *cp;
726 if (strtoll(optarg, &cp, 0) > 0) {
727 setLogSize = strtoll(optarg, &cp, 0);
728 } else {
729 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800730 }
731
732 switch(*cp) {
733 case 'g':
734 case 'G':
735 setLogSize *= 1024;
736 /* FALLTHRU */
737 case 'm':
738 case 'M':
739 setLogSize *= 1024;
740 /* FALLTHRU */
741 case 'k':
742 case 'K':
743 setLogSize *= 1024;
744 /* FALLTHRU */
745 case '\0':
746 break;
747
748 default:
749 setLogSize = 0;
750 }
751
752 if (!setLogSize) {
753 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
Traian Schiau59763032015-04-10 15:51:39 +0300754 return EXIT_FAILURE;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800755 }
756 }
757 break;
758
759 case 'p':
Mark Salyzynf8bff872015-11-30 12:57:56 -0800760 if (!optarg) {
761 getPruneList = 1;
762 break;
763 }
764 // FALLTHRU
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800765
766 case 'P':
767 setPruneList = optarg;
768 break;
769
Joe Onorato6fa09a02010-02-26 10:04:23 -0800770 case 'b': {
Mark Salyzyn45177732016-04-11 14:03:48 -0700771 unsigned idMask = 0;
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700772 while ((optarg = strtok(optarg, ",:; \t\n\r\f")) != NULL) {
773 if (strcmp(optarg, "default") == 0) {
Mark Salyzyn45177732016-04-11 14:03:48 -0700774 idMask |= (1 << LOG_ID_MAIN) |
775 (1 << LOG_ID_SYSTEM) |
776 (1 << LOG_ID_CRASH);
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700777 } else if (strcmp(optarg, "all") == 0) {
Mark Salyzyn45177732016-04-11 14:03:48 -0700778 idMask = (unsigned)-1;
779 } else {
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700780 log_id_t log_id = android_name_to_log_id(optarg);
Mark Salyzyn45177732016-04-11 14:03:48 -0700781 const char *name = android_log_id_to_name(log_id);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800782
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700783 if (strcmp(name, optarg) != 0) {
784 logcat_panic(true, "unknown buffer %s\n", optarg);
Mark Salyzyn34facab2014-02-06 14:48:50 -0800785 }
Mark Salyzyn45177732016-04-11 14:03:48 -0700786 idMask |= (1 << log_id);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800787 }
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700788 optarg = NULL;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800789 }
790
Mark Salyzyn45177732016-04-11 14:03:48 -0700791 for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
792 const char *name = android_log_id_to_name((log_id_t)i);
793 log_id_t log_id = android_name_to_log_id(name);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800794
Mark Salyzyn45177732016-04-11 14:03:48 -0700795 if (log_id != (log_id_t)i) {
796 continue;
797 }
798 if ((idMask & (1 << i)) == 0) {
799 continue;
800 }
Mark Salyzyn083b0372015-12-04 10:59:45 -0800801
Mark Salyzyn45177732016-04-11 14:03:48 -0700802 bool found = false;
803 for (dev = devices; dev; dev = dev->next) {
804 if (!strcmp(name, dev->device)) {
805 found = true;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800806 break;
807 }
Mark Salyzyn45177732016-04-11 14:03:48 -0700808 if (!dev->next) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800809 break;
810 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800811 }
Mark Salyzyn45177732016-04-11 14:03:48 -0700812 if (found) {
813 continue;
814 }
815
816 bool binary = !strcmp(name, "events") ||
817 !strcmp(name, "security");
818 log_device_t* d = new log_device_t(name, binary);
819
Mark Salyzyn083b0372015-12-04 10:59:45 -0800820 if (dev) {
Mark Salyzyn45177732016-04-11 14:03:48 -0700821 dev->next = d;
822 dev = d;
823 } else {
824 devices = dev = d;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800825 }
Mark Salyzyn45177732016-04-11 14:03:48 -0700826 g_devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800827 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800828 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800829 break;
830
831 case 'B':
Traian Schiau59763032015-04-10 15:51:39 +0300832 g_printBinary = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800833 break;
834
835 case 'f':
Mark Salyzyn9812fc42015-10-06 08:59:02 -0700836 if ((tail_time == log_time::EPOCH) && (tail_lines == 0)) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700837 tail_time = lastLogTime(optarg);
838 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800839 // redirect output to a file
Traian Schiau59763032015-04-10 15:51:39 +0300840 g_outputFileName = optarg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800841 break;
842
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700843 case 'r':
844 if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) {
845 logcat_panic(true, "Invalid parameter %s to -r\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800846 }
847 break;
848
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700849 case 'n':
850 if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) {
851 logcat_panic(true, "Invalid parameter %s to -n\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800852 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800853 break;
854
855 case 'v':
856 err = setLogFormat (optarg);
857 if (err < 0) {
Mark Salyzyn1325ebf2016-06-07 13:03:10 -0700858 logcat_panic(true, "Invalid parameter %s to -v\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800859 }
Mark Salyzyne1f20042015-05-06 08:40:40 -0700860 hasSetLogFormat |= err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800861 break;
862
863 case 'Q':
864 /* this is a *hidden* option used to start a version of logcat */
865 /* in an emulated device only. it basically looks for androidboot.logcat= */
866 /* on the kernel command line. If something is found, it extracts a log filter */
867 /* and uses it to run the program. If nothing is found, the program should */
868 /* quit immediately */
869#define KERNEL_OPTION "androidboot.logcat="
870#define CONSOLE_OPTION "androidboot.console="
871 {
872 int fd;
873 char* logcat;
874 char* console;
875 int force_exit = 1;
876 static char cmdline[1024];
877
878 fd = open("/proc/cmdline", O_RDONLY);
879 if (fd >= 0) {
880 int n = read(fd, cmdline, sizeof(cmdline)-1 );
881 if (n < 0) n = 0;
882 cmdline[n] = 0;
883 close(fd);
884 } else {
885 cmdline[0] = 0;
886 }
887
888 logcat = strstr( cmdline, KERNEL_OPTION );
889 console = strstr( cmdline, CONSOLE_OPTION );
890 if (logcat != NULL) {
891 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
892 char* q = strpbrk( p, " \t\n\r" );;
893
894 if (q != NULL)
895 *q = 0;
896
897 forceFilters = p;
898 force_exit = 0;
899 }
900 /* if nothing found or invalid filters, exit quietly */
Traian Schiau59763032015-04-10 15:51:39 +0300901 if (force_exit) {
902 return EXIT_SUCCESS;
903 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800904
905 /* redirect our output to the emulator console */
906 if (console) {
907 char* p = console + sizeof(CONSOLE_OPTION)-1;
908 char* q = strpbrk( p, " \t\n\r" );
909 char devname[64];
910 int len;
911
912 if (q != NULL) {
913 len = q - p;
914 } else
915 len = strlen(p);
916
917 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
918 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
919 if (len < (int)sizeof(devname)) {
920 fd = open( devname, O_WRONLY );
921 if (fd >= 0) {
922 dup2(fd, 1);
923 dup2(fd, 2);
924 close(fd);
925 }
926 }
927 }
928 }
929 break;
930
Mark Salyzyn34facab2014-02-06 14:48:50 -0800931 case 'S':
932 printStatistics = 1;
933 break;
934
Traian Schiau59763032015-04-10 15:51:39 +0300935 case ':':
936 logcat_panic(true, "Option -%c needs an argument\n", optopt);
937 break;
938
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800939 default:
Traian Schiau59763032015-04-10 15:51:39 +0300940 logcat_panic(true, "Unrecognized Option %c\n", optopt);
941 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800942 }
943 }
944
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700945 if (g_maxCount && got_t) {
Mark Salyzynaa730c12016-03-30 12:38:29 -0700946 logcat_panic(true, "Cannot use -m (--max-count) and -t together\n");
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700947 }
Mark Salyzync9202772016-03-30 09:38:31 -0700948 if (g_printItAnyways && (!g_regex || !g_maxCount)) {
949 // One day it would be nice if --print -v color and --regex <expr>
950 // could play with each other and show regex highlighted content.
951 fprintf(stderr, "WARNING: "
952 "--print ignored, to be used in combination with\n"
953 " "
954 "--regex <expr> and --max-count <N>\n");
955 g_printItAnyways = false;
956 }
Casey Dahlin6ac498d2016-03-17 14:04:52 -0700957
Joe Onorato6fa09a02010-02-26 10:04:23 -0800958 if (!devices) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800959 dev = devices = new log_device_t("main", false);
Traian Schiau59763032015-04-10 15:51:39 +0300960 g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800961 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800962 dev = dev->next = new log_device_t("system", false);
Traian Schiau59763032015-04-10 15:51:39 +0300963 g_devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800964 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700965 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800966 dev = dev->next = new log_device_t("crash", false);
Traian Schiau59763032015-04-10 15:51:39 +0300967 g_devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700968 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800969 }
970
Traian Schiau59763032015-04-10 15:51:39 +0300971 if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) {
972 logcat_panic(true, "-r requires -f as well\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800973 }
974
Mark Salyzyn02687e72016-08-03 14:20:41 -0700975 if (setId != NULL) {
976 if (g_outputFileName == NULL) {
977 logcat_panic(true, "--id='%s' requires -f as well\n", setId);
978 }
979
980 std::string file_name = android::base::StringPrintf("%s.id", g_outputFileName);
981 std::string file;
982 bool file_ok = android::base::ReadFileToString(file_name, &file);
983 android::base::WriteStringToFile(setId, file_name,
984 S_IRUSR | S_IWUSR, getuid(), getgid());
985 if (!file_ok || (file.compare(setId) == 0)) {
986 setId = NULL;
987 }
988 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800989
990 if (hasSetLogFormat == 0) {
991 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
992
993 if (logFormat != NULL) {
994 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800995 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800996 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800997 logFormat);
998 }
Mark Salyzyn649fc602014-09-16 09:15:15 -0700999 } else {
1000 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001001 }
1002 }
1003
1004 if (forceFilters) {
1005 err = android_log_addFilterString(g_logformat, forceFilters);
1006 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +03001007 logcat_panic(false, "Invalid filter expression in logcat args\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001008 }
1009 } else if (argc == optind) {
1010 // Add from environment variable
1011 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
1012
1013 if (env_tags_orig != NULL) {
1014 err = android_log_addFilterString(g_logformat, env_tags_orig);
1015
Mark Salyzyn95132e92013-11-22 10:55:48 -08001016 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +03001017 logcat_panic(true,
1018 "Invalid filter expression in ANDROID_LOG_TAGS\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001019 }
1020 }
1021 } else {
1022 // Add from commandline
1023 for (int i = optind ; i < argc ; i++) {
1024 err = android_log_addFilterString(g_logformat, argv[i]);
1025
Mark Salyzyn95132e92013-11-22 10:55:48 -08001026 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +03001027 logcat_panic(true, "Invalid filter expression '%s'\n", argv[i]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001028 }
1029 }
1030 }
1031
Joe Onorato6fa09a02010-02-26 10:04:23 -08001032 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001033 if (tail_time != log_time::EPOCH) {
Kristian Monsen562e5132015-06-05 14:10:12 -07001034 logger_list = android_logger_list_alloc_time(mode, tail_time, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001035 } else {
Kristian Monsen562e5132015-06-05 14:10:12 -07001036 logger_list = android_logger_list_alloc(mode, tail_lines, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -08001037 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001038 const char *openDeviceFail = NULL;
1039 const char *clearFail = NULL;
1040 const char *setSizeFail = NULL;
1041 const char *getSizeFail = NULL;
1042 // We have three orthogonal actions below to clear, set log size and
1043 // get log size. All sharing the same iteration loop.
Joe Onorato6fa09a02010-02-26 10:04:23 -08001044 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001045 dev->logger_list = logger_list;
1046 dev->logger = android_logger_open(logger_list,
1047 android_name_to_log_id(dev->device));
1048 if (!dev->logger) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001049 openDeviceFail = openDeviceFail ?: dev->device;
1050 dev = dev->next;
1051 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001052 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001053
Mark Salyzyn02687e72016-08-03 14:20:41 -07001054 if (clearLog || setId) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001055 if (g_outputFileName) {
1056 int maxRotationCountDigits =
1057 (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
1058
1059 for (int i = g_maxRotatedLogs ; i >= 0 ; --i) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001060 std::string file;
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001061
1062 if (i == 0) {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001063 file = android::base::StringPrintf("%s", g_outputFileName);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001064 } else {
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001065 file = android::base::StringPrintf("%s.%.*d",
1066 g_outputFileName, maxRotationCountDigits, i);
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001067 }
1068
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001069 if (file.length() == 0) {
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001070 perror("while clearing log files");
1071 clearFail = clearFail ?: dev->device;
1072 break;
1073 }
1074
Mark Salyzyn5b1a5382016-08-03 14:20:41 -07001075 err = unlink(file.c_str());
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001076
1077 if (err < 0 && errno != ENOENT && clearFail == NULL) {
1078 perror("while clearing log files");
1079 clearFail = dev->device;
1080 }
Mark Salyzynb7d059b2016-06-06 14:56:00 -07001081 }
1082 } else if (android_logger_clear(dev->logger)) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001083 clearFail = clearFail ?: dev->device;
Joe Onorato6fa09a02010-02-26 10:04:23 -08001084 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001085 }
1086
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001087 if (setLogSize) {
1088 if (android_logger_set_log_size(dev->logger, setLogSize)) {
1089 setSizeFail = setSizeFail ?: dev->device;
1090 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001091 }
1092
Joe Onorato6fa09a02010-02-26 10:04:23 -08001093 if (getLogSize) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001094 long size = android_logger_get_log_size(dev->logger);
1095 long readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001096
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001097 if ((size < 0) || (readable < 0)) {
1098 getSizeFail = getSizeFail ?: dev->device;
1099 } else {
1100 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
1101 "max entry is %db, max payload is %db\n", dev->device,
1102 value_of_size(size), multiplier_of_size(size),
1103 value_of_size(readable), multiplier_of_size(readable),
1104 (int) LOGGER_ENTRY_MAX_LEN,
1105 (int) LOGGER_ENTRY_MAX_PAYLOAD);
Joe Onorato6fa09a02010-02-26 10:04:23 -08001106 }
Joe Onorato6fa09a02010-02-26 10:04:23 -08001107 }
1108
1109 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001110 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -07001111 // report any errors in the above loop and exit
1112 if (openDeviceFail) {
1113 logcat_panic(false, "Unable to open log device '%s'\n", openDeviceFail);
1114 }
1115 if (clearFail) {
1116 logcat_panic(false, "failed to clear the '%s' log\n", clearFail);
1117 }
1118 if (setSizeFail) {
1119 logcat_panic(false, "failed to set the '%s' log size\n", setSizeFail);
1120 }
1121 if (getSizeFail) {
1122 logcat_panic(false, "failed to get the readable '%s' log size",
1123 getSizeFail);
1124 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001125
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001126 if (setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001127 size_t len = strlen(setPruneList);
1128 /*extra 32 bytes are needed by android_logger_set_prune_list */
1129 size_t bLen = len + 32;
1130 char *buf = NULL;
1131 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
1132 buf[len] = '\0';
1133 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
1134 logcat_panic(false, "failed to set the prune list");
1135 }
1136 free(buf);
1137 } else {
1138 logcat_panic(false, "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001139 }
1140 }
1141
Mark Salyzyn1c950472014-04-01 17:19:47 -07001142 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001143 size_t len = 8192;
1144 char *buf;
1145
Mark Salyzyn083b0372015-12-04 10:59:45 -08001146 for (int retry = 32;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001147 (retry >= 0) && ((buf = new char [len]));
Traian Schiau59763032015-04-10 15:51:39 +03001148 delete [] buf, buf = NULL, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001149 if (getPruneList) {
1150 android_logger_get_prune_list(logger_list, buf, len);
1151 } else {
1152 android_logger_get_statistics(logger_list, buf, len);
1153 }
Mark Salyzyn34facab2014-02-06 14:48:50 -08001154 buf[len-1] = '\0';
Traian Schiau59763032015-04-10 15:51:39 +03001155 if (atol(buf) < 3) {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001156 delete [] buf;
1157 buf = NULL;
1158 break;
1159 }
Traian Schiau59763032015-04-10 15:51:39 +03001160 size_t ret = atol(buf) + 1;
1161 if (ret <= len) {
1162 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001163 break;
1164 }
Traian Schiau59763032015-04-10 15:51:39 +03001165 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001166 }
1167
1168 if (!buf) {
Traian Schiau59763032015-04-10 15:51:39 +03001169 logcat_panic(false, "failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -08001170 }
1171
1172 // remove trailing FF
1173 char *cp = buf + len - 1;
1174 *cp = '\0';
1175 bool truncated = *--cp != '\f';
1176 if (!truncated) {
1177 *cp = '\0';
1178 }
1179
1180 // squash out the byte count
1181 cp = buf;
1182 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -07001183 while (isdigit(*cp)) {
1184 ++cp;
1185 }
1186 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001187 ++cp;
1188 }
1189 }
1190
1191 printf("%s", cp);
1192 delete [] buf;
Traian Schiau59763032015-04-10 15:51:39 +03001193 return EXIT_SUCCESS;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001194 }
1195
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001196 if (getLogSize) {
Traian Schiau59763032015-04-10 15:51:39 +03001197 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001198 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001199 if (setLogSize || setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001200 return EXIT_SUCCESS;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001201 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001202 if (clearLog) {
Traian Schiau59763032015-04-10 15:51:39 +03001203 return EXIT_SUCCESS;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001204 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001205
Mark Salyzynad5e4112016-08-04 07:53:52 -07001206 setupOutputAndSchedulingPolicy((mode & ANDROID_LOG_NONBLOCK) == 0);
Mark Salyzyn02687e72016-08-03 14:20:41 -07001207
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001208 //LOG_EVENT_INT(10, 12345);
1209 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
1210 //LOG_EVENT_STRING(0, "whassup, doc?");
1211
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001212 dev = NULL;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001213 log_device_t unexpected("unexpected", false);
Casey Dahlin6ac498d2016-03-17 14:04:52 -07001214
Mark Salyzynaa730c12016-03-30 12:38:29 -07001215 while (!g_maxCount || (g_printCount < g_maxCount)) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001216 struct log_msg log_msg;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001217 log_device_t* d;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001218 int ret = android_logger_list_read(logger_list, &log_msg);
1219
1220 if (ret == 0) {
Traian Schiau59763032015-04-10 15:51:39 +03001221 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001222 }
1223
1224 if (ret < 0) {
1225 if (ret == -EAGAIN) {
1226 break;
1227 }
1228
1229 if (ret == -EIO) {
Traian Schiau59763032015-04-10 15:51:39 +03001230 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001231 }
1232 if (ret == -EINVAL) {
Traian Schiau59763032015-04-10 15:51:39 +03001233 logcat_panic(false, "read: unexpected length.\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001234 }
Traian Schiau59763032015-04-10 15:51:39 +03001235 logcat_panic(false, "logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001236 }
1237
Mark Salyzyn083b0372015-12-04 10:59:45 -08001238 for (d = devices; d; d = d->next) {
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001239 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001240 break;
1241 }
1242 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001243 if (!d) {
Traian Schiau59763032015-04-10 15:51:39 +03001244 g_devCount = 2; // set to Multiple
Mark Salyzyn9421b0c2015-02-26 14:33:35 -08001245 d = &unexpected;
1246 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001247 }
1248
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001249 if (dev != d) {
1250 dev = d;
Traian Schiau59763032015-04-10 15:51:39 +03001251 maybePrintStart(dev, printDividers);
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001252 }
Traian Schiau59763032015-04-10 15:51:39 +03001253 if (g_printBinary) {
1254 printBinary(&log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001255 } else {
Traian Schiau59763032015-04-10 15:51:39 +03001256 processBuffer(dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001257 }
1258 }
1259
1260 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001261
Traian Schiau59763032015-04-10 15:51:39 +03001262 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001263}