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