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