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