blob: 1ed0dfbc2796dbb3b628824ed78433fd4f8f8d8c [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>
29#include <android-base/strings.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070030#include <cutils/sched_policy.h>
Mark Salyzyn65772ca2013-12-13 11:10:11 -080031#include <cutils/sockets.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070032#include <log/event_tag_map.h>
Mark Salyzyn95132e92013-11-22 10:55:48 -080033#include <log/log.h>
Mark Salyzynfa3716b2014-02-14 16:05:05 -080034#include <log/log_read.h>
Colin Cross9227bd32013-07-23 16:59:20 -070035#include <log/logd.h>
Mark Salyzyn3ef730c2015-06-02 07:57:16 -070036#include <log/logger.h>
Colin Cross9227bd32013-07-23 16:59:20 -070037#include <log/logprint.h>
Riley Andrewsaede9892015-06-08 23:36:34 -070038#include <utils/threads.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039
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
70static const char * g_outputFileName = NULL;
Traian Schiau59763032015-04-10 15:51:39 +030071// 0 means "no log rotation"
72static size_t g_logRotateSizeKBytes = 0;
73// 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;
Traian Schiau59763032015-04-10 15:51:39 +030076static size_t g_outByteCount = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077static int g_printBinary = 0;
Mark Salyzyn9421b0c2015-02-26 14:33:35 -080078static int g_devCount = 0; // >1 means multiple
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079
Traian Schiau59763032015-04-10 15:51:39 +030080__noreturn static void logcat_panic(bool showHelp, const char *fmt, ...) __printflike(2,3);
81
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082static int openLogFile (const char *pathname)
83{
Edwin Vane80b221c2012-08-13 12:55:07 -040084 return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085}
86
87static void rotateLogs()
88{
89 int err;
90
91 // Can't rotate logs if we're not outputting to a file
92 if (g_outputFileName == NULL) {
93 return;
94 }
95
96 close(g_outFD);
97
Aristidis Papaioannoueba73442014-10-16 22:19:55 -070098 // Compute the maximum number of digits needed to count up to g_maxRotatedLogs in decimal.
99 // eg: g_maxRotatedLogs == 30 -> log10(30) == 1.477 -> maxRotationCountDigits == 2
100 int maxRotationCountDigits =
101 (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0;
102
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103 for (int i = g_maxRotatedLogs ; i > 0 ; i--) {
104 char *file0, *file1;
105
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700106 asprintf(&file1, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107
108 if (i - 1 == 0) {
109 asprintf(&file0, "%s", g_outputFileName);
110 } else {
Aristidis Papaioannoueba73442014-10-16 22:19:55 -0700111 asprintf(&file0, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112 }
113
Traian Schiau59763032015-04-10 15:51:39 +0300114 if (!file0 || !file1) {
115 perror("while rotating log files");
116 break;
117 }
118
119 err = rename(file0, file1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120
121 if (err < 0 && errno != ENOENT) {
122 perror("while rotating log files");
123 }
124
125 free(file1);
126 free(file0);
127 }
128
Traian Schiau59763032015-04-10 15:51:39 +0300129 g_outFD = openLogFile(g_outputFileName);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130
131 if (g_outFD < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300132 logcat_panic(false, "couldn't open output file");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133 }
134
135 g_outByteCount = 0;
136
137}
138
Mark Salyzyn95132e92013-11-22 10:55:48 -0800139void printBinary(struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140{
Mark Salyzyn95132e92013-11-22 10:55:48 -0800141 size_t size = buf->len();
142
143 TEMP_FAILURE_RETRY(write(g_outFD, buf, size));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144}
145
Mark Salyzyn95132e92013-11-22 10:55:48 -0800146static void processBuffer(log_device_t* dev, struct log_msg *buf)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147{
Mathias Agopian50844522010-03-17 16:10:26 -0700148 int bytesWritten = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149 int err;
150 AndroidLogEntry entry;
151 char binaryMsgBuf[1024];
152
Joe Onorato6fa09a02010-02-26 10:04:23 -0800153 if (dev->binary) {
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800154 static bool hasOpenedEventTagMap = false;
155 static EventTagMap *eventTagMap = NULL;
156
157 if (!eventTagMap && !hasOpenedEventTagMap) {
158 eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE);
159 hasOpenedEventTagMap = true;
160 }
Mark Salyzyn95132e92013-11-22 10:55:48 -0800161 err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry,
Mark Salyzyn9421b0c2015-02-26 14:33:35 -0800162 eventTagMap,
Mark Salyzyn95132e92013-11-22 10:55:48 -0800163 binaryMsgBuf,
164 sizeof(binaryMsgBuf));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165 //printf(">>> pri=%d len=%d msg='%s'\n",
166 // entry.priority, entry.messageLen, entry.message);
167 } else {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800168 err = android_log_processLogBuffer(&buf->entry_v1, &entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800170 if (err < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171 goto error;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800172 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800174 if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800175 bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry);
176
177 if (bytesWritten < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300178 logcat_panic(false, "output error");
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800179 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180 }
181
182 g_outByteCount += bytesWritten;
183
Mark Salyzyn95132e92013-11-22 10:55:48 -0800184 if (g_logRotateSizeKBytes > 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185 && (g_outByteCount / 1024) >= g_logRotateSizeKBytes
186 ) {
187 rotateLogs();
188 }
189
190error:
191 //fprintf (stderr, "Error processing record\n");
192 return;
193}
194
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800195static void maybePrintStart(log_device_t* dev, bool printDividers) {
196 if (!dev->printed || printDividers) {
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800197 if (g_devCount > 1 && !g_printBinary) {
198 char buf[1024];
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800199 snprintf(buf, sizeof(buf), "--------- %s %s\n",
200 dev->printed ? "switch to" : "beginning of",
Mark Salyzyn95132e92013-11-22 10:55:48 -0800201 dev->device);
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800202 if (write(g_outFD, buf, strlen(buf)) < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300203 logcat_panic(false, "output error");
Joe Onorato6fa09a02010-02-26 10:04:23 -0800204 }
205 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800206 dev->printed = true;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800207 }
208}
209
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210static void setupOutput()
211{
212
213 if (g_outputFileName == NULL) {
214 g_outFD = STDOUT_FILENO;
215
216 } else {
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700217 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
218 fprintf(stderr, "failed to set background scheduling policy\n");
219 }
220
221 struct sched_param param;
222 memset(&param, 0, sizeof(param));
223 if (sched_setscheduler((pid_t) 0, SCHED_BATCH, &param) < 0) {
224 fprintf(stderr, "failed to set to batch scheduler\n");
225 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226
Riley Andrewsaede9892015-06-08 23:36:34 -0700227 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
228 fprintf(stderr, "failed set to priority\n");
229 }
230
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 g_outFD = openLogFile (g_outputFileName);
232
233 if (g_outFD < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300234 logcat_panic(false, "couldn't open output file");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235 }
236
Mark Salyzyn3ef730c2015-06-02 07:57:16 -0700237 struct stat statbuf;
Traian Schiau59763032015-04-10 15:51:39 +0300238 if (fstat(g_outFD, &statbuf) == -1) {
239 close(g_outFD);
240 logcat_panic(false, "couldn't get output file stat\n");
241 }
242
243 if ((size_t) statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
244 close(g_outFD);
245 logcat_panic(false, "invalid output file stat\n");
246 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247
248 g_outByteCount = statbuf.st_size;
249 }
250}
251
252static void show_help(const char *cmd)
253{
254 fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd);
255
256 fprintf(stderr, "options include:\n"
257 " -s Set default filter to silent.\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700258 " Like specifying filterspec '*:S'\n"
Mark Salyzynf3555d92015-05-27 07:39:56 -0700259 " -f <filename> Log to file. Default is stdout\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800260 " --file=<filename>\n"
Traian Schiau59763032015-04-10 15:51:39 +0300261 " -r <kbytes> Rotate log every kbytes. Requires -f\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800262 " --rotate_kbytes=<kbytes>\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800263 " -n <count> Sets max number of rotated logs to <count>, default 4\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800264 " --rotate_count=<count>\n"
265 " -v <format> Sets the log print format, where <format> is:\n"
266 " --format=<format>\n"
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700267 " brief color epoch long monotonic printable process raw\n"
268 " tag thread threadtime time usec UTC year zone\n\n"
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800269 " -D print dividers between each log buffer\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800270 " --dividers\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271 " -c clear (flush) the entire log and exit\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800272 " --clear\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273 " -d dump the log and then exit (don't block)\n"
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800274 " -t <count> print only the most recent <count> lines (implies -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700275 " -t '<time>' print most recent lines since specified time (implies -d)\n"
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800276 " -T <count> print only the most recent <count> lines (does not imply -d)\n"
Mark Salyzyn190b7ac2014-09-01 10:41:33 -0700277 " -T '<time>' print most recent lines since specified time (not imply -d)\n"
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700278 " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700279 " 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280 " -g get the size of the log's ring buffer and exit\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800281 " --buffer_size\n"
282 " -G <size> set size of log ring buffer, may suffix with K or M.\n"
283 " --buffer_size=<size>\n"
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800284 " -L dump logs from prior to last reboot\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800285 " --last\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800286 " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800287 " --buffer=<buffer> 'events', 'crash' or 'all'. Multiple -b parameters are\n"
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700288 " allowed and results are interleaved. The default is\n"
289 " -b main -b system -b crash.\n"
Mark Salyzyn34facab2014-02-06 14:48:50 -0800290 " -B output the log in binary.\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800291 " --binary\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700292 " -S output statistics.\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800293 " --statistics\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700294 " -p print prune white and ~black list. Service is specified as\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800295 " --prune UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
Mark Salyzynbbbe14f2014-04-11 13:49:43 -0700296 " with ~, otherwise weighed for longevity if unadorned. All\n"
297 " other pruning activity is oldest first. Special case ~!\n"
298 " represents an automatic quicker pruning for the noisiest\n"
299 " UID as determined by the current statistics.\n"
300 " -P '<list> ...' set prune white and ~black list, using same format as\n"
Mark Salyzynf8bff872015-11-30 12:57:56 -0800301 " --prune='<list> ...' printed above. Must be quoted.\n"
Kristian Monsen562e5132015-06-05 14:10:12 -0700302 " --pid=<pid> Only prints logs from the given pid.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303
304 fprintf(stderr,"\nfilterspecs are a series of \n"
305 " <tag>[:priority]\n\n"
306 "where <tag> is a log component tag (or * for all) and priority is:\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700307 " V Verbose (default for <tag>)\n"
308 " D Debug (default for '*')\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309 " I Info\n"
310 " W Warn\n"
311 " E Error\n"
312 " F Fatal\n"
Mark Salyzynbba894a2015-03-09 09:32:56 -0700313 " S Silent (suppress all output)\n"
314 "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
315 "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
316 "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
317 "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
318 "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
Mark Salyzyn649fc602014-09-16 09:15:15 -0700319 "or defaults to \"threadtime\"\n\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320}
321
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322static int setLogFormat(const char * formatString)
323{
324 static AndroidLogPrintFormat format;
325
326 format = android_log_formatFromString(formatString);
327
328 if (format == FORMAT_OFF) {
329 // FORMAT_OFF means invalid string
330 return -1;
331 }
332
Mark Salyzyne1f20042015-05-06 08:40:40 -0700333 return android_log_setPrintFormat(g_logformat, format);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334}
335
Mark Salyzyn671e3432014-05-06 07:34:59 -0700336static const char multipliers[][2] = {
337 { "" },
338 { "K" },
339 { "M" },
340 { "G" }
341};
342
343static unsigned long value_of_size(unsigned long value)
344{
345 for (unsigned i = 0;
346 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
347 value /= 1024, ++i) ;
348 return value;
349}
350
351static const char *multiplier_of_size(unsigned long value)
352{
353 unsigned i;
354 for (i = 0;
355 (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024);
356 value /= 1024, ++i) ;
357 return multipliers[i];
358}
359
Traian Schiau59763032015-04-10 15:51:39 +0300360/*String to unsigned int, returns -1 if it fails*/
361static bool getSizeTArg(char *ptr, size_t *val, size_t min = 0,
362 size_t max = SIZE_MAX)
363{
Kristian Monsen562e5132015-06-05 14:10:12 -0700364 if (!ptr) {
Traian Schiau59763032015-04-10 15:51:39 +0300365 return false;
366 }
367
Kristian Monsen562e5132015-06-05 14:10:12 -0700368 char *endp;
369 errno = 0;
370 size_t ret = (size_t)strtoll(ptr, &endp, 0);
371
372 if (endp[0] || errno) {
373 return false;
374 }
375
376 if ((ret > max) || (ret < min)) {
Traian Schiau59763032015-04-10 15:51:39 +0300377 return false;
378 }
379
380 *val = ret;
381 return true;
382}
383
384static void logcat_panic(bool showHelp, const char *fmt, ...)
385{
Mark Salyzyn77d7e812015-04-13 09:27:57 -0700386 va_list args;
387 va_start(args, fmt);
388 vfprintf(stderr, fmt, args);
389 va_end(args);
Traian Schiau59763032015-04-10 15:51:39 +0300390
391 if (showHelp) {
392 show_help(getprogname());
393 }
394
395 exit(EXIT_FAILURE);
396}
397
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700398static char *parseTime(log_time &t, const char *cp) {
399
400 char *ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
401 if (ep) {
402 return ep;
403 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700404 ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
405 if (ep) {
406 return ep;
407 }
408 return t.strptime(cp, "%s.%q");
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700409}
Mark Salyzynf3555d92015-05-27 07:39:56 -0700410
411// Find last logged line in gestalt of all matching existing output files
412static log_time lastLogTime(char *outputFileName) {
413 log_time retval(log_time::EPOCH);
414 if (!outputFileName) {
415 return retval;
416 }
417
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800418 clockid_t clock_type = android_log_clockid();
419 log_time now(clock_type);
420 bool monotonic = clock_type == CLOCK_MONOTONIC;
Mark Salyzynf3555d92015-05-27 07:39:56 -0700421
422 std::string directory;
423 char *file = strrchr(outputFileName, '/');
424 if (!file) {
425 directory = ".";
426 file = outputFileName;
427 } else {
428 *file = '\0';
429 directory = outputFileName;
430 *file = '/';
431 ++file;
432 }
433 size_t len = strlen(file);
434 log_time modulo(0, NS_PER_SEC);
435 std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(directory.c_str()), closedir);
436 struct dirent *dp;
437 while ((dp = readdir(dir.get())) != NULL) {
438 if ((dp->d_type != DT_REG)
Mark Salyzynb6bee332015-09-08 08:56:32 -0700439 // If we are using realtime, check all files that match the
440 // basename for latest time. If we are using monotonic time
441 // then only check the main file because time cycles on
442 // every reboot.
443 || strncmp(dp->d_name, file, len + monotonic)
Mark Salyzynf3555d92015-05-27 07:39:56 -0700444 || (dp->d_name[len]
445 && ((dp->d_name[len] != '.')
446 || !isdigit(dp->d_name[len+1])))) {
447 continue;
448 }
449
450 std::string file_name = directory;
451 file_name += "/";
452 file_name += dp->d_name;
453 std::string file;
454 if (!android::base::ReadFileToString(file_name, &file)) {
455 continue;
456 }
457
458 bool found = false;
459 for (const auto& line : android::base::Split(file, "\n")) {
460 log_time t(log_time::EPOCH);
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700461 char *ep = parseTime(t, line.c_str());
Mark Salyzynf3555d92015-05-27 07:39:56 -0700462 if (!ep || (*ep != ' ')) {
463 continue;
464 }
465 // determine the time precision of the logs (eg: msec or usec)
466 for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
467 if (t.tv_nsec % (mod * 10)) {
468 modulo.tv_nsec = mod;
469 break;
470 }
471 }
472 // We filter any times later than current as we may not have the
473 // year stored with each log entry. Also, since it is possible for
474 // entries to be recorded out of order (very rare) we select the
475 // maximum we find just in case.
476 if ((t < now) && (t > retval)) {
477 retval = t;
478 found = true;
479 }
480 }
481 // We count on the basename file to be the definitive end, so stop here.
482 if (!dp->d_name[len] && found) {
483 break;
484 }
485 }
486 if (retval == log_time::EPOCH) {
487 return retval;
488 }
489 // tail_time prints matching or higher, round up by the modulo to prevent
490 // a replay of the last entry we have just checked.
491 retval += modulo;
492 return retval;
493}
494
Traian Schiau59763032015-04-10 15:51:39 +0300495} /* namespace android */
496
497
Joe Onorato6fa09a02010-02-26 10:04:23 -0800498int main(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499{
Traian Schiau59763032015-04-10 15:51:39 +0300500 using namespace android;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501 int err;
502 int hasSetLogFormat = 0;
503 int clearLog = 0;
504 int getLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800505 unsigned long setLogSize = 0;
506 int getPruneList = 0;
507 char *setPruneList = NULL;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800508 int printStatistics = 0;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800509 int mode = ANDROID_LOG_RDONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510 const char *forceFilters = NULL;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800511 log_device_t* devices = NULL;
512 log_device_t* dev;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800513 bool printDividers = false;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800514 struct logger_list *logger_list;
Traian Schiau59763032015-04-10 15:51:39 +0300515 size_t tail_lines = 0;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800516 log_time tail_time(log_time::EPOCH);
Kristian Monsen562e5132015-06-05 14:10:12 -0700517 size_t pid = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518
Mark Salyzyn65772ca2013-12-13 11:10:11 -0800519 signal(SIGPIPE, exit);
520
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521 g_logformat = android_log_format_new();
522
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523 if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
Traian Schiau59763032015-04-10 15:51:39 +0300524 show_help(argv[0]);
525 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800526 }
527
528 for (;;) {
529 int ret;
530
Kristian Monsen562e5132015-06-05 14:10:12 -0700531 int option_index = 0;
532 static const char pid_str[] = "pid";
533 static const struct option long_options[] = {
Mark Salyzynf8bff872015-11-30 12:57:56 -0800534 { "binary", no_argument, NULL, 'B' },
535 { "buffer", required_argument, NULL, 'b' },
536 { "buffer_size", optional_argument, NULL, 'g' },
537 { "clear", no_argument, NULL, 'c' },
538 { "dividers", no_argument, NULL, 'D' },
539 { "file", required_argument, NULL, 'f' },
540 { "format", required_argument, NULL, 'v' },
541 { "last", no_argument, NULL, 'L' },
Kristian Monsen562e5132015-06-05 14:10:12 -0700542 { pid_str, required_argument, NULL, 0 },
Mark Salyzynf8bff872015-11-30 12:57:56 -0800543 { "prune", optional_argument, NULL, 'p' },
544 { "rotate_count", required_argument, NULL, 'n' },
545 { "rotate_kbytes", required_argument, NULL, 'r' },
546 { "statistics", no_argument, NULL, 'S' },
Kristian Monsen562e5132015-06-05 14:10:12 -0700547 { NULL, 0, NULL, 0 }
548 };
549
550 ret = getopt_long(argc, argv, ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:",
551 long_options, &option_index);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800552
553 if (ret < 0) {
554 break;
555 }
556
Kristian Monsen562e5132015-06-05 14:10:12 -0700557 switch (ret) {
558 case 0:
559 // One of the long options
560 if (long_options[option_index].name == pid_str) {
561 // ToDo: determine runtime PID_MAX?
562 if (!getSizeTArg(optarg, &pid, 1)) {
563 logcat_panic(true, "%s %s out of range\n",
564 long_options[option_index].name, optarg);
565 }
566 break;
567 }
568 break;
569
Mark Salyzyn95132e92013-11-22 10:55:48 -0800570 case 's':
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800571 // default to all silent
572 android_log_addFilterRule(g_logformat, "*:s");
573 break;
574
575 case 'c':
576 clearLog = 1;
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800577 mode |= ANDROID_LOG_WRONLY;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800578 break;
579
Mark Salyzyn7c975ac2014-12-15 10:01:31 -0800580 case 'L':
581 mode |= ANDROID_LOG_PSTORE;
582 break;
583
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800584 case 'd':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800585 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800586 break;
587
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800588 case 't':
Mark Salyzyn2d3f38a2015-01-26 10:46:44 -0800589 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
Mark Salyzyn5d3d1f12013-12-09 13:47:00 -0800590 /* FALLTHRU */
591 case 'T':
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800592 if (strspn(optarg, "0123456789") != strlen(optarg)) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700593 char *cp = parseTime(tail_time, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800594 if (!cp) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700595 logcat_panic(false, "-%c \"%s\" not in time format\n",
596 ret, optarg);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800597 }
598 if (*cp) {
599 char c = *cp;
600 *cp = '\0';
601 fprintf(stderr,
602 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
603 ret, optarg, c, cp + 1);
604 *cp = c;
605 }
606 } else {
Traian Schiau59763032015-04-10 15:51:39 +0300607 if (!getSizeTArg(optarg, &tail_lines, 1)) {
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800608 fprintf(stderr,
609 "WARNING: -%c %s invalid, setting to 1\n",
610 ret, optarg);
611 tail_lines = 1;
612 }
613 }
Dan Egnord1d3b6d2010-03-11 20:32:17 -0800614 break;
615
Mark Salyzyn7b30ff82015-01-26 13:41:33 -0800616 case 'D':
617 printDividers = true;
618 break;
619
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800620 case 'g':
Mark Salyzynf8bff872015-11-30 12:57:56 -0800621 if (!optarg) {
622 getLogSize = 1;
623 break;
624 }
625 // FALLTHRU
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800626
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800627 case 'G': {
Traian Schiau59763032015-04-10 15:51:39 +0300628 char *cp;
629 if (strtoll(optarg, &cp, 0) > 0) {
630 setLogSize = strtoll(optarg, &cp, 0);
631 } else {
632 setLogSize = 0;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800633 }
634
635 switch(*cp) {
636 case 'g':
637 case 'G':
638 setLogSize *= 1024;
639 /* FALLTHRU */
640 case 'm':
641 case 'M':
642 setLogSize *= 1024;
643 /* FALLTHRU */
644 case 'k':
645 case 'K':
646 setLogSize *= 1024;
647 /* FALLTHRU */
648 case '\0':
649 break;
650
651 default:
652 setLogSize = 0;
653 }
654
655 if (!setLogSize) {
656 fprintf(stderr, "ERROR: -G <num><multiplier>\n");
Traian Schiau59763032015-04-10 15:51:39 +0300657 return EXIT_FAILURE;
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800658 }
659 }
660 break;
661
662 case 'p':
Mark Salyzynf8bff872015-11-30 12:57:56 -0800663 if (!optarg) {
664 getPruneList = 1;
665 break;
666 }
667 // FALLTHRU
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800668
669 case 'P':
670 setPruneList = optarg;
671 break;
672
Joe Onorato6fa09a02010-02-26 10:04:23 -0800673 case 'b': {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800674 if (strcmp(optarg, "all") == 0) {
675 while (devices) {
676 dev = devices;
677 devices = dev->next;
678 delete dev;
679 }
680
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700681 devices = dev = NULL;
Traian Schiau59763032015-04-10 15:51:39 +0300682 g_devCount = 0;
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700683 for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
684 const char *name = android_log_id_to_name((log_id_t)i);
685 log_id_t log_id = android_name_to_log_id(name);
686
687 if (log_id != (log_id_t)i) {
688 continue;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800689 }
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700690
691 bool binary = strcmp(name, "events") == 0;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800692 log_device_t* d = new log_device_t(name, binary);
Mark Salyzynd0bd1b12014-10-10 15:25:38 -0700693
694 if (dev) {
695 dev->next = d;
696 dev = d;
697 } else {
698 devices = dev = d;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800699 }
Traian Schiau59763032015-04-10 15:51:39 +0300700 g_devCount++;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800701 }
702 break;
703 }
704
Joe Onorato6fa09a02010-02-26 10:04:23 -0800705 bool binary = strcmp(optarg, "events") == 0;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800706
707 if (devices) {
708 dev = devices;
709 while (dev->next) {
710 dev = dev->next;
711 }
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800712 dev->next = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800713 } else {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800714 devices = new log_device_t(optarg, binary);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800715 }
Traian Schiau59763032015-04-10 15:51:39 +0300716 g_devCount++;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800717 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800718 break;
719
720 case 'B':
Traian Schiau59763032015-04-10 15:51:39 +0300721 g_printBinary = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800722 break;
723
724 case 'f':
Mark Salyzyn9812fc42015-10-06 08:59:02 -0700725 if ((tail_time == log_time::EPOCH) && (tail_lines == 0)) {
Mark Salyzynf3555d92015-05-27 07:39:56 -0700726 tail_time = lastLogTime(optarg);
727 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800728 // redirect output to a file
Traian Schiau59763032015-04-10 15:51:39 +0300729 g_outputFileName = optarg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800730 break;
731
732 case 'r':
Traian Schiau59763032015-04-10 15:51:39 +0300733 if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) {
734 logcat_panic(true, "Invalid parameter %s to -r\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800735 }
736 break;
737
738 case 'n':
Traian Schiau59763032015-04-10 15:51:39 +0300739 if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) {
740 logcat_panic(true, "Invalid parameter %s to -n\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800741 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800742 break;
743
744 case 'v':
745 err = setLogFormat (optarg);
746 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300747 logcat_panic(true, "Invalid parameter %s to -v\n", optarg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800748 }
Mark Salyzyne1f20042015-05-06 08:40:40 -0700749 hasSetLogFormat |= err;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800750 break;
751
752 case 'Q':
753 /* this is a *hidden* option used to start a version of logcat */
754 /* in an emulated device only. it basically looks for androidboot.logcat= */
755 /* on the kernel command line. If something is found, it extracts a log filter */
756 /* and uses it to run the program. If nothing is found, the program should */
757 /* quit immediately */
758#define KERNEL_OPTION "androidboot.logcat="
759#define CONSOLE_OPTION "androidboot.console="
760 {
761 int fd;
762 char* logcat;
763 char* console;
764 int force_exit = 1;
765 static char cmdline[1024];
766
767 fd = open("/proc/cmdline", O_RDONLY);
768 if (fd >= 0) {
769 int n = read(fd, cmdline, sizeof(cmdline)-1 );
770 if (n < 0) n = 0;
771 cmdline[n] = 0;
772 close(fd);
773 } else {
774 cmdline[0] = 0;
775 }
776
777 logcat = strstr( cmdline, KERNEL_OPTION );
778 console = strstr( cmdline, CONSOLE_OPTION );
779 if (logcat != NULL) {
780 char* p = logcat + sizeof(KERNEL_OPTION)-1;;
781 char* q = strpbrk( p, " \t\n\r" );;
782
783 if (q != NULL)
784 *q = 0;
785
786 forceFilters = p;
787 force_exit = 0;
788 }
789 /* if nothing found or invalid filters, exit quietly */
Traian Schiau59763032015-04-10 15:51:39 +0300790 if (force_exit) {
791 return EXIT_SUCCESS;
792 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800793
794 /* redirect our output to the emulator console */
795 if (console) {
796 char* p = console + sizeof(CONSOLE_OPTION)-1;
797 char* q = strpbrk( p, " \t\n\r" );
798 char devname[64];
799 int len;
800
801 if (q != NULL) {
802 len = q - p;
803 } else
804 len = strlen(p);
805
806 len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p );
807 fprintf(stderr, "logcat using %s (%d)\n", devname, len);
808 if (len < (int)sizeof(devname)) {
809 fd = open( devname, O_WRONLY );
810 if (fd >= 0) {
811 dup2(fd, 1);
812 dup2(fd, 2);
813 close(fd);
814 }
815 }
816 }
817 }
818 break;
819
Mark Salyzyn34facab2014-02-06 14:48:50 -0800820 case 'S':
821 printStatistics = 1;
822 break;
823
Traian Schiau59763032015-04-10 15:51:39 +0300824 case ':':
825 logcat_panic(true, "Option -%c needs an argument\n", optopt);
826 break;
827
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800828 default:
Traian Schiau59763032015-04-10 15:51:39 +0300829 logcat_panic(true, "Unrecognized Option %c\n", optopt);
830 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800831 }
832 }
833
Joe Onorato6fa09a02010-02-26 10:04:23 -0800834 if (!devices) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800835 dev = devices = new log_device_t("main", false);
Traian Schiau59763032015-04-10 15:51:39 +0300836 g_devCount = 1;
Mark Salyzyn95132e92013-11-22 10:55:48 -0800837 if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800838 dev = dev->next = new log_device_t("system", false);
Traian Schiau59763032015-04-10 15:51:39 +0300839 g_devCount++;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800840 }
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700841 if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
Mark Salyzyn5f6738a2015-02-27 13:41:34 -0800842 dev = dev->next = new log_device_t("crash", false);
Traian Schiau59763032015-04-10 15:51:39 +0300843 g_devCount++;
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700844 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800845 }
846
Traian Schiau59763032015-04-10 15:51:39 +0300847 if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) {
848 logcat_panic(true, "-r requires -f as well\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800849 }
850
Traian Schiau59763032015-04-10 15:51:39 +0300851 setupOutput();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800852
853 if (hasSetLogFormat == 0) {
854 const char* logFormat = getenv("ANDROID_PRINTF_LOG");
855
856 if (logFormat != NULL) {
857 err = setLogFormat(logFormat);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800858 if (err < 0) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800859 fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800860 logFormat);
861 }
Mark Salyzyn649fc602014-09-16 09:15:15 -0700862 } else {
863 setLogFormat("threadtime");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800864 }
865 }
866
867 if (forceFilters) {
868 err = android_log_addFilterString(g_logformat, forceFilters);
869 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300870 logcat_panic(false, "Invalid filter expression in logcat args\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800871 }
872 } else if (argc == optind) {
873 // Add from environment variable
874 char *env_tags_orig = getenv("ANDROID_LOG_TAGS");
875
876 if (env_tags_orig != NULL) {
877 err = android_log_addFilterString(g_logformat, env_tags_orig);
878
Mark Salyzyn95132e92013-11-22 10:55:48 -0800879 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300880 logcat_panic(true,
881 "Invalid filter expression in ANDROID_LOG_TAGS\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800882 }
883 }
884 } else {
885 // Add from commandline
886 for (int i = optind ; i < argc ; i++) {
887 err = android_log_addFilterString(g_logformat, argv[i]);
888
Mark Salyzyn95132e92013-11-22 10:55:48 -0800889 if (err < 0) {
Traian Schiau59763032015-04-10 15:51:39 +0300890 logcat_panic(true, "Invalid filter expression '%s'\n", argv[i]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800891 }
892 }
893 }
894
Joe Onorato6fa09a02010-02-26 10:04:23 -0800895 dev = devices;
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800896 if (tail_time != log_time::EPOCH) {
Kristian Monsen562e5132015-06-05 14:10:12 -0700897 logger_list = android_logger_list_alloc_time(mode, tail_time, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800898 } else {
Kristian Monsen562e5132015-06-05 14:10:12 -0700899 logger_list = android_logger_list_alloc(mode, tail_lines, pid);
Mark Salyzynfa3716b2014-02-14 16:05:05 -0800900 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700901 const char *openDeviceFail = NULL;
902 const char *clearFail = NULL;
903 const char *setSizeFail = NULL;
904 const char *getSizeFail = NULL;
905 // We have three orthogonal actions below to clear, set log size and
906 // get log size. All sharing the same iteration loop.
Joe Onorato6fa09a02010-02-26 10:04:23 -0800907 while (dev) {
Mark Salyzyn95132e92013-11-22 10:55:48 -0800908 dev->logger_list = logger_list;
909 dev->logger = android_logger_open(logger_list,
910 android_name_to_log_id(dev->device));
911 if (!dev->logger) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700912 openDeviceFail = openDeviceFail ?: dev->device;
913 dev = dev->next;
914 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800915 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800916
917 if (clearLog) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700918 if (android_logger_clear(dev->logger)) {
919 clearFail = clearFail ?: dev->device;
Joe Onorato6fa09a02010-02-26 10:04:23 -0800920 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800921 }
922
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700923 if (setLogSize) {
924 if (android_logger_set_log_size(dev->logger, setLogSize)) {
925 setSizeFail = setSizeFail ?: dev->device;
926 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800927 }
928
Joe Onorato6fa09a02010-02-26 10:04:23 -0800929 if (getLogSize) {
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700930 long size = android_logger_get_log_size(dev->logger);
931 long readable = android_logger_get_log_readable_size(dev->logger);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800932
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700933 if ((size < 0) || (readable < 0)) {
934 getSizeFail = getSizeFail ?: dev->device;
935 } else {
936 printf("%s: ring buffer is %ld%sb (%ld%sb consumed), "
937 "max entry is %db, max payload is %db\n", dev->device,
938 value_of_size(size), multiplier_of_size(size),
939 value_of_size(readable), multiplier_of_size(readable),
940 (int) LOGGER_ENTRY_MAX_LEN,
941 (int) LOGGER_ENTRY_MAX_PAYLOAD);
Joe Onorato6fa09a02010-02-26 10:04:23 -0800942 }
Joe Onorato6fa09a02010-02-26 10:04:23 -0800943 }
944
945 dev = dev->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800946 }
Mark Salyzyn603b8e52015-09-16 15:34:00 -0700947 // report any errors in the above loop and exit
948 if (openDeviceFail) {
949 logcat_panic(false, "Unable to open log device '%s'\n", openDeviceFail);
950 }
951 if (clearFail) {
952 logcat_panic(false, "failed to clear the '%s' log\n", clearFail);
953 }
954 if (setSizeFail) {
955 logcat_panic(false, "failed to set the '%s' log size\n", setSizeFail);
956 }
957 if (getSizeFail) {
958 logcat_panic(false, "failed to get the readable '%s' log size",
959 getSizeFail);
960 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800961
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800962 if (setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +0300963 size_t len = strlen(setPruneList);
964 /*extra 32 bytes are needed by android_logger_set_prune_list */
965 size_t bLen = len + 32;
966 char *buf = NULL;
967 if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
968 buf[len] = '\0';
969 if (android_logger_set_prune_list(logger_list, buf, bLen)) {
970 logcat_panic(false, "failed to set the prune list");
971 }
972 free(buf);
973 } else {
974 logcat_panic(false, "failed to set the prune list (alloc)");
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800975 }
976 }
977
Mark Salyzyn1c950472014-04-01 17:19:47 -0700978 if (printStatistics || getPruneList) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800979 size_t len = 8192;
980 char *buf;
981
982 for(int retry = 32;
983 (retry >= 0) && ((buf = new char [len]));
Traian Schiau59763032015-04-10 15:51:39 +0300984 delete [] buf, buf = NULL, --retry) {
Mark Salyzyndfa7a072014-02-11 12:29:31 -0800985 if (getPruneList) {
986 android_logger_get_prune_list(logger_list, buf, len);
987 } else {
988 android_logger_get_statistics(logger_list, buf, len);
989 }
Mark Salyzyn34facab2014-02-06 14:48:50 -0800990 buf[len-1] = '\0';
Traian Schiau59763032015-04-10 15:51:39 +0300991 if (atol(buf) < 3) {
Mark Salyzyn34facab2014-02-06 14:48:50 -0800992 delete [] buf;
993 buf = NULL;
994 break;
995 }
Traian Schiau59763032015-04-10 15:51:39 +0300996 size_t ret = atol(buf) + 1;
997 if (ret <= len) {
998 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -0800999 break;
1000 }
Traian Schiau59763032015-04-10 15:51:39 +03001001 len = ret;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001002 }
1003
1004 if (!buf) {
Traian Schiau59763032015-04-10 15:51:39 +03001005 logcat_panic(false, "failed to read data");
Mark Salyzyn34facab2014-02-06 14:48:50 -08001006 }
1007
1008 // remove trailing FF
1009 char *cp = buf + len - 1;
1010 *cp = '\0';
1011 bool truncated = *--cp != '\f';
1012 if (!truncated) {
1013 *cp = '\0';
1014 }
1015
1016 // squash out the byte count
1017 cp = buf;
1018 if (!truncated) {
Mark Salyzyn22e287d2014-03-21 13:12:16 -07001019 while (isdigit(*cp)) {
1020 ++cp;
1021 }
1022 if (*cp == '\n') {
Mark Salyzyn34facab2014-02-06 14:48:50 -08001023 ++cp;
1024 }
1025 }
1026
1027 printf("%s", cp);
1028 delete [] buf;
Traian Schiau59763032015-04-10 15:51:39 +03001029 return EXIT_SUCCESS;
Mark Salyzyn34facab2014-02-06 14:48:50 -08001030 }
1031
1032
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001033 if (getLogSize) {
Traian Schiau59763032015-04-10 15:51:39 +03001034 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001035 }
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001036 if (setLogSize || setPruneList) {
Traian Schiau59763032015-04-10 15:51:39 +03001037 return EXIT_SUCCESS;
Mark Salyzyndfa7a072014-02-11 12:29:31 -08001038 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001039 if (clearLog) {
Traian Schiau59763032015-04-10 15:51:39 +03001040 return EXIT_SUCCESS;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001041 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001042
1043 //LOG_EVENT_INT(10, 12345);
1044 //LOG_EVENT_LONG(11, 0x1122334455667788LL);
1045 //LOG_EVENT_STRING(0, "whassup, doc?");
1046
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001047 dev = NULL;
Mark Salyzyn5f6738a2015-02-27 13:41:34 -08001048 log_device_t unexpected("unexpected", false);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001049 while (1) {
1050 struct log_msg log_msg;
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001051 log_device_t* d;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001052 int ret = android_logger_list_read(logger_list, &log_msg);
1053
1054 if (ret == 0) {
Traian Schiau59763032015-04-10 15:51:39 +03001055 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001056 }
1057
1058 if (ret < 0) {
1059 if (ret == -EAGAIN) {
1060 break;
1061 }
1062
1063 if (ret == -EIO) {
Traian Schiau59763032015-04-10 15:51:39 +03001064 logcat_panic(false, "read: unexpected EOF!\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001065 }
1066 if (ret == -EINVAL) {
Traian Schiau59763032015-04-10 15:51:39 +03001067 logcat_panic(false, "read: unexpected length.\n");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001068 }
Traian Schiau59763032015-04-10 15:51:39 +03001069 logcat_panic(false, "logcat read failure");
Mark Salyzyn95132e92013-11-22 10:55:48 -08001070 }
1071
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001072 for(d = devices; d; d = d->next) {
1073 if (android_name_to_log_id(d->device) == log_msg.id()) {
Mark Salyzyn95132e92013-11-22 10:55:48 -08001074 break;
1075 }
1076 }
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001077 if (!d) {
Traian Schiau59763032015-04-10 15:51:39 +03001078 g_devCount = 2; // set to Multiple
Mark Salyzyn9421b0c2015-02-26 14:33:35 -08001079 d = &unexpected;
1080 d->binary = log_msg.id() == LOG_ID_EVENTS;
Mark Salyzyn95132e92013-11-22 10:55:48 -08001081 }
1082
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001083 if (dev != d) {
1084 dev = d;
Traian Schiau59763032015-04-10 15:51:39 +03001085 maybePrintStart(dev, printDividers);
Mark Salyzyn7b30ff82015-01-26 13:41:33 -08001086 }
Traian Schiau59763032015-04-10 15:51:39 +03001087 if (g_printBinary) {
1088 printBinary(&log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001089 } else {
Traian Schiau59763032015-04-10 15:51:39 +03001090 processBuffer(dev, &log_msg);
Mark Salyzyn95132e92013-11-22 10:55:48 -08001091 }
1092 }
1093
1094 android_logger_list_free(logger_list);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001095
Traian Schiau59763032015-04-10 15:51:39 +03001096 return EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001097}