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