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