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