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