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