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> |
Mark Salyzyn | f8d62a0 | 2015-03-04 16:20:50 -0800 | [diff] [blame] | 23 | #include <endian.h> |
Mark Salyzyn | a04464a | 2014-04-30 08:50:53 -0700 | [diff] [blame] | 24 | #include <errno.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> |
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 | /* |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 420 | * Recursively convert binary log data to printable form. |
| 421 | * |
| 422 | * This needs to be recursive because you can have lists of lists. |
| 423 | * |
| 424 | * If we run out of room, we stop processing immediately. It's important |
| 425 | * for us to check for space on every output element to avoid producing |
| 426 | * garbled output. |
| 427 | * |
| 428 | * Returns 0 on success, 1 on buffer full, -1 on failure. |
| 429 | */ |
| 430 | static int android_log_printBinaryEvent(const unsigned char** pEventData, |
| 431 | size_t* pEventDataLen, char** pOutBuf, size_t* pOutBufLen) |
| 432 | { |
| 433 | const unsigned char* eventData = *pEventData; |
| 434 | size_t eventDataLen = *pEventDataLen; |
| 435 | char* outBuf = *pOutBuf; |
| 436 | size_t outBufLen = *pOutBufLen; |
| 437 | unsigned char type; |
| 438 | size_t outCount; |
| 439 | int result = 0; |
| 440 | |
| 441 | if (eventDataLen < 1) |
| 442 | return -1; |
| 443 | type = *eventData++; |
| 444 | eventDataLen--; |
| 445 | |
| 446 | //fprintf(stderr, "--- type=%d (rem len=%d)\n", type, eventDataLen); |
| 447 | |
| 448 | switch (type) { |
| 449 | case EVENT_TYPE_INT: |
| 450 | /* 32-bit signed int */ |
| 451 | { |
| 452 | int ival; |
| 453 | |
| 454 | if (eventDataLen < 4) |
| 455 | return -1; |
Mark Salyzyn | 66bfc5c | 2015-03-03 15:23:24 -0800 | [diff] [blame] | 456 | ival = le32toh(*((int32_t *)eventData)); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 457 | eventData += 4; |
| 458 | eventDataLen -= 4; |
| 459 | |
| 460 | outCount = snprintf(outBuf, outBufLen, "%d", ival); |
| 461 | if (outCount < outBufLen) { |
| 462 | outBuf += outCount; |
| 463 | outBufLen -= outCount; |
| 464 | } else { |
| 465 | /* halt output */ |
| 466 | goto no_room; |
| 467 | } |
| 468 | } |
| 469 | break; |
| 470 | case EVENT_TYPE_LONG: |
| 471 | /* 64-bit signed long */ |
| 472 | { |
| 473 | long long lval; |
| 474 | |
| 475 | if (eventDataLen < 8) |
| 476 | return -1; |
Mark Salyzyn | 66bfc5c | 2015-03-03 15:23:24 -0800 | [diff] [blame] | 477 | lval = le64toh(*((int64_t *)eventData)); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 478 | eventData += 8; |
| 479 | eventDataLen -= 8; |
| 480 | |
| 481 | outCount = snprintf(outBuf, outBufLen, "%lld", lval); |
| 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_STRING: |
| 492 | /* UTF-8 chars, not NULL-terminated */ |
| 493 | { |
| 494 | unsigned int strLen; |
| 495 | |
| 496 | if (eventDataLen < 4) |
| 497 | return -1; |
Mark Salyzyn | 66bfc5c | 2015-03-03 15:23:24 -0800 | [diff] [blame] | 498 | strLen = le32toh(*((int32_t *)eventData)); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 499 | eventData += 4; |
| 500 | eventDataLen -= 4; |
| 501 | |
| 502 | if (eventDataLen < strLen) |
| 503 | return -1; |
| 504 | |
| 505 | if (strLen < outBufLen) { |
| 506 | memcpy(outBuf, eventData, strLen); |
| 507 | outBuf += strLen; |
| 508 | outBufLen -= strLen; |
| 509 | } else if (outBufLen > 0) { |
| 510 | /* copy what we can */ |
| 511 | memcpy(outBuf, eventData, outBufLen); |
| 512 | outBuf += outBufLen; |
| 513 | outBufLen -= outBufLen; |
| 514 | goto no_room; |
| 515 | } |
| 516 | eventData += strLen; |
| 517 | eventDataLen -= strLen; |
| 518 | break; |
| 519 | } |
| 520 | case EVENT_TYPE_LIST: |
| 521 | /* N items, all different types */ |
| 522 | { |
| 523 | unsigned char count; |
| 524 | int i; |
| 525 | |
| 526 | if (eventDataLen < 1) |
| 527 | return -1; |
| 528 | |
| 529 | count = *eventData++; |
| 530 | eventDataLen--; |
| 531 | |
| 532 | if (outBufLen > 0) { |
| 533 | *outBuf++ = '['; |
| 534 | outBufLen--; |
| 535 | } else { |
| 536 | goto no_room; |
| 537 | } |
| 538 | |
| 539 | for (i = 0; i < count; i++) { |
| 540 | result = android_log_printBinaryEvent(&eventData, &eventDataLen, |
| 541 | &outBuf, &outBufLen); |
| 542 | if (result != 0) |
| 543 | goto bail; |
| 544 | |
| 545 | if (i < count-1) { |
| 546 | if (outBufLen > 0) { |
| 547 | *outBuf++ = ','; |
| 548 | outBufLen--; |
| 549 | } else { |
| 550 | goto no_room; |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | if (outBufLen > 0) { |
| 556 | *outBuf++ = ']'; |
| 557 | outBufLen--; |
| 558 | } else { |
| 559 | goto no_room; |
| 560 | } |
| 561 | } |
| 562 | break; |
| 563 | default: |
| 564 | fprintf(stderr, "Unknown binary event type %d\n", type); |
| 565 | return -1; |
| 566 | } |
| 567 | |
| 568 | bail: |
| 569 | *pEventData = eventData; |
| 570 | *pEventDataLen = eventDataLen; |
| 571 | *pOutBuf = outBuf; |
| 572 | *pOutBufLen = outBufLen; |
| 573 | return result; |
| 574 | |
| 575 | no_room: |
| 576 | result = 1; |
| 577 | goto bail; |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Convert a binary log entry to ASCII form. |
| 582 | * |
| 583 | * For convenience we mimic the processLogBuffer API. There is no |
| 584 | * pre-defined output length for the binary data, since we're free to format |
| 585 | * it however we choose, which means we can't really use a fixed-size buffer |
| 586 | * here. |
| 587 | */ |
| 588 | int android_log_processBinaryLogBuffer(struct logger_entry *buf, |
| 589 | AndroidLogEntry *entry, const EventTagMap* map, char* messageBuf, |
| 590 | int messageBufLen) |
| 591 | { |
| 592 | size_t inCount; |
| 593 | unsigned int tagIndex; |
| 594 | const unsigned char* eventData; |
| 595 | |
| 596 | entry->tv_sec = buf->sec; |
| 597 | entry->tv_nsec = buf->nsec; |
| 598 | entry->priority = ANDROID_LOG_INFO; |
| 599 | entry->pid = buf->pid; |
| 600 | entry->tid = buf->tid; |
| 601 | |
| 602 | /* |
| 603 | * Pull the tag out. |
| 604 | */ |
| 605 | eventData = (const unsigned char*) buf->msg; |
Mark Salyzyn | 40b2155 | 2013-12-18 12:59:01 -0800 | [diff] [blame] | 606 | struct logger_entry_v2 *buf2 = (struct logger_entry_v2 *)buf; |
| 607 | if (buf2->hdr_size) { |
| 608 | eventData = ((unsigned char *)buf2) + buf2->hdr_size; |
| 609 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 610 | inCount = buf->len; |
| 611 | if (inCount < 4) |
| 612 | return -1; |
Mark Salyzyn | 66bfc5c | 2015-03-03 15:23:24 -0800 | [diff] [blame] | 613 | tagIndex = le32toh(*((int32_t *)eventData)); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 614 | eventData += 4; |
| 615 | inCount -= 4; |
| 616 | |
| 617 | if (map != NULL) { |
| 618 | entry->tag = android_lookupEventTag(map, tagIndex); |
| 619 | } else { |
| 620 | entry->tag = NULL; |
| 621 | } |
| 622 | |
| 623 | /* |
| 624 | * If we don't have a map, or didn't find the tag number in the map, |
| 625 | * stuff a generated tag value into the start of the output buffer and |
| 626 | * shift the buffer pointers down. |
| 627 | */ |
| 628 | if (entry->tag == NULL) { |
| 629 | int tagLen; |
| 630 | |
| 631 | tagLen = snprintf(messageBuf, messageBufLen, "[%d]", tagIndex); |
| 632 | entry->tag = messageBuf; |
| 633 | messageBuf += tagLen+1; |
| 634 | messageBufLen -= tagLen+1; |
| 635 | } |
| 636 | |
| 637 | /* |
| 638 | * Format the event log data into the buffer. |
| 639 | */ |
| 640 | char* outBuf = messageBuf; |
| 641 | size_t outRemaining = messageBufLen-1; /* leave one for nul byte */ |
| 642 | int result; |
| 643 | result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, |
| 644 | &outRemaining); |
| 645 | if (result < 0) { |
| 646 | fprintf(stderr, "Binary log entry conversion failed\n"); |
| 647 | return -1; |
| 648 | } else if (result == 1) { |
| 649 | if (outBuf > messageBuf) { |
| 650 | /* leave an indicator */ |
| 651 | *(outBuf-1) = '!'; |
| 652 | } else { |
| 653 | /* no room to output anything at all */ |
| 654 | *outBuf++ = '!'; |
| 655 | outRemaining--; |
| 656 | } |
| 657 | /* pretend we ate all the data */ |
| 658 | inCount = 0; |
| 659 | } |
| 660 | |
| 661 | /* eat the silly terminating '\n' */ |
| 662 | if (inCount == 1 && *eventData == '\n') { |
| 663 | eventData++; |
| 664 | inCount--; |
| 665 | } |
| 666 | |
| 667 | if (inCount != 0) { |
| 668 | fprintf(stderr, |
Andrew Hsieh | d2c8f52 | 2012-02-27 16:48:18 -0800 | [diff] [blame] | 669 | "Warning: leftover binary log data (%zu bytes)\n", inCount); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | /* |
| 673 | * Terminate the buffer. The NUL byte does not count as part of |
| 674 | * entry->messageLen. |
| 675 | */ |
| 676 | *outBuf = '\0'; |
| 677 | entry->messageLen = outBuf - messageBuf; |
| 678 | assert(entry->messageLen == (messageBufLen-1) - outRemaining); |
| 679 | |
| 680 | entry->message = messageBuf; |
| 681 | |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | /** |
| 686 | * Formats a log message into a buffer |
| 687 | * |
| 688 | * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer |
| 689 | * If return value != defaultBuffer, caller must call free() |
| 690 | * Returns NULL on malloc error |
| 691 | */ |
| 692 | |
| 693 | char *android_log_formatLogLine ( |
| 694 | AndroidLogFormat *p_format, |
| 695 | char *defaultBuffer, |
| 696 | size_t defaultBufferSize, |
| 697 | const AndroidLogEntry *entry, |
| 698 | size_t *p_outLength) |
| 699 | { |
Yabin Cui | 8a98535 | 2014-11-13 10:02:08 -0800 | [diff] [blame] | 700 | #if !defined(_WIN32) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 701 | struct tm tmBuf; |
| 702 | #endif |
| 703 | struct tm* ptm; |
| 704 | char timeBuf[32]; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 705 | char prefixBuf[128], suffixBuf[128]; |
| 706 | char priChar; |
| 707 | int prefixSuffixIsHeaderFooter = 0; |
| 708 | char * ret = NULL; |
| 709 | |
| 710 | priChar = filterPriToChar(entry->priority); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 711 | size_t prefixLen = 0, suffixLen = 0; |
| 712 | size_t len; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 713 | |
| 714 | /* |
| 715 | * Get the current date/time in pretty form |
| 716 | * |
| 717 | * It's often useful when examining a log with "less" to jump to |
| 718 | * a specific point in the file by searching for the date/time stamp. |
| 719 | * For this reason it's very annoying to have regexp meta characters |
| 720 | * in the time stamp. Don't use forward slashes, parenthesis, |
| 721 | * brackets, asterisks, or other special chars here. |
| 722 | */ |
Yabin Cui | 8a98535 | 2014-11-13 10:02:08 -0800 | [diff] [blame] | 723 | #if !defined(_WIN32) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 724 | ptm = localtime_r(&(entry->tv_sec), &tmBuf); |
| 725 | #else |
| 726 | ptm = localtime(&(entry->tv_sec)); |
| 727 | #endif |
| 728 | //strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", ptm); |
| 729 | strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm); |
| 730 | |
| 731 | /* |
| 732 | * Construct a buffer containing the log header and log message. |
| 733 | */ |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 734 | if (p_format->colored_output) { |
| 735 | prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm", |
| 736 | colorFromPri(entry->priority)); |
| 737 | prefixLen = MIN(prefixLen, sizeof(prefixBuf)); |
| 738 | suffixLen = snprintf(suffixBuf, sizeof(suffixBuf), "\x1B[0m"); |
| 739 | suffixLen = MIN(suffixLen, sizeof(suffixBuf)); |
| 740 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 741 | |
| 742 | switch (p_format->format) { |
| 743 | case FORMAT_TAG: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 744 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 745 | "%c/%-8s: ", priChar, entry->tag); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 746 | strcpy(suffixBuf + suffixLen, "\n"); |
| 747 | ++suffixLen; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 748 | break; |
| 749 | case FORMAT_PROCESS: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 750 | len = snprintf(suffixBuf + suffixLen, sizeof(suffixBuf) - suffixLen, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 751 | " (%s)\n", entry->tag); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 752 | suffixLen += MIN(len, sizeof(suffixBuf) - suffixLen); |
| 753 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
| 754 | "%c(%5d) ", priChar, entry->pid); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 755 | break; |
| 756 | case FORMAT_THREAD: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 757 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
Andrew Hsieh | d2c8f52 | 2012-02-27 16:48:18 -0800 | [diff] [blame] | 758 | "%c(%5d:%5d) ", priChar, entry->pid, entry->tid); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 759 | strcpy(suffixBuf + suffixLen, "\n"); |
| 760 | ++suffixLen; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 761 | break; |
| 762 | case FORMAT_RAW: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 763 | prefixBuf[prefixLen] = 0; |
| 764 | len = 0; |
| 765 | strcpy(suffixBuf + suffixLen, "\n"); |
| 766 | ++suffixLen; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 767 | break; |
| 768 | case FORMAT_TIME: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 769 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 770 | "%s.%03ld %c/%-8s(%5d): ", timeBuf, entry->tv_nsec / 1000000, |
| 771 | priChar, entry->tag, entry->pid); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 772 | strcpy(suffixBuf + suffixLen, "\n"); |
| 773 | ++suffixLen; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 774 | break; |
| 775 | case FORMAT_THREADTIME: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 776 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 777 | "%s.%03ld %5d %5d %c %-8s: ", timeBuf, entry->tv_nsec / 1000000, |
Andrew Hsieh | d2c8f52 | 2012-02-27 16:48:18 -0800 | [diff] [blame] | 778 | entry->pid, entry->tid, priChar, entry->tag); |
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_LONG: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 783 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
Andrew Hsieh | d2c8f52 | 2012-02-27 16:48:18 -0800 | [diff] [blame] | 784 | "[ %s.%03ld %5d:%5d %c/%-8s ]\n", |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 785 | timeBuf, entry->tv_nsec / 1000000, entry->pid, |
Andrew Hsieh | d2c8f52 | 2012-02-27 16:48:18 -0800 | [diff] [blame] | 786 | entry->tid, priChar, entry->tag); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 787 | strcpy(suffixBuf + suffixLen, "\n\n"); |
| 788 | suffixLen += 2; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 789 | prefixSuffixIsHeaderFooter = 1; |
| 790 | break; |
| 791 | case FORMAT_BRIEF: |
| 792 | default: |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 793 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 794 | "%c/%-8s(%5d): ", priChar, entry->tag, entry->pid); |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 795 | strcpy(suffixBuf + suffixLen, "\n"); |
| 796 | ++suffixLen; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 797 | break; |
| 798 | } |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 799 | |
Keith Preston | b45b5c9 | 2010-02-11 15:12:53 -0600 | [diff] [blame] | 800 | /* snprintf has a weird return value. It returns what would have been |
| 801 | * written given a large enough buffer. In the case that the prefix is |
| 802 | * longer then our buffer(128), it messes up the calculations below |
| 803 | * possibly causing heap corruption. To avoid this we double check and |
| 804 | * set the length at the maximum (size minus null byte) |
| 805 | */ |
Pierre Zurek | ead88fc | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 806 | prefixLen += MIN(len, sizeof(prefixBuf) - prefixLen); |
Mark Salyzyn | e242842 | 2015-01-22 10:00:04 -0800 | [diff] [blame] | 807 | suffixLen = MIN(suffixLen, sizeof(suffixBuf)); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 808 | |
| 809 | /* the following code is tragically unreadable */ |
| 810 | |
| 811 | size_t numLines; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 812 | char *p; |
| 813 | size_t bufferSize; |
| 814 | const char *pm; |
| 815 | |
| 816 | if (prefixSuffixIsHeaderFooter) { |
| 817 | // we're just wrapping message with a header/footer |
| 818 | numLines = 1; |
| 819 | } else { |
| 820 | pm = entry->message; |
| 821 | numLines = 0; |
| 822 | |
| 823 | // The line-end finding here must match the line-end finding |
| 824 | // in for ( ... numLines...) loop below |
| 825 | while (pm < (entry->message + entry->messageLen)) { |
| 826 | if (*pm++ == '\n') numLines++; |
| 827 | } |
| 828 | // plus one line for anything not newline-terminated at the end |
| 829 | if (pm > entry->message && *(pm-1) != '\n') numLines++; |
| 830 | } |
| 831 | |
| 832 | // this is an upper bound--newlines in message may be counted |
| 833 | // extraneously |
| 834 | bufferSize = (numLines * (prefixLen + suffixLen)) + entry->messageLen + 1; |
| 835 | |
| 836 | if (defaultBufferSize >= bufferSize) { |
| 837 | ret = defaultBuffer; |
| 838 | } else { |
| 839 | ret = (char *)malloc(bufferSize); |
| 840 | |
| 841 | if (ret == NULL) { |
| 842 | return ret; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | ret[0] = '\0'; /* to start strcat off */ |
| 847 | |
| 848 | p = ret; |
| 849 | pm = entry->message; |
| 850 | |
| 851 | if (prefixSuffixIsHeaderFooter) { |
| 852 | strcat(p, prefixBuf); |
| 853 | p += prefixLen; |
| 854 | strncat(p, entry->message, entry->messageLen); |
| 855 | p += entry->messageLen; |
| 856 | strcat(p, suffixBuf); |
| 857 | p += suffixLen; |
| 858 | } else { |
| 859 | while(pm < (entry->message + entry->messageLen)) { |
| 860 | const char *lineStart; |
| 861 | size_t lineLen; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 862 | lineStart = pm; |
| 863 | |
| 864 | // Find the next end-of-line in message |
| 865 | while (pm < (entry->message + entry->messageLen) |
| 866 | && *pm != '\n') pm++; |
| 867 | lineLen = pm - lineStart; |
| 868 | |
| 869 | strcat(p, prefixBuf); |
| 870 | p += prefixLen; |
| 871 | strncat(p, lineStart, lineLen); |
| 872 | p += lineLen; |
| 873 | strcat(p, suffixBuf); |
| 874 | p += suffixLen; |
| 875 | |
| 876 | if (*pm == '\n') pm++; |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | if (p_outLength != NULL) { |
| 881 | *p_outLength = p - ret; |
| 882 | } |
| 883 | |
| 884 | return ret; |
| 885 | } |
| 886 | |
| 887 | /** |
| 888 | * Either print or do not print log line, based on filter |
| 889 | * |
| 890 | * Returns count bytes written |
| 891 | */ |
| 892 | |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 893 | int android_log_printLogLine( |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 894 | AndroidLogFormat *p_format, |
| 895 | int fd, |
| 896 | const AndroidLogEntry *entry) |
| 897 | { |
| 898 | int ret; |
| 899 | char defaultBuffer[512]; |
| 900 | char *outBuffer = NULL; |
| 901 | size_t totalLen; |
| 902 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 903 | outBuffer = android_log_formatLogLine(p_format, defaultBuffer, |
| 904 | sizeof(defaultBuffer), entry, &totalLen); |
| 905 | |
| 906 | if (!outBuffer) |
| 907 | return -1; |
| 908 | |
| 909 | do { |
| 910 | ret = write(fd, outBuffer, totalLen); |
| 911 | } while (ret < 0 && errno == EINTR); |
| 912 | |
| 913 | if (ret < 0) { |
| 914 | fprintf(stderr, "+++ LOG: write failed (errno=%d)\n", errno); |
| 915 | ret = 0; |
| 916 | goto done; |
| 917 | } |
| 918 | |
| 919 | if (((size_t)ret) < totalLen) { |
| 920 | fprintf(stderr, "+++ LOG: write partial (%d of %d)\n", ret, |
| 921 | (int)totalLen); |
| 922 | goto done; |
| 923 | } |
| 924 | |
| 925 | done: |
| 926 | if (outBuffer != defaultBuffer) { |
| 927 | free(outBuffer); |
| 928 | } |
| 929 | |
| 930 | return ret; |
| 931 | } |