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 | |
| 27 | #include <base/file.h> |
| 28 | #include <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 | |
| 401 | log_time now(CLOCK_REALTIME); |
Mark Salyzyn | 0e2fe42 | 2015-09-08 08:56:32 -0700 | [diff] [blame^] | 402 | bool monotonic = android_log_timestamp() == 'm'; |
| 403 | if (monotonic) { |
| 404 | now = log_time(CLOCK_MONOTONIC); |
| 405 | } |
Mark Salyzyn | 3fa657e | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 406 | |
| 407 | std::string directory; |
| 408 | char *file = strrchr(outputFileName, '/'); |
| 409 | if (!file) { |
| 410 | directory = "."; |
| 411 | file = outputFileName; |
| 412 | } else { |
| 413 | *file = '\0'; |
| 414 | directory = outputFileName; |
| 415 | *file = '/'; |
| 416 | ++file; |
| 417 | } |
| 418 | size_t len = strlen(file); |
| 419 | log_time modulo(0, NS_PER_SEC); |
| 420 | std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(directory.c_str()), closedir); |
| 421 | struct dirent *dp; |
| 422 | while ((dp = readdir(dir.get())) != NULL) { |
| 423 | if ((dp->d_type != DT_REG) |
Mark Salyzyn | 0e2fe42 | 2015-09-08 08:56:32 -0700 | [diff] [blame^] | 424 | // If we are using realtime, check all files that match the |
| 425 | // basename for latest time. If we are using monotonic time |
| 426 | // then only check the main file because time cycles on |
| 427 | // every reboot. |
| 428 | || strncmp(dp->d_name, file, len + monotonic) |
Mark Salyzyn | 3fa657e | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 429 | || (dp->d_name[len] |
| 430 | && ((dp->d_name[len] != '.') |
| 431 | || !isdigit(dp->d_name[len+1])))) { |
| 432 | continue; |
| 433 | } |
| 434 | |
| 435 | std::string file_name = directory; |
| 436 | file_name += "/"; |
| 437 | file_name += dp->d_name; |
| 438 | std::string file; |
| 439 | if (!android::base::ReadFileToString(file_name, &file)) { |
| 440 | continue; |
| 441 | } |
| 442 | |
| 443 | bool found = false; |
| 444 | for (const auto& line : android::base::Split(file, "\n")) { |
| 445 | log_time t(log_time::EPOCH); |
Mark Salyzyn | eb0456d | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 446 | char *ep = parseTime(t, line.c_str()); |
Mark Salyzyn | 3fa657e | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 447 | if (!ep || (*ep != ' ')) { |
| 448 | continue; |
| 449 | } |
| 450 | // determine the time precision of the logs (eg: msec or usec) |
| 451 | for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) { |
| 452 | if (t.tv_nsec % (mod * 10)) { |
| 453 | modulo.tv_nsec = mod; |
| 454 | break; |
| 455 | } |
| 456 | } |
| 457 | // We filter any times later than current as we may not have the |
| 458 | // year stored with each log entry. Also, since it is possible for |
| 459 | // entries to be recorded out of order (very rare) we select the |
| 460 | // maximum we find just in case. |
| 461 | if ((t < now) && (t > retval)) { |
| 462 | retval = t; |
| 463 | found = true; |
| 464 | } |
| 465 | } |
| 466 | // We count on the basename file to be the definitive end, so stop here. |
| 467 | if (!dp->d_name[len] && found) { |
| 468 | break; |
| 469 | } |
| 470 | } |
| 471 | if (retval == log_time::EPOCH) { |
| 472 | return retval; |
| 473 | } |
| 474 | // tail_time prints matching or higher, round up by the modulo to prevent |
| 475 | // a replay of the last entry we have just checked. |
| 476 | retval += modulo; |
| 477 | return retval; |
| 478 | } |
| 479 | |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 480 | } /* namespace android */ |
| 481 | |
| 482 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 483 | int main(int argc, char **argv) |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 484 | { |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 485 | using namespace android; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 486 | int err; |
| 487 | int hasSetLogFormat = 0; |
| 488 | int clearLog = 0; |
| 489 | int getLogSize = 0; |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 490 | unsigned long setLogSize = 0; |
| 491 | int getPruneList = 0; |
| 492 | char *setPruneList = NULL; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 493 | int printStatistics = 0; |
Mark Salyzyn | 4bdf253 | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 494 | int mode = ANDROID_LOG_RDONLY; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 495 | const char *forceFilters = NULL; |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 496 | log_device_t* devices = NULL; |
| 497 | log_device_t* dev; |
Mark Salyzyn | 1ff1809 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 498 | bool printDividers = false; |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 499 | struct logger_list *logger_list; |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 500 | size_t tail_lines = 0; |
Mark Salyzyn | 9addf59 | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 501 | log_time tail_time(log_time::EPOCH); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 502 | |
Mark Salyzyn | 12af965 | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 503 | signal(SIGPIPE, exit); |
| 504 | |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 505 | g_logformat = android_log_format_new(); |
| 506 | |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 507 | if (argc == 2 && 0 == strcmp(argv[1], "--help")) { |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 508 | show_help(argv[0]); |
| 509 | return EXIT_SUCCESS; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | for (;;) { |
| 513 | int ret; |
| 514 | |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 515 | 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] | 516 | |
| 517 | if (ret < 0) { |
| 518 | break; |
| 519 | } |
| 520 | |
| 521 | switch(ret) { |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 522 | case 's': |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 523 | // default to all silent |
| 524 | android_log_addFilterRule(g_logformat, "*:s"); |
| 525 | break; |
| 526 | |
| 527 | case 'c': |
| 528 | clearLog = 1; |
Mark Salyzyn | 4bdf253 | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 529 | mode |= ANDROID_LOG_WRONLY; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 530 | break; |
| 531 | |
Mark Salyzyn | 66460fc | 2014-12-15 10:01:31 -0800 | [diff] [blame] | 532 | case 'L': |
| 533 | mode |= ANDROID_LOG_PSTORE; |
| 534 | break; |
| 535 | |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 536 | case 'd': |
Mark Salyzyn | 4bdf253 | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 537 | mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 538 | break; |
| 539 | |
Dan Egnor | cce2b08 | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 540 | case 't': |
Mark Salyzyn | 4bdf253 | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 541 | mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK; |
Mark Salyzyn | 2256e2f | 2013-12-09 13:47:00 -0800 | [diff] [blame] | 542 | /* FALLTHRU */ |
| 543 | case 'T': |
Mark Salyzyn | 9addf59 | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 544 | if (strspn(optarg, "0123456789") != strlen(optarg)) { |
Mark Salyzyn | eb0456d | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 545 | char *cp = parseTime(tail_time, optarg); |
Mark Salyzyn | 9addf59 | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 546 | if (!cp) { |
Mark Salyzyn | eb0456d | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 547 | logcat_panic(false, "-%c \"%s\" not in time format\n", |
| 548 | ret, optarg); |
Mark Salyzyn | 9addf59 | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 549 | } |
| 550 | if (*cp) { |
| 551 | char c = *cp; |
| 552 | *cp = '\0'; |
| 553 | fprintf(stderr, |
| 554 | "WARNING: -%c \"%s\"\"%c%s\" time truncated\n", |
| 555 | ret, optarg, c, cp + 1); |
| 556 | *cp = c; |
| 557 | } |
| 558 | } else { |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 559 | if (!getSizeTArg(optarg, &tail_lines, 1)) { |
Mark Salyzyn | 9addf59 | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 560 | fprintf(stderr, |
| 561 | "WARNING: -%c %s invalid, setting to 1\n", |
| 562 | ret, optarg); |
| 563 | tail_lines = 1; |
| 564 | } |
| 565 | } |
Dan Egnor | cce2b08 | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 566 | break; |
| 567 | |
Mark Salyzyn | 1ff1809 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 568 | case 'D': |
| 569 | printDividers = true; |
| 570 | break; |
| 571 | |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 572 | case 'g': |
| 573 | getLogSize = 1; |
| 574 | break; |
| 575 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 576 | case 'G': { |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 577 | char *cp; |
| 578 | if (strtoll(optarg, &cp, 0) > 0) { |
| 579 | setLogSize = strtoll(optarg, &cp, 0); |
| 580 | } else { |
| 581 | setLogSize = 0; |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | switch(*cp) { |
| 585 | case 'g': |
| 586 | case 'G': |
| 587 | setLogSize *= 1024; |
| 588 | /* FALLTHRU */ |
| 589 | case 'm': |
| 590 | case 'M': |
| 591 | setLogSize *= 1024; |
| 592 | /* FALLTHRU */ |
| 593 | case 'k': |
| 594 | case 'K': |
| 595 | setLogSize *= 1024; |
| 596 | /* FALLTHRU */ |
| 597 | case '\0': |
| 598 | break; |
| 599 | |
| 600 | default: |
| 601 | setLogSize = 0; |
| 602 | } |
| 603 | |
| 604 | if (!setLogSize) { |
| 605 | fprintf(stderr, "ERROR: -G <num><multiplier>\n"); |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 606 | return EXIT_FAILURE; |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | break; |
| 610 | |
| 611 | case 'p': |
| 612 | getPruneList = 1; |
| 613 | break; |
| 614 | |
| 615 | case 'P': |
| 616 | setPruneList = optarg; |
| 617 | break; |
| 618 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 619 | case 'b': { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 620 | if (strcmp(optarg, "all") == 0) { |
| 621 | while (devices) { |
| 622 | dev = devices; |
| 623 | devices = dev->next; |
| 624 | delete dev; |
| 625 | } |
| 626 | |
Mark Salyzyn | 6577769 | 2014-10-10 15:25:38 -0700 | [diff] [blame] | 627 | devices = dev = NULL; |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 628 | g_devCount = 0; |
Mark Salyzyn | 6577769 | 2014-10-10 15:25:38 -0700 | [diff] [blame] | 629 | for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) { |
| 630 | const char *name = android_log_id_to_name((log_id_t)i); |
| 631 | log_id_t log_id = android_name_to_log_id(name); |
| 632 | |
| 633 | if (log_id != (log_id_t)i) { |
| 634 | continue; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 635 | } |
Mark Salyzyn | 6577769 | 2014-10-10 15:25:38 -0700 | [diff] [blame] | 636 | |
| 637 | bool binary = strcmp(name, "events") == 0; |
Mark Salyzyn | a044390 | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 638 | log_device_t* d = new log_device_t(name, binary); |
Mark Salyzyn | 6577769 | 2014-10-10 15:25:38 -0700 | [diff] [blame] | 639 | |
| 640 | if (dev) { |
| 641 | dev->next = d; |
| 642 | dev = d; |
| 643 | } else { |
| 644 | devices = dev = d; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 645 | } |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 646 | g_devCount++; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 647 | } |
| 648 | break; |
| 649 | } |
| 650 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 651 | bool binary = strcmp(optarg, "events") == 0; |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 652 | |
| 653 | if (devices) { |
| 654 | dev = devices; |
| 655 | while (dev->next) { |
| 656 | dev = dev->next; |
| 657 | } |
Mark Salyzyn | a044390 | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 658 | dev->next = new log_device_t(optarg, binary); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 659 | } else { |
Mark Salyzyn | a044390 | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 660 | devices = new log_device_t(optarg, binary); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 661 | } |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 662 | g_devCount++; |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 663 | } |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 664 | break; |
| 665 | |
| 666 | case 'B': |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 667 | g_printBinary = 1; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 668 | break; |
| 669 | |
| 670 | case 'f': |
Mark Salyzyn | 518c3b7 | 2015-10-06 08:59:02 -0700 | [diff] [blame] | 671 | if ((tail_time == log_time::EPOCH) && (tail_lines == 0)) { |
Mark Salyzyn | 3fa657e | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 672 | tail_time = lastLogTime(optarg); |
| 673 | } |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 674 | // redirect output to a file |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 675 | g_outputFileName = optarg; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 676 | break; |
| 677 | |
| 678 | case 'r': |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 679 | if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) { |
| 680 | 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] | 681 | } |
| 682 | break; |
| 683 | |
| 684 | case 'n': |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 685 | if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) { |
| 686 | 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] | 687 | } |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 688 | break; |
| 689 | |
| 690 | case 'v': |
| 691 | err = setLogFormat (optarg); |
| 692 | if (err < 0) { |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 693 | 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] | 694 | } |
Mark Salyzyn | 7661bad | 2015-05-06 08:40:40 -0700 | [diff] [blame] | 695 | hasSetLogFormat |= err; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 696 | break; |
| 697 | |
| 698 | case 'Q': |
| 699 | /* this is a *hidden* option used to start a version of logcat */ |
| 700 | /* in an emulated device only. it basically looks for androidboot.logcat= */ |
| 701 | /* on the kernel command line. If something is found, it extracts a log filter */ |
| 702 | /* and uses it to run the program. If nothing is found, the program should */ |
| 703 | /* quit immediately */ |
| 704 | #define KERNEL_OPTION "androidboot.logcat=" |
| 705 | #define CONSOLE_OPTION "androidboot.console=" |
| 706 | { |
| 707 | int fd; |
| 708 | char* logcat; |
| 709 | char* console; |
| 710 | int force_exit = 1; |
| 711 | static char cmdline[1024]; |
| 712 | |
| 713 | fd = open("/proc/cmdline", O_RDONLY); |
| 714 | if (fd >= 0) { |
| 715 | int n = read(fd, cmdline, sizeof(cmdline)-1 ); |
| 716 | if (n < 0) n = 0; |
| 717 | cmdline[n] = 0; |
| 718 | close(fd); |
| 719 | } else { |
| 720 | cmdline[0] = 0; |
| 721 | } |
| 722 | |
| 723 | logcat = strstr( cmdline, KERNEL_OPTION ); |
| 724 | console = strstr( cmdline, CONSOLE_OPTION ); |
| 725 | if (logcat != NULL) { |
| 726 | char* p = logcat + sizeof(KERNEL_OPTION)-1;; |
| 727 | char* q = strpbrk( p, " \t\n\r" );; |
| 728 | |
| 729 | if (q != NULL) |
| 730 | *q = 0; |
| 731 | |
| 732 | forceFilters = p; |
| 733 | force_exit = 0; |
| 734 | } |
| 735 | /* if nothing found or invalid filters, exit quietly */ |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 736 | if (force_exit) { |
| 737 | return EXIT_SUCCESS; |
| 738 | } |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 739 | |
| 740 | /* redirect our output to the emulator console */ |
| 741 | if (console) { |
| 742 | char* p = console + sizeof(CONSOLE_OPTION)-1; |
| 743 | char* q = strpbrk( p, " \t\n\r" ); |
| 744 | char devname[64]; |
| 745 | int len; |
| 746 | |
| 747 | if (q != NULL) { |
| 748 | len = q - p; |
| 749 | } else |
| 750 | len = strlen(p); |
| 751 | |
| 752 | len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p ); |
| 753 | fprintf(stderr, "logcat using %s (%d)\n", devname, len); |
| 754 | if (len < (int)sizeof(devname)) { |
| 755 | fd = open( devname, O_WRONLY ); |
| 756 | if (fd >= 0) { |
| 757 | dup2(fd, 1); |
| 758 | dup2(fd, 2); |
| 759 | close(fd); |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | break; |
| 765 | |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 766 | case 'S': |
| 767 | printStatistics = 1; |
| 768 | break; |
| 769 | |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 770 | case ':': |
| 771 | logcat_panic(true, "Option -%c needs an argument\n", optopt); |
| 772 | break; |
| 773 | |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 774 | default: |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 775 | logcat_panic(true, "Unrecognized Option %c\n", optopt); |
| 776 | break; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 780 | if (!devices) { |
Mark Salyzyn | a044390 | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 781 | dev = devices = new log_device_t("main", false); |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 782 | g_devCount = 1; |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 783 | if (android_name_to_log_id("system") == LOG_ID_SYSTEM) { |
Mark Salyzyn | a044390 | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 784 | dev = dev->next = new log_device_t("system", false); |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 785 | g_devCount++; |
Joe Onorato | 400da4a | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 786 | } |
Mark Salyzyn | 855c730 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 787 | if (android_name_to_log_id("crash") == LOG_ID_CRASH) { |
Mark Salyzyn | a044390 | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 788 | dev = dev->next = new log_device_t("crash", false); |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 789 | g_devCount++; |
Mark Salyzyn | 855c730 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 790 | } |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 791 | } |
| 792 | |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 793 | if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) { |
| 794 | logcat_panic(true, "-r requires -f as well\n"); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 795 | } |
| 796 | |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 797 | setupOutput(); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 798 | |
| 799 | if (hasSetLogFormat == 0) { |
| 800 | const char* logFormat = getenv("ANDROID_PRINTF_LOG"); |
| 801 | |
| 802 | if (logFormat != NULL) { |
| 803 | err = setLogFormat(logFormat); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 804 | if (err < 0) { |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 805 | 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] | 806 | logFormat); |
| 807 | } |
Mark Salyzyn | dd569af | 2014-09-16 09:15:15 -0700 | [diff] [blame] | 808 | } else { |
| 809 | setLogFormat("threadtime"); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | |
| 813 | if (forceFilters) { |
| 814 | err = android_log_addFilterString(g_logformat, forceFilters); |
| 815 | if (err < 0) { |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 816 | 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] | 817 | } |
| 818 | } else if (argc == optind) { |
| 819 | // Add from environment variable |
| 820 | char *env_tags_orig = getenv("ANDROID_LOG_TAGS"); |
| 821 | |
| 822 | if (env_tags_orig != NULL) { |
| 823 | err = android_log_addFilterString(g_logformat, env_tags_orig); |
| 824 | |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 825 | if (err < 0) { |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 826 | logcat_panic(true, |
| 827 | "Invalid filter expression in ANDROID_LOG_TAGS\n"); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | } else { |
| 831 | // Add from commandline |
| 832 | for (int i = optind ; i < argc ; i++) { |
| 833 | err = android_log_addFilterString(g_logformat, argv[i]); |
| 834 | |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 835 | if (err < 0) { |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 836 | 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] | 837 | } |
| 838 | } |
| 839 | } |
| 840 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 841 | dev = devices; |
Mark Salyzyn | 9addf59 | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 842 | if (tail_time != log_time::EPOCH) { |
| 843 | logger_list = android_logger_list_alloc_time(mode, tail_time, 0); |
| 844 | } else { |
| 845 | logger_list = android_logger_list_alloc(mode, tail_lines, 0); |
| 846 | } |
Mark Salyzyn | 7ccdb90 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 847 | const char *openDeviceFail = NULL; |
| 848 | const char *clearFail = NULL; |
| 849 | const char *setSizeFail = NULL; |
| 850 | const char *getSizeFail = NULL; |
| 851 | // We have three orthogonal actions below to clear, set log size and |
| 852 | // get log size. All sharing the same iteration loop. |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 853 | while (dev) { |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 854 | dev->logger_list = logger_list; |
| 855 | dev->logger = android_logger_open(logger_list, |
| 856 | android_name_to_log_id(dev->device)); |
| 857 | if (!dev->logger) { |
Mark Salyzyn | 7ccdb90 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 858 | openDeviceFail = openDeviceFail ?: dev->device; |
| 859 | dev = dev->next; |
| 860 | continue; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 861 | } |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 862 | |
| 863 | if (clearLog) { |
Mark Salyzyn | 7ccdb90 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 864 | if (android_logger_clear(dev->logger)) { |
| 865 | clearFail = clearFail ?: dev->device; |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 866 | } |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 867 | } |
| 868 | |
Mark Salyzyn | 7ccdb90 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 869 | if (setLogSize) { |
| 870 | if (android_logger_set_log_size(dev->logger, setLogSize)) { |
| 871 | setSizeFail = setSizeFail ?: dev->device; |
| 872 | } |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 873 | } |
| 874 | |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 875 | if (getLogSize) { |
Mark Salyzyn | 7ccdb90 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 876 | long size = android_logger_get_log_size(dev->logger); |
| 877 | long readable = android_logger_get_log_readable_size(dev->logger); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 878 | |
Mark Salyzyn | 7ccdb90 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 879 | if ((size < 0) || (readable < 0)) { |
| 880 | getSizeFail = getSizeFail ?: dev->device; |
| 881 | } else { |
| 882 | printf("%s: ring buffer is %ld%sb (%ld%sb consumed), " |
| 883 | "max entry is %db, max payload is %db\n", dev->device, |
| 884 | value_of_size(size), multiplier_of_size(size), |
| 885 | value_of_size(readable), multiplier_of_size(readable), |
| 886 | (int) LOGGER_ENTRY_MAX_LEN, |
| 887 | (int) LOGGER_ENTRY_MAX_PAYLOAD); |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 888 | } |
Joe Onorato | d23b9cf | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | dev = dev->next; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 892 | } |
Mark Salyzyn | 7ccdb90 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 893 | // report any errors in the above loop and exit |
| 894 | if (openDeviceFail) { |
| 895 | logcat_panic(false, "Unable to open log device '%s'\n", openDeviceFail); |
| 896 | } |
| 897 | if (clearFail) { |
| 898 | logcat_panic(false, "failed to clear the '%s' log\n", clearFail); |
| 899 | } |
| 900 | if (setSizeFail) { |
| 901 | logcat_panic(false, "failed to set the '%s' log size\n", setSizeFail); |
| 902 | } |
| 903 | if (getSizeFail) { |
| 904 | logcat_panic(false, "failed to get the readable '%s' log size", |
| 905 | getSizeFail); |
| 906 | } |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 907 | |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 908 | if (setPruneList) { |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 909 | size_t len = strlen(setPruneList); |
| 910 | /*extra 32 bytes are needed by android_logger_set_prune_list */ |
| 911 | size_t bLen = len + 32; |
| 912 | char *buf = NULL; |
| 913 | if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) { |
| 914 | buf[len] = '\0'; |
| 915 | if (android_logger_set_prune_list(logger_list, buf, bLen)) { |
| 916 | logcat_panic(false, "failed to set the prune list"); |
| 917 | } |
| 918 | free(buf); |
| 919 | } else { |
| 920 | logcat_panic(false, "failed to set the prune list (alloc)"); |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 921 | } |
| 922 | } |
| 923 | |
Mark Salyzyn | c402bc7 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 924 | if (printStatistics || getPruneList) { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 925 | size_t len = 8192; |
| 926 | char *buf; |
| 927 | |
| 928 | for(int retry = 32; |
| 929 | (retry >= 0) && ((buf = new char [len])); |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 930 | delete [] buf, buf = NULL, --retry) { |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 931 | if (getPruneList) { |
| 932 | android_logger_get_prune_list(logger_list, buf, len); |
| 933 | } else { |
| 934 | android_logger_get_statistics(logger_list, buf, len); |
| 935 | } |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 936 | buf[len-1] = '\0'; |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 937 | if (atol(buf) < 3) { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 938 | delete [] buf; |
| 939 | buf = NULL; |
| 940 | break; |
| 941 | } |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 942 | size_t ret = atol(buf) + 1; |
| 943 | if (ret <= len) { |
| 944 | len = ret; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 945 | break; |
| 946 | } |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 947 | len = ret; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | if (!buf) { |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 951 | logcat_panic(false, "failed to read data"); |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | // remove trailing FF |
| 955 | char *cp = buf + len - 1; |
| 956 | *cp = '\0'; |
| 957 | bool truncated = *--cp != '\f'; |
| 958 | if (!truncated) { |
| 959 | *cp = '\0'; |
| 960 | } |
| 961 | |
| 962 | // squash out the byte count |
| 963 | cp = buf; |
| 964 | if (!truncated) { |
Mark Salyzyn | 63a4ad5 | 2014-03-21 13:12:16 -0700 | [diff] [blame] | 965 | while (isdigit(*cp)) { |
| 966 | ++cp; |
| 967 | } |
| 968 | if (*cp == '\n') { |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 969 | ++cp; |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | printf("%s", cp); |
| 974 | delete [] buf; |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 975 | return EXIT_SUCCESS; |
Mark Salyzyn | d774bce | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 979 | if (getLogSize) { |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 980 | return EXIT_SUCCESS; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 981 | } |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 982 | if (setLogSize || setPruneList) { |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 983 | return EXIT_SUCCESS; |
Mark Salyzyn | c89839a | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 984 | } |
Joe Onorato | 400da4a | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 985 | if (clearLog) { |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 986 | return EXIT_SUCCESS; |
Joe Onorato | 400da4a | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 987 | } |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 988 | |
| 989 | //LOG_EVENT_INT(10, 12345); |
| 990 | //LOG_EVENT_LONG(11, 0x1122334455667788LL); |
| 991 | //LOG_EVENT_STRING(0, "whassup, doc?"); |
| 992 | |
Mark Salyzyn | 1ff1809 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 993 | dev = NULL; |
Mark Salyzyn | a044390 | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 994 | log_device_t unexpected("unexpected", false); |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 995 | while (1) { |
| 996 | struct log_msg log_msg; |
Mark Salyzyn | 1ff1809 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 997 | log_device_t* d; |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 998 | int ret = android_logger_list_read(logger_list, &log_msg); |
| 999 | |
| 1000 | if (ret == 0) { |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1001 | logcat_panic(false, "read: unexpected EOF!\n"); |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | if (ret < 0) { |
| 1005 | if (ret == -EAGAIN) { |
| 1006 | break; |
| 1007 | } |
| 1008 | |
| 1009 | if (ret == -EIO) { |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1010 | logcat_panic(false, "read: unexpected EOF!\n"); |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1011 | } |
| 1012 | if (ret == -EINVAL) { |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1013 | logcat_panic(false, "read: unexpected length.\n"); |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1014 | } |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1015 | logcat_panic(false, "logcat read failure"); |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1016 | } |
| 1017 | |
Mark Salyzyn | 1ff1809 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1018 | for(d = devices; d; d = d->next) { |
| 1019 | if (android_name_to_log_id(d->device) == log_msg.id()) { |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1020 | break; |
| 1021 | } |
| 1022 | } |
Mark Salyzyn | 1ff1809 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1023 | if (!d) { |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1024 | g_devCount = 2; // set to Multiple |
Mark Salyzyn | 784d64f | 2015-02-26 14:33:35 -0800 | [diff] [blame] | 1025 | d = &unexpected; |
| 1026 | d->binary = log_msg.id() == LOG_ID_EVENTS; |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1027 | } |
| 1028 | |
Mark Salyzyn | 1ff1809 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1029 | if (dev != d) { |
| 1030 | dev = d; |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1031 | maybePrintStart(dev, printDividers); |
Mark Salyzyn | 1ff1809 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1032 | } |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1033 | if (g_printBinary) { |
| 1034 | printBinary(&log_msg); |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1035 | } else { |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1036 | processBuffer(dev, &log_msg); |
Mark Salyzyn | 2a8a6aa | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | android_logger_list_free(logger_list); |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1041 | |
Traian Schiau | 9f97860 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1042 | return EXIT_SUCCESS; |
The Android Open Source Project | 190995d | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1043 | } |