Mark Salyzyn | cf4aa03 | 2013-11-22 07:54:30 -0800 | [diff] [blame] | 1 | /* |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2 | ** |
Mark Salyzyn | 40b2155 | 2013-12-18 12:59:01 -0800 | [diff] [blame] | 3 | ** Copyright 2006-2014, The Android Open Source Project |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE /* for asprintf */ |
| 19 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | #include <arpa/inet.h> |
Mark Salyzyn | a04464a | 2014-04-30 08:50:53 -0700 | [diff] [blame] | 21 | #include <assert.h> |
| 22 | #include <ctype.h> |
| 23 | #include <errno.h> |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 24 | #include <stdbool.h> |
Mark Salyzyn | a04464a | 2014-04-30 08:50:53 -0700 | [diff] [blame] | 25 | #include <stdint.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Jeff Brown | 44193d9 | 2015-04-28 12:47:02 -0700 | [diff] [blame] | 29 | #include <inttypes.h> |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 30 | #include <sys/param.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 31 | |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 32 | #include <cutils/list.h> |
Colin Cross | 9227bd3 | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 33 | #include <log/logd.h> |
| 34 | #include <log/logprint.h> |
Mark Salyzyn | 2aa510e | 2015-12-08 09:15:06 -0800 | [diff] [blame] | 35 | #include <private/android_filesystem_config.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 36 | |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 37 | #define MS_PER_NSEC 1000000 |
| 38 | #define US_PER_NSEC 1000 |
| 39 | |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 40 | /* open coded fragment, prevent circular dependencies */ |
| 41 | #define WEAK static |
| 42 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 43 | typedef struct FilterInfo_t { |
| 44 | char *mTag; |
| 45 | android_LogPriority mPri; |
| 46 | struct FilterInfo_t *p_next; |
| 47 | } FilterInfo; |
| 48 | |
| 49 | struct AndroidLogFormat_t { |
| 50 | android_LogPriority global_pri; |
| 51 | FilterInfo *filters; |
| 52 | AndroidLogPrintFormat format; |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 53 | bool colored_output; |
Mark Salyzyn | e1f2004 | 2015-05-06 08:40:40 -0700 | [diff] [blame] | 54 | bool usec_time_output; |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 55 | bool printable_output; |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 56 | bool year_output; |
| 57 | bool zone_output; |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 58 | bool epoch_output; |
| 59 | bool monotonic_output; |
Mark Salyzyn | 90e7af3 | 2015-12-07 16:52:42 -0800 | [diff] [blame] | 60 | bool uid_output; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 63 | /* |
| 64 | * gnome-terminal color tags |
| 65 | * See http://misc.flogisoft.com/bash/tip_colors_and_formatting |
| 66 | * for ideas on how to set the forground color of the text for xterm. |
| 67 | * The color manipulation character stream is defined as: |
| 68 | * ESC [ 3 8 ; 5 ; <color#> m |
| 69 | */ |
| 70 | #define ANDROID_COLOR_BLUE 75 |
| 71 | #define ANDROID_COLOR_DEFAULT 231 |
| 72 | #define ANDROID_COLOR_GREEN 40 |
| 73 | #define ANDROID_COLOR_ORANGE 166 |
| 74 | #define ANDROID_COLOR_RED 196 |
| 75 | #define ANDROID_COLOR_YELLOW 226 |
| 76 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 77 | static FilterInfo * filterinfo_new(const char * tag, android_LogPriority pri) |
| 78 | { |
| 79 | FilterInfo *p_ret; |
| 80 | |
| 81 | p_ret = (FilterInfo *)calloc(1, sizeof(FilterInfo)); |
| 82 | p_ret->mTag = strdup(tag); |
| 83 | p_ret->mPri = pri; |
| 84 | |
| 85 | return p_ret; |
| 86 | } |
| 87 | |
Mark Salyzyn | a04464a | 2014-04-30 08:50:53 -0700 | [diff] [blame] | 88 | /* balance to above, filterinfo_free left unimplemented */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * Note: also accepts 0-9 priorities |
| 92 | * returns ANDROID_LOG_UNKNOWN if the character is unrecognized |
| 93 | */ |
| 94 | static android_LogPriority filterCharToPri (char c) |
| 95 | { |
| 96 | android_LogPriority pri; |
| 97 | |
| 98 | c = tolower(c); |
| 99 | |
| 100 | if (c >= '0' && c <= '9') { |
| 101 | if (c >= ('0'+ANDROID_LOG_SILENT)) { |
| 102 | pri = ANDROID_LOG_VERBOSE; |
| 103 | } else { |
| 104 | pri = (android_LogPriority)(c - '0'); |
| 105 | } |
| 106 | } else if (c == 'v') { |
| 107 | pri = ANDROID_LOG_VERBOSE; |
| 108 | } else if (c == 'd') { |
| 109 | pri = ANDROID_LOG_DEBUG; |
| 110 | } else if (c == 'i') { |
| 111 | pri = ANDROID_LOG_INFO; |
| 112 | } else if (c == 'w') { |
| 113 | pri = ANDROID_LOG_WARN; |
| 114 | } else if (c == 'e') { |
| 115 | pri = ANDROID_LOG_ERROR; |
| 116 | } else if (c == 'f') { |
| 117 | pri = ANDROID_LOG_FATAL; |
| 118 | } else if (c == 's') { |
| 119 | pri = ANDROID_LOG_SILENT; |
| 120 | } else if (c == '*') { |
| 121 | pri = ANDROID_LOG_DEFAULT; |
| 122 | } else { |
| 123 | pri = ANDROID_LOG_UNKNOWN; |
| 124 | } |
| 125 | |
| 126 | return pri; |
| 127 | } |
| 128 | |
| 129 | static char filterPriToChar (android_LogPriority pri) |
| 130 | { |
| 131 | switch (pri) { |
| 132 | case ANDROID_LOG_VERBOSE: return 'V'; |
| 133 | case ANDROID_LOG_DEBUG: return 'D'; |
| 134 | case ANDROID_LOG_INFO: return 'I'; |
| 135 | case ANDROID_LOG_WARN: return 'W'; |
| 136 | case ANDROID_LOG_ERROR: return 'E'; |
| 137 | case ANDROID_LOG_FATAL: return 'F'; |
| 138 | case ANDROID_LOG_SILENT: return 'S'; |
| 139 | |
| 140 | case ANDROID_LOG_DEFAULT: |
| 141 | case ANDROID_LOG_UNKNOWN: |
| 142 | default: return '?'; |
| 143 | } |
| 144 | } |
| 145 | |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 146 | static int colorFromPri (android_LogPriority pri) |
| 147 | { |
| 148 | switch (pri) { |
| 149 | case ANDROID_LOG_VERBOSE: return ANDROID_COLOR_DEFAULT; |
| 150 | case ANDROID_LOG_DEBUG: return ANDROID_COLOR_BLUE; |
| 151 | case ANDROID_LOG_INFO: return ANDROID_COLOR_GREEN; |
| 152 | case ANDROID_LOG_WARN: return ANDROID_COLOR_ORANGE; |
| 153 | case ANDROID_LOG_ERROR: return ANDROID_COLOR_RED; |
| 154 | case ANDROID_LOG_FATAL: return ANDROID_COLOR_RED; |
| 155 | case ANDROID_LOG_SILENT: return ANDROID_COLOR_DEFAULT; |
| 156 | |
| 157 | case ANDROID_LOG_DEFAULT: |
| 158 | case ANDROID_LOG_UNKNOWN: |
| 159 | default: return ANDROID_COLOR_DEFAULT; |
| 160 | } |
| 161 | } |
| 162 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 163 | static android_LogPriority filterPriForTag( |
| 164 | AndroidLogFormat *p_format, const char *tag) |
| 165 | { |
| 166 | FilterInfo *p_curFilter; |
| 167 | |
| 168 | for (p_curFilter = p_format->filters |
| 169 | ; p_curFilter != NULL |
| 170 | ; p_curFilter = p_curFilter->p_next |
| 171 | ) { |
| 172 | if (0 == strcmp(tag, p_curFilter->mTag)) { |
| 173 | if (p_curFilter->mPri == ANDROID_LOG_DEFAULT) { |
| 174 | return p_format->global_pri; |
| 175 | } else { |
| 176 | return p_curFilter->mPri; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return p_format->global_pri; |
| 182 | } |
| 183 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 184 | /** |
| 185 | * returns 1 if this log line should be printed based on its priority |
| 186 | * and tag, and 0 if it should not |
| 187 | */ |
| 188 | int android_log_shouldPrintLine ( |
| 189 | AndroidLogFormat *p_format, const char *tag, android_LogPriority pri) |
| 190 | { |
| 191 | return pri >= filterPriForTag(p_format, tag); |
| 192 | } |
| 193 | |
| 194 | AndroidLogFormat *android_log_format_new() |
| 195 | { |
| 196 | AndroidLogFormat *p_ret; |
| 197 | |
| 198 | p_ret = calloc(1, sizeof(AndroidLogFormat)); |
| 199 | |
| 200 | p_ret->global_pri = ANDROID_LOG_VERBOSE; |
| 201 | p_ret->format = FORMAT_BRIEF; |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 202 | p_ret->colored_output = false; |
Mark Salyzyn | e1f2004 | 2015-05-06 08:40:40 -0700 | [diff] [blame] | 203 | p_ret->usec_time_output = false; |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 204 | p_ret->printable_output = false; |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 205 | p_ret->year_output = false; |
| 206 | p_ret->zone_output = false; |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 207 | p_ret->epoch_output = false; |
Mark Salyzyn | ba7a9a0 | 2015-12-01 15:57:25 -0800 | [diff] [blame] | 208 | p_ret->monotonic_output = android_log_clockid() == CLOCK_MONOTONIC; |
Mark Salyzyn | 90e7af3 | 2015-12-07 16:52:42 -0800 | [diff] [blame] | 209 | p_ret->uid_output = false; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 210 | |
| 211 | return p_ret; |
| 212 | } |
| 213 | |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 214 | static list_declare(convertHead); |
| 215 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 216 | void android_log_format_free(AndroidLogFormat *p_format) |
| 217 | { |
| 218 | FilterInfo *p_info, *p_info_old; |
| 219 | |
| 220 | p_info = p_format->filters; |
| 221 | |
| 222 | while (p_info != NULL) { |
| 223 | p_info_old = p_info; |
| 224 | p_info = p_info->p_next; |
| 225 | |
| 226 | free(p_info_old); |
| 227 | } |
| 228 | |
| 229 | free(p_format); |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 230 | |
| 231 | /* Free conversion resource, can always be reconstructed */ |
| 232 | while (!list_empty(&convertHead)) { |
| 233 | struct listnode *node = list_head(&convertHead); |
| 234 | list_remove(node); |
| 235 | free(node); |
| 236 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Mark Salyzyn | e1f2004 | 2015-05-06 08:40:40 -0700 | [diff] [blame] | 239 | int android_log_setPrintFormat(AndroidLogFormat *p_format, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 240 | AndroidLogPrintFormat format) |
| 241 | { |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 242 | switch (format) { |
| 243 | case FORMAT_MODIFIER_COLOR: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 244 | p_format->colored_output = true; |
Mark Salyzyn | e1f2004 | 2015-05-06 08:40:40 -0700 | [diff] [blame] | 245 | return 0; |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 246 | case FORMAT_MODIFIER_TIME_USEC: |
Mark Salyzyn | e1f2004 | 2015-05-06 08:40:40 -0700 | [diff] [blame] | 247 | p_format->usec_time_output = true; |
| 248 | return 0; |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 249 | case FORMAT_MODIFIER_PRINTABLE: |
| 250 | p_format->printable_output = true; |
| 251 | return 0; |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 252 | case FORMAT_MODIFIER_YEAR: |
| 253 | p_format->year_output = true; |
| 254 | return 0; |
| 255 | case FORMAT_MODIFIER_ZONE: |
| 256 | p_format->zone_output = !p_format->zone_output; |
| 257 | return 0; |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 258 | case FORMAT_MODIFIER_EPOCH: |
| 259 | p_format->epoch_output = true; |
| 260 | return 0; |
| 261 | case FORMAT_MODIFIER_MONOTONIC: |
| 262 | p_format->monotonic_output = true; |
| 263 | return 0; |
Mark Salyzyn | 90e7af3 | 2015-12-07 16:52:42 -0800 | [diff] [blame] | 264 | case FORMAT_MODIFIER_UID: |
| 265 | p_format->uid_output = true; |
| 266 | return 0; |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 267 | default: |
| 268 | break; |
Mark Salyzyn | e1f2004 | 2015-05-06 08:40:40 -0700 | [diff] [blame] | 269 | } |
| 270 | p_format->format = format; |
| 271 | return 1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 274 | static const char tz[] = "TZ"; |
| 275 | static const char utc[] = "UTC"; |
| 276 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 277 | /** |
| 278 | * Returns FORMAT_OFF on invalid string |
| 279 | */ |
| 280 | AndroidLogPrintFormat android_log_formatFromString(const char * formatString) |
| 281 | { |
| 282 | static AndroidLogPrintFormat format; |
| 283 | |
| 284 | if (strcmp(formatString, "brief") == 0) format = FORMAT_BRIEF; |
| 285 | else if (strcmp(formatString, "process") == 0) format = FORMAT_PROCESS; |
| 286 | else if (strcmp(formatString, "tag") == 0) format = FORMAT_TAG; |
| 287 | else if (strcmp(formatString, "thread") == 0) format = FORMAT_THREAD; |
| 288 | else if (strcmp(formatString, "raw") == 0) format = FORMAT_RAW; |
| 289 | else if (strcmp(formatString, "time") == 0) format = FORMAT_TIME; |
| 290 | else if (strcmp(formatString, "threadtime") == 0) format = FORMAT_THREADTIME; |
| 291 | else if (strcmp(formatString, "long") == 0) format = FORMAT_LONG; |
Mark Salyzyn | e1f2004 | 2015-05-06 08:40:40 -0700 | [diff] [blame] | 292 | else if (strcmp(formatString, "color") == 0) format = FORMAT_MODIFIER_COLOR; |
| 293 | else if (strcmp(formatString, "usec") == 0) format = FORMAT_MODIFIER_TIME_USEC; |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 294 | else if (strcmp(formatString, "printable") == 0) format = FORMAT_MODIFIER_PRINTABLE; |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 295 | else if (strcmp(formatString, "year") == 0) format = FORMAT_MODIFIER_YEAR; |
| 296 | else if (strcmp(formatString, "zone") == 0) format = FORMAT_MODIFIER_ZONE; |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 297 | else if (strcmp(formatString, "epoch") == 0) format = FORMAT_MODIFIER_EPOCH; |
| 298 | else if (strcmp(formatString, "monotonic") == 0) format = FORMAT_MODIFIER_MONOTONIC; |
Mark Salyzyn | 90e7af3 | 2015-12-07 16:52:42 -0800 | [diff] [blame] | 299 | else if (strcmp(formatString, "uid") == 0) format = FORMAT_MODIFIER_UID; |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 300 | else { |
| 301 | extern char *tzname[2]; |
| 302 | static const char gmt[] = "GMT"; |
| 303 | char *cp = getenv(tz); |
| 304 | if (cp) { |
| 305 | cp = strdup(cp); |
| 306 | } |
| 307 | setenv(tz, formatString, 1); |
| 308 | /* |
| 309 | * Run tzset here to determine if the timezone is legitimate. If the |
| 310 | * zone is GMT, check if that is what was asked for, if not then |
| 311 | * did not match any on the system; report an error to caller. |
| 312 | */ |
| 313 | tzset(); |
| 314 | if (!tzname[0] |
| 315 | || ((!strcmp(tzname[0], utc) |
| 316 | || !strcmp(tzname[0], gmt)) /* error? */ |
| 317 | && strcasecmp(formatString, utc) |
| 318 | && strcasecmp(formatString, gmt))) { /* ok */ |
| 319 | if (cp) { |
| 320 | setenv(tz, cp, 1); |
| 321 | } else { |
| 322 | unsetenv(tz); |
| 323 | } |
| 324 | tzset(); |
| 325 | format = FORMAT_OFF; |
| 326 | } else { |
| 327 | format = FORMAT_MODIFIER_ZONE; |
| 328 | } |
| 329 | free(cp); |
| 330 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 331 | |
| 332 | return format; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * filterExpression: a single filter expression |
| 337 | * eg "AT:d" |
| 338 | * |
| 339 | * returns 0 on success and -1 on invalid expression |
| 340 | * |
| 341 | * Assumes single threaded execution |
| 342 | */ |
| 343 | |
| 344 | int android_log_addFilterRule(AndroidLogFormat *p_format, |
| 345 | const char *filterExpression) |
| 346 | { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 347 | size_t tagNameLength; |
| 348 | android_LogPriority pri = ANDROID_LOG_DEFAULT; |
| 349 | |
| 350 | tagNameLength = strcspn(filterExpression, ":"); |
| 351 | |
| 352 | if (tagNameLength == 0) { |
| 353 | goto error; |
| 354 | } |
| 355 | |
| 356 | if(filterExpression[tagNameLength] == ':') { |
| 357 | pri = filterCharToPri(filterExpression[tagNameLength+1]); |
| 358 | |
| 359 | if (pri == ANDROID_LOG_UNKNOWN) { |
| 360 | goto error; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | if(0 == strncmp("*", filterExpression, tagNameLength)) { |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 365 | /* |
| 366 | * This filter expression refers to the global filter |
| 367 | * The default level for this is DEBUG if the priority |
| 368 | * is unspecified |
| 369 | */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 370 | if (pri == ANDROID_LOG_DEFAULT) { |
| 371 | pri = ANDROID_LOG_DEBUG; |
| 372 | } |
| 373 | |
| 374 | p_format->global_pri = pri; |
| 375 | } else { |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 376 | /* |
| 377 | * for filter expressions that don't refer to the global |
| 378 | * filter, the default is verbose if the priority is unspecified |
| 379 | */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 380 | if (pri == ANDROID_LOG_DEFAULT) { |
| 381 | pri = ANDROID_LOG_VERBOSE; |
| 382 | } |
| 383 | |
| 384 | char *tagName; |
| 385 | |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 386 | /* |
| 387 | * Presently HAVE_STRNDUP is never defined, so the second case is always taken |
| 388 | * Darwin doesn't have strnup, everything else does |
| 389 | */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 390 | #ifdef HAVE_STRNDUP |
| 391 | tagName = strndup(filterExpression, tagNameLength); |
| 392 | #else |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 393 | /* a few extra bytes copied... */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 394 | tagName = strdup(filterExpression); |
| 395 | tagName[tagNameLength] = '\0'; |
| 396 | #endif /*HAVE_STRNDUP*/ |
| 397 | |
| 398 | FilterInfo *p_fi = filterinfo_new(tagName, pri); |
| 399 | free(tagName); |
| 400 | |
| 401 | p_fi->p_next = p_format->filters; |
| 402 | p_format->filters = p_fi; |
| 403 | } |
| 404 | |
| 405 | return 0; |
| 406 | error: |
| 407 | return -1; |
| 408 | } |
| 409 | |
| 410 | |
| 411 | /** |
| 412 | * filterString: a comma/whitespace-separated set of filter expressions |
| 413 | * |
| 414 | * eg "AT:d *:i" |
| 415 | * |
| 416 | * returns 0 on success and -1 on invalid expression |
| 417 | * |
| 418 | * Assumes single threaded execution |
| 419 | * |
| 420 | */ |
| 421 | |
| 422 | int android_log_addFilterString(AndroidLogFormat *p_format, |
| 423 | const char *filterString) |
| 424 | { |
| 425 | char *filterStringCopy = strdup (filterString); |
| 426 | char *p_cur = filterStringCopy; |
| 427 | char *p_ret; |
| 428 | int err; |
| 429 | |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 430 | /* Yes, I'm using strsep */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 431 | while (NULL != (p_ret = strsep(&p_cur, " \t,"))) { |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 432 | /* ignore whitespace-only entries */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 433 | if(p_ret[0] != '\0') { |
| 434 | err = android_log_addFilterRule(p_format, p_ret); |
| 435 | |
| 436 | if (err < 0) { |
| 437 | goto error; |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | free (filterStringCopy); |
| 443 | return 0; |
| 444 | error: |
| 445 | free (filterStringCopy); |
| 446 | return -1; |
| 447 | } |
| 448 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 449 | /** |
| 450 | * Splits a wire-format buffer into an AndroidLogEntry |
| 451 | * entry allocated by caller. Pointers will point directly into buf |
| 452 | * |
| 453 | * Returns 0 on success and -1 on invalid wire format (entry will be |
| 454 | * in unspecified state) |
| 455 | */ |
| 456 | int android_log_processLogBuffer(struct logger_entry *buf, |
| 457 | AndroidLogEntry *entry) |
| 458 | { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 459 | entry->tv_sec = buf->sec; |
| 460 | entry->tv_nsec = buf->nsec; |
Mark Salyzyn | 90e7af3 | 2015-12-07 16:52:42 -0800 | [diff] [blame] | 461 | entry->uid = -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 462 | entry->pid = buf->pid; |
| 463 | entry->tid = buf->tid; |
Kenny Root | 4bf3c02 | 2011-09-30 17:10:14 -0700 | [diff] [blame] | 464 | |
| 465 | /* |
| 466 | * format: <priority:1><tag:N>\0<message:N>\0 |
| 467 | * |
| 468 | * tag str |
Nick Kralevich | e1ede15 | 2011-10-18 15:23:33 -0700 | [diff] [blame] | 469 | * starts at buf->msg+1 |
Kenny Root | 4bf3c02 | 2011-09-30 17:10:14 -0700 | [diff] [blame] | 470 | * msg |
Nick Kralevich | e1ede15 | 2011-10-18 15:23:33 -0700 | [diff] [blame] | 471 | * starts at buf->msg+1+len(tag)+1 |
Jeff Sharkey | a820a0e | 2011-10-26 18:40:39 -0700 | [diff] [blame] | 472 | * |
| 473 | * The message may have been truncated by the kernel log driver. |
| 474 | * When that happens, we must null-terminate the message ourselves. |
Kenny Root | 4bf3c02 | 2011-09-30 17:10:14 -0700 | [diff] [blame] | 475 | */ |
Nick Kralevich | e1ede15 | 2011-10-18 15:23:33 -0700 | [diff] [blame] | 476 | if (buf->len < 3) { |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 477 | /* |
| 478 | * An well-formed entry must consist of at least a priority |
| 479 | * and two null characters |
| 480 | */ |
Nick Kralevich | e1ede15 | 2011-10-18 15:23:33 -0700 | [diff] [blame] | 481 | fprintf(stderr, "+++ LOG: entry too small\n"); |
Kenny Root | 4bf3c02 | 2011-09-30 17:10:14 -0700 | [diff] [blame] | 482 | return -1; |
| 483 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 484 | |
Jeff Sharkey | a820a0e | 2011-10-26 18:40:39 -0700 | [diff] [blame] | 485 | int msgStart = -1; |
| 486 | int msgEnd = -1; |
| 487 | |
Nick Kralevich | e1ede15 | 2011-10-18 15:23:33 -0700 | [diff] [blame] | 488 | int i; |
Mark Salyzyn | 40b2155 | 2013-12-18 12:59:01 -0800 | [diff] [blame] | 489 | char *msg = buf->msg; |
| 490 | struct logger_entry_v2 *buf2 = (struct logger_entry_v2 *)buf; |
| 491 | if (buf2->hdr_size) { |
| 492 | msg = ((char *)buf2) + buf2->hdr_size; |
Mark Salyzyn | 90e7af3 | 2015-12-07 16:52:42 -0800 | [diff] [blame] | 493 | if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) { |
| 494 | entry->uid = ((struct logger_entry_v4 *)buf)->uid; |
| 495 | } |
Mark Salyzyn | 40b2155 | 2013-12-18 12:59:01 -0800 | [diff] [blame] | 496 | } |
Nick Kralevich | e1ede15 | 2011-10-18 15:23:33 -0700 | [diff] [blame] | 497 | for (i = 1; i < buf->len; i++) { |
Mark Salyzyn | 40b2155 | 2013-12-18 12:59:01 -0800 | [diff] [blame] | 498 | if (msg[i] == '\0') { |
Jeff Sharkey | a820a0e | 2011-10-26 18:40:39 -0700 | [diff] [blame] | 499 | if (msgStart == -1) { |
| 500 | msgStart = i + 1; |
| 501 | } else { |
| 502 | msgEnd = i; |
| 503 | break; |
| 504 | } |
Nick Kralevich | e1ede15 | 2011-10-18 15:23:33 -0700 | [diff] [blame] | 505 | } |
| 506 | } |
Jeff Sharkey | a820a0e | 2011-10-26 18:40:39 -0700 | [diff] [blame] | 507 | |
| 508 | if (msgStart == -1) { |
| 509 | fprintf(stderr, "+++ LOG: malformed log message\n"); |
Nick Kralevich | 63f4a84 | 2011-10-17 10:45:03 -0700 | [diff] [blame] | 510 | return -1; |
| 511 | } |
Jeff Sharkey | a820a0e | 2011-10-26 18:40:39 -0700 | [diff] [blame] | 512 | if (msgEnd == -1) { |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 513 | /* incoming message not null-terminated; force it */ |
Mark Salyzyn | d7bcf40 | 2015-12-04 09:24:15 -0800 | [diff] [blame] | 514 | msgEnd = buf->len - 1; /* may result in msgEnd < msgStart */ |
Mark Salyzyn | 40b2155 | 2013-12-18 12:59:01 -0800 | [diff] [blame] | 515 | msg[msgEnd] = '\0'; |
Jeff Sharkey | a820a0e | 2011-10-26 18:40:39 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Mark Salyzyn | 40b2155 | 2013-12-18 12:59:01 -0800 | [diff] [blame] | 518 | entry->priority = msg[0]; |
| 519 | entry->tag = msg + 1; |
| 520 | entry->message = msg + msgStart; |
Mark Salyzyn | d7bcf40 | 2015-12-04 09:24:15 -0800 | [diff] [blame] | 521 | entry->messageLen = (msgEnd < msgStart) ? 0 : (msgEnd - msgStart); |
Nick Kralevich | 63f4a84 | 2011-10-17 10:45:03 -0700 | [diff] [blame] | 522 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | /* |
Mark Salyzyn | 8470dad | 2015-03-06 20:42:57 +0000 | [diff] [blame] | 527 | * Extract a 4-byte value from a byte stream. |
| 528 | */ |
| 529 | static inline uint32_t get4LE(const uint8_t* src) |
| 530 | { |
| 531 | return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24); |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * Extract an 8-byte value from a byte stream. |
| 536 | */ |
| 537 | static inline uint64_t get8LE(const uint8_t* src) |
| 538 | { |
| 539 | uint32_t low, high; |
| 540 | |
| 541 | low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24); |
| 542 | high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24); |
Jeff Brown | 44193d9 | 2015-04-28 12:47:02 -0700 | [diff] [blame] | 543 | return ((uint64_t) high << 32) | (uint64_t) low; |
Mark Salyzyn | 8470dad | 2015-03-06 20:42:57 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | |
| 547 | /* |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 548 | * Recursively convert binary log data to printable form. |
| 549 | * |
| 550 | * This needs to be recursive because you can have lists of lists. |
| 551 | * |
| 552 | * If we run out of room, we stop processing immediately. It's important |
| 553 | * for us to check for space on every output element to avoid producing |
| 554 | * garbled output. |
| 555 | * |
| 556 | * Returns 0 on success, 1 on buffer full, -1 on failure. |
| 557 | */ |
| 558 | static int android_log_printBinaryEvent(const unsigned char** pEventData, |
| 559 | size_t* pEventDataLen, char** pOutBuf, size_t* pOutBufLen) |
| 560 | { |
| 561 | const unsigned char* eventData = *pEventData; |
| 562 | size_t eventDataLen = *pEventDataLen; |
| 563 | char* outBuf = *pOutBuf; |
| 564 | size_t outBufLen = *pOutBufLen; |
| 565 | unsigned char type; |
| 566 | size_t outCount; |
| 567 | int result = 0; |
| 568 | |
| 569 | if (eventDataLen < 1) |
| 570 | return -1; |
| 571 | type = *eventData++; |
| 572 | eventDataLen--; |
| 573 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 574 | switch (type) { |
| 575 | case EVENT_TYPE_INT: |
| 576 | /* 32-bit signed int */ |
| 577 | { |
| 578 | int ival; |
| 579 | |
| 580 | if (eventDataLen < 4) |
| 581 | return -1; |
Mark Salyzyn | 8470dad | 2015-03-06 20:42:57 +0000 | [diff] [blame] | 582 | ival = get4LE(eventData); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 583 | eventData += 4; |
| 584 | eventDataLen -= 4; |
| 585 | |
| 586 | outCount = snprintf(outBuf, outBufLen, "%d", ival); |
| 587 | if (outCount < outBufLen) { |
| 588 | outBuf += outCount; |
| 589 | outBufLen -= outCount; |
| 590 | } else { |
| 591 | /* halt output */ |
| 592 | goto no_room; |
| 593 | } |
| 594 | } |
| 595 | break; |
| 596 | case EVENT_TYPE_LONG: |
| 597 | /* 64-bit signed long */ |
| 598 | { |
Jeff Brown | 44193d9 | 2015-04-28 12:47:02 -0700 | [diff] [blame] | 599 | uint64_t lval; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 600 | |
| 601 | if (eventDataLen < 8) |
| 602 | return -1; |
Mark Salyzyn | 8470dad | 2015-03-06 20:42:57 +0000 | [diff] [blame] | 603 | lval = get8LE(eventData); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 604 | eventData += 8; |
| 605 | eventDataLen -= 8; |
| 606 | |
Jeff Brown | 44193d9 | 2015-04-28 12:47:02 -0700 | [diff] [blame] | 607 | outCount = snprintf(outBuf, outBufLen, "%" PRId64, lval); |
| 608 | if (outCount < outBufLen) { |
| 609 | outBuf += outCount; |
| 610 | outBufLen -= outCount; |
| 611 | } else { |
| 612 | /* halt output */ |
| 613 | goto no_room; |
| 614 | } |
| 615 | } |
| 616 | break; |
| 617 | case EVENT_TYPE_FLOAT: |
| 618 | /* float */ |
| 619 | { |
| 620 | uint32_t ival; |
| 621 | float fval; |
| 622 | |
| 623 | if (eventDataLen < 4) |
| 624 | return -1; |
| 625 | ival = get4LE(eventData); |
| 626 | fval = *(float*)&ival; |
| 627 | eventData += 4; |
| 628 | eventDataLen -= 4; |
| 629 | |
| 630 | outCount = snprintf(outBuf, outBufLen, "%f", fval); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 631 | if (outCount < outBufLen) { |
| 632 | outBuf += outCount; |
| 633 | outBufLen -= outCount; |
| 634 | } else { |
| 635 | /* halt output */ |
| 636 | goto no_room; |
| 637 | } |
| 638 | } |
| 639 | break; |
| 640 | case EVENT_TYPE_STRING: |
| 641 | /* UTF-8 chars, not NULL-terminated */ |
| 642 | { |
| 643 | unsigned int strLen; |
| 644 | |
| 645 | if (eventDataLen < 4) |
| 646 | return -1; |
Mark Salyzyn | 8470dad | 2015-03-06 20:42:57 +0000 | [diff] [blame] | 647 | strLen = get4LE(eventData); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 648 | eventData += 4; |
| 649 | eventDataLen -= 4; |
| 650 | |
| 651 | if (eventDataLen < strLen) |
| 652 | return -1; |
| 653 | |
| 654 | if (strLen < outBufLen) { |
| 655 | memcpy(outBuf, eventData, strLen); |
| 656 | outBuf += strLen; |
| 657 | outBufLen -= strLen; |
| 658 | } else if (outBufLen > 0) { |
| 659 | /* copy what we can */ |
| 660 | memcpy(outBuf, eventData, outBufLen); |
| 661 | outBuf += outBufLen; |
| 662 | outBufLen -= outBufLen; |
| 663 | goto no_room; |
| 664 | } |
| 665 | eventData += strLen; |
| 666 | eventDataLen -= strLen; |
| 667 | break; |
| 668 | } |
| 669 | case EVENT_TYPE_LIST: |
| 670 | /* N items, all different types */ |
| 671 | { |
| 672 | unsigned char count; |
| 673 | int i; |
| 674 | |
| 675 | if (eventDataLen < 1) |
| 676 | return -1; |
| 677 | |
| 678 | count = *eventData++; |
| 679 | eventDataLen--; |
| 680 | |
| 681 | if (outBufLen > 0) { |
| 682 | *outBuf++ = '['; |
| 683 | outBufLen--; |
| 684 | } else { |
| 685 | goto no_room; |
| 686 | } |
| 687 | |
| 688 | for (i = 0; i < count; i++) { |
| 689 | result = android_log_printBinaryEvent(&eventData, &eventDataLen, |
| 690 | &outBuf, &outBufLen); |
| 691 | if (result != 0) |
| 692 | goto bail; |
| 693 | |
| 694 | if (i < count-1) { |
| 695 | if (outBufLen > 0) { |
| 696 | *outBuf++ = ','; |
| 697 | outBufLen--; |
| 698 | } else { |
| 699 | goto no_room; |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | if (outBufLen > 0) { |
| 705 | *outBuf++ = ']'; |
| 706 | outBufLen--; |
| 707 | } else { |
| 708 | goto no_room; |
| 709 | } |
| 710 | } |
| 711 | break; |
| 712 | default: |
| 713 | fprintf(stderr, "Unknown binary event type %d\n", type); |
| 714 | return -1; |
| 715 | } |
| 716 | |
| 717 | bail: |
| 718 | *pEventData = eventData; |
| 719 | *pEventDataLen = eventDataLen; |
| 720 | *pOutBuf = outBuf; |
| 721 | *pOutBufLen = outBufLen; |
| 722 | return result; |
| 723 | |
| 724 | no_room: |
| 725 | result = 1; |
| 726 | goto bail; |
| 727 | } |
| 728 | |
| 729 | /** |
| 730 | * Convert a binary log entry to ASCII form. |
| 731 | * |
| 732 | * For convenience we mimic the processLogBuffer API. There is no |
| 733 | * pre-defined output length for the binary data, since we're free to format |
| 734 | * it however we choose, which means we can't really use a fixed-size buffer |
| 735 | * here. |
| 736 | */ |
| 737 | int android_log_processBinaryLogBuffer(struct logger_entry *buf, |
| 738 | AndroidLogEntry *entry, const EventTagMap* map, char* messageBuf, |
| 739 | int messageBufLen) |
| 740 | { |
| 741 | size_t inCount; |
| 742 | unsigned int tagIndex; |
| 743 | const unsigned char* eventData; |
| 744 | |
| 745 | entry->tv_sec = buf->sec; |
| 746 | entry->tv_nsec = buf->nsec; |
| 747 | entry->priority = ANDROID_LOG_INFO; |
Mark Salyzyn | 90e7af3 | 2015-12-07 16:52:42 -0800 | [diff] [blame] | 748 | entry->uid = -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 749 | entry->pid = buf->pid; |
| 750 | entry->tid = buf->tid; |
| 751 | |
| 752 | /* |
Mark Salyzyn | 7bc8023 | 2015-12-11 12:32:53 -0800 | [diff] [blame^] | 753 | * Pull the tag out, fill in some additional details based on incoming |
| 754 | * buffer version (v3 adds lid, v4 adds uid). |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 755 | */ |
| 756 | eventData = (const unsigned char*) buf->msg; |
Mark Salyzyn | 40b2155 | 2013-12-18 12:59:01 -0800 | [diff] [blame] | 757 | struct logger_entry_v2 *buf2 = (struct logger_entry_v2 *)buf; |
| 758 | if (buf2->hdr_size) { |
| 759 | eventData = ((unsigned char *)buf2) + buf2->hdr_size; |
Mark Salyzyn | 7bc8023 | 2015-12-11 12:32:53 -0800 | [diff] [blame^] | 760 | if ((buf2->hdr_size >= sizeof(struct logger_entry_v3)) && |
| 761 | (((struct logger_entry_v3 *)buf)->lid == LOG_ID_SECURITY)) { |
| 762 | entry->priority = ANDROID_LOG_WARN; |
| 763 | } |
Mark Salyzyn | 90e7af3 | 2015-12-07 16:52:42 -0800 | [diff] [blame] | 764 | if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) { |
| 765 | entry->uid = ((struct logger_entry_v4 *)buf)->uid; |
| 766 | } |
Mark Salyzyn | 40b2155 | 2013-12-18 12:59:01 -0800 | [diff] [blame] | 767 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 768 | inCount = buf->len; |
| 769 | if (inCount < 4) |
| 770 | return -1; |
Mark Salyzyn | 8470dad | 2015-03-06 20:42:57 +0000 | [diff] [blame] | 771 | tagIndex = get4LE(eventData); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 772 | eventData += 4; |
| 773 | inCount -= 4; |
| 774 | |
| 775 | if (map != NULL) { |
| 776 | entry->tag = android_lookupEventTag(map, tagIndex); |
| 777 | } else { |
| 778 | entry->tag = NULL; |
| 779 | } |
| 780 | |
| 781 | /* |
| 782 | * If we don't have a map, or didn't find the tag number in the map, |
| 783 | * stuff a generated tag value into the start of the output buffer and |
| 784 | * shift the buffer pointers down. |
| 785 | */ |
| 786 | if (entry->tag == NULL) { |
| 787 | int tagLen; |
| 788 | |
| 789 | tagLen = snprintf(messageBuf, messageBufLen, "[%d]", tagIndex); |
| 790 | entry->tag = messageBuf; |
| 791 | messageBuf += tagLen+1; |
| 792 | messageBufLen -= tagLen+1; |
| 793 | } |
| 794 | |
| 795 | /* |
| 796 | * Format the event log data into the buffer. |
| 797 | */ |
| 798 | char* outBuf = messageBuf; |
| 799 | size_t outRemaining = messageBufLen-1; /* leave one for nul byte */ |
| 800 | int result; |
| 801 | result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, |
| 802 | &outRemaining); |
| 803 | if (result < 0) { |
| 804 | fprintf(stderr, "Binary log entry conversion failed\n"); |
| 805 | return -1; |
| 806 | } else if (result == 1) { |
| 807 | if (outBuf > messageBuf) { |
| 808 | /* leave an indicator */ |
| 809 | *(outBuf-1) = '!'; |
| 810 | } else { |
| 811 | /* no room to output anything at all */ |
| 812 | *outBuf++ = '!'; |
| 813 | outRemaining--; |
| 814 | } |
| 815 | /* pretend we ate all the data */ |
| 816 | inCount = 0; |
| 817 | } |
| 818 | |
| 819 | /* eat the silly terminating '\n' */ |
| 820 | if (inCount == 1 && *eventData == '\n') { |
| 821 | eventData++; |
| 822 | inCount--; |
| 823 | } |
| 824 | |
| 825 | if (inCount != 0) { |
| 826 | fprintf(stderr, |
Andrew Hsieh | d2c8f52 | 2012-02-27 16:48:18 -0800 | [diff] [blame] | 827 | "Warning: leftover binary log data (%zu bytes)\n", inCount); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | /* |
| 831 | * Terminate the buffer. The NUL byte does not count as part of |
| 832 | * entry->messageLen. |
| 833 | */ |
| 834 | *outBuf = '\0'; |
| 835 | entry->messageLen = outBuf - messageBuf; |
| 836 | assert(entry->messageLen == (messageBufLen-1) - outRemaining); |
| 837 | |
| 838 | entry->message = messageBuf; |
| 839 | |
| 840 | return 0; |
| 841 | } |
| 842 | |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 843 | /* |
| 844 | * One utf8 character at a time |
| 845 | * |
| 846 | * Returns the length of the utf8 character in the buffer, |
| 847 | * or -1 if illegal or truncated |
| 848 | * |
| 849 | * Open coded from libutils/Unicode.cpp, borrowed from utf8_length(), |
| 850 | * can not remove from here because of library circular dependencies. |
| 851 | * Expect one-day utf8_character_length with the same signature could |
| 852 | * _also_ be part of libutils/Unicode.cpp if its usefullness needs to |
| 853 | * propagate globally. |
| 854 | */ |
| 855 | WEAK ssize_t utf8_character_length(const char *src, size_t len) |
| 856 | { |
| 857 | const char *cur = src; |
| 858 | const char first_char = *cur++; |
| 859 | static const uint32_t kUnicodeMaxCodepoint = 0x0010FFFF; |
| 860 | int32_t mask, to_ignore_mask; |
| 861 | size_t num_to_read; |
| 862 | uint32_t utf32; |
| 863 | |
| 864 | if ((first_char & 0x80) == 0) { /* ASCII */ |
Mark Salyzyn | faa92e9 | 2015-09-08 07:57:27 -0700 | [diff] [blame] | 865 | return first_char ? 1 : -1; |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | /* |
| 869 | * (UTF-8's character must not be like 10xxxxxx, |
| 870 | * but 110xxxxx, 1110xxxx, ... or 1111110x) |
| 871 | */ |
| 872 | if ((first_char & 0x40) == 0) { |
| 873 | return -1; |
| 874 | } |
| 875 | |
| 876 | for (utf32 = 1, num_to_read = 1, mask = 0x40, to_ignore_mask = 0x80; |
| 877 | num_to_read < 5 && (first_char & mask); |
| 878 | num_to_read++, to_ignore_mask |= mask, mask >>= 1) { |
| 879 | if (num_to_read > len) { |
| 880 | return -1; |
| 881 | } |
| 882 | if ((*cur & 0xC0) != 0x80) { /* can not be 10xxxxxx? */ |
| 883 | return -1; |
| 884 | } |
| 885 | utf32 = (utf32 << 6) + (*cur++ & 0b00111111); |
| 886 | } |
| 887 | /* "first_char" must be (110xxxxx - 11110xxx) */ |
| 888 | if (num_to_read >= 5) { |
| 889 | return -1; |
| 890 | } |
| 891 | to_ignore_mask |= mask; |
| 892 | utf32 |= ((~to_ignore_mask) & first_char) << (6 * (num_to_read - 1)); |
| 893 | if (utf32 > kUnicodeMaxCodepoint) { |
| 894 | return -1; |
| 895 | } |
| 896 | return num_to_read; |
| 897 | } |
| 898 | |
| 899 | /* |
| 900 | * Convert to printable from message to p buffer, return string length. If p is |
| 901 | * NULL, do not copy, but still return the expected string length. |
| 902 | */ |
| 903 | static size_t convertPrintable(char *p, const char *message, size_t messageLen) |
| 904 | { |
| 905 | char *begin = p; |
| 906 | bool print = p != NULL; |
| 907 | |
| 908 | while (messageLen) { |
| 909 | char buf[6]; |
| 910 | ssize_t len = sizeof(buf) - 1; |
| 911 | if ((size_t)len > messageLen) { |
| 912 | len = messageLen; |
| 913 | } |
| 914 | len = utf8_character_length(message, len); |
| 915 | |
| 916 | if (len < 0) { |
| 917 | snprintf(buf, sizeof(buf), |
| 918 | ((messageLen > 1) && isdigit(message[1])) |
| 919 | ? "\\%03o" |
| 920 | : "\\%o", |
| 921 | *message & 0377); |
| 922 | len = 1; |
| 923 | } else { |
| 924 | buf[0] = '\0'; |
| 925 | if (len == 1) { |
| 926 | if (*message == '\a') { |
| 927 | strcpy(buf, "\\a"); |
| 928 | } else if (*message == '\b') { |
| 929 | strcpy(buf, "\\b"); |
| 930 | } else if (*message == '\t') { |
Mark Salyzyn | 65d5ca2 | 2015-11-18 09:58:00 -0800 | [diff] [blame] | 931 | strcpy(buf, "\t"); // Do not escape tabs |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 932 | } else if (*message == '\v') { |
| 933 | strcpy(buf, "\\v"); |
| 934 | } else if (*message == '\f') { |
| 935 | strcpy(buf, "\\f"); |
| 936 | } else if (*message == '\r') { |
| 937 | strcpy(buf, "\\r"); |
| 938 | } else if (*message == '\\') { |
| 939 | strcpy(buf, "\\\\"); |
| 940 | } else if ((*message < ' ') || (*message & 0x80)) { |
| 941 | snprintf(buf, sizeof(buf), "\\%o", *message & 0377); |
| 942 | } |
| 943 | } |
| 944 | if (!buf[0]) { |
| 945 | strncpy(buf, message, len); |
| 946 | buf[len] = '\0'; |
| 947 | } |
| 948 | } |
| 949 | if (print) { |
| 950 | strcpy(p, buf); |
| 951 | } |
| 952 | p += strlen(buf); |
| 953 | message += len; |
| 954 | messageLen -= len; |
| 955 | } |
| 956 | return p - begin; |
| 957 | } |
| 958 | |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 959 | char *readSeconds(char *e, struct timespec *t) |
| 960 | { |
| 961 | unsigned long multiplier; |
| 962 | char *p; |
| 963 | t->tv_sec = strtoul(e, &p, 10); |
| 964 | if (*p != '.') { |
| 965 | return NULL; |
| 966 | } |
| 967 | t->tv_nsec = 0; |
| 968 | multiplier = NS_PER_SEC; |
| 969 | while (isdigit(*++p) && (multiplier /= 10)) { |
| 970 | t->tv_nsec += (*p - '0') * multiplier; |
| 971 | } |
| 972 | return p; |
| 973 | } |
| 974 | |
| 975 | static struct timespec *sumTimespec(struct timespec *left, |
| 976 | struct timespec *right) |
| 977 | { |
| 978 | left->tv_nsec += right->tv_nsec; |
| 979 | left->tv_sec += right->tv_sec; |
| 980 | if (left->tv_nsec >= (long)NS_PER_SEC) { |
| 981 | left->tv_nsec -= NS_PER_SEC; |
| 982 | left->tv_sec += 1; |
| 983 | } |
| 984 | return left; |
| 985 | } |
| 986 | |
| 987 | static struct timespec *subTimespec(struct timespec *result, |
| 988 | struct timespec *left, |
| 989 | struct timespec *right) |
| 990 | { |
| 991 | result->tv_nsec = left->tv_nsec - right->tv_nsec; |
| 992 | result->tv_sec = left->tv_sec - right->tv_sec; |
| 993 | if (result->tv_nsec < 0) { |
| 994 | result->tv_nsec += NS_PER_SEC; |
| 995 | result->tv_sec -= 1; |
| 996 | } |
| 997 | return result; |
| 998 | } |
| 999 | |
| 1000 | static long long nsecTimespec(struct timespec *now) |
| 1001 | { |
| 1002 | return (long long)now->tv_sec * NS_PER_SEC + now->tv_nsec; |
| 1003 | } |
| 1004 | |
| 1005 | static void convertMonotonic(struct timespec *result, |
| 1006 | const AndroidLogEntry *entry) |
| 1007 | { |
| 1008 | struct listnode *node; |
| 1009 | struct conversionList { |
| 1010 | struct listnode node; /* first */ |
| 1011 | struct timespec time; |
| 1012 | struct timespec convert; |
| 1013 | } *list, *next; |
| 1014 | struct timespec time, convert; |
| 1015 | |
| 1016 | /* If we do not have a conversion list, build one up */ |
| 1017 | if (list_empty(&convertHead)) { |
| 1018 | bool suspended_pending = false; |
| 1019 | struct timespec suspended_monotonic = { 0, 0 }; |
| 1020 | struct timespec suspended_diff = { 0, 0 }; |
| 1021 | |
| 1022 | /* |
| 1023 | * Read dmesg for _some_ synchronization markers and insert |
| 1024 | * Anything in the Android Logger before the dmesg logging span will |
| 1025 | * be highly suspect regarding the monotonic time calculations. |
| 1026 | */ |
| 1027 | FILE *p = popen("/system/bin/dmesg", "r"); |
| 1028 | if (p) { |
| 1029 | char *line = NULL; |
| 1030 | size_t len = 0; |
| 1031 | while (getline(&line, &len, p) > 0) { |
| 1032 | static const char suspend[] = "PM: suspend entry "; |
| 1033 | static const char resume[] = "PM: suspend exit "; |
| 1034 | static const char healthd[] = "healthd"; |
| 1035 | static const char battery[] = ": battery "; |
| 1036 | static const char suspended[] = "Suspended for "; |
| 1037 | struct timespec monotonic; |
| 1038 | struct tm tm; |
| 1039 | char *cp, *e = line; |
| 1040 | bool add_entry = true; |
| 1041 | |
| 1042 | if (*e == '<') { |
| 1043 | while (*e && (*e != '>')) { |
| 1044 | ++e; |
| 1045 | } |
| 1046 | if (*e != '>') { |
| 1047 | continue; |
| 1048 | } |
| 1049 | } |
| 1050 | if (*e != '[') { |
| 1051 | continue; |
| 1052 | } |
| 1053 | while (*++e == ' ') { |
| 1054 | ; |
| 1055 | } |
| 1056 | e = readSeconds(e, &monotonic); |
| 1057 | if (!e || (*e != ']')) { |
| 1058 | continue; |
| 1059 | } |
| 1060 | |
| 1061 | if ((e = strstr(e, suspend))) { |
| 1062 | e += sizeof(suspend) - 1; |
| 1063 | } else if ((e = strstr(line, resume))) { |
| 1064 | e += sizeof(resume) - 1; |
| 1065 | } else if (((e = strstr(line, healthd))) |
| 1066 | && ((e = strstr(e + sizeof(healthd) - 1, battery)))) { |
| 1067 | /* NB: healthd is roughly 150us late, worth the price to |
| 1068 | * deal with ntp-induced or hardware clock drift. */ |
| 1069 | e += sizeof(battery) - 1; |
| 1070 | } else if ((e = strstr(line, suspended))) { |
| 1071 | e += sizeof(suspended) - 1; |
| 1072 | e = readSeconds(e, &time); |
| 1073 | if (!e) { |
| 1074 | continue; |
| 1075 | } |
| 1076 | add_entry = false; |
| 1077 | suspended_pending = true; |
| 1078 | suspended_monotonic = monotonic; |
| 1079 | suspended_diff = time; |
| 1080 | } else { |
| 1081 | continue; |
| 1082 | } |
| 1083 | if (add_entry) { |
| 1084 | /* look for "????-??-?? ??:??:??.????????? UTC" */ |
| 1085 | cp = strstr(e, " UTC"); |
| 1086 | if (!cp || ((cp - e) < 29) || (cp[-10] != '.')) { |
| 1087 | continue; |
| 1088 | } |
| 1089 | e = cp - 29; |
| 1090 | cp = readSeconds(cp - 10, &time); |
| 1091 | if (!cp) { |
| 1092 | continue; |
| 1093 | } |
| 1094 | cp = strptime(e, "%Y-%m-%d %H:%M:%S.", &tm); |
| 1095 | if (!cp) { |
| 1096 | continue; |
| 1097 | } |
| 1098 | cp = getenv(tz); |
| 1099 | if (cp) { |
| 1100 | cp = strdup(cp); |
| 1101 | } |
| 1102 | setenv(tz, utc, 1); |
| 1103 | time.tv_sec = mktime(&tm); |
| 1104 | if (cp) { |
| 1105 | setenv(tz, cp, 1); |
| 1106 | free(cp); |
| 1107 | } else { |
| 1108 | unsetenv(tz); |
| 1109 | } |
| 1110 | list = calloc(1, sizeof(struct conversionList)); |
| 1111 | list_init(&list->node); |
| 1112 | list->time = time; |
| 1113 | subTimespec(&list->convert, &time, &monotonic); |
| 1114 | list_add_tail(&convertHead, &list->node); |
| 1115 | } |
| 1116 | if (suspended_pending && !list_empty(&convertHead)) { |
| 1117 | list = node_to_item(list_tail(&convertHead), |
| 1118 | struct conversionList, node); |
| 1119 | if (subTimespec(&time, |
| 1120 | subTimespec(&time, |
| 1121 | &list->time, |
| 1122 | &list->convert), |
| 1123 | &suspended_monotonic)->tv_sec > 0) { |
| 1124 | /* resume, what is convert factor before? */ |
| 1125 | subTimespec(&convert, &list->convert, &suspended_diff); |
| 1126 | } else { |
| 1127 | /* suspend */ |
| 1128 | convert = list->convert; |
| 1129 | } |
| 1130 | time = suspended_monotonic; |
| 1131 | sumTimespec(&time, &convert); |
| 1132 | /* breakpoint just before sleep */ |
| 1133 | list = calloc(1, sizeof(struct conversionList)); |
| 1134 | list_init(&list->node); |
| 1135 | list->time = time; |
| 1136 | list->convert = convert; |
| 1137 | list_add_tail(&convertHead, &list->node); |
| 1138 | /* breakpoint just after sleep */ |
| 1139 | list = calloc(1, sizeof(struct conversionList)); |
| 1140 | list_init(&list->node); |
| 1141 | list->time = time; |
| 1142 | sumTimespec(&list->time, &suspended_diff); |
| 1143 | list->convert = convert; |
| 1144 | sumTimespec(&list->convert, &suspended_diff); |
| 1145 | list_add_tail(&convertHead, &list->node); |
| 1146 | suspended_pending = false; |
| 1147 | } |
| 1148 | } |
| 1149 | pclose(p); |
| 1150 | } |
| 1151 | /* last entry is our current time conversion */ |
| 1152 | list = calloc(1, sizeof(struct conversionList)); |
| 1153 | list_init(&list->node); |
| 1154 | clock_gettime(CLOCK_REALTIME, &list->time); |
| 1155 | clock_gettime(CLOCK_MONOTONIC, &convert); |
| 1156 | clock_gettime(CLOCK_MONOTONIC, &time); |
| 1157 | /* Correct for instant clock_gettime latency (syscall or ~30ns) */ |
| 1158 | subTimespec(&time, &convert, subTimespec(&time, &time, &convert)); |
| 1159 | /* Calculate conversion factor */ |
| 1160 | subTimespec(&list->convert, &list->time, &time); |
| 1161 | list_add_tail(&convertHead, &list->node); |
| 1162 | if (suspended_pending) { |
| 1163 | /* manufacture a suspend @ point before */ |
| 1164 | subTimespec(&convert, &list->convert, &suspended_diff); |
| 1165 | time = suspended_monotonic; |
| 1166 | sumTimespec(&time, &convert); |
| 1167 | /* breakpoint just after sleep */ |
| 1168 | list = calloc(1, sizeof(struct conversionList)); |
| 1169 | list_init(&list->node); |
| 1170 | list->time = time; |
| 1171 | sumTimespec(&list->time, &suspended_diff); |
| 1172 | list->convert = convert; |
| 1173 | sumTimespec(&list->convert, &suspended_diff); |
| 1174 | list_add_head(&convertHead, &list->node); |
| 1175 | /* breakpoint just before sleep */ |
| 1176 | list = calloc(1, sizeof(struct conversionList)); |
| 1177 | list_init(&list->node); |
| 1178 | list->time = time; |
| 1179 | list->convert = convert; |
| 1180 | list_add_head(&convertHead, &list->node); |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | /* Find the breakpoint in the conversion list */ |
| 1185 | list = node_to_item(list_head(&convertHead), struct conversionList, node); |
| 1186 | next = NULL; |
| 1187 | list_for_each(node, &convertHead) { |
| 1188 | next = node_to_item(node, struct conversionList, node); |
| 1189 | if (entry->tv_sec < next->time.tv_sec) { |
| 1190 | break; |
| 1191 | } else if (entry->tv_sec == next->time.tv_sec) { |
| 1192 | if (entry->tv_nsec < next->time.tv_nsec) { |
| 1193 | break; |
| 1194 | } |
| 1195 | } |
| 1196 | list = next; |
| 1197 | } |
| 1198 | |
| 1199 | /* blend time from one breakpoint to the next */ |
| 1200 | convert = list->convert; |
| 1201 | if (next) { |
| 1202 | unsigned long long total, run; |
| 1203 | |
| 1204 | total = nsecTimespec(subTimespec(&time, &next->time, &list->time)); |
| 1205 | time.tv_sec = entry->tv_sec; |
| 1206 | time.tv_nsec = entry->tv_nsec; |
| 1207 | run = nsecTimespec(subTimespec(&time, &time, &list->time)); |
| 1208 | if (run < total) { |
| 1209 | long long crun; |
| 1210 | |
| 1211 | float f = nsecTimespec(subTimespec(&time, &next->convert, &convert)); |
| 1212 | f *= run; |
| 1213 | f /= total; |
| 1214 | crun = f; |
| 1215 | convert.tv_sec += crun / (long long)NS_PER_SEC; |
| 1216 | if (crun < 0) { |
| 1217 | convert.tv_nsec -= (-crun) % NS_PER_SEC; |
| 1218 | if (convert.tv_nsec < 0) { |
| 1219 | convert.tv_nsec += NS_PER_SEC; |
| 1220 | convert.tv_sec -= 1; |
| 1221 | } |
| 1222 | } else { |
| 1223 | convert.tv_nsec += crun % NS_PER_SEC; |
| 1224 | if (convert.tv_nsec >= (long)NS_PER_SEC) { |
| 1225 | convert.tv_nsec -= NS_PER_SEC; |
| 1226 | convert.tv_sec += 1; |
| 1227 | } |
| 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | /* Apply the correction factor */ |
| 1233 | result->tv_sec = entry->tv_sec; |
| 1234 | result->tv_nsec = entry->tv_nsec; |
| 1235 | subTimespec(result, result, &convert); |
| 1236 | } |
| 1237 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1238 | /** |
| 1239 | * Formats a log message into a buffer |
| 1240 | * |
| 1241 | * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer |
| 1242 | * If return value != defaultBuffer, caller must call free() |
| 1243 | * Returns NULL on malloc error |
| 1244 | */ |
| 1245 | |
| 1246 | char *android_log_formatLogLine ( |
| 1247 | AndroidLogFormat *p_format, |
| 1248 | char *defaultBuffer, |
| 1249 | size_t defaultBufferSize, |
| 1250 | const AndroidLogEntry *entry, |
| 1251 | size_t *p_outLength) |
| 1252 | { |
Yabin Cui | 8a98535 | 2014-11-13 10:02:08 -0800 | [diff] [blame] | 1253 | #if !defined(_WIN32) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1254 | struct tm tmBuf; |
| 1255 | #endif |
| 1256 | struct tm* ptm; |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 1257 | char timeBuf[64]; /* good margin, 23+nul for msec, 26+nul for usec */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1258 | char prefixBuf[128], suffixBuf[128]; |
| 1259 | char priChar; |
| 1260 | int prefixSuffixIsHeaderFooter = 0; |
Mark Salyzyn | 90e7af3 | 2015-12-07 16:52:42 -0800 | [diff] [blame] | 1261 | char *ret; |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1262 | time_t now; |
| 1263 | unsigned long nsec; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1264 | |
| 1265 | priChar = filterPriToChar(entry->priority); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 1266 | size_t prefixLen = 0, suffixLen = 0; |
| 1267 | size_t len; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1268 | |
| 1269 | /* |
| 1270 | * Get the current date/time in pretty form |
| 1271 | * |
| 1272 | * It's often useful when examining a log with "less" to jump to |
| 1273 | * a specific point in the file by searching for the date/time stamp. |
| 1274 | * For this reason it's very annoying to have regexp meta characters |
| 1275 | * in the time stamp. Don't use forward slashes, parenthesis, |
| 1276 | * brackets, asterisks, or other special chars here. |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 1277 | * |
| 1278 | * The caller may have affected the timezone environment, this is |
| 1279 | * expected to be sensitive to that. |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1280 | */ |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1281 | now = entry->tv_sec; |
| 1282 | nsec = entry->tv_nsec; |
| 1283 | if (p_format->monotonic_output) { |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 1284 | // prevent convertMonotonic from being called if logd is monotonic |
Mark Salyzyn | ba7a9a0 | 2015-12-01 15:57:25 -0800 | [diff] [blame] | 1285 | if (android_log_clockid() != CLOCK_MONOTONIC) { |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 1286 | struct timespec time; |
| 1287 | convertMonotonic(&time, entry); |
| 1288 | now = time.tv_sec; |
| 1289 | nsec = time.tv_nsec; |
| 1290 | } |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1291 | } |
| 1292 | if (now < 0) { |
| 1293 | nsec = NS_PER_SEC - nsec; |
| 1294 | } |
| 1295 | if (p_format->epoch_output || p_format->monotonic_output) { |
| 1296 | ptm = NULL; |
| 1297 | snprintf(timeBuf, sizeof(timeBuf), |
| 1298 | p_format->monotonic_output ? "%6lld" : "%19lld", |
| 1299 | (long long)now); |
| 1300 | } else { |
Yabin Cui | 8a98535 | 2014-11-13 10:02:08 -0800 | [diff] [blame] | 1301 | #if !defined(_WIN32) |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1302 | ptm = localtime_r(&now, &tmBuf); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1303 | #else |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1304 | ptm = localtime(&now); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1305 | #endif |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1306 | strftime(timeBuf, sizeof(timeBuf), |
| 1307 | &"%Y-%m-%d %H:%M:%S"[p_format->year_output ? 0 : 3], |
| 1308 | ptm); |
| 1309 | } |
Mark Salyzyn | e1f2004 | 2015-05-06 08:40:40 -0700 | [diff] [blame] | 1310 | len = strlen(timeBuf); |
| 1311 | if (p_format->usec_time_output) { |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 1312 | len += snprintf(timeBuf + len, sizeof(timeBuf) - len, |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1313 | ".%06ld", nsec / US_PER_NSEC); |
Mark Salyzyn | e1f2004 | 2015-05-06 08:40:40 -0700 | [diff] [blame] | 1314 | } else { |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 1315 | len += snprintf(timeBuf + len, sizeof(timeBuf) - len, |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1316 | ".%03ld", nsec / MS_PER_NSEC); |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 1317 | } |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1318 | if (p_format->zone_output && ptm) { |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 1319 | strftime(timeBuf + len, sizeof(timeBuf) - len, " %z", ptm); |
Mark Salyzyn | e1f2004 | 2015-05-06 08:40:40 -0700 | [diff] [blame] | 1320 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1321 | |
| 1322 | /* |
| 1323 | * Construct a buffer containing the log header and log message. |
| 1324 | */ |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 1325 | if (p_format->colored_output) { |
| 1326 | prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm", |
| 1327 | colorFromPri(entry->priority)); |
| 1328 | prefixLen = MIN(prefixLen, sizeof(prefixBuf)); |
| 1329 | suffixLen = snprintf(suffixBuf, sizeof(suffixBuf), "\x1B[0m"); |
| 1330 | suffixLen = MIN(suffixLen, sizeof(suffixBuf)); |
| 1331 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1332 | |
Mark Salyzyn | 90e7af3 | 2015-12-07 16:52:42 -0800 | [diff] [blame] | 1333 | char uid[16]; |
| 1334 | uid[0] = '\0'; |
| 1335 | if (p_format->uid_output) { |
| 1336 | if (entry->uid >= 0) { |
Mark Salyzyn | 2aa510e | 2015-12-08 09:15:06 -0800 | [diff] [blame] | 1337 | const struct android_id_info *info = android_ids; |
| 1338 | size_t i; |
| 1339 | |
| 1340 | for (i = 0; i < android_id_count; ++i) { |
| 1341 | if (info->aid == (unsigned int)entry->uid) { |
| 1342 | break; |
| 1343 | } |
| 1344 | ++info; |
| 1345 | } |
| 1346 | if ((i < android_id_count) && (strlen(info->name) <= 5)) { |
| 1347 | snprintf(uid, sizeof(uid), "%5s:", info->name); |
| 1348 | } else { |
| 1349 | // Not worth parsing package list, names all longer than 5 |
| 1350 | snprintf(uid, sizeof(uid), "%5d:", entry->uid); |
| 1351 | } |
Mark Salyzyn | 90e7af3 | 2015-12-07 16:52:42 -0800 | [diff] [blame] | 1352 | } else { |
| 1353 | snprintf(uid, sizeof(uid), " "); |
| 1354 | } |
| 1355 | } |
| 1356 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1357 | switch (p_format->format) { |
| 1358 | case FORMAT_TAG: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 1359 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1360 | "%c/%-8s: ", priChar, entry->tag); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 1361 | strcpy(suffixBuf + suffixLen, "\n"); |
| 1362 | ++suffixLen; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1363 | break; |
| 1364 | case FORMAT_PROCESS: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 1365 | len = snprintf(suffixBuf + suffixLen, sizeof(suffixBuf) - suffixLen, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1366 | " (%s)\n", entry->tag); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 1367 | suffixLen += MIN(len, sizeof(suffixBuf) - suffixLen); |
| 1368 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
Mark Salyzyn | 90e7af3 | 2015-12-07 16:52:42 -0800 | [diff] [blame] | 1369 | "%c(%s%5d) ", priChar, uid, entry->pid); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1370 | break; |
| 1371 | case FORMAT_THREAD: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 1372 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
Mark Salyzyn | 90e7af3 | 2015-12-07 16:52:42 -0800 | [diff] [blame] | 1373 | "%c(%s%5d:%5d) ", priChar, uid, entry->pid, entry->tid); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 1374 | strcpy(suffixBuf + suffixLen, "\n"); |
| 1375 | ++suffixLen; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1376 | break; |
| 1377 | case FORMAT_RAW: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 1378 | prefixBuf[prefixLen] = 0; |
| 1379 | len = 0; |
| 1380 | strcpy(suffixBuf + suffixLen, "\n"); |
| 1381 | ++suffixLen; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1382 | break; |
| 1383 | case FORMAT_TIME: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 1384 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
Mark Salyzyn | 90e7af3 | 2015-12-07 16:52:42 -0800 | [diff] [blame] | 1385 | "%s %c/%-8s(%s%5d): ", timeBuf, priChar, entry->tag, |
| 1386 | uid, entry->pid); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 1387 | strcpy(suffixBuf + suffixLen, "\n"); |
| 1388 | ++suffixLen; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1389 | break; |
| 1390 | case FORMAT_THREADTIME: |
Mark Salyzyn | 90e7af3 | 2015-12-07 16:52:42 -0800 | [diff] [blame] | 1391 | ret = strchr(uid, ':'); |
| 1392 | if (ret) { |
| 1393 | *ret = ' '; |
| 1394 | } |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 1395 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
Mark Salyzyn | 90e7af3 | 2015-12-07 16:52:42 -0800 | [diff] [blame] | 1396 | "%s %s%5d %5d %c %-8s: ", timeBuf, |
| 1397 | uid, entry->pid, entry->tid, priChar, entry->tag); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 1398 | strcpy(suffixBuf + suffixLen, "\n"); |
| 1399 | ++suffixLen; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1400 | break; |
| 1401 | case FORMAT_LONG: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 1402 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
Mark Salyzyn | 90e7af3 | 2015-12-07 16:52:42 -0800 | [diff] [blame] | 1403 | "[ %s %s%5d:%5d %c/%-8s ]\n", |
| 1404 | timeBuf, uid, entry->pid, entry->tid, priChar, entry->tag); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 1405 | strcpy(suffixBuf + suffixLen, "\n\n"); |
| 1406 | suffixLen += 2; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1407 | prefixSuffixIsHeaderFooter = 1; |
| 1408 | break; |
| 1409 | case FORMAT_BRIEF: |
| 1410 | default: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 1411 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
Mark Salyzyn | 90e7af3 | 2015-12-07 16:52:42 -0800 | [diff] [blame] | 1412 | "%c/%-8s(%s%5d): ", priChar, entry->tag, uid, entry->pid); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 1413 | strcpy(suffixBuf + suffixLen, "\n"); |
| 1414 | ++suffixLen; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1415 | break; |
| 1416 | } |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 1417 | |
Keith Preston | b45b5c9 | 2010-02-11 15:12:53 -0600 | [diff] [blame] | 1418 | /* snprintf has a weird return value. It returns what would have been |
| 1419 | * written given a large enough buffer. In the case that the prefix is |
| 1420 | * longer then our buffer(128), it messes up the calculations below |
| 1421 | * possibly causing heap corruption. To avoid this we double check and |
| 1422 | * set the length at the maximum (size minus null byte) |
| 1423 | */ |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 1424 | prefixLen += MIN(len, sizeof(prefixBuf) - prefixLen); |
Mark Salyzyn | e242842 | 2015-01-22 10:00:04 -0800 | [diff] [blame] | 1425 | suffixLen = MIN(suffixLen, sizeof(suffixBuf)); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1426 | |
| 1427 | /* the following code is tragically unreadable */ |
| 1428 | |
| 1429 | size_t numLines; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1430 | char *p; |
| 1431 | size_t bufferSize; |
| 1432 | const char *pm; |
| 1433 | |
| 1434 | if (prefixSuffixIsHeaderFooter) { |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1435 | /* we're just wrapping message with a header/footer */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1436 | numLines = 1; |
| 1437 | } else { |
| 1438 | pm = entry->message; |
| 1439 | numLines = 0; |
| 1440 | |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1441 | /* |
| 1442 | * The line-end finding here must match the line-end finding |
| 1443 | * in for ( ... numLines...) loop below |
| 1444 | */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1445 | while (pm < (entry->message + entry->messageLen)) { |
| 1446 | if (*pm++ == '\n') numLines++; |
| 1447 | } |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1448 | /* plus one line for anything not newline-terminated at the end */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1449 | if (pm > entry->message && *(pm-1) != '\n') numLines++; |
| 1450 | } |
| 1451 | |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1452 | /* |
| 1453 | * this is an upper bound--newlines in message may be counted |
| 1454 | * extraneously |
| 1455 | */ |
| 1456 | bufferSize = (numLines * (prefixLen + suffixLen)) + 1; |
| 1457 | if (p_format->printable_output) { |
| 1458 | /* Calculate extra length to convert non-printable to printable */ |
| 1459 | bufferSize += convertPrintable(NULL, entry->message, entry->messageLen); |
| 1460 | } else { |
| 1461 | bufferSize += entry->messageLen; |
| 1462 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1463 | |
| 1464 | if (defaultBufferSize >= bufferSize) { |
| 1465 | ret = defaultBuffer; |
| 1466 | } else { |
| 1467 | ret = (char *)malloc(bufferSize); |
| 1468 | |
| 1469 | if (ret == NULL) { |
| 1470 | return ret; |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | ret[0] = '\0'; /* to start strcat off */ |
| 1475 | |
| 1476 | p = ret; |
| 1477 | pm = entry->message; |
| 1478 | |
| 1479 | if (prefixSuffixIsHeaderFooter) { |
| 1480 | strcat(p, prefixBuf); |
| 1481 | p += prefixLen; |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1482 | if (p_format->printable_output) { |
| 1483 | p += convertPrintable(p, entry->message, entry->messageLen); |
| 1484 | } else { |
| 1485 | strncat(p, entry->message, entry->messageLen); |
| 1486 | p += entry->messageLen; |
| 1487 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1488 | strcat(p, suffixBuf); |
| 1489 | p += suffixLen; |
| 1490 | } else { |
| 1491 | while(pm < (entry->message + entry->messageLen)) { |
| 1492 | const char *lineStart; |
| 1493 | size_t lineLen; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1494 | lineStart = pm; |
| 1495 | |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1496 | /* Find the next end-of-line in message */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1497 | while (pm < (entry->message + entry->messageLen) |
| 1498 | && *pm != '\n') pm++; |
| 1499 | lineLen = pm - lineStart; |
| 1500 | |
| 1501 | strcat(p, prefixBuf); |
| 1502 | p += prefixLen; |
Mark Salyzyn | b932b2f | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1503 | if (p_format->printable_output) { |
| 1504 | p += convertPrintable(p, lineStart, lineLen); |
| 1505 | } else { |
| 1506 | strncat(p, lineStart, lineLen); |
| 1507 | p += lineLen; |
| 1508 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1509 | strcat(p, suffixBuf); |
| 1510 | p += suffixLen; |
| 1511 | |
| 1512 | if (*pm == '\n') pm++; |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | if (p_outLength != NULL) { |
| 1517 | *p_outLength = p - ret; |
| 1518 | } |
| 1519 | |
| 1520 | return ret; |
| 1521 | } |
| 1522 | |
| 1523 | /** |
| 1524 | * Either print or do not print log line, based on filter |
| 1525 | * |
| 1526 | * Returns count bytes written |
| 1527 | */ |
| 1528 | |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 1529 | int android_log_printLogLine( |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1530 | AndroidLogFormat *p_format, |
| 1531 | int fd, |
| 1532 | const AndroidLogEntry *entry) |
| 1533 | { |
| 1534 | int ret; |
| 1535 | char defaultBuffer[512]; |
| 1536 | char *outBuffer = NULL; |
| 1537 | size_t totalLen; |
| 1538 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1539 | outBuffer = android_log_formatLogLine(p_format, defaultBuffer, |
| 1540 | sizeof(defaultBuffer), entry, &totalLen); |
| 1541 | |
| 1542 | if (!outBuffer) |
| 1543 | return -1; |
| 1544 | |
| 1545 | do { |
| 1546 | ret = write(fd, outBuffer, totalLen); |
| 1547 | } while (ret < 0 && errno == EINTR); |
| 1548 | |
| 1549 | if (ret < 0) { |
| 1550 | fprintf(stderr, "+++ LOG: write failed (errno=%d)\n", errno); |
| 1551 | ret = 0; |
| 1552 | goto done; |
| 1553 | } |
| 1554 | |
| 1555 | if (((size_t)ret) < totalLen) { |
| 1556 | fprintf(stderr, "+++ LOG: write partial (%d of %d)\n", ret, |
| 1557 | (int)totalLen); |
| 1558 | goto done; |
| 1559 | } |
| 1560 | |
| 1561 | done: |
| 1562 | if (outBuffer != defaultBuffer) { |
| 1563 | free(outBuffer); |
| 1564 | } |
| 1565 | |
| 1566 | return ret; |
| 1567 | } |