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> |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 9 | #include <getopt.h> |
Aristidis Papaioannou | eba7344 | 2014-10-16 22:19:55 -0700 | [diff] [blame] | 10 | #include <math.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 11 | #include <sched.h> |
| 12 | #include <signal.h> |
| 13 | #include <stdarg.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 16 | #include <string.h> |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 17 | #include <sys/cdefs.h> |
Riley Andrews | aede989 | 2015-06-08 23:36:34 -0700 | [diff] [blame] | 18 | #include <sys/resource.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 19 | #include <sys/socket.h> |
| 20 | #include <sys/stat.h> |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 21 | #include <sys/types.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 22 | #include <time.h> |
| 23 | #include <unistd.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 24 | |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 25 | #include <memory> |
| 26 | #include <string> |
| 27 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 28 | #include <android-base/file.h> |
| 29 | #include <android-base/strings.h> |
Mark Salyzyn | 33c2625 | 2016-04-12 09:11:46 -0700 | [diff] [blame^] | 30 | #include <cutils/properties.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 31 | #include <cutils/sched_policy.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 32 | #include <cutils/sockets.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 33 | #include <log/event_tag_map.h> |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 34 | #include <log/log.h> |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 35 | #include <log/log_read.h> |
Colin Cross | 9227bd3 | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 36 | #include <log/logd.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 37 | #include <log/logger.h> |
Colin Cross | 9227bd3 | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 38 | #include <log/logprint.h> |
Elliott Hughes | b9e53b4 | 2016-02-17 11:58:01 -0800 | [diff] [blame] | 39 | #include <system/thread_defs.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | |
Casey Dahlin | dc42a87 | 2016-03-17 16:18:55 -0700 | [diff] [blame] | 41 | #include <pcrecpp.h> |
| 42 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | #define DEFAULT_MAX_ROTATED_LOGS 4 |
| 44 | |
| 45 | static AndroidLogFormat * g_logformat; |
| 46 | |
| 47 | /* logd prefixes records with a length field */ |
| 48 | #define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t) |
| 49 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 50 | struct log_device_t { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 51 | const char* device; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 52 | bool binary; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 53 | struct logger *logger; |
| 54 | struct logger_list *logger_list; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 55 | bool printed; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 56 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 57 | log_device_t* next; |
| 58 | |
Mark Salyzyn | 5f6738a | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 59 | log_device_t(const char* d, bool b) { |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 60 | device = d; |
| 61 | binary = b; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 62 | next = NULL; |
| 63 | printed = false; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 64 | logger = NULL; |
| 65 | logger_list = NULL; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 66 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 67 | }; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 68 | |
| 69 | namespace android { |
| 70 | |
| 71 | /* Global Variables */ |
| 72 | |
Mark Salyzyn | c6d6652 | 2016-03-30 12:38:29 -0700 | [diff] [blame] | 73 | static const char * g_outputFileName; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 74 | // 0 means "no log rotation" |
Mark Salyzyn | c6d6652 | 2016-03-30 12:38:29 -0700 | [diff] [blame] | 75 | static size_t g_logRotateSizeKBytes; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 76 | // 0 means "unbounded" |
| 77 | static size_t g_maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 78 | static int g_outFD = -1; |
Mark Salyzyn | c6d6652 | 2016-03-30 12:38:29 -0700 | [diff] [blame] | 79 | static size_t g_outByteCount; |
| 80 | static int g_printBinary; |
| 81 | static int g_devCount; // >1 means multiple |
Casey Dahlin | dc42a87 | 2016-03-17 16:18:55 -0700 | [diff] [blame] | 82 | static pcrecpp::RE* g_regex; |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 83 | // 0 means "infinite" |
Mark Salyzyn | c6d6652 | 2016-03-30 12:38:29 -0700 | [diff] [blame] | 84 | static size_t g_maxCount; |
| 85 | static size_t g_printCount; |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 86 | static bool g_printItAnyways; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 87 | |
Mark Salyzyn | aa730c1 | 2016-03-30 12:38:29 -0700 | [diff] [blame] | 88 | // if showHelp is set, newline required in fmt statement to transition to usage |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 89 | __noreturn static void logcat_panic(bool showHelp, const char *fmt, ...) __printflike(2,3); |
| 90 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 91 | static int openLogFile (const char *pathname) |
| 92 | { |
Edwin Vane | 80b221c | 2012-08-13 12:55:07 -0400 | [diff] [blame] | 93 | 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] | 94 | } |
| 95 | |
| 96 | static void rotateLogs() |
| 97 | { |
| 98 | int err; |
| 99 | |
| 100 | // Can't rotate logs if we're not outputting to a file |
| 101 | if (g_outputFileName == NULL) { |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | close(g_outFD); |
| 106 | |
Aristidis Papaioannou | eba7344 | 2014-10-16 22:19:55 -0700 | [diff] [blame] | 107 | // Compute the maximum number of digits needed to count up to g_maxRotatedLogs in decimal. |
| 108 | // eg: g_maxRotatedLogs == 30 -> log10(30) == 1.477 -> maxRotationCountDigits == 2 |
| 109 | int maxRotationCountDigits = |
| 110 | (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0; |
| 111 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 112 | for (int i = g_maxRotatedLogs ; i > 0 ; i--) { |
| 113 | char *file0, *file1; |
| 114 | |
Aristidis Papaioannou | eba7344 | 2014-10-16 22:19:55 -0700 | [diff] [blame] | 115 | asprintf(&file1, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 116 | |
| 117 | if (i - 1 == 0) { |
| 118 | asprintf(&file0, "%s", g_outputFileName); |
| 119 | } else { |
Aristidis Papaioannou | eba7344 | 2014-10-16 22:19:55 -0700 | [diff] [blame] | 120 | asprintf(&file0, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 123 | if (!file0 || !file1) { |
| 124 | perror("while rotating log files"); |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | err = rename(file0, file1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 129 | |
| 130 | if (err < 0 && errno != ENOENT) { |
| 131 | perror("while rotating log files"); |
| 132 | } |
| 133 | |
| 134 | free(file1); |
| 135 | free(file0); |
| 136 | } |
| 137 | |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 138 | g_outFD = openLogFile(g_outputFileName); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 139 | |
| 140 | if (g_outFD < 0) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 141 | logcat_panic(false, "couldn't open output file"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | g_outByteCount = 0; |
| 145 | |
| 146 | } |
| 147 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 148 | void printBinary(struct log_msg *buf) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 149 | { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 150 | size_t size = buf->len(); |
| 151 | |
| 152 | TEMP_FAILURE_RETRY(write(g_outFD, buf, size)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Mark Salyzyn | aa730c1 | 2016-03-30 12:38:29 -0700 | [diff] [blame] | 155 | static bool regexOk(const AndroidLogEntry& entry) |
Casey Dahlin | dc42a87 | 2016-03-17 16:18:55 -0700 | [diff] [blame] | 156 | { |
Mark Salyzyn | aa730c1 | 2016-03-30 12:38:29 -0700 | [diff] [blame] | 157 | if (!g_regex) { |
Casey Dahlin | dc42a87 | 2016-03-17 16:18:55 -0700 | [diff] [blame] | 158 | return true; |
| 159 | } |
| 160 | |
Casey Dahlin | dc42a87 | 2016-03-17 16:18:55 -0700 | [diff] [blame] | 161 | std::string messageString(entry.message, entry.messageLen); |
| 162 | |
| 163 | return g_regex->PartialMatch(messageString); |
| 164 | } |
| 165 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 166 | 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] | 167 | { |
Mathias Agopian | 5084452 | 2010-03-17 16:10:26 -0700 | [diff] [blame] | 168 | int bytesWritten = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 169 | int err; |
| 170 | AndroidLogEntry entry; |
| 171 | char binaryMsgBuf[1024]; |
| 172 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 173 | if (dev->binary) { |
Mark Salyzyn | 9421b0c | 2015-02-26 14:33:35 -0800 | [diff] [blame] | 174 | static bool hasOpenedEventTagMap = false; |
| 175 | static EventTagMap *eventTagMap = NULL; |
| 176 | |
| 177 | if (!eventTagMap && !hasOpenedEventTagMap) { |
| 178 | eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE); |
| 179 | hasOpenedEventTagMap = true; |
| 180 | } |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 181 | err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry, |
Mark Salyzyn | 9421b0c | 2015-02-26 14:33:35 -0800 | [diff] [blame] | 182 | eventTagMap, |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 183 | binaryMsgBuf, |
| 184 | sizeof(binaryMsgBuf)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 185 | //printf(">>> pri=%d len=%d msg='%s'\n", |
| 186 | // entry.priority, entry.messageLen, entry.message); |
| 187 | } else { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 188 | err = android_log_processLogBuffer(&buf->entry_v1, &entry); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 189 | } |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 190 | if (err < 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 191 | goto error; |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 192 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 193 | |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 194 | if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) { |
| 195 | bool match = regexOk(entry); |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 196 | |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 197 | g_printCount += match; |
| 198 | if (match || g_printItAnyways) { |
| 199 | bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry); |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 200 | |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 201 | if (bytesWritten < 0) { |
| 202 | logcat_panic(false, "output error"); |
| 203 | } |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 204 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | g_outByteCount += bytesWritten; |
| 208 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 209 | if (g_logRotateSizeKBytes > 0 |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 210 | && (g_outByteCount / 1024) >= g_logRotateSizeKBytes |
| 211 | ) { |
| 212 | rotateLogs(); |
| 213 | } |
| 214 | |
| 215 | error: |
| 216 | //fprintf (stderr, "Error processing record\n"); |
| 217 | return; |
| 218 | } |
| 219 | |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 220 | static void maybePrintStart(log_device_t* dev, bool printDividers) { |
| 221 | if (!dev->printed || printDividers) { |
Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 222 | if (g_devCount > 1 && !g_printBinary) { |
| 223 | char buf[1024]; |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 224 | snprintf(buf, sizeof(buf), "--------- %s %s\n", |
| 225 | dev->printed ? "switch to" : "beginning of", |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 226 | dev->device); |
Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 227 | if (write(g_outFD, buf, strlen(buf)) < 0) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 228 | logcat_panic(false, "output error"); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 229 | } |
| 230 | } |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 231 | dev->printed = true; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 235 | static void setupOutput() |
| 236 | { |
| 237 | |
| 238 | if (g_outputFileName == NULL) { |
| 239 | g_outFD = STDOUT_FILENO; |
| 240 | |
| 241 | } else { |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 242 | if (set_sched_policy(0, SP_BACKGROUND) < 0) { |
| 243 | fprintf(stderr, "failed to set background scheduling policy\n"); |
| 244 | } |
| 245 | |
| 246 | struct sched_param param; |
| 247 | memset(¶m, 0, sizeof(param)); |
| 248 | if (sched_setscheduler((pid_t) 0, SCHED_BATCH, ¶m) < 0) { |
| 249 | fprintf(stderr, "failed to set to batch scheduler\n"); |
| 250 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 251 | |
Riley Andrews | aede989 | 2015-06-08 23:36:34 -0700 | [diff] [blame] | 252 | if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) { |
| 253 | fprintf(stderr, "failed set to priority\n"); |
| 254 | } |
| 255 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 256 | g_outFD = openLogFile (g_outputFileName); |
| 257 | |
| 258 | if (g_outFD < 0) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 259 | logcat_panic(false, "couldn't open output file"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 262 | struct stat statbuf; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 263 | if (fstat(g_outFD, &statbuf) == -1) { |
| 264 | close(g_outFD); |
| 265 | logcat_panic(false, "couldn't get output file stat\n"); |
| 266 | } |
| 267 | |
| 268 | if ((size_t) statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) { |
| 269 | close(g_outFD); |
| 270 | logcat_panic(false, "invalid output file stat\n"); |
| 271 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 272 | |
| 273 | g_outByteCount = statbuf.st_size; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | static void show_help(const char *cmd) |
| 278 | { |
| 279 | fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd); |
| 280 | |
| 281 | fprintf(stderr, "options include:\n" |
Mark Salyzyn | 378f474 | 2016-04-12 09:11:46 -0700 | [diff] [blame] | 282 | " -s Set default filter to silent. Equivalent to filterspec '*:S'\n" |
| 283 | " -f <file>, --file=<file> Log to file. Default is stdout\n" |
Mark Salyzyn | 33c2625 | 2016-04-12 09:11:46 -0700 | [diff] [blame^] | 284 | " -r <kbytes>, --rotate-kbytes=<kbytes> Rotate log every kbytes. Requires -f\n" |
| 285 | " option. Permits property expansion.\n" |
| 286 | " -n <count>, --rotate-count=<count> Sets max number of rotated logs to\n" |
| 287 | " <count>, default 4. Permits property expansion.\n" |
Mark Salyzyn | 378f474 | 2016-04-12 09:11:46 -0700 | [diff] [blame] | 288 | " -v <format>, --format=<format>\n" |
| 289 | " Sets the log print format, where <format> is:\n" |
| 290 | " brief color epoch long monotonic printable process raw\n" |
| 291 | " tag thread threadtime time uid usec UTC year zone\n" |
| 292 | " -D, --dividers Print dividers between each log buffer\n" |
| 293 | " -c, --clear Clear (flush) the entire log and exit\n" |
| 294 | " -d Dump the log and then exit (don't block)\n" |
| 295 | " -e <expr>, --regex=<expr>\n" |
| 296 | " Only print lines where the log message matches <expr>\n" |
| 297 | " where <expr> is a regular expression\n" |
| 298 | // Leave --head undocumented as alias for -m |
| 299 | " -m <count>, --max-count=<count>\n" |
| 300 | " Quit after printing <count> lines. This is meant to be\n" |
| 301 | " paired with --regex, but will work on its own.\n" |
| 302 | " --print Paired with --regex and --max-count to let content bypass\n" |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 303 | " regex filter but still stop at number of matches.\n" |
Mark Salyzyn | 378f474 | 2016-04-12 09:11:46 -0700 | [diff] [blame] | 304 | // Leave --tail undocumented as alias for -t |
| 305 | " -t <count> Print only the most recent <count> lines (implies -d)\n" |
| 306 | " -t '<time>' Print most recent lines since specified time (implies -d)\n" |
| 307 | " -T <count> Print only the most recent <count> lines (does not imply -d)\n" |
| 308 | " -T '<time>' Print most recent lines since specified time (not imply -d)\n" |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 309 | " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n" |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 310 | " 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n" |
Mark Salyzyn | 378f474 | 2016-04-12 09:11:46 -0700 | [diff] [blame] | 311 | " -g, --buffer-size Get the size of the ring buffer.\n" |
| 312 | " -G <size>, --buffer-size=<size>\n" |
| 313 | " Set size of log ring buffer, may suffix with K or M.\n" |
| 314 | " -L, -last Dump logs from prior to last reboot\n" |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 315 | // Leave security (Device Owner only installations) and |
| 316 | // kernel (userdebug and eng) buffers undocumented. |
Mark Salyzyn | 378f474 | 2016-04-12 09:11:46 -0700 | [diff] [blame] | 317 | " -b <buffer>, --buffer=<buffer> Request alternate ring buffer, 'main',\n" |
| 318 | " 'system', 'radio', 'events', 'crash', 'default' or 'all'.\n" |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 319 | " Multiple -b parameters or comma separated list of buffers are\n" |
| 320 | " allowed. Buffers interleaved. Default -b main,system,crash.\n" |
Mark Salyzyn | 33c2625 | 2016-04-12 09:11:46 -0700 | [diff] [blame^] | 321 | " Permits property expansion.\n" |
Mark Salyzyn | 378f474 | 2016-04-12 09:11:46 -0700 | [diff] [blame] | 322 | " -B, --binary Output the log in binary.\n" |
| 323 | " -S, --statistics Output statistics.\n" |
| 324 | " -p, --prune Print prune white and ~black list. Service is specified as\n" |
| 325 | " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n" |
Mark Salyzyn | bbbe14f | 2014-04-11 13:49:43 -0700 | [diff] [blame] | 326 | " with ~, otherwise weighed for longevity if unadorned. All\n" |
| 327 | " other pruning activity is oldest first. Special case ~!\n" |
| 328 | " represents an automatic quicker pruning for the noisiest\n" |
| 329 | " UID as determined by the current statistics.\n" |
Mark Salyzyn | 378f474 | 2016-04-12 09:11:46 -0700 | [diff] [blame] | 330 | " -P '<list> ...', --prune='<list> ...'\n" |
| 331 | " Set prune white and ~black list, using same format as\n" |
| 332 | " listed above. Must be quoted.\n" |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 333 | " --pid=<pid> Only prints logs from the given pid.\n" |
Mark Salyzyn | 378f474 | 2016-04-12 09:11:46 -0700 | [diff] [blame] | 334 | // Check ANDROID_LOG_WRAP_DEFAULT_TIMEOUT value for match to 2 hours |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 335 | " --wrap Sleep for 2 hours or when buffer about to wrap whichever\n" |
| 336 | " comes first. Improves efficiency of polling by providing\n" |
| 337 | " an about-to-wrap wakeup.\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 338 | |
Mark Salyzyn | 33c2625 | 2016-04-12 09:11:46 -0700 | [diff] [blame^] | 339 | fprintf(stderr,"\nProperty expansion where available, may need to be single quoted to prevent\n" |
| 340 | "shell expansion:\n" |
| 341 | " ${key} - Expand string with property value associated with key\n" |
| 342 | " ${key:-default} - Expand, if property key value clear, use default\n" |
| 343 | "\nfilterspecs are a series of \n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 344 | " <tag>[:priority]\n\n" |
| 345 | "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] | 346 | " V Verbose (default for <tag>)\n" |
| 347 | " D Debug (default for '*')\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 348 | " I Info\n" |
| 349 | " W Warn\n" |
| 350 | " E Error\n" |
| 351 | " F Fatal\n" |
Mark Salyzyn | bba894a | 2015-03-09 09:32:56 -0700 | [diff] [blame] | 352 | " S Silent (suppress all output)\n" |
| 353 | "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n" |
| 354 | "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n" |
| 355 | "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n" |
| 356 | "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n" |
| 357 | "\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] | 358 | "or defaults to \"threadtime\"\n\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 359 | } |
| 360 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 361 | static int setLogFormat(const char * formatString) |
| 362 | { |
| 363 | static AndroidLogPrintFormat format; |
| 364 | |
| 365 | format = android_log_formatFromString(formatString); |
| 366 | |
| 367 | if (format == FORMAT_OFF) { |
| 368 | // FORMAT_OFF means invalid string |
| 369 | return -1; |
| 370 | } |
| 371 | |
Mark Salyzyn | e1f2004 | 2015-05-06 08:40:40 -0700 | [diff] [blame] | 372 | return android_log_setPrintFormat(g_logformat, format); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 375 | static const char multipliers[][2] = { |
| 376 | { "" }, |
| 377 | { "K" }, |
| 378 | { "M" }, |
| 379 | { "G" } |
| 380 | }; |
| 381 | |
| 382 | static unsigned long value_of_size(unsigned long value) |
| 383 | { |
| 384 | for (unsigned i = 0; |
| 385 | (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024); |
| 386 | value /= 1024, ++i) ; |
| 387 | return value; |
| 388 | } |
| 389 | |
| 390 | static const char *multiplier_of_size(unsigned long value) |
| 391 | { |
| 392 | unsigned i; |
| 393 | for (i = 0; |
| 394 | (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024); |
| 395 | value /= 1024, ++i) ; |
| 396 | return multipliers[i]; |
| 397 | } |
| 398 | |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 399 | /*String to unsigned int, returns -1 if it fails*/ |
Mark Salyzyn | 33c2625 | 2016-04-12 09:11:46 -0700 | [diff] [blame^] | 400 | static bool getSizeTArg(const char *ptr, size_t *val, size_t min = 0, |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 401 | size_t max = SIZE_MAX) |
| 402 | { |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 403 | if (!ptr) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 404 | return false; |
| 405 | } |
| 406 | |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 407 | char *endp; |
| 408 | errno = 0; |
| 409 | size_t ret = (size_t)strtoll(ptr, &endp, 0); |
| 410 | |
| 411 | if (endp[0] || errno) { |
| 412 | return false; |
| 413 | } |
| 414 | |
| 415 | if ((ret > max) || (ret < min)) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 416 | return false; |
| 417 | } |
| 418 | |
| 419 | *val = ret; |
| 420 | return true; |
| 421 | } |
| 422 | |
| 423 | static void logcat_panic(bool showHelp, const char *fmt, ...) |
| 424 | { |
Mark Salyzyn | 77d7e81 | 2015-04-13 09:27:57 -0700 | [diff] [blame] | 425 | va_list args; |
| 426 | va_start(args, fmt); |
| 427 | vfprintf(stderr, fmt, args); |
| 428 | va_end(args); |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 429 | |
| 430 | if (showHelp) { |
| 431 | show_help(getprogname()); |
| 432 | } |
| 433 | |
| 434 | exit(EXIT_FAILURE); |
| 435 | } |
| 436 | |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 437 | static char *parseTime(log_time &t, const char *cp) { |
| 438 | |
| 439 | char *ep = t.strptime(cp, "%m-%d %H:%M:%S.%q"); |
| 440 | if (ep) { |
| 441 | return ep; |
| 442 | } |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 443 | ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q"); |
| 444 | if (ep) { |
| 445 | return ep; |
| 446 | } |
| 447 | return t.strptime(cp, "%s.%q"); |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 448 | } |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 449 | |
| 450 | // Find last logged line in gestalt of all matching existing output files |
| 451 | static log_time lastLogTime(char *outputFileName) { |
| 452 | log_time retval(log_time::EPOCH); |
| 453 | if (!outputFileName) { |
| 454 | return retval; |
| 455 | } |
| 456 | |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 457 | std::string directory; |
| 458 | char *file = strrchr(outputFileName, '/'); |
| 459 | if (!file) { |
| 460 | directory = "."; |
| 461 | file = outputFileName; |
| 462 | } else { |
| 463 | *file = '\0'; |
| 464 | directory = outputFileName; |
| 465 | *file = '/'; |
| 466 | ++file; |
| 467 | } |
Mark Salyzyn | c18c213 | 2016-04-01 07:52:20 -0700 | [diff] [blame] | 468 | |
| 469 | std::unique_ptr<DIR, int(*)(DIR*)> |
| 470 | dir(opendir(directory.c_str()), closedir); |
| 471 | if (!dir.get()) { |
| 472 | return retval; |
| 473 | } |
| 474 | |
| 475 | clockid_t clock_type = android_log_clockid(); |
| 476 | log_time now(clock_type); |
| 477 | bool monotonic = clock_type == CLOCK_MONOTONIC; |
| 478 | |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 479 | size_t len = strlen(file); |
| 480 | log_time modulo(0, NS_PER_SEC); |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 481 | struct dirent *dp; |
Mark Salyzyn | c18c213 | 2016-04-01 07:52:20 -0700 | [diff] [blame] | 482 | |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 483 | while ((dp = readdir(dir.get())) != NULL) { |
| 484 | if ((dp->d_type != DT_REG) |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 485 | // If we are using realtime, check all files that match the |
| 486 | // basename for latest time. If we are using monotonic time |
| 487 | // then only check the main file because time cycles on |
| 488 | // every reboot. |
| 489 | || strncmp(dp->d_name, file, len + monotonic) |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 490 | || (dp->d_name[len] |
| 491 | && ((dp->d_name[len] != '.') |
| 492 | || !isdigit(dp->d_name[len+1])))) { |
| 493 | continue; |
| 494 | } |
| 495 | |
| 496 | std::string file_name = directory; |
| 497 | file_name += "/"; |
| 498 | file_name += dp->d_name; |
| 499 | std::string file; |
| 500 | if (!android::base::ReadFileToString(file_name, &file)) { |
| 501 | continue; |
| 502 | } |
| 503 | |
| 504 | bool found = false; |
| 505 | for (const auto& line : android::base::Split(file, "\n")) { |
| 506 | log_time t(log_time::EPOCH); |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 507 | char *ep = parseTime(t, line.c_str()); |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 508 | if (!ep || (*ep != ' ')) { |
| 509 | continue; |
| 510 | } |
| 511 | // determine the time precision of the logs (eg: msec or usec) |
| 512 | for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) { |
| 513 | if (t.tv_nsec % (mod * 10)) { |
| 514 | modulo.tv_nsec = mod; |
| 515 | break; |
| 516 | } |
| 517 | } |
| 518 | // We filter any times later than current as we may not have the |
| 519 | // year stored with each log entry. Also, since it is possible for |
| 520 | // entries to be recorded out of order (very rare) we select the |
| 521 | // maximum we find just in case. |
| 522 | if ((t < now) && (t > retval)) { |
| 523 | retval = t; |
| 524 | found = true; |
| 525 | } |
| 526 | } |
| 527 | // We count on the basename file to be the definitive end, so stop here. |
| 528 | if (!dp->d_name[len] && found) { |
| 529 | break; |
| 530 | } |
| 531 | } |
| 532 | if (retval == log_time::EPOCH) { |
| 533 | return retval; |
| 534 | } |
| 535 | // tail_time prints matching or higher, round up by the modulo to prevent |
| 536 | // a replay of the last entry we have just checked. |
| 537 | retval += modulo; |
| 538 | return retval; |
| 539 | } |
| 540 | |
Mark Salyzyn | 33c2625 | 2016-04-12 09:11:46 -0700 | [diff] [blame^] | 541 | // Expand multiple flat property references ${<tag>:-default} or ${tag}. |
| 542 | // |
| 543 | // ToDo: Do we permit nesting? |
| 544 | // ${persist.logcat.something:-${ro.logcat.something:-maybesomething}} |
| 545 | // For now this will result in a syntax error for caller and is acceptable. |
| 546 | // |
| 547 | std::string expand(const char *str) |
| 548 | { |
| 549 | std::string retval(str); |
| 550 | |
| 551 | // Caller has no use for ${, } or :- as literals so no use for escape |
| 552 | // character. Result expectations are a number or a string, with validity |
| 553 | // checking for both in caller. Recursive expansion or other syntax errors |
| 554 | // will result in content caller can not obviously tolerate, error must |
| 555 | // report substring if applicable, expanded and original content (if |
| 556 | // different) so that it will be clear to user what they did wrong. |
| 557 | for (size_t pos; (pos = retval.find("${")) != std::string::npos; ) { |
| 558 | size_t epos = retval.find("}", pos + 2); |
| 559 | if (epos == std::string::npos) { |
| 560 | break; // Caller will error out, showing this unexpanded. |
| 561 | } |
| 562 | size_t def = retval.find(":-", pos + 2); |
| 563 | if (def >= epos) { |
| 564 | def = std::string::npos; |
| 565 | } |
| 566 | std::string default_value(""); |
| 567 | std::string key; |
| 568 | if (def == std::string::npos) { |
| 569 | key = retval.substr(pos + 2, epos - (pos + 2)); |
| 570 | } else { |
| 571 | key = retval.substr(pos + 2, def - (pos + 2)); |
| 572 | default_value = retval.substr(def + 2, epos - (def + 2)); |
| 573 | } |
| 574 | char value[PROPERTY_VALUE_MAX]; |
| 575 | property_get(key.c_str(), value, default_value.c_str()); |
| 576 | // Caller will error out, syntactically empty content at this point |
| 577 | // will not be tolerated as expected. |
| 578 | retval.replace(pos, epos - pos + 1, value); |
| 579 | } |
| 580 | |
| 581 | return retval; |
| 582 | } |
| 583 | |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 584 | } /* namespace android */ |
| 585 | |
| 586 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 587 | int main(int argc, char **argv) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 588 | { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 589 | using namespace android; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 590 | int err; |
| 591 | int hasSetLogFormat = 0; |
| 592 | int clearLog = 0; |
| 593 | int getLogSize = 0; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 594 | unsigned long setLogSize = 0; |
| 595 | int getPruneList = 0; |
| 596 | char *setPruneList = NULL; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 597 | int printStatistics = 0; |
Mark Salyzyn | 2d3f38a | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 598 | int mode = ANDROID_LOG_RDONLY; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 599 | const char *forceFilters = NULL; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 600 | log_device_t* devices = NULL; |
| 601 | log_device_t* dev; |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 602 | bool printDividers = false; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 603 | struct logger_list *logger_list; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 604 | size_t tail_lines = 0; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 605 | log_time tail_time(log_time::EPOCH); |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 606 | size_t pid = 0; |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 607 | bool got_t = false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 608 | |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 609 | signal(SIGPIPE, exit); |
| 610 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 611 | g_logformat = android_log_format_new(); |
| 612 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 613 | if (argc == 2 && 0 == strcmp(argv[1], "--help")) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 614 | show_help(argv[0]); |
| 615 | return EXIT_SUCCESS; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | for (;;) { |
| 619 | int ret; |
| 620 | |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 621 | int option_index = 0; |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 622 | // list of long-argument only strings for later comparison |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 623 | static const char pid_str[] = "pid"; |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 624 | static const char wrap_str[] = "wrap"; |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 625 | static const char print_str[] = "print"; |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 626 | static const struct option long_options[] = { |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 627 | { "binary", no_argument, NULL, 'B' }, |
| 628 | { "buffer", required_argument, NULL, 'b' }, |
Mark Salyzyn | d85f646 | 2016-03-30 09:15:09 -0700 | [diff] [blame] | 629 | { "buffer-size", optional_argument, NULL, 'g' }, |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 630 | { "clear", no_argument, NULL, 'c' }, |
| 631 | { "dividers", no_argument, NULL, 'D' }, |
| 632 | { "file", required_argument, NULL, 'f' }, |
| 633 | { "format", required_argument, NULL, 'v' }, |
Mark Salyzyn | c18c213 | 2016-04-01 07:52:20 -0700 | [diff] [blame] | 634 | // hidden and undocumented reserved alias for --regex |
| 635 | { "grep", required_argument, NULL, 'e' }, |
Mark Salyzyn | d85f646 | 2016-03-30 09:15:09 -0700 | [diff] [blame] | 636 | // hidden and undocumented reserved alias for --max-count |
| 637 | { "head", required_argument, NULL, 'm' }, |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 638 | { "last", no_argument, NULL, 'L' }, |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 639 | { "max-count", required_argument, NULL, 'm' }, |
Mark Salyzyn | c18c213 | 2016-04-01 07:52:20 -0700 | [diff] [blame] | 640 | { pid_str, required_argument, NULL, 0 }, |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 641 | { print_str, no_argument, NULL, 0 }, |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 642 | { "prune", optional_argument, NULL, 'p' }, |
Casey Dahlin | dc42a87 | 2016-03-17 16:18:55 -0700 | [diff] [blame] | 643 | { "regex", required_argument, NULL, 'e' }, |
Mark Salyzyn | d85f646 | 2016-03-30 09:15:09 -0700 | [diff] [blame] | 644 | { "rotate-count", required_argument, NULL, 'n' }, |
| 645 | { "rotate-kbytes", required_argument, NULL, 'r' }, |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 646 | { "statistics", no_argument, NULL, 'S' }, |
Mark Salyzyn | d85f646 | 2016-03-30 09:15:09 -0700 | [diff] [blame] | 647 | // hidden and undocumented reserved alias for -t |
| 648 | { "tail", required_argument, NULL, 't' }, |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 649 | // support, but ignore and do not document, the optional argument |
| 650 | { wrap_str, optional_argument, NULL, 0 }, |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 651 | { NULL, 0, NULL, 0 } |
| 652 | }; |
| 653 | |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 654 | ret = getopt_long(argc, argv, ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:m:e:", |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 655 | long_options, &option_index); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 656 | |
| 657 | if (ret < 0) { |
| 658 | break; |
| 659 | } |
| 660 | |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 661 | switch (ret) { |
| 662 | case 0: |
| 663 | // One of the long options |
| 664 | if (long_options[option_index].name == pid_str) { |
| 665 | // ToDo: determine runtime PID_MAX? |
| 666 | if (!getSizeTArg(optarg, &pid, 1)) { |
| 667 | logcat_panic(true, "%s %s out of range\n", |
| 668 | long_options[option_index].name, optarg); |
| 669 | } |
| 670 | break; |
| 671 | } |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 672 | if (long_options[option_index].name == wrap_str) { |
| 673 | mode |= ANDROID_LOG_WRAP | |
| 674 | ANDROID_LOG_RDONLY | |
| 675 | ANDROID_LOG_NONBLOCK; |
| 676 | // ToDo: implement API that supports setting a wrap timeout |
| 677 | size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT; |
| 678 | if (optarg && !getSizeTArg(optarg, &dummy, 1)) { |
| 679 | logcat_panic(true, "%s %s out of range\n", |
| 680 | long_options[option_index].name, optarg); |
| 681 | } |
| 682 | if (dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) { |
| 683 | fprintf(stderr, |
| 684 | "WARNING: %s %u seconds, ignoring %zu\n", |
| 685 | long_options[option_index].name, |
| 686 | ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy); |
| 687 | } |
| 688 | break; |
| 689 | } |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 690 | if (long_options[option_index].name == print_str) { |
| 691 | g_printItAnyways = true; |
| 692 | break; |
| 693 | } |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 694 | break; |
| 695 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 696 | case 's': |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 697 | // default to all silent |
| 698 | android_log_addFilterRule(g_logformat, "*:s"); |
| 699 | break; |
| 700 | |
| 701 | case 'c': |
| 702 | clearLog = 1; |
Mark Salyzyn | 2d3f38a | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 703 | mode |= ANDROID_LOG_WRONLY; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 704 | break; |
| 705 | |
Mark Salyzyn | 7c975ac | 2014-12-15 10:01:31 -0800 | [diff] [blame] | 706 | case 'L': |
| 707 | mode |= ANDROID_LOG_PSTORE; |
| 708 | break; |
| 709 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 710 | case 'd': |
Mark Salyzyn | 2d3f38a | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 711 | mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 712 | break; |
| 713 | |
Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 714 | case 't': |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 715 | got_t = true; |
Mark Salyzyn | 2d3f38a | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 716 | mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK; |
Mark Salyzyn | 5d3d1f1 | 2013-12-09 13:47:00 -0800 | [diff] [blame] | 717 | /* FALLTHRU */ |
| 718 | case 'T': |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 719 | if (strspn(optarg, "0123456789") != strlen(optarg)) { |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 720 | char *cp = parseTime(tail_time, optarg); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 721 | if (!cp) { |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 722 | logcat_panic(false, "-%c \"%s\" not in time format\n", |
| 723 | ret, optarg); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 724 | } |
| 725 | if (*cp) { |
| 726 | char c = *cp; |
| 727 | *cp = '\0'; |
| 728 | fprintf(stderr, |
| 729 | "WARNING: -%c \"%s\"\"%c%s\" time truncated\n", |
| 730 | ret, optarg, c, cp + 1); |
| 731 | *cp = c; |
| 732 | } |
| 733 | } else { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 734 | if (!getSizeTArg(optarg, &tail_lines, 1)) { |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 735 | fprintf(stderr, |
| 736 | "WARNING: -%c %s invalid, setting to 1\n", |
| 737 | ret, optarg); |
| 738 | tail_lines = 1; |
| 739 | } |
| 740 | } |
Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 741 | break; |
| 742 | |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 743 | case 'D': |
| 744 | printDividers = true; |
| 745 | break; |
| 746 | |
Casey Dahlin | dc42a87 | 2016-03-17 16:18:55 -0700 | [diff] [blame] | 747 | case 'e': |
| 748 | g_regex = new pcrecpp::RE(optarg); |
| 749 | break; |
| 750 | |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 751 | case 'm': { |
| 752 | char *end = NULL; |
| 753 | if (!getSizeTArg(optarg, &g_maxCount)) { |
| 754 | logcat_panic(false, "-%c \"%s\" isn't an " |
| 755 | "integer greater than zero\n", ret, optarg); |
| 756 | } |
| 757 | } |
| 758 | break; |
| 759 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 760 | case 'g': |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 761 | if (!optarg) { |
| 762 | getLogSize = 1; |
| 763 | break; |
| 764 | } |
| 765 | // FALLTHRU |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 766 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 767 | case 'G': { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 768 | char *cp; |
| 769 | if (strtoll(optarg, &cp, 0) > 0) { |
| 770 | setLogSize = strtoll(optarg, &cp, 0); |
| 771 | } else { |
| 772 | setLogSize = 0; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | switch(*cp) { |
| 776 | case 'g': |
| 777 | case 'G': |
| 778 | setLogSize *= 1024; |
| 779 | /* FALLTHRU */ |
| 780 | case 'm': |
| 781 | case 'M': |
| 782 | setLogSize *= 1024; |
| 783 | /* FALLTHRU */ |
| 784 | case 'k': |
| 785 | case 'K': |
| 786 | setLogSize *= 1024; |
| 787 | /* FALLTHRU */ |
| 788 | case '\0': |
| 789 | break; |
| 790 | |
| 791 | default: |
| 792 | setLogSize = 0; |
| 793 | } |
| 794 | |
| 795 | if (!setLogSize) { |
| 796 | fprintf(stderr, "ERROR: -G <num><multiplier>\n"); |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 797 | return EXIT_FAILURE; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | break; |
| 801 | |
| 802 | case 'p': |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 803 | if (!optarg) { |
| 804 | getPruneList = 1; |
| 805 | break; |
| 806 | } |
| 807 | // FALLTHRU |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 808 | |
| 809 | case 'P': |
| 810 | setPruneList = optarg; |
| 811 | break; |
| 812 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 813 | case 'b': { |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 814 | unsigned idMask = 0; |
Mark Salyzyn | 33c2625 | 2016-04-12 09:11:46 -0700 | [diff] [blame^] | 815 | std::string expanded = expand(optarg); |
| 816 | std::istringstream copy(expanded); |
| 817 | std::string token; |
| 818 | // wish for strtok and ",:; \t\n\r\f" for hidden flexibility |
| 819 | while (std::getline(copy, token, ',')) { // settle for "," |
| 820 | if (token.compare("default") == 0) { |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 821 | idMask |= (1 << LOG_ID_MAIN) | |
| 822 | (1 << LOG_ID_SYSTEM) | |
| 823 | (1 << LOG_ID_CRASH); |
Mark Salyzyn | 33c2625 | 2016-04-12 09:11:46 -0700 | [diff] [blame^] | 824 | } else if (token.compare("all") == 0) { |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 825 | idMask = (unsigned)-1; |
| 826 | } else { |
Mark Salyzyn | 33c2625 | 2016-04-12 09:11:46 -0700 | [diff] [blame^] | 827 | log_id_t log_id = android_name_to_log_id(token.c_str()); |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 828 | const char *name = android_log_id_to_name(log_id); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 829 | |
Mark Salyzyn | 33c2625 | 2016-04-12 09:11:46 -0700 | [diff] [blame^] | 830 | if (token.compare(name) != 0) { |
| 831 | bool strDifferent = expanded.compare(token); |
| 832 | if (expanded.compare(optarg)) { |
| 833 | expanded += " expanded from "; |
| 834 | expanded += optarg; |
| 835 | } |
| 836 | if (strDifferent) { |
| 837 | expanded = token + " within " + expanded; |
| 838 | } |
| 839 | logcat_panic(true, "unknown buffer -b %s\n", |
| 840 | expanded.c_str()); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 841 | } |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 842 | idMask |= (1 << log_id); |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 843 | } |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 844 | } |
| 845 | |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 846 | for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) { |
| 847 | const char *name = android_log_id_to_name((log_id_t)i); |
| 848 | log_id_t log_id = android_name_to_log_id(name); |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 849 | |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 850 | if (log_id != (log_id_t)i) { |
| 851 | continue; |
| 852 | } |
| 853 | if ((idMask & (1 << i)) == 0) { |
| 854 | continue; |
| 855 | } |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 856 | |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 857 | bool found = false; |
| 858 | for (dev = devices; dev; dev = dev->next) { |
| 859 | if (!strcmp(name, dev->device)) { |
| 860 | found = true; |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 861 | break; |
| 862 | } |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 863 | if (!dev->next) { |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 864 | break; |
| 865 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 866 | } |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 867 | if (found) { |
| 868 | continue; |
| 869 | } |
| 870 | |
| 871 | bool binary = !strcmp(name, "events") || |
| 872 | !strcmp(name, "security"); |
| 873 | log_device_t* d = new log_device_t(name, binary); |
| 874 | |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 875 | if (dev) { |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 876 | dev->next = d; |
| 877 | dev = d; |
| 878 | } else { |
| 879 | devices = dev = d; |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 880 | } |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 881 | g_devCount++; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 882 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 883 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 884 | break; |
| 885 | |
| 886 | case 'B': |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 887 | g_printBinary = 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 888 | break; |
| 889 | |
| 890 | case 'f': |
Mark Salyzyn | 9812fc4 | 2015-10-06 08:59:02 -0700 | [diff] [blame] | 891 | if ((tail_time == log_time::EPOCH) && (tail_lines == 0)) { |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 892 | tail_time = lastLogTime(optarg); |
| 893 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 894 | // redirect output to a file |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 895 | g_outputFileName = optarg; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 896 | break; |
| 897 | |
Mark Salyzyn | 33c2625 | 2016-04-12 09:11:46 -0700 | [diff] [blame^] | 898 | case 'r': { |
| 899 | std::string expanded = expand(optarg); |
| 900 | if (!getSizeTArg(expanded.c_str(), &g_logRotateSizeKBytes, 1)) { |
| 901 | if (expanded.compare(optarg)) { |
| 902 | expanded += " expanded from "; |
| 903 | expanded += optarg; |
| 904 | } |
| 905 | logcat_panic(true, "Invalid parameter -r %s\n", |
| 906 | expanded.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 907 | } |
Mark Salyzyn | 33c2625 | 2016-04-12 09:11:46 -0700 | [diff] [blame^] | 908 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 909 | break; |
| 910 | |
Mark Salyzyn | 33c2625 | 2016-04-12 09:11:46 -0700 | [diff] [blame^] | 911 | case 'n': { |
| 912 | std::string expanded = expand(optarg); |
| 913 | if (!getSizeTArg(expanded.c_str(), &g_maxRotatedLogs, 1)) { |
| 914 | if (expanded.compare(optarg)) { |
| 915 | expanded += " expanded from "; |
| 916 | expanded += optarg; |
| 917 | } |
| 918 | logcat_panic(true, "Invalid parameter -n %s\n", |
| 919 | expanded.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 920 | } |
Mark Salyzyn | 33c2625 | 2016-04-12 09:11:46 -0700 | [diff] [blame^] | 921 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 922 | break; |
| 923 | |
| 924 | case 'v': |
| 925 | err = setLogFormat (optarg); |
| 926 | if (err < 0) { |
Mark Salyzyn | 33c2625 | 2016-04-12 09:11:46 -0700 | [diff] [blame^] | 927 | logcat_panic(true, "Invalid parameter -v %s\n", optarg); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 928 | } |
Mark Salyzyn | e1f2004 | 2015-05-06 08:40:40 -0700 | [diff] [blame] | 929 | hasSetLogFormat |= err; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 930 | break; |
| 931 | |
| 932 | case 'Q': |
| 933 | /* this is a *hidden* option used to start a version of logcat */ |
| 934 | /* in an emulated device only. it basically looks for androidboot.logcat= */ |
| 935 | /* on the kernel command line. If something is found, it extracts a log filter */ |
| 936 | /* and uses it to run the program. If nothing is found, the program should */ |
| 937 | /* quit immediately */ |
| 938 | #define KERNEL_OPTION "androidboot.logcat=" |
| 939 | #define CONSOLE_OPTION "androidboot.console=" |
| 940 | { |
| 941 | int fd; |
| 942 | char* logcat; |
| 943 | char* console; |
| 944 | int force_exit = 1; |
| 945 | static char cmdline[1024]; |
| 946 | |
| 947 | fd = open("/proc/cmdline", O_RDONLY); |
| 948 | if (fd >= 0) { |
| 949 | int n = read(fd, cmdline, sizeof(cmdline)-1 ); |
| 950 | if (n < 0) n = 0; |
| 951 | cmdline[n] = 0; |
| 952 | close(fd); |
| 953 | } else { |
| 954 | cmdline[0] = 0; |
| 955 | } |
| 956 | |
| 957 | logcat = strstr( cmdline, KERNEL_OPTION ); |
| 958 | console = strstr( cmdline, CONSOLE_OPTION ); |
| 959 | if (logcat != NULL) { |
| 960 | char* p = logcat + sizeof(KERNEL_OPTION)-1;; |
| 961 | char* q = strpbrk( p, " \t\n\r" );; |
| 962 | |
| 963 | if (q != NULL) |
| 964 | *q = 0; |
| 965 | |
| 966 | forceFilters = p; |
| 967 | force_exit = 0; |
| 968 | } |
| 969 | /* if nothing found or invalid filters, exit quietly */ |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 970 | if (force_exit) { |
| 971 | return EXIT_SUCCESS; |
| 972 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 973 | |
| 974 | /* redirect our output to the emulator console */ |
| 975 | if (console) { |
| 976 | char* p = console + sizeof(CONSOLE_OPTION)-1; |
| 977 | char* q = strpbrk( p, " \t\n\r" ); |
| 978 | char devname[64]; |
| 979 | int len; |
| 980 | |
| 981 | if (q != NULL) { |
| 982 | len = q - p; |
| 983 | } else |
| 984 | len = strlen(p); |
| 985 | |
| 986 | len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p ); |
| 987 | fprintf(stderr, "logcat using %s (%d)\n", devname, len); |
| 988 | if (len < (int)sizeof(devname)) { |
| 989 | fd = open( devname, O_WRONLY ); |
| 990 | if (fd >= 0) { |
| 991 | dup2(fd, 1); |
| 992 | dup2(fd, 2); |
| 993 | close(fd); |
| 994 | } |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | break; |
| 999 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1000 | case 'S': |
| 1001 | printStatistics = 1; |
| 1002 | break; |
| 1003 | |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1004 | case ':': |
| 1005 | logcat_panic(true, "Option -%c needs an argument\n", optopt); |
| 1006 | break; |
| 1007 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1008 | default: |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1009 | logcat_panic(true, "Unrecognized Option %c\n", optopt); |
| 1010 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 1014 | if (g_maxCount && got_t) { |
Mark Salyzyn | aa730c1 | 2016-03-30 12:38:29 -0700 | [diff] [blame] | 1015 | logcat_panic(true, "Cannot use -m (--max-count) and -t together\n"); |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 1016 | } |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 1017 | if (g_printItAnyways && (!g_regex || !g_maxCount)) { |
| 1018 | // One day it would be nice if --print -v color and --regex <expr> |
| 1019 | // could play with each other and show regex highlighted content. |
| 1020 | fprintf(stderr, "WARNING: " |
| 1021 | "--print ignored, to be used in combination with\n" |
| 1022 | " " |
| 1023 | "--regex <expr> and --max-count <N>\n"); |
| 1024 | g_printItAnyways = false; |
| 1025 | } |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 1026 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1027 | if (!devices) { |
Mark Salyzyn | 5f6738a | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 1028 | dev = devices = new log_device_t("main", false); |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1029 | g_devCount = 1; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1030 | if (android_name_to_log_id("system") == LOG_ID_SYSTEM) { |
Mark Salyzyn | 5f6738a | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 1031 | dev = dev->next = new log_device_t("system", false); |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1032 | g_devCount++; |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 1033 | } |
Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 1034 | if (android_name_to_log_id("crash") == LOG_ID_CRASH) { |
Mark Salyzyn | 5f6738a | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 1035 | dev = dev->next = new log_device_t("crash", false); |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1036 | g_devCount++; |
Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 1037 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1038 | } |
| 1039 | |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1040 | if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) { |
| 1041 | logcat_panic(true, "-r requires -f as well\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1042 | } |
| 1043 | |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1044 | setupOutput(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1045 | |
| 1046 | if (hasSetLogFormat == 0) { |
| 1047 | const char* logFormat = getenv("ANDROID_PRINTF_LOG"); |
| 1048 | |
| 1049 | if (logFormat != NULL) { |
| 1050 | err = setLogFormat(logFormat); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1051 | if (err < 0) { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1052 | 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] | 1053 | logFormat); |
| 1054 | } |
Mark Salyzyn | 649fc60 | 2014-09-16 09:15:15 -0700 | [diff] [blame] | 1055 | } else { |
| 1056 | setLogFormat("threadtime"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | if (forceFilters) { |
| 1061 | err = android_log_addFilterString(g_logformat, forceFilters); |
| 1062 | if (err < 0) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1063 | 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] | 1064 | } |
| 1065 | } else if (argc == optind) { |
| 1066 | // Add from environment variable |
| 1067 | char *env_tags_orig = getenv("ANDROID_LOG_TAGS"); |
| 1068 | |
| 1069 | if (env_tags_orig != NULL) { |
| 1070 | err = android_log_addFilterString(g_logformat, env_tags_orig); |
| 1071 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1072 | if (err < 0) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1073 | logcat_panic(true, |
| 1074 | "Invalid filter expression in ANDROID_LOG_TAGS\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | } else { |
| 1078 | // Add from commandline |
| 1079 | for (int i = optind ; i < argc ; i++) { |
| 1080 | err = android_log_addFilterString(g_logformat, argv[i]); |
| 1081 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1082 | if (err < 0) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1083 | 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] | 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1088 | dev = devices; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 1089 | if (tail_time != log_time::EPOCH) { |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 1090 | logger_list = android_logger_list_alloc_time(mode, tail_time, pid); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 1091 | } else { |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 1092 | logger_list = android_logger_list_alloc(mode, tail_lines, pid); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 1093 | } |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1094 | const char *openDeviceFail = NULL; |
| 1095 | const char *clearFail = NULL; |
| 1096 | const char *setSizeFail = NULL; |
| 1097 | const char *getSizeFail = NULL; |
| 1098 | // We have three orthogonal actions below to clear, set log size and |
| 1099 | // get log size. All sharing the same iteration loop. |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1100 | while (dev) { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1101 | dev->logger_list = logger_list; |
| 1102 | dev->logger = android_logger_open(logger_list, |
| 1103 | android_name_to_log_id(dev->device)); |
| 1104 | if (!dev->logger) { |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1105 | openDeviceFail = openDeviceFail ?: dev->device; |
| 1106 | dev = dev->next; |
| 1107 | continue; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1108 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1109 | |
| 1110 | if (clearLog) { |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1111 | if (android_logger_clear(dev->logger)) { |
| 1112 | clearFail = clearFail ?: dev->device; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1113 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1114 | } |
| 1115 | |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1116 | if (setLogSize) { |
| 1117 | if (android_logger_set_log_size(dev->logger, setLogSize)) { |
| 1118 | setSizeFail = setSizeFail ?: dev->device; |
| 1119 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1120 | } |
| 1121 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1122 | if (getLogSize) { |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1123 | long size = android_logger_get_log_size(dev->logger); |
| 1124 | long readable = android_logger_get_log_readable_size(dev->logger); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1125 | |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1126 | if ((size < 0) || (readable < 0)) { |
| 1127 | getSizeFail = getSizeFail ?: dev->device; |
| 1128 | } else { |
| 1129 | printf("%s: ring buffer is %ld%sb (%ld%sb consumed), " |
| 1130 | "max entry is %db, max payload is %db\n", dev->device, |
| 1131 | value_of_size(size), multiplier_of_size(size), |
| 1132 | value_of_size(readable), multiplier_of_size(readable), |
| 1133 | (int) LOGGER_ENTRY_MAX_LEN, |
| 1134 | (int) LOGGER_ENTRY_MAX_PAYLOAD); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1135 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | dev = dev->next; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1139 | } |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1140 | // report any errors in the above loop and exit |
| 1141 | if (openDeviceFail) { |
| 1142 | logcat_panic(false, "Unable to open log device '%s'\n", openDeviceFail); |
| 1143 | } |
| 1144 | if (clearFail) { |
| 1145 | logcat_panic(false, "failed to clear the '%s' log\n", clearFail); |
| 1146 | } |
| 1147 | if (setSizeFail) { |
| 1148 | logcat_panic(false, "failed to set the '%s' log size\n", setSizeFail); |
| 1149 | } |
| 1150 | if (getSizeFail) { |
| 1151 | logcat_panic(false, "failed to get the readable '%s' log size", |
| 1152 | getSizeFail); |
| 1153 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1154 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1155 | if (setPruneList) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1156 | size_t len = strlen(setPruneList); |
| 1157 | /*extra 32 bytes are needed by android_logger_set_prune_list */ |
| 1158 | size_t bLen = len + 32; |
| 1159 | char *buf = NULL; |
| 1160 | if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) { |
| 1161 | buf[len] = '\0'; |
| 1162 | if (android_logger_set_prune_list(logger_list, buf, bLen)) { |
| 1163 | logcat_panic(false, "failed to set the prune list"); |
| 1164 | } |
| 1165 | free(buf); |
| 1166 | } else { |
| 1167 | logcat_panic(false, "failed to set the prune list (alloc)"); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 1171 | if (printStatistics || getPruneList) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1172 | size_t len = 8192; |
| 1173 | char *buf; |
| 1174 | |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 1175 | for (int retry = 32; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1176 | (retry >= 0) && ((buf = new char [len])); |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1177 | delete [] buf, buf = NULL, --retry) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1178 | if (getPruneList) { |
| 1179 | android_logger_get_prune_list(logger_list, buf, len); |
| 1180 | } else { |
| 1181 | android_logger_get_statistics(logger_list, buf, len); |
| 1182 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1183 | buf[len-1] = '\0'; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1184 | if (atol(buf) < 3) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1185 | delete [] buf; |
| 1186 | buf = NULL; |
| 1187 | break; |
| 1188 | } |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1189 | size_t ret = atol(buf) + 1; |
| 1190 | if (ret <= len) { |
| 1191 | len = ret; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1192 | break; |
| 1193 | } |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1194 | len = ret; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | if (!buf) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1198 | logcat_panic(false, "failed to read data"); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | // remove trailing FF |
| 1202 | char *cp = buf + len - 1; |
| 1203 | *cp = '\0'; |
| 1204 | bool truncated = *--cp != '\f'; |
| 1205 | if (!truncated) { |
| 1206 | *cp = '\0'; |
| 1207 | } |
| 1208 | |
| 1209 | // squash out the byte count |
| 1210 | cp = buf; |
| 1211 | if (!truncated) { |
Mark Salyzyn | 22e287d | 2014-03-21 13:12:16 -0700 | [diff] [blame] | 1212 | while (isdigit(*cp)) { |
| 1213 | ++cp; |
| 1214 | } |
| 1215 | if (*cp == '\n') { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1216 | ++cp; |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | printf("%s", cp); |
| 1221 | delete [] buf; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1222 | return EXIT_SUCCESS; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1226 | if (getLogSize) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1227 | return EXIT_SUCCESS; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1228 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1229 | if (setLogSize || setPruneList) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1230 | return EXIT_SUCCESS; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1231 | } |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 1232 | if (clearLog) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1233 | return EXIT_SUCCESS; |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 1234 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1235 | |
| 1236 | //LOG_EVENT_INT(10, 12345); |
| 1237 | //LOG_EVENT_LONG(11, 0x1122334455667788LL); |
| 1238 | //LOG_EVENT_STRING(0, "whassup, doc?"); |
| 1239 | |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1240 | dev = NULL; |
Mark Salyzyn | 5f6738a | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 1241 | log_device_t unexpected("unexpected", false); |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 1242 | |
Mark Salyzyn | aa730c1 | 2016-03-30 12:38:29 -0700 | [diff] [blame] | 1243 | while (!g_maxCount || (g_printCount < g_maxCount)) { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1244 | struct log_msg log_msg; |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1245 | log_device_t* d; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1246 | int ret = android_logger_list_read(logger_list, &log_msg); |
| 1247 | |
| 1248 | if (ret == 0) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1249 | logcat_panic(false, "read: unexpected EOF!\n"); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | if (ret < 0) { |
| 1253 | if (ret == -EAGAIN) { |
| 1254 | break; |
| 1255 | } |
| 1256 | |
| 1257 | if (ret == -EIO) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1258 | logcat_panic(false, "read: unexpected EOF!\n"); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1259 | } |
| 1260 | if (ret == -EINVAL) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1261 | logcat_panic(false, "read: unexpected length.\n"); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1262 | } |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1263 | logcat_panic(false, "logcat read failure"); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1264 | } |
| 1265 | |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 1266 | for (d = devices; d; d = d->next) { |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1267 | if (android_name_to_log_id(d->device) == log_msg.id()) { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1268 | break; |
| 1269 | } |
| 1270 | } |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1271 | if (!d) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1272 | g_devCount = 2; // set to Multiple |
Mark Salyzyn | 9421b0c | 2015-02-26 14:33:35 -0800 | [diff] [blame] | 1273 | d = &unexpected; |
| 1274 | d->binary = log_msg.id() == LOG_ID_EVENTS; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1275 | } |
| 1276 | |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1277 | if (dev != d) { |
| 1278 | dev = d; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1279 | maybePrintStart(dev, printDividers); |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1280 | } |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1281 | if (g_printBinary) { |
| 1282 | printBinary(&log_msg); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1283 | } else { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1284 | processBuffer(dev, &log_msg); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | android_logger_list_free(logger_list); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1289 | |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1290 | return EXIT_SUCCESS; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1291 | } |