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