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