blob: 97e28fdaf828092c73117db14bd8ed3fc51ffa72 [file] [log] [blame]
Mark Salyzyncf4aa032013-11-22 07:54:30 -08001/*
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07002**
Mark Salyzyn40b21552013-12-18 12:59:01 -08003** Copyright 2006-2014, The Android Open Source Project
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07004**
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 Project4f6e8d72008-10-21 07:00:00 -070020#include <arpa/inet.h>
Mark Salyzyna04464a2014-04-30 08:50:53 -070021#include <assert.h>
22#include <ctype.h>
23#include <errno.h>
Pierre Zurekead88fc2010-10-17 22:39:37 +020024#include <stdbool.h>
Mark Salyzyna04464a2014-04-30 08:50:53 -070025#include <stdint.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Jeff Brown44193d92015-04-28 12:47:02 -070029#include <inttypes.h>
Pierre Zurekead88fc2010-10-17 22:39:37 +020030#include <sys/param.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070031
Colin Cross9227bd32013-07-23 16:59:20 -070032#include <log/logd.h>
33#include <log/logprint.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070034
Mark Salyzynb932b2f2015-05-15 09:01:58 -070035/* open coded fragment, prevent circular dependencies */
36#define WEAK static
37
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070038typedef struct FilterInfo_t {
39 char *mTag;
40 android_LogPriority mPri;
41 struct FilterInfo_t *p_next;
42} FilterInfo;
43
44struct AndroidLogFormat_t {
45 android_LogPriority global_pri;
46 FilterInfo *filters;
47 AndroidLogPrintFormat format;
Pierre Zurekead88fc2010-10-17 22:39:37 +020048 bool colored_output;
Mark Salyzyne1f20042015-05-06 08:40:40 -070049 bool usec_time_output;
Mark Salyzynb932b2f2015-05-15 09:01:58 -070050 bool printable_output;
Mark Salyzynf28f6a92015-08-31 08:01:33 -070051 bool year_output;
52 bool zone_output;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070053};
54
Pierre Zurekead88fc2010-10-17 22:39:37 +020055/*
56 * gnome-terminal color tags
57 * See http://misc.flogisoft.com/bash/tip_colors_and_formatting
58 * for ideas on how to set the forground color of the text for xterm.
59 * The color manipulation character stream is defined as:
60 * ESC [ 3 8 ; 5 ; <color#> m
61 */
62#define ANDROID_COLOR_BLUE 75
63#define ANDROID_COLOR_DEFAULT 231
64#define ANDROID_COLOR_GREEN 40
65#define ANDROID_COLOR_ORANGE 166
66#define ANDROID_COLOR_RED 196
67#define ANDROID_COLOR_YELLOW 226
68
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070069static FilterInfo * filterinfo_new(const char * tag, android_LogPriority pri)
70{
71 FilterInfo *p_ret;
72
73 p_ret = (FilterInfo *)calloc(1, sizeof(FilterInfo));
74 p_ret->mTag = strdup(tag);
75 p_ret->mPri = pri;
76
77 return p_ret;
78}
79
Mark Salyzyna04464a2014-04-30 08:50:53 -070080/* balance to above, filterinfo_free left unimplemented */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070081
82/*
83 * Note: also accepts 0-9 priorities
84 * returns ANDROID_LOG_UNKNOWN if the character is unrecognized
85 */
86static android_LogPriority filterCharToPri (char c)
87{
88 android_LogPriority pri;
89
90 c = tolower(c);
91
92 if (c >= '0' && c <= '9') {
93 if (c >= ('0'+ANDROID_LOG_SILENT)) {
94 pri = ANDROID_LOG_VERBOSE;
95 } else {
96 pri = (android_LogPriority)(c - '0');
97 }
98 } else if (c == 'v') {
99 pri = ANDROID_LOG_VERBOSE;
100 } else if (c == 'd') {
101 pri = ANDROID_LOG_DEBUG;
102 } else if (c == 'i') {
103 pri = ANDROID_LOG_INFO;
104 } else if (c == 'w') {
105 pri = ANDROID_LOG_WARN;
106 } else if (c == 'e') {
107 pri = ANDROID_LOG_ERROR;
108 } else if (c == 'f') {
109 pri = ANDROID_LOG_FATAL;
110 } else if (c == 's') {
111 pri = ANDROID_LOG_SILENT;
112 } else if (c == '*') {
113 pri = ANDROID_LOG_DEFAULT;
114 } else {
115 pri = ANDROID_LOG_UNKNOWN;
116 }
117
118 return pri;
119}
120
121static char filterPriToChar (android_LogPriority pri)
122{
123 switch (pri) {
124 case ANDROID_LOG_VERBOSE: return 'V';
125 case ANDROID_LOG_DEBUG: return 'D';
126 case ANDROID_LOG_INFO: return 'I';
127 case ANDROID_LOG_WARN: return 'W';
128 case ANDROID_LOG_ERROR: return 'E';
129 case ANDROID_LOG_FATAL: return 'F';
130 case ANDROID_LOG_SILENT: return 'S';
131
132 case ANDROID_LOG_DEFAULT:
133 case ANDROID_LOG_UNKNOWN:
134 default: return '?';
135 }
136}
137
Pierre Zurekead88fc2010-10-17 22:39:37 +0200138static int colorFromPri (android_LogPriority pri)
139{
140 switch (pri) {
141 case ANDROID_LOG_VERBOSE: return ANDROID_COLOR_DEFAULT;
142 case ANDROID_LOG_DEBUG: return ANDROID_COLOR_BLUE;
143 case ANDROID_LOG_INFO: return ANDROID_COLOR_GREEN;
144 case ANDROID_LOG_WARN: return ANDROID_COLOR_ORANGE;
145 case ANDROID_LOG_ERROR: return ANDROID_COLOR_RED;
146 case ANDROID_LOG_FATAL: return ANDROID_COLOR_RED;
147 case ANDROID_LOG_SILENT: return ANDROID_COLOR_DEFAULT;
148
149 case ANDROID_LOG_DEFAULT:
150 case ANDROID_LOG_UNKNOWN:
151 default: return ANDROID_COLOR_DEFAULT;
152 }
153}
154
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700155static android_LogPriority filterPriForTag(
156 AndroidLogFormat *p_format, const char *tag)
157{
158 FilterInfo *p_curFilter;
159
160 for (p_curFilter = p_format->filters
161 ; p_curFilter != NULL
162 ; p_curFilter = p_curFilter->p_next
163 ) {
164 if (0 == strcmp(tag, p_curFilter->mTag)) {
165 if (p_curFilter->mPri == ANDROID_LOG_DEFAULT) {
166 return p_format->global_pri;
167 } else {
168 return p_curFilter->mPri;
169 }
170 }
171 }
172
173 return p_format->global_pri;
174}
175
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700176/**
177 * returns 1 if this log line should be printed based on its priority
178 * and tag, and 0 if it should not
179 */
180int android_log_shouldPrintLine (
181 AndroidLogFormat *p_format, const char *tag, android_LogPriority pri)
182{
183 return pri >= filterPriForTag(p_format, tag);
184}
185
186AndroidLogFormat *android_log_format_new()
187{
188 AndroidLogFormat *p_ret;
189
190 p_ret = calloc(1, sizeof(AndroidLogFormat));
191
192 p_ret->global_pri = ANDROID_LOG_VERBOSE;
193 p_ret->format = FORMAT_BRIEF;
Pierre Zurekead88fc2010-10-17 22:39:37 +0200194 p_ret->colored_output = false;
Mark Salyzyne1f20042015-05-06 08:40:40 -0700195 p_ret->usec_time_output = false;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700196 p_ret->printable_output = false;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700197 p_ret->year_output = false;
198 p_ret->zone_output = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700199
200 return p_ret;
201}
202
203void android_log_format_free(AndroidLogFormat *p_format)
204{
205 FilterInfo *p_info, *p_info_old;
206
207 p_info = p_format->filters;
208
209 while (p_info != NULL) {
210 p_info_old = p_info;
211 p_info = p_info->p_next;
212
213 free(p_info_old);
214 }
215
216 free(p_format);
217}
218
219
220
Mark Salyzyne1f20042015-05-06 08:40:40 -0700221int android_log_setPrintFormat(AndroidLogFormat *p_format,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700222 AndroidLogPrintFormat format)
223{
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700224 switch (format) {
225 case FORMAT_MODIFIER_COLOR:
Pierre Zurekead88fc2010-10-17 22:39:37 +0200226 p_format->colored_output = true;
Mark Salyzyne1f20042015-05-06 08:40:40 -0700227 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700228 case FORMAT_MODIFIER_TIME_USEC:
Mark Salyzyne1f20042015-05-06 08:40:40 -0700229 p_format->usec_time_output = true;
230 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700231 case FORMAT_MODIFIER_PRINTABLE:
232 p_format->printable_output = true;
233 return 0;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700234 case FORMAT_MODIFIER_YEAR:
235 p_format->year_output = true;
236 return 0;
237 case FORMAT_MODIFIER_ZONE:
238 p_format->zone_output = !p_format->zone_output;
239 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700240 default:
241 break;
Mark Salyzyne1f20042015-05-06 08:40:40 -0700242 }
243 p_format->format = format;
244 return 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700245}
246
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700247static const char tz[] = "TZ";
248static const char utc[] = "UTC";
249
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700250/**
251 * Returns FORMAT_OFF on invalid string
252 */
253AndroidLogPrintFormat android_log_formatFromString(const char * formatString)
254{
255 static AndroidLogPrintFormat format;
256
257 if (strcmp(formatString, "brief") == 0) format = FORMAT_BRIEF;
258 else if (strcmp(formatString, "process") == 0) format = FORMAT_PROCESS;
259 else if (strcmp(formatString, "tag") == 0) format = FORMAT_TAG;
260 else if (strcmp(formatString, "thread") == 0) format = FORMAT_THREAD;
261 else if (strcmp(formatString, "raw") == 0) format = FORMAT_RAW;
262 else if (strcmp(formatString, "time") == 0) format = FORMAT_TIME;
263 else if (strcmp(formatString, "threadtime") == 0) format = FORMAT_THREADTIME;
264 else if (strcmp(formatString, "long") == 0) format = FORMAT_LONG;
Mark Salyzyne1f20042015-05-06 08:40:40 -0700265 else if (strcmp(formatString, "color") == 0) format = FORMAT_MODIFIER_COLOR;
266 else if (strcmp(formatString, "usec") == 0) format = FORMAT_MODIFIER_TIME_USEC;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700267 else if (strcmp(formatString, "printable") == 0) format = FORMAT_MODIFIER_PRINTABLE;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700268 else if (strcmp(formatString, "year") == 0) format = FORMAT_MODIFIER_YEAR;
269 else if (strcmp(formatString, "zone") == 0) format = FORMAT_MODIFIER_ZONE;
270 else {
271 extern char *tzname[2];
272 static const char gmt[] = "GMT";
273 char *cp = getenv(tz);
274 if (cp) {
275 cp = strdup(cp);
276 }
277 setenv(tz, formatString, 1);
278 /*
279 * Run tzset here to determine if the timezone is legitimate. If the
280 * zone is GMT, check if that is what was asked for, if not then
281 * did not match any on the system; report an error to caller.
282 */
283 tzset();
284 if (!tzname[0]
285 || ((!strcmp(tzname[0], utc)
286 || !strcmp(tzname[0], gmt)) /* error? */
287 && strcasecmp(formatString, utc)
288 && strcasecmp(formatString, gmt))) { /* ok */
289 if (cp) {
290 setenv(tz, cp, 1);
291 } else {
292 unsetenv(tz);
293 }
294 tzset();
295 format = FORMAT_OFF;
296 } else {
297 format = FORMAT_MODIFIER_ZONE;
298 }
299 free(cp);
300 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301
302 return format;
303}
304
305/**
306 * filterExpression: a single filter expression
307 * eg "AT:d"
308 *
309 * returns 0 on success and -1 on invalid expression
310 *
311 * Assumes single threaded execution
312 */
313
314int android_log_addFilterRule(AndroidLogFormat *p_format,
315 const char *filterExpression)
316{
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700317 size_t tagNameLength;
318 android_LogPriority pri = ANDROID_LOG_DEFAULT;
319
320 tagNameLength = strcspn(filterExpression, ":");
321
322 if (tagNameLength == 0) {
323 goto error;
324 }
325
326 if(filterExpression[tagNameLength] == ':') {
327 pri = filterCharToPri(filterExpression[tagNameLength+1]);
328
329 if (pri == ANDROID_LOG_UNKNOWN) {
330 goto error;
331 }
332 }
333
334 if(0 == strncmp("*", filterExpression, tagNameLength)) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700335 /*
336 * This filter expression refers to the global filter
337 * The default level for this is DEBUG if the priority
338 * is unspecified
339 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700340 if (pri == ANDROID_LOG_DEFAULT) {
341 pri = ANDROID_LOG_DEBUG;
342 }
343
344 p_format->global_pri = pri;
345 } else {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700346 /*
347 * for filter expressions that don't refer to the global
348 * filter, the default is verbose if the priority is unspecified
349 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700350 if (pri == ANDROID_LOG_DEFAULT) {
351 pri = ANDROID_LOG_VERBOSE;
352 }
353
354 char *tagName;
355
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700356/*
357 * Presently HAVE_STRNDUP is never defined, so the second case is always taken
358 * Darwin doesn't have strnup, everything else does
359 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700360#ifdef HAVE_STRNDUP
361 tagName = strndup(filterExpression, tagNameLength);
362#else
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700363 /* a few extra bytes copied... */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700364 tagName = strdup(filterExpression);
365 tagName[tagNameLength] = '\0';
366#endif /*HAVE_STRNDUP*/
367
368 FilterInfo *p_fi = filterinfo_new(tagName, pri);
369 free(tagName);
370
371 p_fi->p_next = p_format->filters;
372 p_format->filters = p_fi;
373 }
374
375 return 0;
376error:
377 return -1;
378}
379
380
381/**
382 * filterString: a comma/whitespace-separated set of filter expressions
383 *
384 * eg "AT:d *:i"
385 *
386 * returns 0 on success and -1 on invalid expression
387 *
388 * Assumes single threaded execution
389 *
390 */
391
392int android_log_addFilterString(AndroidLogFormat *p_format,
393 const char *filterString)
394{
395 char *filterStringCopy = strdup (filterString);
396 char *p_cur = filterStringCopy;
397 char *p_ret;
398 int err;
399
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700400 /* Yes, I'm using strsep */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700401 while (NULL != (p_ret = strsep(&p_cur, " \t,"))) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700402 /* ignore whitespace-only entries */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700403 if(p_ret[0] != '\0') {
404 err = android_log_addFilterRule(p_format, p_ret);
405
406 if (err < 0) {
407 goto error;
408 }
409 }
410 }
411
412 free (filterStringCopy);
413 return 0;
414error:
415 free (filterStringCopy);
416 return -1;
417}
418
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700419/**
420 * Splits a wire-format buffer into an AndroidLogEntry
421 * entry allocated by caller. Pointers will point directly into buf
422 *
423 * Returns 0 on success and -1 on invalid wire format (entry will be
424 * in unspecified state)
425 */
426int android_log_processLogBuffer(struct logger_entry *buf,
427 AndroidLogEntry *entry)
428{
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700429 entry->tv_sec = buf->sec;
430 entry->tv_nsec = buf->nsec;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700431 entry->pid = buf->pid;
432 entry->tid = buf->tid;
Kenny Root4bf3c022011-09-30 17:10:14 -0700433
434 /*
435 * format: <priority:1><tag:N>\0<message:N>\0
436 *
437 * tag str
Nick Kraleviche1ede152011-10-18 15:23:33 -0700438 * starts at buf->msg+1
Kenny Root4bf3c022011-09-30 17:10:14 -0700439 * msg
Nick Kraleviche1ede152011-10-18 15:23:33 -0700440 * starts at buf->msg+1+len(tag)+1
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700441 *
442 * The message may have been truncated by the kernel log driver.
443 * When that happens, we must null-terminate the message ourselves.
Kenny Root4bf3c022011-09-30 17:10:14 -0700444 */
Nick Kraleviche1ede152011-10-18 15:23:33 -0700445 if (buf->len < 3) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700446 /*
447 * An well-formed entry must consist of at least a priority
448 * and two null characters
449 */
Nick Kraleviche1ede152011-10-18 15:23:33 -0700450 fprintf(stderr, "+++ LOG: entry too small\n");
Kenny Root4bf3c022011-09-30 17:10:14 -0700451 return -1;
452 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700453
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700454 int msgStart = -1;
455 int msgEnd = -1;
456
Nick Kraleviche1ede152011-10-18 15:23:33 -0700457 int i;
Mark Salyzyn40b21552013-12-18 12:59:01 -0800458 char *msg = buf->msg;
459 struct logger_entry_v2 *buf2 = (struct logger_entry_v2 *)buf;
460 if (buf2->hdr_size) {
461 msg = ((char *)buf2) + buf2->hdr_size;
462 }
Nick Kraleviche1ede152011-10-18 15:23:33 -0700463 for (i = 1; i < buf->len; i++) {
Mark Salyzyn40b21552013-12-18 12:59:01 -0800464 if (msg[i] == '\0') {
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700465 if (msgStart == -1) {
466 msgStart = i + 1;
467 } else {
468 msgEnd = i;
469 break;
470 }
Nick Kraleviche1ede152011-10-18 15:23:33 -0700471 }
472 }
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700473
474 if (msgStart == -1) {
475 fprintf(stderr, "+++ LOG: malformed log message\n");
Nick Kralevich63f4a842011-10-17 10:45:03 -0700476 return -1;
477 }
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700478 if (msgEnd == -1) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700479 /* incoming message not null-terminated; force it */
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700480 msgEnd = buf->len - 1;
Mark Salyzyn40b21552013-12-18 12:59:01 -0800481 msg[msgEnd] = '\0';
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700482 }
483
Mark Salyzyn40b21552013-12-18 12:59:01 -0800484 entry->priority = msg[0];
485 entry->tag = msg + 1;
486 entry->message = msg + msgStart;
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700487 entry->messageLen = msgEnd - msgStart;
Nick Kralevich63f4a842011-10-17 10:45:03 -0700488
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700489 return 0;
490}
491
492/*
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000493 * Extract a 4-byte value from a byte stream.
494 */
495static inline uint32_t get4LE(const uint8_t* src)
496{
497 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
498}
499
500/*
501 * Extract an 8-byte value from a byte stream.
502 */
503static inline uint64_t get8LE(const uint8_t* src)
504{
505 uint32_t low, high;
506
507 low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
508 high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24);
Jeff Brown44193d92015-04-28 12:47:02 -0700509 return ((uint64_t) high << 32) | (uint64_t) low;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000510}
511
512
513/*
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700514 * Recursively convert binary log data to printable form.
515 *
516 * This needs to be recursive because you can have lists of lists.
517 *
518 * If we run out of room, we stop processing immediately. It's important
519 * for us to check for space on every output element to avoid producing
520 * garbled output.
521 *
522 * Returns 0 on success, 1 on buffer full, -1 on failure.
523 */
524static int android_log_printBinaryEvent(const unsigned char** pEventData,
525 size_t* pEventDataLen, char** pOutBuf, size_t* pOutBufLen)
526{
527 const unsigned char* eventData = *pEventData;
528 size_t eventDataLen = *pEventDataLen;
529 char* outBuf = *pOutBuf;
530 size_t outBufLen = *pOutBufLen;
531 unsigned char type;
532 size_t outCount;
533 int result = 0;
534
535 if (eventDataLen < 1)
536 return -1;
537 type = *eventData++;
538 eventDataLen--;
539
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700540 switch (type) {
541 case EVENT_TYPE_INT:
542 /* 32-bit signed int */
543 {
544 int ival;
545
546 if (eventDataLen < 4)
547 return -1;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000548 ival = get4LE(eventData);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700549 eventData += 4;
550 eventDataLen -= 4;
551
552 outCount = snprintf(outBuf, outBufLen, "%d", ival);
553 if (outCount < outBufLen) {
554 outBuf += outCount;
555 outBufLen -= outCount;
556 } else {
557 /* halt output */
558 goto no_room;
559 }
560 }
561 break;
562 case EVENT_TYPE_LONG:
563 /* 64-bit signed long */
564 {
Jeff Brown44193d92015-04-28 12:47:02 -0700565 uint64_t lval;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700566
567 if (eventDataLen < 8)
568 return -1;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000569 lval = get8LE(eventData);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700570 eventData += 8;
571 eventDataLen -= 8;
572
Jeff Brown44193d92015-04-28 12:47:02 -0700573 outCount = snprintf(outBuf, outBufLen, "%" PRId64, lval);
574 if (outCount < outBufLen) {
575 outBuf += outCount;
576 outBufLen -= outCount;
577 } else {
578 /* halt output */
579 goto no_room;
580 }
581 }
582 break;
583 case EVENT_TYPE_FLOAT:
584 /* float */
585 {
586 uint32_t ival;
587 float fval;
588
589 if (eventDataLen < 4)
590 return -1;
591 ival = get4LE(eventData);
592 fval = *(float*)&ival;
593 eventData += 4;
594 eventDataLen -= 4;
595
596 outCount = snprintf(outBuf, outBufLen, "%f", fval);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700597 if (outCount < outBufLen) {
598 outBuf += outCount;
599 outBufLen -= outCount;
600 } else {
601 /* halt output */
602 goto no_room;
603 }
604 }
605 break;
606 case EVENT_TYPE_STRING:
607 /* UTF-8 chars, not NULL-terminated */
608 {
609 unsigned int strLen;
610
611 if (eventDataLen < 4)
612 return -1;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000613 strLen = get4LE(eventData);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700614 eventData += 4;
615 eventDataLen -= 4;
616
617 if (eventDataLen < strLen)
618 return -1;
619
620 if (strLen < outBufLen) {
621 memcpy(outBuf, eventData, strLen);
622 outBuf += strLen;
623 outBufLen -= strLen;
624 } else if (outBufLen > 0) {
625 /* copy what we can */
626 memcpy(outBuf, eventData, outBufLen);
627 outBuf += outBufLen;
628 outBufLen -= outBufLen;
629 goto no_room;
630 }
631 eventData += strLen;
632 eventDataLen -= strLen;
633 break;
634 }
635 case EVENT_TYPE_LIST:
636 /* N items, all different types */
637 {
638 unsigned char count;
639 int i;
640
641 if (eventDataLen < 1)
642 return -1;
643
644 count = *eventData++;
645 eventDataLen--;
646
647 if (outBufLen > 0) {
648 *outBuf++ = '[';
649 outBufLen--;
650 } else {
651 goto no_room;
652 }
653
654 for (i = 0; i < count; i++) {
655 result = android_log_printBinaryEvent(&eventData, &eventDataLen,
656 &outBuf, &outBufLen);
657 if (result != 0)
658 goto bail;
659
660 if (i < count-1) {
661 if (outBufLen > 0) {
662 *outBuf++ = ',';
663 outBufLen--;
664 } else {
665 goto no_room;
666 }
667 }
668 }
669
670 if (outBufLen > 0) {
671 *outBuf++ = ']';
672 outBufLen--;
673 } else {
674 goto no_room;
675 }
676 }
677 break;
678 default:
679 fprintf(stderr, "Unknown binary event type %d\n", type);
680 return -1;
681 }
682
683bail:
684 *pEventData = eventData;
685 *pEventDataLen = eventDataLen;
686 *pOutBuf = outBuf;
687 *pOutBufLen = outBufLen;
688 return result;
689
690no_room:
691 result = 1;
692 goto bail;
693}
694
695/**
696 * Convert a binary log entry to ASCII form.
697 *
698 * For convenience we mimic the processLogBuffer API. There is no
699 * pre-defined output length for the binary data, since we're free to format
700 * it however we choose, which means we can't really use a fixed-size buffer
701 * here.
702 */
703int android_log_processBinaryLogBuffer(struct logger_entry *buf,
704 AndroidLogEntry *entry, const EventTagMap* map, char* messageBuf,
705 int messageBufLen)
706{
707 size_t inCount;
708 unsigned int tagIndex;
709 const unsigned char* eventData;
710
711 entry->tv_sec = buf->sec;
712 entry->tv_nsec = buf->nsec;
713 entry->priority = ANDROID_LOG_INFO;
714 entry->pid = buf->pid;
715 entry->tid = buf->tid;
716
717 /*
718 * Pull the tag out.
719 */
720 eventData = (const unsigned char*) buf->msg;
Mark Salyzyn40b21552013-12-18 12:59:01 -0800721 struct logger_entry_v2 *buf2 = (struct logger_entry_v2 *)buf;
722 if (buf2->hdr_size) {
723 eventData = ((unsigned char *)buf2) + buf2->hdr_size;
724 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700725 inCount = buf->len;
726 if (inCount < 4)
727 return -1;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000728 tagIndex = get4LE(eventData);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700729 eventData += 4;
730 inCount -= 4;
731
732 if (map != NULL) {
733 entry->tag = android_lookupEventTag(map, tagIndex);
734 } else {
735 entry->tag = NULL;
736 }
737
738 /*
739 * If we don't have a map, or didn't find the tag number in the map,
740 * stuff a generated tag value into the start of the output buffer and
741 * shift the buffer pointers down.
742 */
743 if (entry->tag == NULL) {
744 int tagLen;
745
746 tagLen = snprintf(messageBuf, messageBufLen, "[%d]", tagIndex);
747 entry->tag = messageBuf;
748 messageBuf += tagLen+1;
749 messageBufLen -= tagLen+1;
750 }
751
752 /*
753 * Format the event log data into the buffer.
754 */
755 char* outBuf = messageBuf;
756 size_t outRemaining = messageBufLen-1; /* leave one for nul byte */
757 int result;
758 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf,
759 &outRemaining);
760 if (result < 0) {
761 fprintf(stderr, "Binary log entry conversion failed\n");
762 return -1;
763 } else if (result == 1) {
764 if (outBuf > messageBuf) {
765 /* leave an indicator */
766 *(outBuf-1) = '!';
767 } else {
768 /* no room to output anything at all */
769 *outBuf++ = '!';
770 outRemaining--;
771 }
772 /* pretend we ate all the data */
773 inCount = 0;
774 }
775
776 /* eat the silly terminating '\n' */
777 if (inCount == 1 && *eventData == '\n') {
778 eventData++;
779 inCount--;
780 }
781
782 if (inCount != 0) {
783 fprintf(stderr,
Andrew Hsiehd2c8f522012-02-27 16:48:18 -0800784 "Warning: leftover binary log data (%zu bytes)\n", inCount);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700785 }
786
787 /*
788 * Terminate the buffer. The NUL byte does not count as part of
789 * entry->messageLen.
790 */
791 *outBuf = '\0';
792 entry->messageLen = outBuf - messageBuf;
793 assert(entry->messageLen == (messageBufLen-1) - outRemaining);
794
795 entry->message = messageBuf;
796
797 return 0;
798}
799
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700800/*
801 * One utf8 character at a time
802 *
803 * Returns the length of the utf8 character in the buffer,
804 * or -1 if illegal or truncated
805 *
806 * Open coded from libutils/Unicode.cpp, borrowed from utf8_length(),
807 * can not remove from here because of library circular dependencies.
808 * Expect one-day utf8_character_length with the same signature could
809 * _also_ be part of libutils/Unicode.cpp if its usefullness needs to
810 * propagate globally.
811 */
812WEAK ssize_t utf8_character_length(const char *src, size_t len)
813{
814 const char *cur = src;
815 const char first_char = *cur++;
816 static const uint32_t kUnicodeMaxCodepoint = 0x0010FFFF;
817 int32_t mask, to_ignore_mask;
818 size_t num_to_read;
819 uint32_t utf32;
820
821 if ((first_char & 0x80) == 0) { /* ASCII */
822 return 1;
823 }
824
825 /*
826 * (UTF-8's character must not be like 10xxxxxx,
827 * but 110xxxxx, 1110xxxx, ... or 1111110x)
828 */
829 if ((first_char & 0x40) == 0) {
830 return -1;
831 }
832
833 for (utf32 = 1, num_to_read = 1, mask = 0x40, to_ignore_mask = 0x80;
834 num_to_read < 5 && (first_char & mask);
835 num_to_read++, to_ignore_mask |= mask, mask >>= 1) {
836 if (num_to_read > len) {
837 return -1;
838 }
839 if ((*cur & 0xC0) != 0x80) { /* can not be 10xxxxxx? */
840 return -1;
841 }
842 utf32 = (utf32 << 6) + (*cur++ & 0b00111111);
843 }
844 /* "first_char" must be (110xxxxx - 11110xxx) */
845 if (num_to_read >= 5) {
846 return -1;
847 }
848 to_ignore_mask |= mask;
849 utf32 |= ((~to_ignore_mask) & first_char) << (6 * (num_to_read - 1));
850 if (utf32 > kUnicodeMaxCodepoint) {
851 return -1;
852 }
853 return num_to_read;
854}
855
856/*
857 * Convert to printable from message to p buffer, return string length. If p is
858 * NULL, do not copy, but still return the expected string length.
859 */
860static size_t convertPrintable(char *p, const char *message, size_t messageLen)
861{
862 char *begin = p;
863 bool print = p != NULL;
864
865 while (messageLen) {
866 char buf[6];
867 ssize_t len = sizeof(buf) - 1;
868 if ((size_t)len > messageLen) {
869 len = messageLen;
870 }
871 len = utf8_character_length(message, len);
872
873 if (len < 0) {
874 snprintf(buf, sizeof(buf),
875 ((messageLen > 1) && isdigit(message[1]))
876 ? "\\%03o"
877 : "\\%o",
878 *message & 0377);
879 len = 1;
880 } else {
881 buf[0] = '\0';
882 if (len == 1) {
883 if (*message == '\a') {
884 strcpy(buf, "\\a");
885 } else if (*message == '\b') {
886 strcpy(buf, "\\b");
887 } else if (*message == '\t') {
888 strcpy(buf, "\\t");
889 } else if (*message == '\v') {
890 strcpy(buf, "\\v");
891 } else if (*message == '\f') {
892 strcpy(buf, "\\f");
893 } else if (*message == '\r') {
894 strcpy(buf, "\\r");
895 } else if (*message == '\\') {
896 strcpy(buf, "\\\\");
897 } else if ((*message < ' ') || (*message & 0x80)) {
898 snprintf(buf, sizeof(buf), "\\%o", *message & 0377);
899 }
900 }
901 if (!buf[0]) {
902 strncpy(buf, message, len);
903 buf[len] = '\0';
904 }
905 }
906 if (print) {
907 strcpy(p, buf);
908 }
909 p += strlen(buf);
910 message += len;
911 messageLen -= len;
912 }
913 return p - begin;
914}
915
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700916/**
917 * Formats a log message into a buffer
918 *
919 * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer
920 * If return value != defaultBuffer, caller must call free()
921 * Returns NULL on malloc error
922 */
923
924char *android_log_formatLogLine (
925 AndroidLogFormat *p_format,
926 char *defaultBuffer,
927 size_t defaultBufferSize,
928 const AndroidLogEntry *entry,
929 size_t *p_outLength)
930{
Yabin Cui8a985352014-11-13 10:02:08 -0800931#if !defined(_WIN32)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700932 struct tm tmBuf;
933#endif
934 struct tm* ptm;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700935 char timeBuf[64]; /* good margin, 23+nul for msec, 26+nul for usec */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700936 char prefixBuf[128], suffixBuf[128];
937 char priChar;
938 int prefixSuffixIsHeaderFooter = 0;
939 char * ret = NULL;
940
941 priChar = filterPriToChar(entry->priority);
Pierre Zurekead88fc2010-10-17 22:39:37 +0200942 size_t prefixLen = 0, suffixLen = 0;
943 size_t len;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700944
945 /*
946 * Get the current date/time in pretty form
947 *
948 * It's often useful when examining a log with "less" to jump to
949 * a specific point in the file by searching for the date/time stamp.
950 * For this reason it's very annoying to have regexp meta characters
951 * in the time stamp. Don't use forward slashes, parenthesis,
952 * brackets, asterisks, or other special chars here.
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700953 *
954 * The caller may have affected the timezone environment, this is
955 * expected to be sensitive to that.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700956 */
Yabin Cui8a985352014-11-13 10:02:08 -0800957#if !defined(_WIN32)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700958 ptm = localtime_r(&(entry->tv_sec), &tmBuf);
959#else
960 ptm = localtime(&(entry->tv_sec));
961#endif
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700962 strftime(timeBuf, sizeof(timeBuf),
963 &"%Y-%m-%d %H:%M:%S"[p_format->year_output ? 0 : 3],
964 ptm);
Mark Salyzyne1f20042015-05-06 08:40:40 -0700965 len = strlen(timeBuf);
966 if (p_format->usec_time_output) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700967 len += snprintf(timeBuf + len, sizeof(timeBuf) - len,
968 ".%06ld", entry->tv_nsec / 1000);
Mark Salyzyne1f20042015-05-06 08:40:40 -0700969 } else {
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700970 len += snprintf(timeBuf + len, sizeof(timeBuf) - len,
971 ".%03ld", entry->tv_nsec / 1000000);
972 }
973 if (p_format->zone_output) {
974 strftime(timeBuf + len, sizeof(timeBuf) - len, " %z", ptm);
Mark Salyzyne1f20042015-05-06 08:40:40 -0700975 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700976
977 /*
978 * Construct a buffer containing the log header and log message.
979 */
Pierre Zurekead88fc2010-10-17 22:39:37 +0200980 if (p_format->colored_output) {
981 prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm",
982 colorFromPri(entry->priority));
983 prefixLen = MIN(prefixLen, sizeof(prefixBuf));
984 suffixLen = snprintf(suffixBuf, sizeof(suffixBuf), "\x1B[0m");
985 suffixLen = MIN(suffixLen, sizeof(suffixBuf));
986 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700987
988 switch (p_format->format) {
989 case FORMAT_TAG:
Pierre Zurekead88fc2010-10-17 22:39:37 +0200990 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700991 "%c/%-8s: ", priChar, entry->tag);
Pierre Zurekead88fc2010-10-17 22:39:37 +0200992 strcpy(suffixBuf + suffixLen, "\n");
993 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700994 break;
995 case FORMAT_PROCESS:
Pierre Zurekead88fc2010-10-17 22:39:37 +0200996 len = snprintf(suffixBuf + suffixLen, sizeof(suffixBuf) - suffixLen,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700997 " (%s)\n", entry->tag);
Pierre Zurekead88fc2010-10-17 22:39:37 +0200998 suffixLen += MIN(len, sizeof(suffixBuf) - suffixLen);
999 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
1000 "%c(%5d) ", priChar, entry->pid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001001 break;
1002 case FORMAT_THREAD:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001003 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Andrew Hsiehd2c8f522012-02-27 16:48:18 -08001004 "%c(%5d:%5d) ", priChar, entry->pid, entry->tid);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001005 strcpy(suffixBuf + suffixLen, "\n");
1006 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001007 break;
1008 case FORMAT_RAW:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001009 prefixBuf[prefixLen] = 0;
1010 len = 0;
1011 strcpy(suffixBuf + suffixLen, "\n");
1012 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001013 break;
1014 case FORMAT_TIME:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001015 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyne1f20042015-05-06 08:40:40 -07001016 "%s %c/%-8s(%5d): ", timeBuf, priChar, entry->tag, entry->pid);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001017 strcpy(suffixBuf + suffixLen, "\n");
1018 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001019 break;
1020 case FORMAT_THREADTIME:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001021 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyne1f20042015-05-06 08:40:40 -07001022 "%s %5d %5d %c %-8s: ", timeBuf,
Andrew Hsiehd2c8f522012-02-27 16:48:18 -08001023 entry->pid, entry->tid, priChar, entry->tag);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001024 strcpy(suffixBuf + suffixLen, "\n");
1025 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001026 break;
1027 case FORMAT_LONG:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001028 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyne1f20042015-05-06 08:40:40 -07001029 "[ %s %5d:%5d %c/%-8s ]\n",
1030 timeBuf, entry->pid, entry->tid, priChar, entry->tag);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001031 strcpy(suffixBuf + suffixLen, "\n\n");
1032 suffixLen += 2;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001033 prefixSuffixIsHeaderFooter = 1;
1034 break;
1035 case FORMAT_BRIEF:
1036 default:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001037 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001038 "%c/%-8s(%5d): ", priChar, entry->tag, entry->pid);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001039 strcpy(suffixBuf + suffixLen, "\n");
1040 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001041 break;
1042 }
Pierre Zurekead88fc2010-10-17 22:39:37 +02001043
Keith Prestonb45b5c92010-02-11 15:12:53 -06001044 /* snprintf has a weird return value. It returns what would have been
1045 * written given a large enough buffer. In the case that the prefix is
1046 * longer then our buffer(128), it messes up the calculations below
1047 * possibly causing heap corruption. To avoid this we double check and
1048 * set the length at the maximum (size minus null byte)
1049 */
Pierre Zurekead88fc2010-10-17 22:39:37 +02001050 prefixLen += MIN(len, sizeof(prefixBuf) - prefixLen);
Mark Salyzyne2428422015-01-22 10:00:04 -08001051 suffixLen = MIN(suffixLen, sizeof(suffixBuf));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001052
1053 /* the following code is tragically unreadable */
1054
1055 size_t numLines;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001056 char *p;
1057 size_t bufferSize;
1058 const char *pm;
1059
1060 if (prefixSuffixIsHeaderFooter) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001061 /* we're just wrapping message with a header/footer */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001062 numLines = 1;
1063 } else {
1064 pm = entry->message;
1065 numLines = 0;
1066
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001067 /*
1068 * The line-end finding here must match the line-end finding
1069 * in for ( ... numLines...) loop below
1070 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001071 while (pm < (entry->message + entry->messageLen)) {
1072 if (*pm++ == '\n') numLines++;
1073 }
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001074 /* plus one line for anything not newline-terminated at the end */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001075 if (pm > entry->message && *(pm-1) != '\n') numLines++;
1076 }
1077
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001078 /*
1079 * this is an upper bound--newlines in message may be counted
1080 * extraneously
1081 */
1082 bufferSize = (numLines * (prefixLen + suffixLen)) + 1;
1083 if (p_format->printable_output) {
1084 /* Calculate extra length to convert non-printable to printable */
1085 bufferSize += convertPrintable(NULL, entry->message, entry->messageLen);
1086 } else {
1087 bufferSize += entry->messageLen;
1088 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001089
1090 if (defaultBufferSize >= bufferSize) {
1091 ret = defaultBuffer;
1092 } else {
1093 ret = (char *)malloc(bufferSize);
1094
1095 if (ret == NULL) {
1096 return ret;
1097 }
1098 }
1099
1100 ret[0] = '\0'; /* to start strcat off */
1101
1102 p = ret;
1103 pm = entry->message;
1104
1105 if (prefixSuffixIsHeaderFooter) {
1106 strcat(p, prefixBuf);
1107 p += prefixLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001108 if (p_format->printable_output) {
1109 p += convertPrintable(p, entry->message, entry->messageLen);
1110 } else {
1111 strncat(p, entry->message, entry->messageLen);
1112 p += entry->messageLen;
1113 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001114 strcat(p, suffixBuf);
1115 p += suffixLen;
1116 } else {
1117 while(pm < (entry->message + entry->messageLen)) {
1118 const char *lineStart;
1119 size_t lineLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001120 lineStart = pm;
1121
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001122 /* Find the next end-of-line in message */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001123 while (pm < (entry->message + entry->messageLen)
1124 && *pm != '\n') pm++;
1125 lineLen = pm - lineStart;
1126
1127 strcat(p, prefixBuf);
1128 p += prefixLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001129 if (p_format->printable_output) {
1130 p += convertPrintable(p, lineStart, lineLen);
1131 } else {
1132 strncat(p, lineStart, lineLen);
1133 p += lineLen;
1134 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001135 strcat(p, suffixBuf);
1136 p += suffixLen;
1137
1138 if (*pm == '\n') pm++;
1139 }
1140 }
1141
1142 if (p_outLength != NULL) {
1143 *p_outLength = p - ret;
1144 }
1145
1146 return ret;
1147}
1148
1149/**
1150 * Either print or do not print log line, based on filter
1151 *
1152 * Returns count bytes written
1153 */
1154
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -08001155int android_log_printLogLine(
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001156 AndroidLogFormat *p_format,
1157 int fd,
1158 const AndroidLogEntry *entry)
1159{
1160 int ret;
1161 char defaultBuffer[512];
1162 char *outBuffer = NULL;
1163 size_t totalLen;
1164
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001165 outBuffer = android_log_formatLogLine(p_format, defaultBuffer,
1166 sizeof(defaultBuffer), entry, &totalLen);
1167
1168 if (!outBuffer)
1169 return -1;
1170
1171 do {
1172 ret = write(fd, outBuffer, totalLen);
1173 } while (ret < 0 && errno == EINTR);
1174
1175 if (ret < 0) {
1176 fprintf(stderr, "+++ LOG: write failed (errno=%d)\n", errno);
1177 ret = 0;
1178 goto done;
1179 }
1180
1181 if (((size_t)ret) < totalLen) {
1182 fprintf(stderr, "+++ LOG: write partial (%d of %d)\n", ret,
1183 (int)totalLen);
1184 goto done;
1185 }
1186
1187done:
1188 if (outBuffer != defaultBuffer) {
1189 free(outBuffer);
1190 }
1191
1192 return ret;
1193}