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