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