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