| 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 |  | 
| Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 584 | void reportErrorName(const char **current, | 
|  | 585 | const char* name, | 
|  | 586 | bool blockSecurity) { | 
|  | 587 | if (*current) { | 
|  | 588 | return; | 
|  | 589 | } | 
|  | 590 | if (blockSecurity && (android_name_to_log_id(name) == LOG_ID_SECURITY)) { | 
|  | 591 | return; | 
|  | 592 | } | 
|  | 593 | *current = name; | 
|  | 594 | } | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 595 |  | 
| Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 596 | int main(int argc, char **argv) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 597 | { | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 598 | using namespace android; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 599 | int err; | 
|  | 600 | int hasSetLogFormat = 0; | 
| Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 601 | bool clearLog = false; | 
|  | 602 | bool allSelected = false; | 
|  | 603 | bool getLogSize = false; | 
|  | 604 | bool getPruneList = false; | 
|  | 605 | bool printStatistics = false; | 
|  | 606 | bool printDividers = false; | 
| Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 607 | unsigned long setLogSize = 0; | 
| Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 608 | char *setPruneList = NULL; | 
| Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 609 | char *setId = NULL; | 
| Mark Salyzyn | 2d3f38a | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 610 | int mode = ANDROID_LOG_RDONLY; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 611 | const char *forceFilters = NULL; | 
| Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 612 | log_device_t* devices = NULL; | 
|  | 613 | log_device_t* dev; | 
| Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 614 | struct logger_list *logger_list; | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 615 | size_t tail_lines = 0; | 
| Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 616 | log_time tail_time(log_time::EPOCH); | 
| Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 617 | size_t pid = 0; | 
| Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 618 | bool got_t = false; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 619 |  | 
| Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 620 | signal(SIGPIPE, exit); | 
|  | 621 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 622 | g_logformat = android_log_format_new(); | 
|  | 623 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 624 | if (argc == 2 && 0 == strcmp(argv[1], "--help")) { | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 625 | show_help(argv[0]); | 
|  | 626 | return EXIT_SUCCESS; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 627 | } | 
|  | 628 |  | 
|  | 629 | for (;;) { | 
|  | 630 | int ret; | 
|  | 631 |  | 
| Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 632 | int option_index = 0; | 
| Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 633 | // list of long-argument only strings for later comparison | 
| Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 634 | static const char pid_str[] = "pid"; | 
| Mark Salyzyn | 538bc12 | 2016-11-16 15:28:31 -0800 | [diff] [blame] | 635 | static const char debug_str[] = "debug"; | 
| Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 636 | static const char id_str[] = "id"; | 
| Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 637 | static const char wrap_str[] = "wrap"; | 
| Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 638 | static const char print_str[] = "print"; | 
| Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 639 | static const struct option long_options[] = { | 
| Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 640 | { "binary",        no_argument,       NULL,   'B' }, | 
|  | 641 | { "buffer",        required_argument, NULL,   'b' }, | 
| Mark Salyzyn | d85f646 | 2016-03-30 09:15:09 -0700 | [diff] [blame] | 642 | { "buffer-size",   optional_argument, NULL,   'g' }, | 
| Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 643 | { "clear",         no_argument,       NULL,   'c' }, | 
| Mark Salyzyn | 538bc12 | 2016-11-16 15:28:31 -0800 | [diff] [blame] | 644 | { debug_str,       no_argument,       NULL,   0 }, | 
| Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 645 | { "dividers",      no_argument,       NULL,   'D' }, | 
|  | 646 | { "file",          required_argument, NULL,   'f' }, | 
|  | 647 | { "format",        required_argument, NULL,   'v' }, | 
| Mark Salyzyn | c18c213 | 2016-04-01 07:52:20 -0700 | [diff] [blame] | 648 | // hidden and undocumented reserved alias for --regex | 
|  | 649 | { "grep",          required_argument, NULL,   'e' }, | 
| Mark Salyzyn | d85f646 | 2016-03-30 09:15:09 -0700 | [diff] [blame] | 650 | // hidden and undocumented reserved alias for --max-count | 
|  | 651 | { "head",          required_argument, NULL,   'm' }, | 
| Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 652 | { id_str,          required_argument, NULL,   0 }, | 
| Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 653 | { "last",          no_argument,       NULL,   'L' }, | 
| Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 654 | { "max-count",     required_argument, NULL,   'm' }, | 
| Mark Salyzyn | c18c213 | 2016-04-01 07:52:20 -0700 | [diff] [blame] | 655 | { pid_str,         required_argument, NULL,   0 }, | 
| Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 656 | { print_str,       no_argument,       NULL,   0 }, | 
| Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 657 | { "prune",         optional_argument, NULL,   'p' }, | 
| Casey Dahlin | dc42a87 | 2016-03-17 16:18:55 -0700 | [diff] [blame] | 658 | { "regex",         required_argument, NULL,   'e' }, | 
| Mark Salyzyn | d85f646 | 2016-03-30 09:15:09 -0700 | [diff] [blame] | 659 | { "rotate-count",  required_argument, NULL,   'n' }, | 
|  | 660 | { "rotate-kbytes", required_argument, NULL,   'r' }, | 
| Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 661 | { "statistics",    no_argument,       NULL,   'S' }, | 
| Mark Salyzyn | d85f646 | 2016-03-30 09:15:09 -0700 | [diff] [blame] | 662 | // hidden and undocumented reserved alias for -t | 
|  | 663 | { "tail",          required_argument, NULL,   't' }, | 
| Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 664 | // support, but ignore and do not document, the optional argument | 
|  | 665 | { wrap_str,        optional_argument, NULL,   0 }, | 
| Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 666 | { NULL,            0,                 NULL,   0 } | 
|  | 667 | }; | 
|  | 668 |  | 
| Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 669 | 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] | 670 | long_options, &option_index); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 671 |  | 
|  | 672 | if (ret < 0) { | 
|  | 673 | break; | 
|  | 674 | } | 
|  | 675 |  | 
| Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 676 | switch (ret) { | 
|  | 677 | case 0: | 
| Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 678 | // only long options | 
| Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 679 | if (long_options[option_index].name == pid_str) { | 
|  | 680 | // ToDo: determine runtime PID_MAX? | 
|  | 681 | if (!getSizeTArg(optarg, &pid, 1)) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 682 | logcat_panic(HELP_TRUE, "%s %s out of range\n", | 
| Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 683 | long_options[option_index].name, optarg); | 
|  | 684 | } | 
|  | 685 | break; | 
|  | 686 | } | 
| Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 687 | if (long_options[option_index].name == wrap_str) { | 
|  | 688 | mode |= ANDROID_LOG_WRAP | | 
|  | 689 | ANDROID_LOG_RDONLY | | 
|  | 690 | ANDROID_LOG_NONBLOCK; | 
|  | 691 | // ToDo: implement API that supports setting a wrap timeout | 
|  | 692 | size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT; | 
|  | 693 | if (optarg && !getSizeTArg(optarg, &dummy, 1)) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 694 | logcat_panic(HELP_TRUE, "%s %s out of range\n", | 
| Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 695 | long_options[option_index].name, optarg); | 
|  | 696 | } | 
|  | 697 | if (dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) { | 
|  | 698 | fprintf(stderr, | 
|  | 699 | "WARNING: %s %u seconds, ignoring %zu\n", | 
|  | 700 | long_options[option_index].name, | 
|  | 701 | ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy); | 
|  | 702 | } | 
|  | 703 | break; | 
|  | 704 | } | 
| Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 705 | if (long_options[option_index].name == print_str) { | 
|  | 706 | g_printItAnyways = true; | 
|  | 707 | break; | 
|  | 708 | } | 
| Mark Salyzyn | 538bc12 | 2016-11-16 15:28:31 -0800 | [diff] [blame] | 709 | if (long_options[option_index].name == debug_str) { | 
|  | 710 | g_debug = true; | 
|  | 711 | break; | 
|  | 712 | } | 
| Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 713 | if (long_options[option_index].name == id_str) { | 
|  | 714 | setId = optarg && optarg[0] ? optarg : NULL; | 
|  | 715 | break; | 
|  | 716 | } | 
| Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 717 | break; | 
|  | 718 |  | 
| Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 719 | case 's': | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 720 | // default to all silent | 
|  | 721 | android_log_addFilterRule(g_logformat, "*:s"); | 
|  | 722 | break; | 
|  | 723 |  | 
|  | 724 | case 'c': | 
| Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 725 | clearLog = true; | 
| Mark Salyzyn | 2d3f38a | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 726 | mode |= ANDROID_LOG_WRONLY; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 727 | break; | 
|  | 728 |  | 
| Mark Salyzyn | 7c975ac | 2014-12-15 10:01:31 -0800 | [diff] [blame] | 729 | case 'L': | 
| Mark Salyzyn | ad5e411 | 2016-08-04 07:53:52 -0700 | [diff] [blame] | 730 | mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_PSTORE | ANDROID_LOG_NONBLOCK; | 
| Mark Salyzyn | 7c975ac | 2014-12-15 10:01:31 -0800 | [diff] [blame] | 731 | break; | 
|  | 732 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 733 | case 'd': | 
| Mark Salyzyn | 2d3f38a | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 734 | mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 735 | break; | 
|  | 736 |  | 
| Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 737 | case 't': | 
| Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 738 | got_t = true; | 
| Mark Salyzyn | 2d3f38a | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 739 | mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK; | 
| Mark Salyzyn | 5d3d1f1 | 2013-12-09 13:47:00 -0800 | [diff] [blame] | 740 | /* FALLTHRU */ | 
|  | 741 | case 'T': | 
| Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 742 | if (strspn(optarg, "0123456789") != strlen(optarg)) { | 
| Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 743 | char *cp = parseTime(tail_time, optarg); | 
| Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 744 | if (!cp) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 745 | logcat_panic(HELP_FALSE, | 
|  | 746 | "-%c \"%s\" not in time format\n", | 
| Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 747 | ret, optarg); | 
| Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 748 | } | 
|  | 749 | if (*cp) { | 
|  | 750 | char c = *cp; | 
|  | 751 | *cp = '\0'; | 
|  | 752 | fprintf(stderr, | 
|  | 753 | "WARNING: -%c \"%s\"\"%c%s\" time truncated\n", | 
|  | 754 | ret, optarg, c, cp + 1); | 
|  | 755 | *cp = c; | 
|  | 756 | } | 
|  | 757 | } else { | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 758 | if (!getSizeTArg(optarg, &tail_lines, 1)) { | 
| Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 759 | fprintf(stderr, | 
|  | 760 | "WARNING: -%c %s invalid, setting to 1\n", | 
|  | 761 | ret, optarg); | 
|  | 762 | tail_lines = 1; | 
|  | 763 | } | 
|  | 764 | } | 
| Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 765 | break; | 
|  | 766 |  | 
| Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 767 | case 'D': | 
|  | 768 | printDividers = true; | 
|  | 769 | break; | 
|  | 770 |  | 
| Casey Dahlin | dc42a87 | 2016-03-17 16:18:55 -0700 | [diff] [blame] | 771 | case 'e': | 
|  | 772 | g_regex = new pcrecpp::RE(optarg); | 
|  | 773 | break; | 
|  | 774 |  | 
| Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 775 | case 'm': { | 
|  | 776 | char *end = NULL; | 
|  | 777 | if (!getSizeTArg(optarg, &g_maxCount)) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 778 | logcat_panic(HELP_FALSE, "-%c \"%s\" isn't an " | 
| Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 779 | "integer greater than zero\n", ret, optarg); | 
|  | 780 | } | 
|  | 781 | } | 
|  | 782 | break; | 
|  | 783 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 784 | case 'g': | 
| Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 785 | if (!optarg) { | 
| Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 786 | getLogSize = true; | 
| Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 787 | break; | 
|  | 788 | } | 
|  | 789 | // FALLTHRU | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 790 |  | 
| Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 791 | case 'G': { | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 792 | char *cp; | 
|  | 793 | if (strtoll(optarg, &cp, 0) > 0) { | 
|  | 794 | setLogSize = strtoll(optarg, &cp, 0); | 
|  | 795 | } else { | 
|  | 796 | setLogSize = 0; | 
| Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 797 | } | 
|  | 798 |  | 
|  | 799 | switch(*cp) { | 
|  | 800 | case 'g': | 
|  | 801 | case 'G': | 
|  | 802 | setLogSize *= 1024; | 
|  | 803 | /* FALLTHRU */ | 
|  | 804 | case 'm': | 
|  | 805 | case 'M': | 
|  | 806 | setLogSize *= 1024; | 
|  | 807 | /* FALLTHRU */ | 
|  | 808 | case 'k': | 
|  | 809 | case 'K': | 
|  | 810 | setLogSize *= 1024; | 
|  | 811 | /* FALLTHRU */ | 
|  | 812 | case '\0': | 
|  | 813 | break; | 
|  | 814 |  | 
|  | 815 | default: | 
|  | 816 | setLogSize = 0; | 
|  | 817 | } | 
|  | 818 |  | 
|  | 819 | if (!setLogSize) { | 
|  | 820 | fprintf(stderr, "ERROR: -G <num><multiplier>\n"); | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 821 | return EXIT_FAILURE; | 
| Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 822 | } | 
|  | 823 | } | 
|  | 824 | break; | 
|  | 825 |  | 
|  | 826 | case 'p': | 
| Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 827 | if (!optarg) { | 
| Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 828 | getPruneList = true; | 
| Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 829 | break; | 
|  | 830 | } | 
|  | 831 | // FALLTHRU | 
| Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 832 |  | 
|  | 833 | case 'P': | 
|  | 834 | setPruneList = optarg; | 
|  | 835 | break; | 
|  | 836 |  | 
| Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 837 | case 'b': { | 
| Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 838 | unsigned idMask = 0; | 
| Mark Salyzyn | 1325ebf | 2016-06-07 13:03:10 -0700 | [diff] [blame] | 839 | while ((optarg = strtok(optarg, ",:; \t\n\r\f")) != NULL) { | 
|  | 840 | if (strcmp(optarg, "default") == 0) { | 
| Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 841 | idMask |= (1 << LOG_ID_MAIN) | | 
|  | 842 | (1 << LOG_ID_SYSTEM) | | 
|  | 843 | (1 << LOG_ID_CRASH); | 
| Mark Salyzyn | 1325ebf | 2016-06-07 13:03:10 -0700 | [diff] [blame] | 844 | } else if (strcmp(optarg, "all") == 0) { | 
| Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 845 | allSelected = true; | 
| Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 846 | idMask = (unsigned)-1; | 
|  | 847 | } else { | 
| Mark Salyzyn | 1325ebf | 2016-06-07 13:03:10 -0700 | [diff] [blame] | 848 | log_id_t log_id = android_name_to_log_id(optarg); | 
| Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 849 | const char *name = android_log_id_to_name(log_id); | 
| Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 850 |  | 
| Mark Salyzyn | 1325ebf | 2016-06-07 13:03:10 -0700 | [diff] [blame] | 851 | if (strcmp(name, optarg) != 0) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 852 | logcat_panic(HELP_TRUE, | 
|  | 853 | "unknown buffer %s\n", optarg); | 
| Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 854 | } | 
| Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 855 | if (log_id == LOG_ID_SECURITY) allSelected = false; | 
| Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 856 | idMask |= (1 << log_id); | 
| Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 857 | } | 
| Mark Salyzyn | 1325ebf | 2016-06-07 13:03:10 -0700 | [diff] [blame] | 858 | optarg = NULL; | 
| Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 859 | } | 
|  | 860 |  | 
| Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 861 | for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) { | 
|  | 862 | const char *name = android_log_id_to_name((log_id_t)i); | 
|  | 863 | log_id_t log_id = android_name_to_log_id(name); | 
| Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 864 |  | 
| Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 865 | if (log_id != (log_id_t)i) { | 
|  | 866 | continue; | 
|  | 867 | } | 
|  | 868 | if ((idMask & (1 << i)) == 0) { | 
|  | 869 | continue; | 
|  | 870 | } | 
| Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 871 |  | 
| Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 872 | bool found = false; | 
|  | 873 | for (dev = devices; dev; dev = dev->next) { | 
|  | 874 | if (!strcmp(name, dev->device)) { | 
|  | 875 | found = true; | 
| Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 876 | break; | 
|  | 877 | } | 
| Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 878 | if (!dev->next) { | 
| Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 879 | break; | 
|  | 880 | } | 
| Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 881 | } | 
| Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 882 | if (found) { | 
|  | 883 | continue; | 
|  | 884 | } | 
|  | 885 |  | 
|  | 886 | bool binary = !strcmp(name, "events") || | 
|  | 887 | !strcmp(name, "security"); | 
|  | 888 | log_device_t* d = new log_device_t(name, binary); | 
|  | 889 |  | 
| Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 890 | if (dev) { | 
| Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 891 | dev->next = d; | 
|  | 892 | dev = d; | 
|  | 893 | } else { | 
|  | 894 | devices = dev = d; | 
| Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 895 | } | 
| Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 896 | g_devCount++; | 
| Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 897 | } | 
| Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 898 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 899 | break; | 
|  | 900 |  | 
|  | 901 | case 'B': | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 902 | g_printBinary = 1; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 903 | break; | 
|  | 904 |  | 
|  | 905 | case 'f': | 
| Mark Salyzyn | 9812fc4 | 2015-10-06 08:59:02 -0700 | [diff] [blame] | 906 | if ((tail_time == log_time::EPOCH) && (tail_lines == 0)) { | 
| Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 907 | tail_time = lastLogTime(optarg); | 
|  | 908 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 909 | // redirect output to a file | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 910 | g_outputFileName = optarg; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 911 | break; | 
|  | 912 |  | 
| Mark Salyzyn | 1325ebf | 2016-06-07 13:03:10 -0700 | [diff] [blame] | 913 | case 'r': | 
|  | 914 | if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 915 | logcat_panic(HELP_TRUE, | 
|  | 916 | "Invalid parameter \"%s\" to -r\n", optarg); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 917 | } | 
|  | 918 | break; | 
|  | 919 |  | 
| Mark Salyzyn | 1325ebf | 2016-06-07 13:03:10 -0700 | [diff] [blame] | 920 | case 'n': | 
|  | 921 | if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 922 | logcat_panic(HELP_TRUE, | 
|  | 923 | "Invalid parameter \"%s\" to -n\n", optarg); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 924 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 925 | break; | 
|  | 926 |  | 
|  | 927 | case 'v': | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 928 | if (!strcmp(optarg, "help") || !strcmp(optarg, "--help")) { | 
|  | 929 | show_format_help(); | 
|  | 930 | exit(0); | 
|  | 931 | } | 
|  | 932 | err = setLogFormat(optarg); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 933 | if (err < 0) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 934 | logcat_panic(HELP_FORMAT, | 
|  | 935 | "Invalid parameter \"%s\" to -v\n", optarg); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 936 | } | 
| Mark Salyzyn | e1f2004 | 2015-05-06 08:40:40 -0700 | [diff] [blame] | 937 | hasSetLogFormat |= err; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 938 | break; | 
|  | 939 |  | 
|  | 940 | case 'Q': | 
|  | 941 | /* this is a *hidden* option used to start a version of logcat                 */ | 
|  | 942 | /* in an emulated device only. it basically looks for androidboot.logcat=      */ | 
|  | 943 | /* on the kernel command line. If something is found, it extracts a log filter */ | 
|  | 944 | /* and uses it to run the program. If nothing is found, the program should     */ | 
|  | 945 | /* quit immediately                                                            */ | 
|  | 946 | #define  KERNEL_OPTION  "androidboot.logcat=" | 
|  | 947 | #define  CONSOLE_OPTION "androidboot.console=" | 
|  | 948 | { | 
|  | 949 | int          fd; | 
|  | 950 | char*        logcat; | 
|  | 951 | char*        console; | 
|  | 952 | int          force_exit = 1; | 
|  | 953 | static char  cmdline[1024]; | 
|  | 954 |  | 
|  | 955 | fd = open("/proc/cmdline", O_RDONLY); | 
|  | 956 | if (fd >= 0) { | 
|  | 957 | int  n = read(fd, cmdline, sizeof(cmdline)-1 ); | 
|  | 958 | if (n < 0) n = 0; | 
|  | 959 | cmdline[n] = 0; | 
|  | 960 | close(fd); | 
|  | 961 | } else { | 
|  | 962 | cmdline[0] = 0; | 
|  | 963 | } | 
|  | 964 |  | 
|  | 965 | logcat  = strstr( cmdline, KERNEL_OPTION ); | 
|  | 966 | console = strstr( cmdline, CONSOLE_OPTION ); | 
|  | 967 | if (logcat != NULL) { | 
|  | 968 | char*  p = logcat + sizeof(KERNEL_OPTION)-1;; | 
|  | 969 | char*  q = strpbrk( p, " \t\n\r" );; | 
|  | 970 |  | 
|  | 971 | if (q != NULL) | 
|  | 972 | *q = 0; | 
|  | 973 |  | 
|  | 974 | forceFilters = p; | 
|  | 975 | force_exit   = 0; | 
|  | 976 | } | 
|  | 977 | /* if nothing found or invalid filters, exit quietly */ | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 978 | if (force_exit) { | 
|  | 979 | return EXIT_SUCCESS; | 
|  | 980 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 981 |  | 
|  | 982 | /* redirect our output to the emulator console */ | 
|  | 983 | if (console) { | 
|  | 984 | char*  p = console + sizeof(CONSOLE_OPTION)-1; | 
|  | 985 | char*  q = strpbrk( p, " \t\n\r" ); | 
|  | 986 | char   devname[64]; | 
|  | 987 | int    len; | 
|  | 988 |  | 
|  | 989 | if (q != NULL) { | 
|  | 990 | len = q - p; | 
|  | 991 | } else | 
|  | 992 | len = strlen(p); | 
|  | 993 |  | 
|  | 994 | len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p ); | 
|  | 995 | fprintf(stderr, "logcat using %s (%d)\n", devname, len); | 
|  | 996 | if (len < (int)sizeof(devname)) { | 
|  | 997 | fd = open( devname, O_WRONLY ); | 
|  | 998 | if (fd >= 0) { | 
|  | 999 | dup2(fd, 1); | 
|  | 1000 | dup2(fd, 2); | 
|  | 1001 | close(fd); | 
|  | 1002 | } | 
|  | 1003 | } | 
|  | 1004 | } | 
|  | 1005 | } | 
|  | 1006 | break; | 
|  | 1007 |  | 
| Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1008 | case 'S': | 
| Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 1009 | printStatistics = true; | 
| Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1010 | break; | 
|  | 1011 |  | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1012 | case ':': | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1013 | logcat_panic(HELP_TRUE, | 
|  | 1014 | "Option -%c needs an argument\n", optopt); | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1015 | break; | 
|  | 1016 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1017 | default: | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1018 | logcat_panic(HELP_TRUE, | 
|  | 1019 | "Unrecognized Option %c\n", optopt); | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1020 | break; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1021 | } | 
|  | 1022 | } | 
|  | 1023 |  | 
| Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 1024 | if (g_maxCount && got_t) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1025 | logcat_panic(HELP_TRUE, | 
|  | 1026 | "Cannot use -m (--max-count) and -t together\n"); | 
| Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 1027 | } | 
| Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 1028 | if (g_printItAnyways && (!g_regex || !g_maxCount)) { | 
|  | 1029 | // One day it would be nice if --print -v color and --regex <expr> | 
|  | 1030 | // could play with each other and show regex highlighted content. | 
|  | 1031 | fprintf(stderr, "WARNING: " | 
|  | 1032 | "--print ignored, to be used in combination with\n" | 
|  | 1033 | "         " | 
|  | 1034 | "--regex <expr> and --max-count <N>\n"); | 
|  | 1035 | g_printItAnyways = false; | 
|  | 1036 | } | 
| Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 1037 |  | 
| Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1038 | if (!devices) { | 
| Mark Salyzyn | 5f6738a | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 1039 | dev = devices = new log_device_t("main", false); | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1040 | g_devCount = 1; | 
| Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1041 | if (android_name_to_log_id("system") == LOG_ID_SYSTEM) { | 
| Mark Salyzyn | 5f6738a | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 1042 | dev = dev->next = new log_device_t("system", false); | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1043 | g_devCount++; | 
| Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 1044 | } | 
| Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 1045 | if (android_name_to_log_id("crash") == LOG_ID_CRASH) { | 
| Mark Salyzyn | 5f6738a | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 1046 | dev = dev->next = new log_device_t("crash", false); | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1047 | g_devCount++; | 
| Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 1048 | } | 
| Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1049 | } | 
|  | 1050 |  | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1051 | if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1052 | 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] | 1053 | } | 
|  | 1054 |  | 
| Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 1055 | if (setId != NULL) { | 
|  | 1056 | if (g_outputFileName == NULL) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1057 | logcat_panic(HELP_TRUE, "--id='%s' requires -f as well\n", setId); | 
| Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 1058 | } | 
|  | 1059 |  | 
|  | 1060 | std::string file_name = android::base::StringPrintf("%s.id", g_outputFileName); | 
|  | 1061 | std::string file; | 
|  | 1062 | bool file_ok = android::base::ReadFileToString(file_name, &file); | 
|  | 1063 | android::base::WriteStringToFile(setId, file_name, | 
|  | 1064 | S_IRUSR | S_IWUSR, getuid(), getgid()); | 
|  | 1065 | if (!file_ok || (file.compare(setId) == 0)) { | 
|  | 1066 | setId = NULL; | 
|  | 1067 | } | 
|  | 1068 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1069 |  | 
|  | 1070 | if (hasSetLogFormat == 0) { | 
|  | 1071 | const char* logFormat = getenv("ANDROID_PRINTF_LOG"); | 
|  | 1072 |  | 
|  | 1073 | if (logFormat != NULL) { | 
|  | 1074 | err = setLogFormat(logFormat); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1075 | if (err < 0) { | 
| Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1076 | 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] | 1077 | logFormat); | 
|  | 1078 | } | 
| Mark Salyzyn | 649fc60 | 2014-09-16 09:15:15 -0700 | [diff] [blame] | 1079 | } else { | 
|  | 1080 | setLogFormat("threadtime"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1081 | } | 
|  | 1082 | } | 
|  | 1083 |  | 
|  | 1084 | if (forceFilters) { | 
|  | 1085 | err = android_log_addFilterString(g_logformat, forceFilters); | 
|  | 1086 | if (err < 0) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1087 | logcat_panic(HELP_FALSE, | 
|  | 1088 | "Invalid filter expression in logcat args\n"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1089 | } | 
|  | 1090 | } else if (argc == optind) { | 
|  | 1091 | // Add from environment variable | 
|  | 1092 | char *env_tags_orig = getenv("ANDROID_LOG_TAGS"); | 
|  | 1093 |  | 
|  | 1094 | if (env_tags_orig != NULL) { | 
|  | 1095 | err = android_log_addFilterString(g_logformat, env_tags_orig); | 
|  | 1096 |  | 
| Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1097 | if (err < 0) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1098 | logcat_panic(HELP_TRUE, | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1099 | "Invalid filter expression in ANDROID_LOG_TAGS\n"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1100 | } | 
|  | 1101 | } | 
|  | 1102 | } else { | 
|  | 1103 | // Add from commandline | 
|  | 1104 | for (int i = optind ; i < argc ; i++) { | 
|  | 1105 | err = android_log_addFilterString(g_logformat, argv[i]); | 
|  | 1106 |  | 
| Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1107 | if (err < 0) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1108 | logcat_panic(HELP_TRUE, | 
|  | 1109 | "Invalid filter expression '%s'\n", argv[i]); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1110 | } | 
|  | 1111 | } | 
|  | 1112 | } | 
|  | 1113 |  | 
| Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1114 | dev = devices; | 
| Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 1115 | if (tail_time != log_time::EPOCH) { | 
| Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 1116 | logger_list = android_logger_list_alloc_time(mode, tail_time, pid); | 
| Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 1117 | } else { | 
| Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 1118 | logger_list = android_logger_list_alloc(mode, tail_lines, pid); | 
| Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 1119 | } | 
| Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1120 | const char *openDeviceFail = NULL; | 
|  | 1121 | const char *clearFail = NULL; | 
|  | 1122 | const char *setSizeFail = NULL; | 
|  | 1123 | const char *getSizeFail = NULL; | 
|  | 1124 | // We have three orthogonal actions below to clear, set log size and | 
|  | 1125 | // get log size. All sharing the same iteration loop. | 
| Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1126 | while (dev) { | 
| Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1127 | dev->logger_list = logger_list; | 
|  | 1128 | dev->logger = android_logger_open(logger_list, | 
|  | 1129 | android_name_to_log_id(dev->device)); | 
|  | 1130 | if (!dev->logger) { | 
| Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 1131 | reportErrorName(&openDeviceFail, dev->device, allSelected); | 
| Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1132 | dev = dev->next; | 
|  | 1133 | continue; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1134 | } | 
| Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1135 |  | 
| Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 1136 | if (clearLog || setId) { | 
| Mark Salyzyn | b7d059b | 2016-06-06 14:56:00 -0700 | [diff] [blame] | 1137 | if (g_outputFileName) { | 
|  | 1138 | int maxRotationCountDigits = | 
|  | 1139 | (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0; | 
|  | 1140 |  | 
|  | 1141 | for (int i = g_maxRotatedLogs ; i >= 0 ; --i) { | 
| Mark Salyzyn | 5b1a538 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 1142 | std::string file; | 
| Mark Salyzyn | b7d059b | 2016-06-06 14:56:00 -0700 | [diff] [blame] | 1143 |  | 
|  | 1144 | if (i == 0) { | 
| Mark Salyzyn | 5b1a538 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 1145 | file = android::base::StringPrintf("%s", g_outputFileName); | 
| Mark Salyzyn | b7d059b | 2016-06-06 14:56:00 -0700 | [diff] [blame] | 1146 | } else { | 
| Mark Salyzyn | 5b1a538 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 1147 | file = android::base::StringPrintf("%s.%.*d", | 
|  | 1148 | g_outputFileName, maxRotationCountDigits, i); | 
| Mark Salyzyn | b7d059b | 2016-06-06 14:56:00 -0700 | [diff] [blame] | 1149 | } | 
|  | 1150 |  | 
| Mark Salyzyn | 5b1a538 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 1151 | if (file.length() == 0) { | 
| Mark Salyzyn | b7d059b | 2016-06-06 14:56:00 -0700 | [diff] [blame] | 1152 | perror("while clearing log files"); | 
| Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 1153 | reportErrorName(&clearFail, dev->device, allSelected); | 
| Mark Salyzyn | b7d059b | 2016-06-06 14:56:00 -0700 | [diff] [blame] | 1154 | break; | 
|  | 1155 | } | 
|  | 1156 |  | 
| Mark Salyzyn | 5b1a538 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 1157 | err = unlink(file.c_str()); | 
| Mark Salyzyn | b7d059b | 2016-06-06 14:56:00 -0700 | [diff] [blame] | 1158 |  | 
|  | 1159 | if (err < 0 && errno != ENOENT && clearFail == NULL) { | 
|  | 1160 | perror("while clearing log files"); | 
| Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 1161 | reportErrorName(&clearFail, dev->device, allSelected); | 
| Mark Salyzyn | b7d059b | 2016-06-06 14:56:00 -0700 | [diff] [blame] | 1162 | } | 
| Mark Salyzyn | b7d059b | 2016-06-06 14:56:00 -0700 | [diff] [blame] | 1163 | } | 
|  | 1164 | } else if (android_logger_clear(dev->logger)) { | 
| Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 1165 | reportErrorName(&clearFail, dev->device, allSelected); | 
| Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1166 | } | 
| Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1167 | } | 
|  | 1168 |  | 
| Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1169 | if (setLogSize) { | 
|  | 1170 | if (android_logger_set_log_size(dev->logger, setLogSize)) { | 
| Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 1171 | reportErrorName(&setSizeFail, dev->device, allSelected); | 
| Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1172 | } | 
| Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1173 | } | 
|  | 1174 |  | 
| Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1175 | if (getLogSize) { | 
| Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1176 | long size = android_logger_get_log_size(dev->logger); | 
|  | 1177 | long readable = android_logger_get_log_readable_size(dev->logger); | 
| Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1178 |  | 
| Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1179 | if ((size < 0) || (readable < 0)) { | 
| Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 1180 | reportErrorName(&getSizeFail, dev->device, allSelected); | 
| Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1181 | } else { | 
|  | 1182 | printf("%s: ring buffer is %ld%sb (%ld%sb consumed), " | 
|  | 1183 | "max entry is %db, max payload is %db\n", dev->device, | 
|  | 1184 | value_of_size(size), multiplier_of_size(size), | 
|  | 1185 | value_of_size(readable), multiplier_of_size(readable), | 
|  | 1186 | (int) LOGGER_ENTRY_MAX_LEN, | 
|  | 1187 | (int) LOGGER_ENTRY_MAX_PAYLOAD); | 
| Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1188 | } | 
| Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1189 | } | 
|  | 1190 |  | 
|  | 1191 | dev = dev->next; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1192 | } | 
| Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1193 | // report any errors in the above loop and exit | 
|  | 1194 | if (openDeviceFail) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1195 | logcat_panic(HELP_FALSE, | 
|  | 1196 | "Unable to open log device '%s'\n", openDeviceFail); | 
| Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1197 | } | 
|  | 1198 | if (clearFail) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1199 | logcat_panic(HELP_FALSE, | 
|  | 1200 | "failed to clear the '%s' log\n", clearFail); | 
| Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1201 | } | 
|  | 1202 | if (setSizeFail) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1203 | logcat_panic(HELP_FALSE, | 
|  | 1204 | "failed to set the '%s' log size\n", setSizeFail); | 
| Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1205 | } | 
|  | 1206 | if (getSizeFail) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1207 | logcat_panic(HELP_FALSE, | 
|  | 1208 | "failed to get the readable '%s' log size", getSizeFail); | 
| Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1209 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1210 |  | 
| Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1211 | if (setPruneList) { | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1212 | size_t len = strlen(setPruneList); | 
|  | 1213 | /*extra 32 bytes are needed by  android_logger_set_prune_list */ | 
|  | 1214 | size_t bLen = len + 32; | 
|  | 1215 | char *buf = NULL; | 
|  | 1216 | if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) { | 
|  | 1217 | buf[len] = '\0'; | 
|  | 1218 | if (android_logger_set_prune_list(logger_list, buf, bLen)) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1219 | logcat_panic(HELP_FALSE, "failed to set the prune list"); | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1220 | } | 
|  | 1221 | free(buf); | 
|  | 1222 | } else { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1223 | logcat_panic(HELP_FALSE, "failed to set the prune list (alloc)"); | 
| Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1224 | } | 
|  | 1225 | } | 
|  | 1226 |  | 
| Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 1227 | if (printStatistics || getPruneList) { | 
| Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1228 | size_t len = 8192; | 
|  | 1229 | char *buf; | 
|  | 1230 |  | 
| Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 1231 | for (int retry = 32; | 
| Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1232 | (retry >= 0) && ((buf = new char [len])); | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1233 | delete [] buf, buf = NULL, --retry) { | 
| Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1234 | if (getPruneList) { | 
|  | 1235 | android_logger_get_prune_list(logger_list, buf, len); | 
|  | 1236 | } else { | 
|  | 1237 | android_logger_get_statistics(logger_list, buf, len); | 
|  | 1238 | } | 
| Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1239 | buf[len-1] = '\0'; | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1240 | if (atol(buf) < 3) { | 
| Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1241 | delete [] buf; | 
|  | 1242 | buf = NULL; | 
|  | 1243 | break; | 
|  | 1244 | } | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1245 | size_t ret = atol(buf) + 1; | 
|  | 1246 | if (ret <= len) { | 
|  | 1247 | len = ret; | 
| Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1248 | break; | 
|  | 1249 | } | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1250 | len = ret; | 
| Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1251 | } | 
|  | 1252 |  | 
|  | 1253 | if (!buf) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1254 | logcat_panic(HELP_FALSE, "failed to read data"); | 
| Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1255 | } | 
|  | 1256 |  | 
|  | 1257 | // remove trailing FF | 
|  | 1258 | char *cp = buf + len - 1; | 
|  | 1259 | *cp = '\0'; | 
|  | 1260 | bool truncated = *--cp != '\f'; | 
|  | 1261 | if (!truncated) { | 
|  | 1262 | *cp = '\0'; | 
|  | 1263 | } | 
|  | 1264 |  | 
|  | 1265 | // squash out the byte count | 
|  | 1266 | cp = buf; | 
|  | 1267 | if (!truncated) { | 
| Mark Salyzyn | 22e287d | 2014-03-21 13:12:16 -0700 | [diff] [blame] | 1268 | while (isdigit(*cp)) { | 
|  | 1269 | ++cp; | 
|  | 1270 | } | 
|  | 1271 | if (*cp == '\n') { | 
| Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1272 | ++cp; | 
|  | 1273 | } | 
|  | 1274 | } | 
|  | 1275 |  | 
|  | 1276 | printf("%s", cp); | 
|  | 1277 | delete [] buf; | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1278 | return EXIT_SUCCESS; | 
| Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1279 | } | 
|  | 1280 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1281 | if (getLogSize) { | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1282 | return EXIT_SUCCESS; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1283 | } | 
| Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1284 | if (setLogSize || setPruneList) { | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1285 | return EXIT_SUCCESS; | 
| Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1286 | } | 
| Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 1287 | if (clearLog) { | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1288 | return EXIT_SUCCESS; | 
| Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 1289 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1290 |  | 
| Mark Salyzyn | ad5e411 | 2016-08-04 07:53:52 -0700 | [diff] [blame] | 1291 | setupOutputAndSchedulingPolicy((mode & ANDROID_LOG_NONBLOCK) == 0); | 
| Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 1292 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1293 | //LOG_EVENT_INT(10, 12345); | 
|  | 1294 | //LOG_EVENT_LONG(11, 0x1122334455667788LL); | 
|  | 1295 | //LOG_EVENT_STRING(0, "whassup, doc?"); | 
|  | 1296 |  | 
| Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1297 | dev = NULL; | 
| Mark Salyzyn | 5f6738a | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 1298 | log_device_t unexpected("unexpected", false); | 
| Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 1299 |  | 
| Mark Salyzyn | aa730c1 | 2016-03-30 12:38:29 -0700 | [diff] [blame] | 1300 | while (!g_maxCount || (g_printCount < g_maxCount)) { | 
| Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1301 | struct log_msg log_msg; | 
| Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1302 | log_device_t* d; | 
| Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1303 | int ret = android_logger_list_read(logger_list, &log_msg); | 
|  | 1304 |  | 
|  | 1305 | if (ret == 0) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1306 | logcat_panic(HELP_FALSE, "read: unexpected EOF!\n"); | 
| Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1307 | } | 
|  | 1308 |  | 
|  | 1309 | if (ret < 0) { | 
|  | 1310 | if (ret == -EAGAIN) { | 
|  | 1311 | break; | 
|  | 1312 | } | 
|  | 1313 |  | 
|  | 1314 | if (ret == -EIO) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1315 | logcat_panic(HELP_FALSE, "read: unexpected EOF!\n"); | 
| Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1316 | } | 
|  | 1317 | if (ret == -EINVAL) { | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1318 | logcat_panic(HELP_FALSE, "read: unexpected length.\n"); | 
| Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1319 | } | 
| Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1320 | logcat_panic(HELP_FALSE, "logcat read failure"); | 
| Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1321 | } | 
|  | 1322 |  | 
| Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 1323 | for (d = devices; d; d = d->next) { | 
| Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1324 | if (android_name_to_log_id(d->device) == log_msg.id()) { | 
| Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1325 | break; | 
|  | 1326 | } | 
|  | 1327 | } | 
| Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1328 | if (!d) { | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1329 | g_devCount = 2; // set to Multiple | 
| Mark Salyzyn | 9421b0c | 2015-02-26 14:33:35 -0800 | [diff] [blame] | 1330 | d = &unexpected; | 
|  | 1331 | d->binary = log_msg.id() == LOG_ID_EVENTS; | 
| Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1332 | } | 
|  | 1333 |  | 
| Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1334 | if (dev != d) { | 
|  | 1335 | dev = d; | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1336 | maybePrintStart(dev, printDividers); | 
| Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1337 | } | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1338 | if (g_printBinary) { | 
|  | 1339 | printBinary(&log_msg); | 
| Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1340 | } else { | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1341 | processBuffer(dev, &log_msg); | 
| Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1342 | } | 
|  | 1343 | } | 
|  | 1344 |  | 
|  | 1345 | android_logger_list_free(logger_list); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1346 |  | 
| Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1347 | return EXIT_SUCCESS; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1348 | } |