Mark Salyzyn | cf4aa03 | 2013-11-22 07:54:30 -0800 | [diff] [blame] | 1 | /* |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2 | ** |
Mark Salyzyn | 40b2155 | 2013-12-18 12:59:01 -0800 | [diff] [blame] | 3 | ** Copyright 2006-2014, The Android Open Source Project |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE /* for asprintf */ |
| 19 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | #include <arpa/inet.h> |
Mark Salyzyn | a04464a | 2014-04-30 08:50:53 -0700 | [diff] [blame] | 21 | #include <assert.h> |
| 22 | #include <ctype.h> |
| 23 | #include <errno.h> |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 24 | #include <stdbool.h> |
Mark Salyzyn | a04464a | 2014-04-30 08:50:53 -0700 | [diff] [blame] | 25 | #include <stdint.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Jeff Brown | 44193d9 | 2015-04-28 12:47:02 -0700 | [diff] [blame] | 29 | #include <inttypes.h> |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 30 | #include <sys/param.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 31 | |
Colin Cross | 9227bd3 | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 32 | #include <log/logd.h> |
| 33 | #include <log/logprint.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 34 | |
| 35 | typedef struct FilterInfo_t { |
| 36 | char *mTag; |
| 37 | android_LogPriority mPri; |
| 38 | struct FilterInfo_t *p_next; |
| 39 | } FilterInfo; |
| 40 | |
| 41 | struct AndroidLogFormat_t { |
| 42 | android_LogPriority global_pri; |
| 43 | FilterInfo *filters; |
| 44 | AndroidLogPrintFormat format; |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 45 | bool colored_output; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 48 | /* |
| 49 | * gnome-terminal color tags |
| 50 | * See http://misc.flogisoft.com/bash/tip_colors_and_formatting |
| 51 | * for ideas on how to set the forground color of the text for xterm. |
| 52 | * The color manipulation character stream is defined as: |
| 53 | * ESC [ 3 8 ; 5 ; <color#> m |
| 54 | */ |
| 55 | #define ANDROID_COLOR_BLUE 75 |
| 56 | #define ANDROID_COLOR_DEFAULT 231 |
| 57 | #define ANDROID_COLOR_GREEN 40 |
| 58 | #define ANDROID_COLOR_ORANGE 166 |
| 59 | #define ANDROID_COLOR_RED 196 |
| 60 | #define ANDROID_COLOR_YELLOW 226 |
| 61 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 62 | static FilterInfo * filterinfo_new(const char * tag, android_LogPriority pri) |
| 63 | { |
| 64 | FilterInfo *p_ret; |
| 65 | |
| 66 | p_ret = (FilterInfo *)calloc(1, sizeof(FilterInfo)); |
| 67 | p_ret->mTag = strdup(tag); |
| 68 | p_ret->mPri = pri; |
| 69 | |
| 70 | return p_ret; |
| 71 | } |
| 72 | |
Mark Salyzyn | a04464a | 2014-04-30 08:50:53 -0700 | [diff] [blame] | 73 | /* balance to above, filterinfo_free left unimplemented */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * Note: also accepts 0-9 priorities |
| 77 | * returns ANDROID_LOG_UNKNOWN if the character is unrecognized |
| 78 | */ |
| 79 | static android_LogPriority filterCharToPri (char c) |
| 80 | { |
| 81 | android_LogPriority pri; |
| 82 | |
| 83 | c = tolower(c); |
| 84 | |
| 85 | if (c >= '0' && c <= '9') { |
| 86 | if (c >= ('0'+ANDROID_LOG_SILENT)) { |
| 87 | pri = ANDROID_LOG_VERBOSE; |
| 88 | } else { |
| 89 | pri = (android_LogPriority)(c - '0'); |
| 90 | } |
| 91 | } else if (c == 'v') { |
| 92 | pri = ANDROID_LOG_VERBOSE; |
| 93 | } else if (c == 'd') { |
| 94 | pri = ANDROID_LOG_DEBUG; |
| 95 | } else if (c == 'i') { |
| 96 | pri = ANDROID_LOG_INFO; |
| 97 | } else if (c == 'w') { |
| 98 | pri = ANDROID_LOG_WARN; |
| 99 | } else if (c == 'e') { |
| 100 | pri = ANDROID_LOG_ERROR; |
| 101 | } else if (c == 'f') { |
| 102 | pri = ANDROID_LOG_FATAL; |
| 103 | } else if (c == 's') { |
| 104 | pri = ANDROID_LOG_SILENT; |
| 105 | } else if (c == '*') { |
| 106 | pri = ANDROID_LOG_DEFAULT; |
| 107 | } else { |
| 108 | pri = ANDROID_LOG_UNKNOWN; |
| 109 | } |
| 110 | |
| 111 | return pri; |
| 112 | } |
| 113 | |
| 114 | static char filterPriToChar (android_LogPriority pri) |
| 115 | { |
| 116 | switch (pri) { |
| 117 | case ANDROID_LOG_VERBOSE: return 'V'; |
| 118 | case ANDROID_LOG_DEBUG: return 'D'; |
| 119 | case ANDROID_LOG_INFO: return 'I'; |
| 120 | case ANDROID_LOG_WARN: return 'W'; |
| 121 | case ANDROID_LOG_ERROR: return 'E'; |
| 122 | case ANDROID_LOG_FATAL: return 'F'; |
| 123 | case ANDROID_LOG_SILENT: return 'S'; |
| 124 | |
| 125 | case ANDROID_LOG_DEFAULT: |
| 126 | case ANDROID_LOG_UNKNOWN: |
| 127 | default: return '?'; |
| 128 | } |
| 129 | } |
| 130 | |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 131 | static int colorFromPri (android_LogPriority pri) |
| 132 | { |
| 133 | switch (pri) { |
| 134 | case ANDROID_LOG_VERBOSE: return ANDROID_COLOR_DEFAULT; |
| 135 | case ANDROID_LOG_DEBUG: return ANDROID_COLOR_BLUE; |
| 136 | case ANDROID_LOG_INFO: return ANDROID_COLOR_GREEN; |
| 137 | case ANDROID_LOG_WARN: return ANDROID_COLOR_ORANGE; |
| 138 | case ANDROID_LOG_ERROR: return ANDROID_COLOR_RED; |
| 139 | case ANDROID_LOG_FATAL: return ANDROID_COLOR_RED; |
| 140 | case ANDROID_LOG_SILENT: return ANDROID_COLOR_DEFAULT; |
| 141 | |
| 142 | case ANDROID_LOG_DEFAULT: |
| 143 | case ANDROID_LOG_UNKNOWN: |
| 144 | default: return ANDROID_COLOR_DEFAULT; |
| 145 | } |
| 146 | } |
| 147 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 148 | static android_LogPriority filterPriForTag( |
| 149 | AndroidLogFormat *p_format, const char *tag) |
| 150 | { |
| 151 | FilterInfo *p_curFilter; |
| 152 | |
| 153 | for (p_curFilter = p_format->filters |
| 154 | ; p_curFilter != NULL |
| 155 | ; p_curFilter = p_curFilter->p_next |
| 156 | ) { |
| 157 | if (0 == strcmp(tag, p_curFilter->mTag)) { |
| 158 | if (p_curFilter->mPri == ANDROID_LOG_DEFAULT) { |
| 159 | return p_format->global_pri; |
| 160 | } else { |
| 161 | return p_curFilter->mPri; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return p_format->global_pri; |
| 167 | } |
| 168 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 169 | /** |
| 170 | * returns 1 if this log line should be printed based on its priority |
| 171 | * and tag, and 0 if it should not |
| 172 | */ |
| 173 | int android_log_shouldPrintLine ( |
| 174 | AndroidLogFormat *p_format, const char *tag, android_LogPriority pri) |
| 175 | { |
| 176 | return pri >= filterPriForTag(p_format, tag); |
| 177 | } |
| 178 | |
| 179 | AndroidLogFormat *android_log_format_new() |
| 180 | { |
| 181 | AndroidLogFormat *p_ret; |
| 182 | |
| 183 | p_ret = calloc(1, sizeof(AndroidLogFormat)); |
| 184 | |
| 185 | p_ret->global_pri = ANDROID_LOG_VERBOSE; |
| 186 | p_ret->format = FORMAT_BRIEF; |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 187 | p_ret->colored_output = false; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 188 | |
| 189 | return p_ret; |
| 190 | } |
| 191 | |
| 192 | void android_log_format_free(AndroidLogFormat *p_format) |
| 193 | { |
| 194 | FilterInfo *p_info, *p_info_old; |
| 195 | |
| 196 | p_info = p_format->filters; |
| 197 | |
| 198 | while (p_info != NULL) { |
| 199 | p_info_old = p_info; |
| 200 | p_info = p_info->p_next; |
| 201 | |
| 202 | free(p_info_old); |
| 203 | } |
| 204 | |
| 205 | free(p_format); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | |
| 210 | void android_log_setPrintFormat(AndroidLogFormat *p_format, |
| 211 | AndroidLogPrintFormat format) |
| 212 | { |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 213 | if (format == FORMAT_COLOR) |
| 214 | p_format->colored_output = true; |
| 215 | else |
| 216 | p_format->format = format; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Returns FORMAT_OFF on invalid string |
| 221 | */ |
| 222 | AndroidLogPrintFormat android_log_formatFromString(const char * formatString) |
| 223 | { |
| 224 | static AndroidLogPrintFormat format; |
| 225 | |
| 226 | if (strcmp(formatString, "brief") == 0) format = FORMAT_BRIEF; |
| 227 | else if (strcmp(formatString, "process") == 0) format = FORMAT_PROCESS; |
| 228 | else if (strcmp(formatString, "tag") == 0) format = FORMAT_TAG; |
| 229 | else if (strcmp(formatString, "thread") == 0) format = FORMAT_THREAD; |
| 230 | else if (strcmp(formatString, "raw") == 0) format = FORMAT_RAW; |
| 231 | else if (strcmp(formatString, "time") == 0) format = FORMAT_TIME; |
| 232 | else if (strcmp(formatString, "threadtime") == 0) format = FORMAT_THREADTIME; |
| 233 | else if (strcmp(formatString, "long") == 0) format = FORMAT_LONG; |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 234 | else if (strcmp(formatString, "color") == 0) format = FORMAT_COLOR; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 235 | else format = FORMAT_OFF; |
| 236 | |
| 237 | return format; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * filterExpression: a single filter expression |
| 242 | * eg "AT:d" |
| 243 | * |
| 244 | * returns 0 on success and -1 on invalid expression |
| 245 | * |
| 246 | * Assumes single threaded execution |
| 247 | */ |
| 248 | |
| 249 | int android_log_addFilterRule(AndroidLogFormat *p_format, |
| 250 | const char *filterExpression) |
| 251 | { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 252 | size_t tagNameLength; |
| 253 | android_LogPriority pri = ANDROID_LOG_DEFAULT; |
| 254 | |
| 255 | tagNameLength = strcspn(filterExpression, ":"); |
| 256 | |
| 257 | if (tagNameLength == 0) { |
| 258 | goto error; |
| 259 | } |
| 260 | |
| 261 | if(filterExpression[tagNameLength] == ':') { |
| 262 | pri = filterCharToPri(filterExpression[tagNameLength+1]); |
| 263 | |
| 264 | if (pri == ANDROID_LOG_UNKNOWN) { |
| 265 | goto error; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | if(0 == strncmp("*", filterExpression, tagNameLength)) { |
| 270 | // This filter expression refers to the global filter |
| 271 | // The default level for this is DEBUG if the priority |
| 272 | // is unspecified |
| 273 | if (pri == ANDROID_LOG_DEFAULT) { |
| 274 | pri = ANDROID_LOG_DEBUG; |
| 275 | } |
| 276 | |
| 277 | p_format->global_pri = pri; |
| 278 | } else { |
| 279 | // for filter expressions that don't refer to the global |
| 280 | // filter, the default is verbose if the priority is unspecified |
| 281 | if (pri == ANDROID_LOG_DEFAULT) { |
| 282 | pri = ANDROID_LOG_VERBOSE; |
| 283 | } |
| 284 | |
| 285 | char *tagName; |
| 286 | |
| 287 | // Presently HAVE_STRNDUP is never defined, so the second case is always taken |
| 288 | // Darwin doesn't have strnup, everything else does |
| 289 | #ifdef HAVE_STRNDUP |
| 290 | tagName = strndup(filterExpression, tagNameLength); |
| 291 | #else |
| 292 | //a few extra bytes copied... |
| 293 | tagName = strdup(filterExpression); |
| 294 | tagName[tagNameLength] = '\0'; |
| 295 | #endif /*HAVE_STRNDUP*/ |
| 296 | |
| 297 | FilterInfo *p_fi = filterinfo_new(tagName, pri); |
| 298 | free(tagName); |
| 299 | |
| 300 | p_fi->p_next = p_format->filters; |
| 301 | p_format->filters = p_fi; |
| 302 | } |
| 303 | |
| 304 | return 0; |
| 305 | error: |
| 306 | return -1; |
| 307 | } |
| 308 | |
| 309 | |
| 310 | /** |
| 311 | * filterString: a comma/whitespace-separated set of filter expressions |
| 312 | * |
| 313 | * eg "AT:d *:i" |
| 314 | * |
| 315 | * returns 0 on success and -1 on invalid expression |
| 316 | * |
| 317 | * Assumes single threaded execution |
| 318 | * |
| 319 | */ |
| 320 | |
| 321 | int android_log_addFilterString(AndroidLogFormat *p_format, |
| 322 | const char *filterString) |
| 323 | { |
| 324 | char *filterStringCopy = strdup (filterString); |
| 325 | char *p_cur = filterStringCopy; |
| 326 | char *p_ret; |
| 327 | int err; |
| 328 | |
| 329 | // Yes, I'm using strsep |
| 330 | while (NULL != (p_ret = strsep(&p_cur, " \t,"))) { |
| 331 | // ignore whitespace-only entries |
| 332 | if(p_ret[0] != '\0') { |
| 333 | err = android_log_addFilterRule(p_format, p_ret); |
| 334 | |
| 335 | if (err < 0) { |
| 336 | goto error; |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | free (filterStringCopy); |
| 342 | return 0; |
| 343 | error: |
| 344 | free (filterStringCopy); |
| 345 | return -1; |
| 346 | } |
| 347 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 348 | /** |
| 349 | * Splits a wire-format buffer into an AndroidLogEntry |
| 350 | * entry allocated by caller. Pointers will point directly into buf |
| 351 | * |
| 352 | * Returns 0 on success and -1 on invalid wire format (entry will be |
| 353 | * in unspecified state) |
| 354 | */ |
| 355 | int android_log_processLogBuffer(struct logger_entry *buf, |
| 356 | AndroidLogEntry *entry) |
| 357 | { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 358 | entry->tv_sec = buf->sec; |
| 359 | entry->tv_nsec = buf->nsec; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 360 | entry->pid = buf->pid; |
| 361 | entry->tid = buf->tid; |
Kenny Root | 4bf3c02 | 2011-09-30 17:10:14 -0700 | [diff] [blame] | 362 | |
| 363 | /* |
| 364 | * format: <priority:1><tag:N>\0<message:N>\0 |
| 365 | * |
| 366 | * tag str |
Nick Kralevich | e1ede15 | 2011-10-18 15:23:33 -0700 | [diff] [blame] | 367 | * starts at buf->msg+1 |
Kenny Root | 4bf3c02 | 2011-09-30 17:10:14 -0700 | [diff] [blame] | 368 | * msg |
Nick Kralevich | e1ede15 | 2011-10-18 15:23:33 -0700 | [diff] [blame] | 369 | * starts at buf->msg+1+len(tag)+1 |
Jeff Sharkey | a820a0e | 2011-10-26 18:40:39 -0700 | [diff] [blame] | 370 | * |
| 371 | * The message may have been truncated by the kernel log driver. |
| 372 | * When that happens, we must null-terminate the message ourselves. |
Kenny Root | 4bf3c02 | 2011-09-30 17:10:14 -0700 | [diff] [blame] | 373 | */ |
Nick Kralevich | e1ede15 | 2011-10-18 15:23:33 -0700 | [diff] [blame] | 374 | if (buf->len < 3) { |
| 375 | // An well-formed entry must consist of at least a priority |
| 376 | // and two null characters |
| 377 | fprintf(stderr, "+++ LOG: entry too small\n"); |
Kenny Root | 4bf3c02 | 2011-09-30 17:10:14 -0700 | [diff] [blame] | 378 | return -1; |
| 379 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 380 | |
Jeff Sharkey | a820a0e | 2011-10-26 18:40:39 -0700 | [diff] [blame] | 381 | int msgStart = -1; |
| 382 | int msgEnd = -1; |
| 383 | |
Nick Kralevich | e1ede15 | 2011-10-18 15:23:33 -0700 | [diff] [blame] | 384 | int i; |
Mark Salyzyn | 40b2155 | 2013-12-18 12:59:01 -0800 | [diff] [blame] | 385 | char *msg = buf->msg; |
| 386 | struct logger_entry_v2 *buf2 = (struct logger_entry_v2 *)buf; |
| 387 | if (buf2->hdr_size) { |
| 388 | msg = ((char *)buf2) + buf2->hdr_size; |
| 389 | } |
Nick Kralevich | e1ede15 | 2011-10-18 15:23:33 -0700 | [diff] [blame] | 390 | for (i = 1; i < buf->len; i++) { |
Mark Salyzyn | 40b2155 | 2013-12-18 12:59:01 -0800 | [diff] [blame] | 391 | if (msg[i] == '\0') { |
Jeff Sharkey | a820a0e | 2011-10-26 18:40:39 -0700 | [diff] [blame] | 392 | if (msgStart == -1) { |
| 393 | msgStart = i + 1; |
| 394 | } else { |
| 395 | msgEnd = i; |
| 396 | break; |
| 397 | } |
Nick Kralevich | e1ede15 | 2011-10-18 15:23:33 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
Jeff Sharkey | a820a0e | 2011-10-26 18:40:39 -0700 | [diff] [blame] | 400 | |
| 401 | if (msgStart == -1) { |
| 402 | fprintf(stderr, "+++ LOG: malformed log message\n"); |
Nick Kralevich | 63f4a84 | 2011-10-17 10:45:03 -0700 | [diff] [blame] | 403 | return -1; |
| 404 | } |
Jeff Sharkey | a820a0e | 2011-10-26 18:40:39 -0700 | [diff] [blame] | 405 | if (msgEnd == -1) { |
| 406 | // incoming message not null-terminated; force it |
| 407 | msgEnd = buf->len - 1; |
Mark Salyzyn | 40b2155 | 2013-12-18 12:59:01 -0800 | [diff] [blame] | 408 | msg[msgEnd] = '\0'; |
Jeff Sharkey | a820a0e | 2011-10-26 18:40:39 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Mark Salyzyn | 40b2155 | 2013-12-18 12:59:01 -0800 | [diff] [blame] | 411 | entry->priority = msg[0]; |
| 412 | entry->tag = msg + 1; |
| 413 | entry->message = msg + msgStart; |
Jeff Sharkey | a820a0e | 2011-10-26 18:40:39 -0700 | [diff] [blame] | 414 | entry->messageLen = msgEnd - msgStart; |
Nick Kralevich | 63f4a84 | 2011-10-17 10:45:03 -0700 | [diff] [blame] | 415 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | /* |
Mark Salyzyn | 8470dad | 2015-03-06 20:42:57 +0000 | [diff] [blame] | 420 | * Extract a 4-byte value from a byte stream. |
| 421 | */ |
| 422 | static inline uint32_t get4LE(const uint8_t* src) |
| 423 | { |
| 424 | return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24); |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * Extract an 8-byte value from a byte stream. |
| 429 | */ |
| 430 | static inline uint64_t get8LE(const uint8_t* src) |
| 431 | { |
| 432 | uint32_t low, high; |
| 433 | |
| 434 | low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24); |
| 435 | high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24); |
Jeff Brown | 44193d9 | 2015-04-28 12:47:02 -0700 | [diff] [blame] | 436 | return ((uint64_t) high << 32) | (uint64_t) low; |
Mark Salyzyn | 8470dad | 2015-03-06 20:42:57 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | |
| 440 | /* |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 441 | * Recursively convert binary log data to printable form. |
| 442 | * |
| 443 | * This needs to be recursive because you can have lists of lists. |
| 444 | * |
| 445 | * If we run out of room, we stop processing immediately. It's important |
| 446 | * for us to check for space on every output element to avoid producing |
| 447 | * garbled output. |
| 448 | * |
| 449 | * Returns 0 on success, 1 on buffer full, -1 on failure. |
| 450 | */ |
| 451 | static int android_log_printBinaryEvent(const unsigned char** pEventData, |
| 452 | size_t* pEventDataLen, char** pOutBuf, size_t* pOutBufLen) |
| 453 | { |
| 454 | const unsigned char* eventData = *pEventData; |
| 455 | size_t eventDataLen = *pEventDataLen; |
| 456 | char* outBuf = *pOutBuf; |
| 457 | size_t outBufLen = *pOutBufLen; |
| 458 | unsigned char type; |
| 459 | size_t outCount; |
| 460 | int result = 0; |
| 461 | |
| 462 | if (eventDataLen < 1) |
| 463 | return -1; |
| 464 | type = *eventData++; |
| 465 | eventDataLen--; |
| 466 | |
| 467 | //fprintf(stderr, "--- type=%d (rem len=%d)\n", type, eventDataLen); |
| 468 | |
| 469 | switch (type) { |
| 470 | case EVENT_TYPE_INT: |
| 471 | /* 32-bit signed int */ |
| 472 | { |
| 473 | int ival; |
| 474 | |
| 475 | if (eventDataLen < 4) |
| 476 | return -1; |
Mark Salyzyn | 8470dad | 2015-03-06 20:42:57 +0000 | [diff] [blame] | 477 | ival = get4LE(eventData); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 478 | eventData += 4; |
| 479 | eventDataLen -= 4; |
| 480 | |
| 481 | outCount = snprintf(outBuf, outBufLen, "%d", ival); |
| 482 | if (outCount < outBufLen) { |
| 483 | outBuf += outCount; |
| 484 | outBufLen -= outCount; |
| 485 | } else { |
| 486 | /* halt output */ |
| 487 | goto no_room; |
| 488 | } |
| 489 | } |
| 490 | break; |
| 491 | case EVENT_TYPE_LONG: |
| 492 | /* 64-bit signed long */ |
| 493 | { |
Jeff Brown | 44193d9 | 2015-04-28 12:47:02 -0700 | [diff] [blame] | 494 | uint64_t lval; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 495 | |
| 496 | if (eventDataLen < 8) |
| 497 | return -1; |
Mark Salyzyn | 8470dad | 2015-03-06 20:42:57 +0000 | [diff] [blame] | 498 | lval = get8LE(eventData); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 499 | eventData += 8; |
| 500 | eventDataLen -= 8; |
| 501 | |
Jeff Brown | 44193d9 | 2015-04-28 12:47:02 -0700 | [diff] [blame] | 502 | outCount = snprintf(outBuf, outBufLen, "%" PRId64, lval); |
| 503 | if (outCount < outBufLen) { |
| 504 | outBuf += outCount; |
| 505 | outBufLen -= outCount; |
| 506 | } else { |
| 507 | /* halt output */ |
| 508 | goto no_room; |
| 509 | } |
| 510 | } |
| 511 | break; |
| 512 | case EVENT_TYPE_FLOAT: |
| 513 | /* float */ |
| 514 | { |
| 515 | uint32_t ival; |
| 516 | float fval; |
| 517 | |
| 518 | if (eventDataLen < 4) |
| 519 | return -1; |
| 520 | ival = get4LE(eventData); |
| 521 | fval = *(float*)&ival; |
| 522 | eventData += 4; |
| 523 | eventDataLen -= 4; |
| 524 | |
| 525 | outCount = snprintf(outBuf, outBufLen, "%f", fval); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 526 | if (outCount < outBufLen) { |
| 527 | outBuf += outCount; |
| 528 | outBufLen -= outCount; |
| 529 | } else { |
| 530 | /* halt output */ |
| 531 | goto no_room; |
| 532 | } |
| 533 | } |
| 534 | break; |
| 535 | case EVENT_TYPE_STRING: |
| 536 | /* UTF-8 chars, not NULL-terminated */ |
| 537 | { |
| 538 | unsigned int strLen; |
| 539 | |
| 540 | if (eventDataLen < 4) |
| 541 | return -1; |
Mark Salyzyn | 8470dad | 2015-03-06 20:42:57 +0000 | [diff] [blame] | 542 | strLen = get4LE(eventData); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 543 | eventData += 4; |
| 544 | eventDataLen -= 4; |
| 545 | |
| 546 | if (eventDataLen < strLen) |
| 547 | return -1; |
| 548 | |
| 549 | if (strLen < outBufLen) { |
| 550 | memcpy(outBuf, eventData, strLen); |
| 551 | outBuf += strLen; |
| 552 | outBufLen -= strLen; |
| 553 | } else if (outBufLen > 0) { |
| 554 | /* copy what we can */ |
| 555 | memcpy(outBuf, eventData, outBufLen); |
| 556 | outBuf += outBufLen; |
| 557 | outBufLen -= outBufLen; |
| 558 | goto no_room; |
| 559 | } |
| 560 | eventData += strLen; |
| 561 | eventDataLen -= strLen; |
| 562 | break; |
| 563 | } |
| 564 | case EVENT_TYPE_LIST: |
| 565 | /* N items, all different types */ |
| 566 | { |
| 567 | unsigned char count; |
| 568 | int i; |
| 569 | |
| 570 | if (eventDataLen < 1) |
| 571 | return -1; |
| 572 | |
| 573 | count = *eventData++; |
| 574 | eventDataLen--; |
| 575 | |
| 576 | if (outBufLen > 0) { |
| 577 | *outBuf++ = '['; |
| 578 | outBufLen--; |
| 579 | } else { |
| 580 | goto no_room; |
| 581 | } |
| 582 | |
| 583 | for (i = 0; i < count; i++) { |
| 584 | result = android_log_printBinaryEvent(&eventData, &eventDataLen, |
| 585 | &outBuf, &outBufLen); |
| 586 | if (result != 0) |
| 587 | goto bail; |
| 588 | |
| 589 | if (i < count-1) { |
| 590 | if (outBufLen > 0) { |
| 591 | *outBuf++ = ','; |
| 592 | outBufLen--; |
| 593 | } else { |
| 594 | goto no_room; |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | if (outBufLen > 0) { |
| 600 | *outBuf++ = ']'; |
| 601 | outBufLen--; |
| 602 | } else { |
| 603 | goto no_room; |
| 604 | } |
| 605 | } |
| 606 | break; |
| 607 | default: |
| 608 | fprintf(stderr, "Unknown binary event type %d\n", type); |
| 609 | return -1; |
| 610 | } |
| 611 | |
| 612 | bail: |
| 613 | *pEventData = eventData; |
| 614 | *pEventDataLen = eventDataLen; |
| 615 | *pOutBuf = outBuf; |
| 616 | *pOutBufLen = outBufLen; |
| 617 | return result; |
| 618 | |
| 619 | no_room: |
| 620 | result = 1; |
| 621 | goto bail; |
| 622 | } |
| 623 | |
| 624 | /** |
| 625 | * Convert a binary log entry to ASCII form. |
| 626 | * |
| 627 | * For convenience we mimic the processLogBuffer API. There is no |
| 628 | * pre-defined output length for the binary data, since we're free to format |
| 629 | * it however we choose, which means we can't really use a fixed-size buffer |
| 630 | * here. |
| 631 | */ |
| 632 | int android_log_processBinaryLogBuffer(struct logger_entry *buf, |
| 633 | AndroidLogEntry *entry, const EventTagMap* map, char* messageBuf, |
| 634 | int messageBufLen) |
| 635 | { |
| 636 | size_t inCount; |
| 637 | unsigned int tagIndex; |
| 638 | const unsigned char* eventData; |
| 639 | |
| 640 | entry->tv_sec = buf->sec; |
| 641 | entry->tv_nsec = buf->nsec; |
| 642 | entry->priority = ANDROID_LOG_INFO; |
| 643 | entry->pid = buf->pid; |
| 644 | entry->tid = buf->tid; |
| 645 | |
| 646 | /* |
| 647 | * Pull the tag out. |
| 648 | */ |
| 649 | eventData = (const unsigned char*) buf->msg; |
Mark Salyzyn | 40b2155 | 2013-12-18 12:59:01 -0800 | [diff] [blame] | 650 | struct logger_entry_v2 *buf2 = (struct logger_entry_v2 *)buf; |
| 651 | if (buf2->hdr_size) { |
| 652 | eventData = ((unsigned char *)buf2) + buf2->hdr_size; |
| 653 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 654 | inCount = buf->len; |
| 655 | if (inCount < 4) |
| 656 | return -1; |
Mark Salyzyn | 8470dad | 2015-03-06 20:42:57 +0000 | [diff] [blame] | 657 | tagIndex = get4LE(eventData); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 658 | eventData += 4; |
| 659 | inCount -= 4; |
| 660 | |
| 661 | if (map != NULL) { |
| 662 | entry->tag = android_lookupEventTag(map, tagIndex); |
| 663 | } else { |
| 664 | entry->tag = NULL; |
| 665 | } |
| 666 | |
| 667 | /* |
| 668 | * If we don't have a map, or didn't find the tag number in the map, |
| 669 | * stuff a generated tag value into the start of the output buffer and |
| 670 | * shift the buffer pointers down. |
| 671 | */ |
| 672 | if (entry->tag == NULL) { |
| 673 | int tagLen; |
| 674 | |
| 675 | tagLen = snprintf(messageBuf, messageBufLen, "[%d]", tagIndex); |
| 676 | entry->tag = messageBuf; |
| 677 | messageBuf += tagLen+1; |
| 678 | messageBufLen -= tagLen+1; |
| 679 | } |
| 680 | |
| 681 | /* |
| 682 | * Format the event log data into the buffer. |
| 683 | */ |
| 684 | char* outBuf = messageBuf; |
| 685 | size_t outRemaining = messageBufLen-1; /* leave one for nul byte */ |
| 686 | int result; |
| 687 | result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, |
| 688 | &outRemaining); |
| 689 | if (result < 0) { |
| 690 | fprintf(stderr, "Binary log entry conversion failed\n"); |
| 691 | return -1; |
| 692 | } else if (result == 1) { |
| 693 | if (outBuf > messageBuf) { |
| 694 | /* leave an indicator */ |
| 695 | *(outBuf-1) = '!'; |
| 696 | } else { |
| 697 | /* no room to output anything at all */ |
| 698 | *outBuf++ = '!'; |
| 699 | outRemaining--; |
| 700 | } |
| 701 | /* pretend we ate all the data */ |
| 702 | inCount = 0; |
| 703 | } |
| 704 | |
| 705 | /* eat the silly terminating '\n' */ |
| 706 | if (inCount == 1 && *eventData == '\n') { |
| 707 | eventData++; |
| 708 | inCount--; |
| 709 | } |
| 710 | |
| 711 | if (inCount != 0) { |
| 712 | fprintf(stderr, |
Andrew Hsieh | d2c8f52 | 2012-02-27 16:48:18 -0800 | [diff] [blame] | 713 | "Warning: leftover binary log data (%zu bytes)\n", inCount); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Terminate the buffer. The NUL byte does not count as part of |
| 718 | * entry->messageLen. |
| 719 | */ |
| 720 | *outBuf = '\0'; |
| 721 | entry->messageLen = outBuf - messageBuf; |
| 722 | assert(entry->messageLen == (messageBufLen-1) - outRemaining); |
| 723 | |
| 724 | entry->message = messageBuf; |
| 725 | |
| 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | /** |
| 730 | * Formats a log message into a buffer |
| 731 | * |
| 732 | * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer |
| 733 | * If return value != defaultBuffer, caller must call free() |
| 734 | * Returns NULL on malloc error |
| 735 | */ |
| 736 | |
| 737 | char *android_log_formatLogLine ( |
| 738 | AndroidLogFormat *p_format, |
| 739 | char *defaultBuffer, |
| 740 | size_t defaultBufferSize, |
| 741 | const AndroidLogEntry *entry, |
| 742 | size_t *p_outLength) |
| 743 | { |
Yabin Cui | 8a98535 | 2014-11-13 10:02:08 -0800 | [diff] [blame] | 744 | #if !defined(_WIN32) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 745 | struct tm tmBuf; |
| 746 | #endif |
| 747 | struct tm* ptm; |
| 748 | char timeBuf[32]; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 749 | char prefixBuf[128], suffixBuf[128]; |
| 750 | char priChar; |
| 751 | int prefixSuffixIsHeaderFooter = 0; |
| 752 | char * ret = NULL; |
| 753 | |
| 754 | priChar = filterPriToChar(entry->priority); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 755 | size_t prefixLen = 0, suffixLen = 0; |
| 756 | size_t len; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 757 | |
| 758 | /* |
| 759 | * Get the current date/time in pretty form |
| 760 | * |
| 761 | * It's often useful when examining a log with "less" to jump to |
| 762 | * a specific point in the file by searching for the date/time stamp. |
| 763 | * For this reason it's very annoying to have regexp meta characters |
| 764 | * in the time stamp. Don't use forward slashes, parenthesis, |
| 765 | * brackets, asterisks, or other special chars here. |
| 766 | */ |
Yabin Cui | 8a98535 | 2014-11-13 10:02:08 -0800 | [diff] [blame] | 767 | #if !defined(_WIN32) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 768 | ptm = localtime_r(&(entry->tv_sec), &tmBuf); |
| 769 | #else |
| 770 | ptm = localtime(&(entry->tv_sec)); |
| 771 | #endif |
| 772 | //strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", ptm); |
| 773 | strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm); |
| 774 | |
| 775 | /* |
| 776 | * Construct a buffer containing the log header and log message. |
| 777 | */ |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 778 | if (p_format->colored_output) { |
| 779 | prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm", |
| 780 | colorFromPri(entry->priority)); |
| 781 | prefixLen = MIN(prefixLen, sizeof(prefixBuf)); |
| 782 | suffixLen = snprintf(suffixBuf, sizeof(suffixBuf), "\x1B[0m"); |
| 783 | suffixLen = MIN(suffixLen, sizeof(suffixBuf)); |
| 784 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 785 | |
| 786 | switch (p_format->format) { |
| 787 | case FORMAT_TAG: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 788 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 789 | "%c/%-8s: ", priChar, entry->tag); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 790 | strcpy(suffixBuf + suffixLen, "\n"); |
| 791 | ++suffixLen; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 792 | break; |
| 793 | case FORMAT_PROCESS: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 794 | len = snprintf(suffixBuf + suffixLen, sizeof(suffixBuf) - suffixLen, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 795 | " (%s)\n", entry->tag); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 796 | suffixLen += MIN(len, sizeof(suffixBuf) - suffixLen); |
| 797 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
| 798 | "%c(%5d) ", priChar, entry->pid); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 799 | break; |
| 800 | case FORMAT_THREAD: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 801 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
Andrew Hsieh | d2c8f52 | 2012-02-27 16:48:18 -0800 | [diff] [blame] | 802 | "%c(%5d:%5d) ", priChar, entry->pid, entry->tid); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 803 | strcpy(suffixBuf + suffixLen, "\n"); |
| 804 | ++suffixLen; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 805 | break; |
| 806 | case FORMAT_RAW: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 807 | prefixBuf[prefixLen] = 0; |
| 808 | len = 0; |
| 809 | strcpy(suffixBuf + suffixLen, "\n"); |
| 810 | ++suffixLen; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 811 | break; |
| 812 | case FORMAT_TIME: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 813 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 814 | "%s.%03ld %c/%-8s(%5d): ", timeBuf, entry->tv_nsec / 1000000, |
| 815 | priChar, entry->tag, entry->pid); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 816 | strcpy(suffixBuf + suffixLen, "\n"); |
| 817 | ++suffixLen; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 818 | break; |
| 819 | case FORMAT_THREADTIME: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 820 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 821 | "%s.%03ld %5d %5d %c %-8s: ", timeBuf, entry->tv_nsec / 1000000, |
Andrew Hsieh | d2c8f52 | 2012-02-27 16:48:18 -0800 | [diff] [blame] | 822 | entry->pid, entry->tid, priChar, entry->tag); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 823 | strcpy(suffixBuf + suffixLen, "\n"); |
| 824 | ++suffixLen; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 825 | break; |
| 826 | case FORMAT_LONG: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 827 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
Andrew Hsieh | d2c8f52 | 2012-02-27 16:48:18 -0800 | [diff] [blame] | 828 | "[ %s.%03ld %5d:%5d %c/%-8s ]\n", |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 829 | timeBuf, entry->tv_nsec / 1000000, entry->pid, |
Andrew Hsieh | d2c8f52 | 2012-02-27 16:48:18 -0800 | [diff] [blame] | 830 | entry->tid, priChar, entry->tag); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 831 | strcpy(suffixBuf + suffixLen, "\n\n"); |
| 832 | suffixLen += 2; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 833 | prefixSuffixIsHeaderFooter = 1; |
| 834 | break; |
| 835 | case FORMAT_BRIEF: |
| 836 | default: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 837 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 838 | "%c/%-8s(%5d): ", priChar, entry->tag, entry->pid); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 839 | strcpy(suffixBuf + suffixLen, "\n"); |
| 840 | ++suffixLen; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 841 | break; |
| 842 | } |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 843 | |
Keith Preston | b45b5c9 | 2010-02-11 15:12:53 -0600 | [diff] [blame] | 844 | /* snprintf has a weird return value. It returns what would have been |
| 845 | * written given a large enough buffer. In the case that the prefix is |
| 846 | * longer then our buffer(128), it messes up the calculations below |
| 847 | * possibly causing heap corruption. To avoid this we double check and |
| 848 | * set the length at the maximum (size minus null byte) |
| 849 | */ |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 850 | prefixLen += MIN(len, sizeof(prefixBuf) - prefixLen); |
Mark Salyzyn | e242842 | 2015-01-22 10:00:04 -0800 | [diff] [blame] | 851 | suffixLen = MIN(suffixLen, sizeof(suffixBuf)); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 852 | |
| 853 | /* the following code is tragically unreadable */ |
| 854 | |
| 855 | size_t numLines; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 856 | char *p; |
| 857 | size_t bufferSize; |
| 858 | const char *pm; |
| 859 | |
| 860 | if (prefixSuffixIsHeaderFooter) { |
| 861 | // we're just wrapping message with a header/footer |
| 862 | numLines = 1; |
| 863 | } else { |
| 864 | pm = entry->message; |
| 865 | numLines = 0; |
| 866 | |
| 867 | // The line-end finding here must match the line-end finding |
| 868 | // in for ( ... numLines...) loop below |
| 869 | while (pm < (entry->message + entry->messageLen)) { |
| 870 | if (*pm++ == '\n') numLines++; |
| 871 | } |
| 872 | // plus one line for anything not newline-terminated at the end |
| 873 | if (pm > entry->message && *(pm-1) != '\n') numLines++; |
| 874 | } |
| 875 | |
| 876 | // this is an upper bound--newlines in message may be counted |
| 877 | // extraneously |
| 878 | bufferSize = (numLines * (prefixLen + suffixLen)) + entry->messageLen + 1; |
| 879 | |
| 880 | if (defaultBufferSize >= bufferSize) { |
| 881 | ret = defaultBuffer; |
| 882 | } else { |
| 883 | ret = (char *)malloc(bufferSize); |
| 884 | |
| 885 | if (ret == NULL) { |
| 886 | return ret; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | ret[0] = '\0'; /* to start strcat off */ |
| 891 | |
| 892 | p = ret; |
| 893 | pm = entry->message; |
| 894 | |
| 895 | if (prefixSuffixIsHeaderFooter) { |
| 896 | strcat(p, prefixBuf); |
| 897 | p += prefixLen; |
| 898 | strncat(p, entry->message, entry->messageLen); |
| 899 | p += entry->messageLen; |
| 900 | strcat(p, suffixBuf); |
| 901 | p += suffixLen; |
| 902 | } else { |
| 903 | while(pm < (entry->message + entry->messageLen)) { |
| 904 | const char *lineStart; |
| 905 | size_t lineLen; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 906 | lineStart = pm; |
| 907 | |
| 908 | // Find the next end-of-line in message |
| 909 | while (pm < (entry->message + entry->messageLen) |
| 910 | && *pm != '\n') pm++; |
| 911 | lineLen = pm - lineStart; |
| 912 | |
| 913 | strcat(p, prefixBuf); |
| 914 | p += prefixLen; |
| 915 | strncat(p, lineStart, lineLen); |
| 916 | p += lineLen; |
| 917 | strcat(p, suffixBuf); |
| 918 | p += suffixLen; |
| 919 | |
| 920 | if (*pm == '\n') pm++; |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | if (p_outLength != NULL) { |
| 925 | *p_outLength = p - ret; |
| 926 | } |
| 927 | |
| 928 | return ret; |
| 929 | } |
| 930 | |
| 931 | /** |
| 932 | * Either print or do not print log line, based on filter |
| 933 | * |
| 934 | * Returns count bytes written |
| 935 | */ |
| 936 | |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 937 | int android_log_printLogLine( |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 938 | AndroidLogFormat *p_format, |
| 939 | int fd, |
| 940 | const AndroidLogEntry *entry) |
| 941 | { |
| 942 | int ret; |
| 943 | char defaultBuffer[512]; |
| 944 | char *outBuffer = NULL; |
| 945 | size_t totalLen; |
| 946 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 947 | outBuffer = android_log_formatLogLine(p_format, defaultBuffer, |
| 948 | sizeof(defaultBuffer), entry, &totalLen); |
| 949 | |
| 950 | if (!outBuffer) |
| 951 | return -1; |
| 952 | |
| 953 | do { |
| 954 | ret = write(fd, outBuffer, totalLen); |
| 955 | } while (ret < 0 && errno == EINTR); |
| 956 | |
| 957 | if (ret < 0) { |
| 958 | fprintf(stderr, "+++ LOG: write failed (errno=%d)\n", errno); |
| 959 | ret = 0; |
| 960 | goto done; |
| 961 | } |
| 962 | |
| 963 | if (((size_t)ret) < totalLen) { |
| 964 | fprintf(stderr, "+++ LOG: write partial (%d of %d)\n", ret, |
| 965 | (int)totalLen); |
| 966 | goto done; |
| 967 | } |
| 968 | |
| 969 | done: |
| 970 | if (outBuffer != defaultBuffer) { |
| 971 | free(outBuffer); |
| 972 | } |
| 973 | |
| 974 | return ret; |
| 975 | } |