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