Mark Salyzyn | 1ff1809 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1 | // Copyright 2006-2015 The Android Open Source Project |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 2 | |
Mark Salyzyn | 12af965 | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 3 | #include <assert.h> |
| 4 | #include <ctype.h> |
| 5 | #include <errno.h> |
| 6 | #include <fcntl.h> |
Aristidis Papaioannou | f7cc662 | 2014-10-16 22:19:55 -0700 | [diff] [blame] | 7 | #include <math.h> |
Mark Salyzyn | 12af965 | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <stdarg.h> |
| 11 | #include <string.h> |
| 12 | #include <signal.h> |
| 13 | #include <time.h> |
| 14 | #include <unistd.h> |
| 15 | #include <sys/socket.h> |
| 16 | #include <sys/stat.h> |
| 17 | #include <arpa/inet.h> |
| 18 | |
| 19 | #include <cutils/sockets.h> |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 20 | #include <log/log.h> |
Mark Salyzyn | 9addf59 | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 21 | #include <log/log_read.h> |
Colin Cross | e355ded | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 22 | #include <log/logger.h> |
| 23 | #include <log/logd.h> |
| 24 | #include <log/logprint.h> |
| 25 | #include <log/event_tag_map.h> |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | |
| 27 | #define DEFAULT_LOG_ROTATE_SIZE_KBYTES 16 |
| 28 | #define DEFAULT_MAX_ROTATED_LOGS 4 |
| 29 | |
| 30 | static AndroidLogFormat * g_logformat; |
| 31 | |
| 32 | /* logd prefixes records with a length field */ |
| 33 | #define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t) |
| 34 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 35 | struct log_device_t { |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 36 | const char* device; |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 37 | bool binary; |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 38 | struct logger *logger; |
| 39 | struct logger_list *logger_list; |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 40 | bool printed; |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 41 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 42 | log_device_t* next; |
| 43 | |
Mark Salyzyn | a044390 | 2015-02-27 13:41:34 -0800 | [diff] [blame^] | 44 | log_device_t(const char* d, bool b) { |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 45 | device = d; |
| 46 | binary = b; |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 47 | next = NULL; |
| 48 | printed = false; |
| 49 | } |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 50 | }; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | |
| 52 | namespace android { |
| 53 | |
| 54 | /* Global Variables */ |
| 55 | |
| 56 | static const char * g_outputFileName = NULL; |
| 57 | static int g_logRotateSizeKBytes = 0; // 0 means "no log rotation" |
| 58 | static int g_maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS; // 0 means "unbounded" |
| 59 | static int g_outFD = -1; |
| 60 | static off_t g_outByteCount = 0; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 61 | static int g_printBinary = 0; |
Mark Salyzyn | 784d64f | 2015-02-26 14:33:35 -0800 | [diff] [blame] | 62 | static int g_devCount = 0; // >1 means multiple |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 63 | |
| 64 | static int openLogFile (const char *pathname) |
| 65 | { |
Edwin Vane | ff5c94d | 2012-08-13 12:55:07 -0400 | [diff] [blame] | 66 | return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static void rotateLogs() |
| 70 | { |
| 71 | int err; |
| 72 | |
| 73 | // Can't rotate logs if we're not outputting to a file |
| 74 | if (g_outputFileName == NULL) { |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | close(g_outFD); |
| 79 | |
Aristidis Papaioannou | f7cc662 | 2014-10-16 22:19:55 -0700 | [diff] [blame] | 80 | // Compute the maximum number of digits needed to count up to g_maxRotatedLogs in decimal. |
| 81 | // eg: g_maxRotatedLogs == 30 -> log10(30) == 1.477 -> maxRotationCountDigits == 2 |
| 82 | int maxRotationCountDigits = |
| 83 | (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0; |
| 84 | |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 85 | for (int i = g_maxRotatedLogs ; i > 0 ; i--) { |
| 86 | char *file0, *file1; |
| 87 | |
Aristidis Papaioannou | f7cc662 | 2014-10-16 22:19:55 -0700 | [diff] [blame] | 88 | asprintf(&file1, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 89 | |
| 90 | if (i - 1 == 0) { |
| 91 | asprintf(&file0, "%s", g_outputFileName); |
| 92 | } else { |
Aristidis Papaioannou | f7cc662 | 2014-10-16 22:19:55 -0700 | [diff] [blame] | 93 | asprintf(&file0, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | err = rename (file0, file1); |
| 97 | |
| 98 | if (err < 0 && errno != ENOENT) { |
| 99 | perror("while rotating log files"); |
| 100 | } |
| 101 | |
| 102 | free(file1); |
| 103 | free(file0); |
| 104 | } |
| 105 | |
| 106 | g_outFD = openLogFile (g_outputFileName); |
| 107 | |
| 108 | if (g_outFD < 0) { |
| 109 | perror ("couldn't open output file"); |
| 110 | exit(-1); |
| 111 | } |
| 112 | |
| 113 | g_outByteCount = 0; |
| 114 | |
| 115 | } |
| 116 | |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 117 | void printBinary(struct log_msg *buf) |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 118 | { |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 119 | size_t size = buf->len(); |
| 120 | |
| 121 | TEMP_FAILURE_RETRY(write(g_outFD, buf, size)); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 124 | static void processBuffer(log_device_t* dev, struct log_msg *buf) |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 125 | { |
Mathias Agopian | b9218fb | 2010-03-17 16:10:26 -0700 | [diff] [blame] | 126 | int bytesWritten = 0; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 127 | int err; |
| 128 | AndroidLogEntry entry; |
| 129 | char binaryMsgBuf[1024]; |
| 130 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 131 | if (dev->binary) { |
Mark Salyzyn | 784d64f | 2015-02-26 14:33:35 -0800 | [diff] [blame] | 132 | static bool hasOpenedEventTagMap = false; |
| 133 | static EventTagMap *eventTagMap = NULL; |
| 134 | |
| 135 | if (!eventTagMap && !hasOpenedEventTagMap) { |
| 136 | eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE); |
| 137 | hasOpenedEventTagMap = true; |
| 138 | } |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 139 | err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry, |
Mark Salyzyn | 784d64f | 2015-02-26 14:33:35 -0800 | [diff] [blame] | 140 | eventTagMap, |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 141 | binaryMsgBuf, |
| 142 | sizeof(binaryMsgBuf)); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 143 | //printf(">>> pri=%d len=%d msg='%s'\n", |
| 144 | // entry.priority, entry.messageLen, entry.message); |
| 145 | } else { |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 146 | err = android_log_processLogBuffer(&buf->entry_v1, &entry); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 147 | } |
Joe Onorato | 400da4a | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 148 | if (err < 0) { |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 149 | goto error; |
Joe Onorato | 400da4a | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 150 | } |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 151 | |
Joe Onorato | 400da4a | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 152 | if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) { |
Joe Onorato | 400da4a | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 153 | bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry); |
| 154 | |
| 155 | if (bytesWritten < 0) { |
| 156 | perror("output error"); |
| 157 | exit(-1); |
| 158 | } |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | g_outByteCount += bytesWritten; |
| 162 | |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 163 | if (g_logRotateSizeKBytes > 0 |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 164 | && (g_outByteCount / 1024) >= g_logRotateSizeKBytes |
| 165 | ) { |
| 166 | rotateLogs(); |
| 167 | } |
| 168 | |
| 169 | error: |
| 170 | //fprintf (stderr, "Error processing record\n"); |
| 171 | return; |
| 172 | } |
| 173 | |
Mark Salyzyn | 1ff1809 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 174 | static void maybePrintStart(log_device_t* dev, bool printDividers) { |
| 175 | if (!dev->printed || printDividers) { |
Dan Egnor | cce2b08 | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 176 | if (g_devCount > 1 && !g_printBinary) { |
| 177 | char buf[1024]; |
Mark Salyzyn | 1ff1809 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 178 | snprintf(buf, sizeof(buf), "--------- %s %s\n", |
| 179 | dev->printed ? "switch to" : "beginning of", |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 180 | dev->device); |
Dan Egnor | cce2b08 | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 181 | if (write(g_outFD, buf, strlen(buf)) < 0) { |
| 182 | perror("output error"); |
| 183 | exit(-1); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 184 | } |
| 185 | } |
Mark Salyzyn | 1ff1809 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 186 | dev->printed = true; |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 190 | static void setupOutput() |
| 191 | { |
| 192 | |
| 193 | if (g_outputFileName == NULL) { |
| 194 | g_outFD = STDOUT_FILENO; |
| 195 | |
| 196 | } else { |
| 197 | struct stat statbuf; |
| 198 | |
| 199 | g_outFD = openLogFile (g_outputFileName); |
| 200 | |
| 201 | if (g_outFD < 0) { |
| 202 | perror ("couldn't open output file"); |
| 203 | exit(-1); |
| 204 | } |
| 205 | |
| 206 | fstat(g_outFD, &statbuf); |
| 207 | |
| 208 | g_outByteCount = statbuf.st_size; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | static void show_help(const char *cmd) |
| 213 | { |
| 214 | fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd); |
| 215 | |
| 216 | fprintf(stderr, "options include:\n" |
| 217 | " -s Set default filter to silent.\n" |
| 218 | " Like specifying filterspec '*:s'\n" |
| 219 | " -f <filename> Log to file. Default to stdout\n" |
| 220 | " -r [<kbytes>] Rotate log every kbytes. (16 if unspecified). Requires -f\n" |
| 221 | " -n <count> Sets max number of rotated logs to <count>, default 4\n" |
Pierre Zurek | 507ea56 | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 222 | " -v <format> Sets the log print format, where <format> is:\n\n" |
| 223 | " brief color long process raw tag thread threadtime time\n\n" |
Mark Salyzyn | 1ff1809 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 224 | " -D print dividers between each log buffer\n" |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 225 | " -c clear (flush) the entire log and exit\n" |
| 226 | " -d dump the log and then exit (don't block)\n" |
Dan Egnor | cce2b08 | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 227 | " -t <count> print only the most recent <count> lines (implies -d)\n" |
Mark Salyzyn | 1ef4818 | 2014-09-01 10:41:33 -0700 | [diff] [blame] | 228 | " -t '<time>' print most recent lines since specified time (implies -d)\n" |
Mark Salyzyn | 2256e2f | 2013-12-09 13:47:00 -0800 | [diff] [blame] | 229 | " -T <count> print only the most recent <count> lines (does not imply -d)\n" |
Mark Salyzyn | 1ef4818 | 2014-09-01 10:41:33 -0700 | [diff] [blame] | 230 | " -T '<time>' print most recent lines since specified time (not imply -d)\n" |
| 231 | " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm'\n" |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 232 | " -g get the size of the log's ring buffer and exit\n" |
Mark Salyzyn | 66460fc | 2014-12-15 10:01:31 -0800 | [diff] [blame] | 233 | " -L dump logs from prior to last reboot\n" |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 234 | " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n" |
Mark Salyzyn | 855c730 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 235 | " 'events', 'crash' or 'all'. Multiple -b parameters are\n" |
| 236 | " allowed and results are interleaved. The default is\n" |
| 237 | " -b main -b system -b crash.\n" |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 238 | " -B output the log in binary.\n" |
Mark Salyzyn | e00fbe5 | 2014-04-11 13:49:43 -0700 | [diff] [blame] | 239 | " -S output statistics.\n" |
| 240 | " -G <size> set size of log ring buffer, may suffix with K or M.\n" |
| 241 | " -p print prune white and ~black list. Service is specified as\n" |
| 242 | " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n" |
| 243 | " with ~, otherwise weighed for longevity if unadorned. All\n" |
| 244 | " other pruning activity is oldest first. Special case ~!\n" |
| 245 | " represents an automatic quicker pruning for the noisiest\n" |
| 246 | " UID as determined by the current statistics.\n" |
| 247 | " -P '<list> ...' set prune white and ~black list, using same format as\n" |
| 248 | " printed above. Must be quoted.\n"); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 249 | |
| 250 | fprintf(stderr,"\nfilterspecs are a series of \n" |
| 251 | " <tag>[:priority]\n\n" |
| 252 | "where <tag> is a log component tag (or * for all) and priority is:\n" |
| 253 | " V Verbose\n" |
| 254 | " D Debug\n" |
| 255 | " I Info\n" |
| 256 | " W Warn\n" |
| 257 | " E Error\n" |
| 258 | " F Fatal\n" |
| 259 | " S Silent (supress all output)\n" |
| 260 | "\n'*' means '*:d' and <tag> by itself means <tag>:v\n" |
| 261 | "\nIf not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.\n" |
| 262 | "If no filterspec is found, filter defaults to '*:I'\n" |
| 263 | "\nIf not specified with -v, format is set from ANDROID_PRINTF_LOG\n" |
Mark Salyzyn | dd569af | 2014-09-16 09:15:15 -0700 | [diff] [blame] | 264 | "or defaults to \"threadtime\"\n\n"); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 265 | |
| 266 | |
| 267 | |
| 268 | } |
| 269 | |
| 270 | |
| 271 | } /* namespace android */ |
| 272 | |
| 273 | static int setLogFormat(const char * formatString) |
| 274 | { |
| 275 | static AndroidLogPrintFormat format; |
| 276 | |
| 277 | format = android_log_formatFromString(formatString); |
| 278 | |
| 279 | if (format == FORMAT_OFF) { |
| 280 | // FORMAT_OFF means invalid string |
| 281 | return -1; |
| 282 | } |
| 283 | |
| 284 | android_log_setPrintFormat(g_logformat, format); |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 289 | static const char multipliers[][2] = { |
| 290 | { "" }, |
| 291 | { "K" }, |
| 292 | { "M" }, |
| 293 | { "G" } |
| 294 | }; |
| 295 | |
| 296 | static unsigned long value_of_size(unsigned long value) |
| 297 | { |
| 298 | for (unsigned i = 0; |
| 299 | (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024); |
| 300 | value /= 1024, ++i) ; |
| 301 | return value; |
| 302 | } |
| 303 | |
| 304 | static const char *multiplier_of_size(unsigned long value) |
| 305 | { |
| 306 | unsigned i; |
| 307 | for (i = 0; |
| 308 | (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024); |
| 309 | value /= 1024, ++i) ; |
| 310 | return multipliers[i]; |
| 311 | } |
| 312 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 313 | int main(int argc, char **argv) |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 314 | { |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 315 | int err; |
| 316 | int hasSetLogFormat = 0; |
| 317 | int clearLog = 0; |
| 318 | int getLogSize = 0; |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 319 | unsigned long setLogSize = 0; |
| 320 | int getPruneList = 0; |
| 321 | char *setPruneList = NULL; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 322 | int printStatistics = 0; |
Mark Salyzyn | 4bdf253 | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 323 | int mode = ANDROID_LOG_RDONLY; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 324 | const char *forceFilters = NULL; |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 325 | log_device_t* devices = NULL; |
| 326 | log_device_t* dev; |
Mark Salyzyn | 1ff1809 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 327 | bool printDividers = false; |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 328 | struct logger_list *logger_list; |
Mark Salyzyn | 9addf59 | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 329 | unsigned int tail_lines = 0; |
| 330 | log_time tail_time(log_time::EPOCH); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 331 | |
Mark Salyzyn | 12af965 | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 332 | signal(SIGPIPE, exit); |
| 333 | |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 334 | g_logformat = android_log_format_new(); |
| 335 | |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 336 | if (argc == 2 && 0 == strcmp(argv[1], "--help")) { |
| 337 | android::show_help(argv[0]); |
| 338 | exit(0); |
| 339 | } |
| 340 | |
| 341 | for (;;) { |
| 342 | int ret; |
| 343 | |
Mark Salyzyn | 66460fc | 2014-12-15 10:01:31 -0800 | [diff] [blame] | 344 | ret = getopt(argc, argv, "cdDLt:T:gG:sQf:r:n:v:b:BSpP:"); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 345 | |
| 346 | if (ret < 0) { |
| 347 | break; |
| 348 | } |
| 349 | |
| 350 | switch(ret) { |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 351 | case 's': |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 352 | // default to all silent |
| 353 | android_log_addFilterRule(g_logformat, "*:s"); |
| 354 | break; |
| 355 | |
| 356 | case 'c': |
| 357 | clearLog = 1; |
Mark Salyzyn | 4bdf253 | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 358 | mode |= ANDROID_LOG_WRONLY; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 359 | break; |
| 360 | |
Mark Salyzyn | 66460fc | 2014-12-15 10:01:31 -0800 | [diff] [blame] | 361 | case 'L': |
| 362 | mode |= ANDROID_LOG_PSTORE; |
| 363 | break; |
| 364 | |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 365 | case 'd': |
Mark Salyzyn | 4bdf253 | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 366 | mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 367 | break; |
| 368 | |
Dan Egnor | cce2b08 | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 369 | case 't': |
Mark Salyzyn | 4bdf253 | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 370 | mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK; |
Mark Salyzyn | 2256e2f | 2013-12-09 13:47:00 -0800 | [diff] [blame] | 371 | /* FALLTHRU */ |
| 372 | case 'T': |
Mark Salyzyn | 9addf59 | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 373 | if (strspn(optarg, "0123456789") != strlen(optarg)) { |
| 374 | char *cp = tail_time.strptime(optarg, |
| 375 | log_time::default_format); |
| 376 | if (!cp) { |
| 377 | fprintf(stderr, |
| 378 | "ERROR: -%c \"%s\" not in \"%s\" time format\n", |
| 379 | ret, optarg, log_time::default_format); |
| 380 | exit(1); |
| 381 | } |
| 382 | if (*cp) { |
| 383 | char c = *cp; |
| 384 | *cp = '\0'; |
| 385 | fprintf(stderr, |
| 386 | "WARNING: -%c \"%s\"\"%c%s\" time truncated\n", |
| 387 | ret, optarg, c, cp + 1); |
| 388 | *cp = c; |
| 389 | } |
| 390 | } else { |
| 391 | tail_lines = atoi(optarg); |
| 392 | if (!tail_lines) { |
| 393 | fprintf(stderr, |
| 394 | "WARNING: -%c %s invalid, setting to 1\n", |
| 395 | ret, optarg); |
| 396 | tail_lines = 1; |
| 397 | } |
| 398 | } |
Dan Egnor | cce2b08 | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 399 | break; |
| 400 | |
Mark Salyzyn | 1ff1809 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 401 | case 'D': |
| 402 | printDividers = true; |
| 403 | break; |
| 404 | |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 405 | case 'g': |
| 406 | getLogSize = 1; |
| 407 | break; |
| 408 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 409 | case 'G': { |
| 410 | // would use atol if not for the multiplier |
| 411 | char *cp = optarg; |
| 412 | setLogSize = 0; |
| 413 | while (('0' <= *cp) && (*cp <= '9')) { |
| 414 | setLogSize *= 10; |
| 415 | setLogSize += *cp - '0'; |
| 416 | ++cp; |
| 417 | } |
| 418 | |
| 419 | switch(*cp) { |
| 420 | case 'g': |
| 421 | case 'G': |
| 422 | setLogSize *= 1024; |
| 423 | /* FALLTHRU */ |
| 424 | case 'm': |
| 425 | case 'M': |
| 426 | setLogSize *= 1024; |
| 427 | /* FALLTHRU */ |
| 428 | case 'k': |
| 429 | case 'K': |
| 430 | setLogSize *= 1024; |
| 431 | /* FALLTHRU */ |
| 432 | case '\0': |
| 433 | break; |
| 434 | |
| 435 | default: |
| 436 | setLogSize = 0; |
| 437 | } |
| 438 | |
| 439 | if (!setLogSize) { |
| 440 | fprintf(stderr, "ERROR: -G <num><multiplier>\n"); |
| 441 | exit(1); |
| 442 | } |
| 443 | } |
| 444 | break; |
| 445 | |
| 446 | case 'p': |
| 447 | getPruneList = 1; |
| 448 | break; |
| 449 | |
| 450 | case 'P': |
| 451 | setPruneList = optarg; |
| 452 | break; |
| 453 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 454 | case 'b': { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 455 | if (strcmp(optarg, "all") == 0) { |
| 456 | while (devices) { |
| 457 | dev = devices; |
| 458 | devices = dev->next; |
| 459 | delete dev; |
| 460 | } |
| 461 | |
Mark Salyzyn | 6577769 | 2014-10-10 15:25:38 -0700 | [diff] [blame] | 462 | devices = dev = NULL; |
| 463 | android::g_devCount = 0; |
Mark Salyzyn | 6577769 | 2014-10-10 15:25:38 -0700 | [diff] [blame] | 464 | for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) { |
| 465 | const char *name = android_log_id_to_name((log_id_t)i); |
| 466 | log_id_t log_id = android_name_to_log_id(name); |
| 467 | |
| 468 | if (log_id != (log_id_t)i) { |
| 469 | continue; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 470 | } |
Mark Salyzyn | 6577769 | 2014-10-10 15:25:38 -0700 | [diff] [blame] | 471 | |
| 472 | bool binary = strcmp(name, "events") == 0; |
Mark Salyzyn | a044390 | 2015-02-27 13:41:34 -0800 | [diff] [blame^] | 473 | log_device_t* d = new log_device_t(name, binary); |
Mark Salyzyn | 6577769 | 2014-10-10 15:25:38 -0700 | [diff] [blame] | 474 | |
| 475 | if (dev) { |
| 476 | dev->next = d; |
| 477 | dev = d; |
| 478 | } else { |
| 479 | devices = dev = d; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 480 | } |
Mark Salyzyn | 6577769 | 2014-10-10 15:25:38 -0700 | [diff] [blame] | 481 | android::g_devCount++; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 482 | } |
| 483 | break; |
| 484 | } |
| 485 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 486 | bool binary = strcmp(optarg, "events") == 0; |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 487 | |
| 488 | if (devices) { |
| 489 | dev = devices; |
| 490 | while (dev->next) { |
| 491 | dev = dev->next; |
| 492 | } |
Mark Salyzyn | a044390 | 2015-02-27 13:41:34 -0800 | [diff] [blame^] | 493 | dev->next = new log_device_t(optarg, binary); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 494 | } else { |
Mark Salyzyn | a044390 | 2015-02-27 13:41:34 -0800 | [diff] [blame^] | 495 | devices = new log_device_t(optarg, binary); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 496 | } |
| 497 | android::g_devCount++; |
| 498 | } |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 499 | break; |
| 500 | |
| 501 | case 'B': |
| 502 | android::g_printBinary = 1; |
| 503 | break; |
| 504 | |
| 505 | case 'f': |
| 506 | // redirect output to a file |
| 507 | |
| 508 | android::g_outputFileName = optarg; |
| 509 | |
| 510 | break; |
| 511 | |
| 512 | case 'r': |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 513 | if (optarg == NULL) { |
| 514 | android::g_logRotateSizeKBytes |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 515 | = DEFAULT_LOG_ROTATE_SIZE_KBYTES; |
| 516 | } else { |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 517 | if (!isdigit(optarg[0])) { |
| 518 | fprintf(stderr,"Invalid parameter to -r\n"); |
| 519 | android::show_help(argv[0]); |
| 520 | exit(-1); |
| 521 | } |
| 522 | android::g_logRotateSizeKBytes = atoi(optarg); |
| 523 | } |
| 524 | break; |
| 525 | |
| 526 | case 'n': |
| 527 | if (!isdigit(optarg[0])) { |
| 528 | fprintf(stderr,"Invalid parameter to -r\n"); |
| 529 | android::show_help(argv[0]); |
| 530 | exit(-1); |
| 531 | } |
| 532 | |
| 533 | android::g_maxRotatedLogs = atoi(optarg); |
| 534 | break; |
| 535 | |
| 536 | case 'v': |
| 537 | err = setLogFormat (optarg); |
| 538 | if (err < 0) { |
| 539 | fprintf(stderr,"Invalid parameter to -v\n"); |
| 540 | android::show_help(argv[0]); |
| 541 | exit(-1); |
| 542 | } |
| 543 | |
Mark Salyzyn | dd569af | 2014-09-16 09:15:15 -0700 | [diff] [blame] | 544 | if (strcmp("color", optarg)) { // exception for modifiers |
| 545 | hasSetLogFormat = 1; |
| 546 | } |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 547 | break; |
| 548 | |
| 549 | case 'Q': |
| 550 | /* this is a *hidden* option used to start a version of logcat */ |
| 551 | /* in an emulated device only. it basically looks for androidboot.logcat= */ |
| 552 | /* on the kernel command line. If something is found, it extracts a log filter */ |
| 553 | /* and uses it to run the program. If nothing is found, the program should */ |
| 554 | /* quit immediately */ |
| 555 | #define KERNEL_OPTION "androidboot.logcat=" |
| 556 | #define CONSOLE_OPTION "androidboot.console=" |
| 557 | { |
| 558 | int fd; |
| 559 | char* logcat; |
| 560 | char* console; |
| 561 | int force_exit = 1; |
| 562 | static char cmdline[1024]; |
| 563 | |
| 564 | fd = open("/proc/cmdline", O_RDONLY); |
| 565 | if (fd >= 0) { |
| 566 | int n = read(fd, cmdline, sizeof(cmdline)-1 ); |
| 567 | if (n < 0) n = 0; |
| 568 | cmdline[n] = 0; |
| 569 | close(fd); |
| 570 | } else { |
| 571 | cmdline[0] = 0; |
| 572 | } |
| 573 | |
| 574 | logcat = strstr( cmdline, KERNEL_OPTION ); |
| 575 | console = strstr( cmdline, CONSOLE_OPTION ); |
| 576 | if (logcat != NULL) { |
| 577 | char* p = logcat + sizeof(KERNEL_OPTION)-1;; |
| 578 | char* q = strpbrk( p, " \t\n\r" );; |
| 579 | |
| 580 | if (q != NULL) |
| 581 | *q = 0; |
| 582 | |
| 583 | forceFilters = p; |
| 584 | force_exit = 0; |
| 585 | } |
| 586 | /* if nothing found or invalid filters, exit quietly */ |
| 587 | if (force_exit) |
| 588 | exit(0); |
| 589 | |
| 590 | /* redirect our output to the emulator console */ |
| 591 | if (console) { |
| 592 | char* p = console + sizeof(CONSOLE_OPTION)-1; |
| 593 | char* q = strpbrk( p, " \t\n\r" ); |
| 594 | char devname[64]; |
| 595 | int len; |
| 596 | |
| 597 | if (q != NULL) { |
| 598 | len = q - p; |
| 599 | } else |
| 600 | len = strlen(p); |
| 601 | |
| 602 | len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p ); |
| 603 | fprintf(stderr, "logcat using %s (%d)\n", devname, len); |
| 604 | if (len < (int)sizeof(devname)) { |
| 605 | fd = open( devname, O_WRONLY ); |
| 606 | if (fd >= 0) { |
| 607 | dup2(fd, 1); |
| 608 | dup2(fd, 2); |
| 609 | close(fd); |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | break; |
| 615 | |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 616 | case 'S': |
| 617 | printStatistics = 1; |
| 618 | break; |
| 619 | |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 620 | default: |
| 621 | fprintf(stderr,"Unrecognized Option\n"); |
| 622 | android::show_help(argv[0]); |
| 623 | exit(-1); |
| 624 | break; |
| 625 | } |
| 626 | } |
| 627 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 628 | if (!devices) { |
Mark Salyzyn | a044390 | 2015-02-27 13:41:34 -0800 | [diff] [blame^] | 629 | dev = devices = new log_device_t("main", false); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 630 | android::g_devCount = 1; |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 631 | if (android_name_to_log_id("system") == LOG_ID_SYSTEM) { |
Mark Salyzyn | a044390 | 2015-02-27 13:41:34 -0800 | [diff] [blame^] | 632 | dev = dev->next = new log_device_t("system", false); |
Joe Onorato | 400da4a | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 633 | android::g_devCount++; |
| 634 | } |
Mark Salyzyn | 855c730 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 635 | if (android_name_to_log_id("crash") == LOG_ID_CRASH) { |
Mark Salyzyn | a044390 | 2015-02-27 13:41:34 -0800 | [diff] [blame^] | 636 | dev = dev->next = new log_device_t("crash", false); |
Mark Salyzyn | 855c730 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 637 | android::g_devCount++; |
| 638 | } |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 639 | } |
| 640 | |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 641 | if (android::g_logRotateSizeKBytes != 0 |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 642 | && android::g_outputFileName == NULL |
| 643 | ) { |
| 644 | fprintf(stderr,"-r requires -f as well\n"); |
| 645 | android::show_help(argv[0]); |
| 646 | exit(-1); |
| 647 | } |
| 648 | |
| 649 | android::setupOutput(); |
| 650 | |
| 651 | if (hasSetLogFormat == 0) { |
| 652 | const char* logFormat = getenv("ANDROID_PRINTF_LOG"); |
| 653 | |
| 654 | if (logFormat != NULL) { |
| 655 | err = setLogFormat(logFormat); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 656 | if (err < 0) { |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 657 | fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n", |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 658 | logFormat); |
| 659 | } |
Mark Salyzyn | dd569af | 2014-09-16 09:15:15 -0700 | [diff] [blame] | 660 | } else { |
| 661 | setLogFormat("threadtime"); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
| 665 | if (forceFilters) { |
| 666 | err = android_log_addFilterString(g_logformat, forceFilters); |
| 667 | if (err < 0) { |
| 668 | fprintf (stderr, "Invalid filter expression in -logcat option\n"); |
| 669 | exit(0); |
| 670 | } |
| 671 | } else if (argc == optind) { |
| 672 | // Add from environment variable |
| 673 | char *env_tags_orig = getenv("ANDROID_LOG_TAGS"); |
| 674 | |
| 675 | if (env_tags_orig != NULL) { |
| 676 | err = android_log_addFilterString(g_logformat, env_tags_orig); |
| 677 | |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 678 | if (err < 0) { |
| 679 | fprintf(stderr, "Invalid filter expression in" |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 680 | " ANDROID_LOG_TAGS\n"); |
| 681 | android::show_help(argv[0]); |
| 682 | exit(-1); |
| 683 | } |
| 684 | } |
| 685 | } else { |
| 686 | // Add from commandline |
| 687 | for (int i = optind ; i < argc ; i++) { |
| 688 | err = android_log_addFilterString(g_logformat, argv[i]); |
| 689 | |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 690 | if (err < 0) { |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 691 | fprintf (stderr, "Invalid filter expression '%s'\n", argv[i]); |
| 692 | android::show_help(argv[0]); |
| 693 | exit(-1); |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 698 | dev = devices; |
Mark Salyzyn | 9addf59 | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 699 | if (tail_time != log_time::EPOCH) { |
| 700 | logger_list = android_logger_list_alloc_time(mode, tail_time, 0); |
| 701 | } else { |
| 702 | logger_list = android_logger_list_alloc(mode, tail_lines, 0); |
| 703 | } |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 704 | while (dev) { |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 705 | dev->logger_list = logger_list; |
| 706 | dev->logger = android_logger_open(logger_list, |
| 707 | android_name_to_log_id(dev->device)); |
| 708 | if (!dev->logger) { |
| 709 | fprintf(stderr, "Unable to open log device '%s'\n", dev->device); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 710 | exit(EXIT_FAILURE); |
| 711 | } |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 712 | |
| 713 | if (clearLog) { |
| 714 | int ret; |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 715 | ret = android_logger_clear(dev->logger); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 716 | if (ret) { |
Mark Salyzyn | 0f498aa | 2014-03-17 10:36:46 -0700 | [diff] [blame] | 717 | perror("failed to clear the log"); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 718 | exit(EXIT_FAILURE); |
| 719 | } |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 720 | } |
| 721 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 722 | if (setLogSize && android_logger_set_log_size(dev->logger, setLogSize)) { |
Mark Salyzyn | 0f498aa | 2014-03-17 10:36:46 -0700 | [diff] [blame] | 723 | perror("failed to set the log size"); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 724 | exit(EXIT_FAILURE); |
| 725 | } |
| 726 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 727 | if (getLogSize) { |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 728 | long size, readable; |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 729 | |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 730 | size = android_logger_get_log_size(dev->logger); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 731 | if (size < 0) { |
Mark Salyzyn | 0f498aa | 2014-03-17 10:36:46 -0700 | [diff] [blame] | 732 | perror("failed to get the log size"); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 733 | exit(EXIT_FAILURE); |
| 734 | } |
| 735 | |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 736 | readable = android_logger_get_log_readable_size(dev->logger); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 737 | if (readable < 0) { |
Mark Salyzyn | 0f498aa | 2014-03-17 10:36:46 -0700 | [diff] [blame] | 738 | perror("failed to get the readable log size"); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 739 | exit(EXIT_FAILURE); |
| 740 | } |
| 741 | |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 742 | printf("%s: ring buffer is %ld%sb (%ld%sb consumed), " |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 743 | "max entry is %db, max payload is %db\n", dev->device, |
Mark Salyzyn | 4e756fb | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 744 | value_of_size(size), multiplier_of_size(size), |
| 745 | value_of_size(readable), multiplier_of_size(readable), |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 746 | (int) LOGGER_ENTRY_MAX_LEN, (int) LOGGER_ENTRY_MAX_PAYLOAD); |
| 747 | } |
| 748 | |
| 749 | dev = dev->next; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 750 | } |
| 751 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 752 | if (setPruneList) { |
| 753 | size_t len = strlen(setPruneList) + 32; // margin to allow rc |
| 754 | char *buf = (char *) malloc(len); |
| 755 | |
| 756 | strcpy(buf, setPruneList); |
| 757 | int ret = android_logger_set_prune_list(logger_list, buf, len); |
| 758 | free(buf); |
| 759 | |
| 760 | if (ret) { |
Mark Salyzyn | 0f498aa | 2014-03-17 10:36:46 -0700 | [diff] [blame] | 761 | perror("failed to set the prune list"); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 762 | exit(EXIT_FAILURE); |
| 763 | } |
| 764 | } |
| 765 | |
Mark Salyzyn | c402bc7 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 766 | if (printStatistics || getPruneList) { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 767 | size_t len = 8192; |
| 768 | char *buf; |
| 769 | |
| 770 | for(int retry = 32; |
| 771 | (retry >= 0) && ((buf = new char [len])); |
| 772 | delete [] buf, --retry) { |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 773 | if (getPruneList) { |
| 774 | android_logger_get_prune_list(logger_list, buf, len); |
| 775 | } else { |
| 776 | android_logger_get_statistics(logger_list, buf, len); |
| 777 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 778 | buf[len-1] = '\0'; |
| 779 | size_t ret = atol(buf) + 1; |
| 780 | if (ret < 4) { |
| 781 | delete [] buf; |
| 782 | buf = NULL; |
| 783 | break; |
| 784 | } |
| 785 | bool check = ret <= len; |
| 786 | len = ret; |
| 787 | if (check) { |
| 788 | break; |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | if (!buf) { |
Mark Salyzyn | 0f498aa | 2014-03-17 10:36:46 -0700 | [diff] [blame] | 793 | perror("failed to read data"); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 794 | exit(EXIT_FAILURE); |
| 795 | } |
| 796 | |
| 797 | // remove trailing FF |
| 798 | char *cp = buf + len - 1; |
| 799 | *cp = '\0'; |
| 800 | bool truncated = *--cp != '\f'; |
| 801 | if (!truncated) { |
| 802 | *cp = '\0'; |
| 803 | } |
| 804 | |
| 805 | // squash out the byte count |
| 806 | cp = buf; |
| 807 | if (!truncated) { |
Mark Salyzyn | 63a4ad5 | 2014-03-21 13:12:16 -0700 | [diff] [blame] | 808 | while (isdigit(*cp)) { |
| 809 | ++cp; |
| 810 | } |
| 811 | if (*cp == '\n') { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 812 | ++cp; |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | printf("%s", cp); |
| 817 | delete [] buf; |
| 818 | exit(0); |
| 819 | } |
| 820 | |
| 821 | |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 822 | if (getLogSize) { |
Kenny Root | fa7c09a | 2011-09-30 17:10:14 -0700 | [diff] [blame] | 823 | exit(0); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 824 | } |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 825 | if (setLogSize || setPruneList) { |
| 826 | exit(0); |
| 827 | } |
Joe Onorato | 400da4a | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 828 | if (clearLog) { |
Kenny Root | fa7c09a | 2011-09-30 17:10:14 -0700 | [diff] [blame] | 829 | exit(0); |
Joe Onorato | 400da4a | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 830 | } |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 831 | |
| 832 | //LOG_EVENT_INT(10, 12345); |
| 833 | //LOG_EVENT_LONG(11, 0x1122334455667788LL); |
| 834 | //LOG_EVENT_STRING(0, "whassup, doc?"); |
| 835 | |
Mark Salyzyn | 1ff1809 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 836 | dev = NULL; |
Mark Salyzyn | a044390 | 2015-02-27 13:41:34 -0800 | [diff] [blame^] | 837 | log_device_t unexpected("unexpected", false); |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 838 | while (1) { |
| 839 | struct log_msg log_msg; |
Mark Salyzyn | 1ff1809 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 840 | log_device_t* d; |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 841 | int ret = android_logger_list_read(logger_list, &log_msg); |
| 842 | |
| 843 | if (ret == 0) { |
Mark Salyzyn | 784d64f | 2015-02-26 14:33:35 -0800 | [diff] [blame] | 844 | fprintf(stderr, "read: unexpected EOF!\n"); |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 845 | exit(EXIT_FAILURE); |
| 846 | } |
| 847 | |
| 848 | if (ret < 0) { |
| 849 | if (ret == -EAGAIN) { |
| 850 | break; |
| 851 | } |
| 852 | |
| 853 | if (ret == -EIO) { |
Mark Salyzyn | 784d64f | 2015-02-26 14:33:35 -0800 | [diff] [blame] | 854 | fprintf(stderr, "read: unexpected EOF!\n"); |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 855 | exit(EXIT_FAILURE); |
| 856 | } |
| 857 | if (ret == -EINVAL) { |
| 858 | fprintf(stderr, "read: unexpected length.\n"); |
| 859 | exit(EXIT_FAILURE); |
| 860 | } |
Mark Salyzyn | 0f498aa | 2014-03-17 10:36:46 -0700 | [diff] [blame] | 861 | perror("logcat read failure"); |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 862 | exit(EXIT_FAILURE); |
| 863 | } |
| 864 | |
Mark Salyzyn | 1ff1809 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 865 | for(d = devices; d; d = d->next) { |
| 866 | if (android_name_to_log_id(d->device) == log_msg.id()) { |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 867 | break; |
| 868 | } |
| 869 | } |
Mark Salyzyn | 1ff1809 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 870 | if (!d) { |
Mark Salyzyn | 784d64f | 2015-02-26 14:33:35 -0800 | [diff] [blame] | 871 | android::g_devCount = 2; // set to Multiple |
| 872 | d = &unexpected; |
| 873 | d->binary = log_msg.id() == LOG_ID_EVENTS; |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 874 | } |
| 875 | |
Mark Salyzyn | 1ff1809 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 876 | if (dev != d) { |
| 877 | dev = d; |
| 878 | android::maybePrintStart(dev, printDividers); |
| 879 | } |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 880 | if (android::g_printBinary) { |
| 881 | android::printBinary(&log_msg); |
| 882 | } else { |
| 883 | android::processBuffer(dev, &log_msg); |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | android_logger_list_free(logger_list); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 888 | |
| 889 | return 0; |
| 890 | } |