blob: 2c4e619e48201108755e1d0f779e5f5aa8452455 [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>
William Roberts8a5b9ca2016-04-08 12:13:17 -070024#include <pwd.h>
Pierre Zurekead88fc2010-10-17 22:39:37 +020025#include <stdbool.h>
Mark Salyzyna04464a2014-04-30 08:50:53 -070026#include <stdint.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
Jeff Brown44193d92015-04-28 12:47:02 -070030#include <inttypes.h>
Pierre Zurekead88fc2010-10-17 22:39:37 +020031#include <sys/param.h>
William Roberts8a5b9ca2016-04-08 12:13:17 -070032#include <sys/types.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070033
Mark Salyzyn6584d0a2016-09-28 13:26:55 -070034#include <android/log.h>
Mark Salyzyn4cbed022015-08-31 15:53:41 -070035#include <cutils/list.h>
Colin Cross9227bd32013-07-23 16:59:20 -070036#include <log/logprint.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070037
Mark Salyzyn018a96d2016-03-01 13:45:42 -080038#include "log_portability.h"
Mark Salyzynbe1d3c22016-03-10 08:25:33 -080039
Mark Salyzyn4cbed022015-08-31 15:53:41 -070040#define MS_PER_NSEC 1000000
41#define US_PER_NSEC 1000
42
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070043typedef struct FilterInfo_t {
44 char *mTag;
45 android_LogPriority mPri;
46 struct FilterInfo_t *p_next;
47} FilterInfo;
48
49struct AndroidLogFormat_t {
50 android_LogPriority global_pri;
51 FilterInfo *filters;
52 AndroidLogPrintFormat format;
Pierre Zurekead88fc2010-10-17 22:39:37 +020053 bool colored_output;
Mark Salyzyne1f20042015-05-06 08:40:40 -070054 bool usec_time_output;
Mark Salyzynb932b2f2015-05-15 09:01:58 -070055 bool printable_output;
Mark Salyzynf28f6a92015-08-31 08:01:33 -070056 bool year_output;
57 bool zone_output;
Mark Salyzyn4cbed022015-08-31 15:53:41 -070058 bool epoch_output;
59 bool monotonic_output;
Mark Salyzyn90e7af32015-12-07 16:52:42 -080060 bool uid_output;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070061};
62
Pierre Zurekead88fc2010-10-17 22:39:37 +020063/*
64 * gnome-terminal color tags
65 * See http://misc.flogisoft.com/bash/tip_colors_and_formatting
66 * for ideas on how to set the forground color of the text for xterm.
67 * The color manipulation character stream is defined as:
68 * ESC [ 3 8 ; 5 ; <color#> m
69 */
70#define ANDROID_COLOR_BLUE 75
71#define ANDROID_COLOR_DEFAULT 231
72#define ANDROID_COLOR_GREEN 40
73#define ANDROID_COLOR_ORANGE 166
74#define ANDROID_COLOR_RED 196
75#define ANDROID_COLOR_YELLOW 226
76
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070077static FilterInfo * filterinfo_new(const char * tag, android_LogPriority pri)
78{
79 FilterInfo *p_ret;
80
81 p_ret = (FilterInfo *)calloc(1, sizeof(FilterInfo));
82 p_ret->mTag = strdup(tag);
83 p_ret->mPri = pri;
84
85 return p_ret;
86}
87
Mark Salyzyna04464a2014-04-30 08:50:53 -070088/* balance to above, filterinfo_free left unimplemented */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070089
90/*
91 * Note: also accepts 0-9 priorities
92 * returns ANDROID_LOG_UNKNOWN if the character is unrecognized
93 */
94static android_LogPriority filterCharToPri (char c)
95{
96 android_LogPriority pri;
97
98 c = tolower(c);
99
100 if (c >= '0' && c <= '9') {
101 if (c >= ('0'+ANDROID_LOG_SILENT)) {
102 pri = ANDROID_LOG_VERBOSE;
103 } else {
104 pri = (android_LogPriority)(c - '0');
105 }
106 } else if (c == 'v') {
107 pri = ANDROID_LOG_VERBOSE;
108 } else if (c == 'd') {
109 pri = ANDROID_LOG_DEBUG;
110 } else if (c == 'i') {
111 pri = ANDROID_LOG_INFO;
112 } else if (c == 'w') {
113 pri = ANDROID_LOG_WARN;
114 } else if (c == 'e') {
115 pri = ANDROID_LOG_ERROR;
116 } else if (c == 'f') {
117 pri = ANDROID_LOG_FATAL;
118 } else if (c == 's') {
119 pri = ANDROID_LOG_SILENT;
120 } else if (c == '*') {
121 pri = ANDROID_LOG_DEFAULT;
122 } else {
123 pri = ANDROID_LOG_UNKNOWN;
124 }
125
126 return pri;
127}
128
129static char filterPriToChar (android_LogPriority pri)
130{
131 switch (pri) {
132 case ANDROID_LOG_VERBOSE: return 'V';
133 case ANDROID_LOG_DEBUG: return 'D';
134 case ANDROID_LOG_INFO: return 'I';
135 case ANDROID_LOG_WARN: return 'W';
136 case ANDROID_LOG_ERROR: return 'E';
137 case ANDROID_LOG_FATAL: return 'F';
138 case ANDROID_LOG_SILENT: return 'S';
139
140 case ANDROID_LOG_DEFAULT:
141 case ANDROID_LOG_UNKNOWN:
142 default: return '?';
143 }
144}
145
Pierre Zurekead88fc2010-10-17 22:39:37 +0200146static int colorFromPri (android_LogPriority pri)
147{
148 switch (pri) {
149 case ANDROID_LOG_VERBOSE: return ANDROID_COLOR_DEFAULT;
150 case ANDROID_LOG_DEBUG: return ANDROID_COLOR_BLUE;
151 case ANDROID_LOG_INFO: return ANDROID_COLOR_GREEN;
152 case ANDROID_LOG_WARN: return ANDROID_COLOR_ORANGE;
153 case ANDROID_LOG_ERROR: return ANDROID_COLOR_RED;
154 case ANDROID_LOG_FATAL: return ANDROID_COLOR_RED;
155 case ANDROID_LOG_SILENT: return ANDROID_COLOR_DEFAULT;
156
157 case ANDROID_LOG_DEFAULT:
158 case ANDROID_LOG_UNKNOWN:
159 default: return ANDROID_COLOR_DEFAULT;
160 }
161}
162
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700163static android_LogPriority filterPriForTag(
164 AndroidLogFormat *p_format, const char *tag)
165{
166 FilterInfo *p_curFilter;
167
168 for (p_curFilter = p_format->filters
169 ; p_curFilter != NULL
170 ; p_curFilter = p_curFilter->p_next
171 ) {
172 if (0 == strcmp(tag, p_curFilter->mTag)) {
173 if (p_curFilter->mPri == ANDROID_LOG_DEFAULT) {
174 return p_format->global_pri;
175 } else {
176 return p_curFilter->mPri;
177 }
178 }
179 }
180
181 return p_format->global_pri;
182}
183
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700184/**
185 * returns 1 if this log line should be printed based on its priority
186 * and tag, and 0 if it should not
187 */
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800188LIBLOG_ABI_PUBLIC int android_log_shouldPrintLine (
189 AndroidLogFormat *p_format,
190 const char *tag,
191 android_LogPriority pri)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700192{
193 return pri >= filterPriForTag(p_format, tag);
194}
195
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800196LIBLOG_ABI_PUBLIC AndroidLogFormat *android_log_format_new()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700197{
198 AndroidLogFormat *p_ret;
199
200 p_ret = calloc(1, sizeof(AndroidLogFormat));
201
202 p_ret->global_pri = ANDROID_LOG_VERBOSE;
203 p_ret->format = FORMAT_BRIEF;
Pierre Zurekead88fc2010-10-17 22:39:37 +0200204 p_ret->colored_output = false;
Mark Salyzyne1f20042015-05-06 08:40:40 -0700205 p_ret->usec_time_output = false;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700206 p_ret->printable_output = false;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700207 p_ret->year_output = false;
208 p_ret->zone_output = false;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700209 p_ret->epoch_output = false;
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800210 p_ret->monotonic_output = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800211 p_ret->uid_output = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700212
213 return p_ret;
214}
215
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700216static list_declare(convertHead);
217
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800218LIBLOG_ABI_PUBLIC void android_log_format_free(AndroidLogFormat *p_format)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700219{
220 FilterInfo *p_info, *p_info_old;
221
222 p_info = p_format->filters;
223
224 while (p_info != NULL) {
225 p_info_old = p_info;
226 p_info = p_info->p_next;
227
228 free(p_info_old);
229 }
230
231 free(p_format);
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700232
233 /* Free conversion resource, can always be reconstructed */
234 while (!list_empty(&convertHead)) {
235 struct listnode *node = list_head(&convertHead);
236 list_remove(node);
237 free(node);
238 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700239}
240
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800241LIBLOG_ABI_PUBLIC int android_log_setPrintFormat(
242 AndroidLogFormat *p_format,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700243 AndroidLogPrintFormat format)
244{
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700245 switch (format) {
246 case FORMAT_MODIFIER_COLOR:
Pierre Zurekead88fc2010-10-17 22:39:37 +0200247 p_format->colored_output = true;
Mark Salyzyne1f20042015-05-06 08:40:40 -0700248 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700249 case FORMAT_MODIFIER_TIME_USEC:
Mark Salyzyne1f20042015-05-06 08:40:40 -0700250 p_format->usec_time_output = true;
251 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700252 case FORMAT_MODIFIER_PRINTABLE:
253 p_format->printable_output = true;
254 return 0;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700255 case FORMAT_MODIFIER_YEAR:
256 p_format->year_output = true;
257 return 0;
258 case FORMAT_MODIFIER_ZONE:
259 p_format->zone_output = !p_format->zone_output;
260 return 0;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700261 case FORMAT_MODIFIER_EPOCH:
262 p_format->epoch_output = true;
263 return 0;
264 case FORMAT_MODIFIER_MONOTONIC:
265 p_format->monotonic_output = true;
266 return 0;
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800267 case FORMAT_MODIFIER_UID:
268 p_format->uid_output = true;
269 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700270 default:
271 break;
Mark Salyzyne1f20042015-05-06 08:40:40 -0700272 }
273 p_format->format = format;
274 return 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700275}
276
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700277static const char tz[] = "TZ";
278static const char utc[] = "UTC";
279
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700280/**
281 * Returns FORMAT_OFF on invalid string
282 */
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800283LIBLOG_ABI_PUBLIC AndroidLogPrintFormat android_log_formatFromString(
284 const char * formatString)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700285{
286 static AndroidLogPrintFormat format;
287
288 if (strcmp(formatString, "brief") == 0) format = FORMAT_BRIEF;
289 else if (strcmp(formatString, "process") == 0) format = FORMAT_PROCESS;
290 else if (strcmp(formatString, "tag") == 0) format = FORMAT_TAG;
291 else if (strcmp(formatString, "thread") == 0) format = FORMAT_THREAD;
292 else if (strcmp(formatString, "raw") == 0) format = FORMAT_RAW;
293 else if (strcmp(formatString, "time") == 0) format = FORMAT_TIME;
294 else if (strcmp(formatString, "threadtime") == 0) format = FORMAT_THREADTIME;
295 else if (strcmp(formatString, "long") == 0) format = FORMAT_LONG;
Mark Salyzyne1f20042015-05-06 08:40:40 -0700296 else if (strcmp(formatString, "color") == 0) format = FORMAT_MODIFIER_COLOR;
297 else if (strcmp(formatString, "usec") == 0) format = FORMAT_MODIFIER_TIME_USEC;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700298 else if (strcmp(formatString, "printable") == 0) format = FORMAT_MODIFIER_PRINTABLE;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700299 else if (strcmp(formatString, "year") == 0) format = FORMAT_MODIFIER_YEAR;
300 else if (strcmp(formatString, "zone") == 0) format = FORMAT_MODIFIER_ZONE;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700301 else if (strcmp(formatString, "epoch") == 0) format = FORMAT_MODIFIER_EPOCH;
302 else if (strcmp(formatString, "monotonic") == 0) format = FORMAT_MODIFIER_MONOTONIC;
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800303 else if (strcmp(formatString, "uid") == 0) format = FORMAT_MODIFIER_UID;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700304 else {
305 extern char *tzname[2];
306 static const char gmt[] = "GMT";
307 char *cp = getenv(tz);
308 if (cp) {
309 cp = strdup(cp);
310 }
311 setenv(tz, formatString, 1);
312 /*
313 * Run tzset here to determine if the timezone is legitimate. If the
314 * zone is GMT, check if that is what was asked for, if not then
315 * did not match any on the system; report an error to caller.
316 */
317 tzset();
318 if (!tzname[0]
319 || ((!strcmp(tzname[0], utc)
320 || !strcmp(tzname[0], gmt)) /* error? */
321 && strcasecmp(formatString, utc)
322 && strcasecmp(formatString, gmt))) { /* ok */
323 if (cp) {
324 setenv(tz, cp, 1);
325 } else {
326 unsetenv(tz);
327 }
328 tzset();
329 format = FORMAT_OFF;
330 } else {
331 format = FORMAT_MODIFIER_ZONE;
332 }
333 free(cp);
334 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700335
336 return format;
337}
338
339/**
340 * filterExpression: a single filter expression
341 * eg "AT:d"
342 *
343 * returns 0 on success and -1 on invalid expression
344 *
345 * Assumes single threaded execution
346 */
347
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800348LIBLOG_ABI_PUBLIC int android_log_addFilterRule(
349 AndroidLogFormat *p_format,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700350 const char *filterExpression)
351{
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700352 size_t tagNameLength;
353 android_LogPriority pri = ANDROID_LOG_DEFAULT;
354
355 tagNameLength = strcspn(filterExpression, ":");
356
357 if (tagNameLength == 0) {
358 goto error;
359 }
360
361 if(filterExpression[tagNameLength] == ':') {
362 pri = filterCharToPri(filterExpression[tagNameLength+1]);
363
364 if (pri == ANDROID_LOG_UNKNOWN) {
365 goto error;
366 }
367 }
368
369 if(0 == strncmp("*", filterExpression, tagNameLength)) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700370 /*
371 * This filter expression refers to the global filter
372 * The default level for this is DEBUG if the priority
373 * is unspecified
374 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700375 if (pri == ANDROID_LOG_DEFAULT) {
376 pri = ANDROID_LOG_DEBUG;
377 }
378
379 p_format->global_pri = pri;
380 } else {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700381 /*
382 * for filter expressions that don't refer to the global
383 * filter, the default is verbose if the priority is unspecified
384 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700385 if (pri == ANDROID_LOG_DEFAULT) {
386 pri = ANDROID_LOG_VERBOSE;
387 }
388
389 char *tagName;
390
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700391/*
392 * Presently HAVE_STRNDUP is never defined, so the second case is always taken
393 * Darwin doesn't have strnup, everything else does
394 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700395#ifdef HAVE_STRNDUP
396 tagName = strndup(filterExpression, tagNameLength);
397#else
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700398 /* a few extra bytes copied... */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700399 tagName = strdup(filterExpression);
400 tagName[tagNameLength] = '\0';
401#endif /*HAVE_STRNDUP*/
402
403 FilterInfo *p_fi = filterinfo_new(tagName, pri);
404 free(tagName);
405
406 p_fi->p_next = p_format->filters;
407 p_format->filters = p_fi;
408 }
409
410 return 0;
411error:
412 return -1;
413}
414
415
416/**
417 * filterString: a comma/whitespace-separated set of filter expressions
418 *
419 * eg "AT:d *:i"
420 *
421 * returns 0 on success and -1 on invalid expression
422 *
423 * Assumes single threaded execution
424 *
425 */
426
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800427LIBLOG_ABI_PUBLIC int android_log_addFilterString(
428 AndroidLogFormat *p_format,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700429 const char *filterString)
430{
431 char *filterStringCopy = strdup (filterString);
432 char *p_cur = filterStringCopy;
433 char *p_ret;
434 int err;
435
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700436 /* Yes, I'm using strsep */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700437 while (NULL != (p_ret = strsep(&p_cur, " \t,"))) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700438 /* ignore whitespace-only entries */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700439 if(p_ret[0] != '\0') {
440 err = android_log_addFilterRule(p_format, p_ret);
441
442 if (err < 0) {
443 goto error;
444 }
445 }
446 }
447
448 free (filterStringCopy);
449 return 0;
450error:
451 free (filterStringCopy);
452 return -1;
453}
454
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700455/**
456 * Splits a wire-format buffer into an AndroidLogEntry
457 * entry allocated by caller. Pointers will point directly into buf
458 *
459 * Returns 0 on success and -1 on invalid wire format (entry will be
460 * in unspecified state)
461 */
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800462LIBLOG_ABI_PUBLIC int android_log_processLogBuffer(
463 struct logger_entry *buf,
464 AndroidLogEntry *entry)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700465{
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700466 entry->tv_sec = buf->sec;
467 entry->tv_nsec = buf->nsec;
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800468 entry->uid = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700469 entry->pid = buf->pid;
470 entry->tid = buf->tid;
Kenny Root4bf3c022011-09-30 17:10:14 -0700471
472 /*
473 * format: <priority:1><tag:N>\0<message:N>\0
474 *
475 * tag str
Nick Kraleviche1ede152011-10-18 15:23:33 -0700476 * starts at buf->msg+1
Kenny Root4bf3c022011-09-30 17:10:14 -0700477 * msg
Nick Kraleviche1ede152011-10-18 15:23:33 -0700478 * starts at buf->msg+1+len(tag)+1
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700479 *
480 * The message may have been truncated by the kernel log driver.
481 * When that happens, we must null-terminate the message ourselves.
Kenny Root4bf3c022011-09-30 17:10:14 -0700482 */
Nick Kraleviche1ede152011-10-18 15:23:33 -0700483 if (buf->len < 3) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700484 /*
485 * An well-formed entry must consist of at least a priority
486 * and two null characters
487 */
Nick Kraleviche1ede152011-10-18 15:23:33 -0700488 fprintf(stderr, "+++ LOG: entry too small\n");
Kenny Root4bf3c022011-09-30 17:10:14 -0700489 return -1;
490 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700491
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700492 int msgStart = -1;
493 int msgEnd = -1;
494
Nick Kraleviche1ede152011-10-18 15:23:33 -0700495 int i;
Mark Salyzyn40b21552013-12-18 12:59:01 -0800496 char *msg = buf->msg;
497 struct logger_entry_v2 *buf2 = (struct logger_entry_v2 *)buf;
498 if (buf2->hdr_size) {
Mark Salyzyn305374c2016-08-18 14:59:41 -0700499 if ((buf2->hdr_size < sizeof(((struct log_msg *)NULL)->entry_v1)) ||
500 (buf2->hdr_size > sizeof(((struct log_msg *)NULL)->entry))) {
501 fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
502 return -1;
503 }
Mark Salyzyn40b21552013-12-18 12:59:01 -0800504 msg = ((char *)buf2) + buf2->hdr_size;
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800505 if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) {
506 entry->uid = ((struct logger_entry_v4 *)buf)->uid;
507 }
Mark Salyzyn40b21552013-12-18 12:59:01 -0800508 }
Nick Kraleviche1ede152011-10-18 15:23:33 -0700509 for (i = 1; i < buf->len; i++) {
Mark Salyzyn40b21552013-12-18 12:59:01 -0800510 if (msg[i] == '\0') {
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700511 if (msgStart == -1) {
512 msgStart = i + 1;
513 } else {
514 msgEnd = i;
515 break;
516 }
Nick Kraleviche1ede152011-10-18 15:23:33 -0700517 }
518 }
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700519
520 if (msgStart == -1) {
Mark Salyzyn083c5342016-03-21 09:45:34 -0700521 /* +++ LOG: malformed log message, DYB */
522 for (i = 1; i < buf->len; i++) {
523 /* odd characters in tag? */
524 if ((msg[i] <= ' ') || (msg[i] == ':') || (msg[i] >= 0x7f)) {
525 msg[i] = '\0';
526 msgStart = i + 1;
527 break;
528 }
529 }
530 if (msgStart == -1) {
531 msgStart = buf->len - 1; /* All tag, no message, print truncates */
532 }
Nick Kralevich63f4a842011-10-17 10:45:03 -0700533 }
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700534 if (msgEnd == -1) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700535 /* incoming message not null-terminated; force it */
Mark Salyzynd7bcf402015-12-04 09:24:15 -0800536 msgEnd = buf->len - 1; /* may result in msgEnd < msgStart */
Mark Salyzyn40b21552013-12-18 12:59:01 -0800537 msg[msgEnd] = '\0';
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700538 }
539
Mark Salyzyn40b21552013-12-18 12:59:01 -0800540 entry->priority = msg[0];
541 entry->tag = msg + 1;
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700542 entry->tagLen = msgStart - 1;
Mark Salyzyn40b21552013-12-18 12:59:01 -0800543 entry->message = msg + msgStart;
Mark Salyzynd7bcf402015-12-04 09:24:15 -0800544 entry->messageLen = (msgEnd < msgStart) ? 0 : (msgEnd - msgStart);
Nick Kralevich63f4a842011-10-17 10:45:03 -0700545
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700546 return 0;
547}
548
549/*
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000550 * Extract a 4-byte value from a byte stream.
551 */
552static inline uint32_t get4LE(const uint8_t* src)
553{
554 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
555}
556
557/*
558 * Extract an 8-byte value from a byte stream.
559 */
560static inline uint64_t get8LE(const uint8_t* src)
561{
562 uint32_t low, high;
563
564 low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
565 high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24);
Jeff Brown44193d92015-04-28 12:47:02 -0700566 return ((uint64_t) high << 32) | (uint64_t) low;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000567}
568
569
570/*
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700571 * Recursively convert binary log data to printable form.
572 *
573 * This needs to be recursive because you can have lists of lists.
574 *
575 * If we run out of room, we stop processing immediately. It's important
576 * for us to check for space on every output element to avoid producing
577 * garbled output.
578 *
579 * Returns 0 on success, 1 on buffer full, -1 on failure.
580 */
581static int android_log_printBinaryEvent(const unsigned char** pEventData,
582 size_t* pEventDataLen, char** pOutBuf, size_t* pOutBufLen)
583{
584 const unsigned char* eventData = *pEventData;
585 size_t eventDataLen = *pEventDataLen;
586 char* outBuf = *pOutBuf;
587 size_t outBufLen = *pOutBufLen;
588 unsigned char type;
589 size_t outCount;
590 int result = 0;
591
592 if (eventDataLen < 1)
593 return -1;
594 type = *eventData++;
595 eventDataLen--;
596
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700597 switch (type) {
598 case EVENT_TYPE_INT:
599 /* 32-bit signed int */
600 {
601 int ival;
602
603 if (eventDataLen < 4)
604 return -1;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000605 ival = get4LE(eventData);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700606 eventData += 4;
607 eventDataLen -= 4;
608
609 outCount = snprintf(outBuf, outBufLen, "%d", ival);
610 if (outCount < outBufLen) {
611 outBuf += outCount;
612 outBufLen -= outCount;
613 } else {
614 /* halt output */
615 goto no_room;
616 }
617 }
618 break;
619 case EVENT_TYPE_LONG:
620 /* 64-bit signed long */
621 {
Jeff Brown44193d92015-04-28 12:47:02 -0700622 uint64_t lval;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700623
624 if (eventDataLen < 8)
625 return -1;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000626 lval = get8LE(eventData);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700627 eventData += 8;
628 eventDataLen -= 8;
629
Jeff Brown44193d92015-04-28 12:47:02 -0700630 outCount = snprintf(outBuf, outBufLen, "%" PRId64, lval);
631 if (outCount < outBufLen) {
632 outBuf += outCount;
633 outBufLen -= outCount;
634 } else {
635 /* halt output */
636 goto no_room;
637 }
638 }
639 break;
640 case EVENT_TYPE_FLOAT:
641 /* float */
642 {
643 uint32_t ival;
644 float fval;
645
646 if (eventDataLen < 4)
647 return -1;
648 ival = get4LE(eventData);
649 fval = *(float*)&ival;
650 eventData += 4;
651 eventDataLen -= 4;
652
653 outCount = snprintf(outBuf, outBufLen, "%f", fval);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700654 if (outCount < outBufLen) {
655 outBuf += outCount;
656 outBufLen -= outCount;
657 } else {
658 /* halt output */
659 goto no_room;
660 }
661 }
662 break;
663 case EVENT_TYPE_STRING:
664 /* UTF-8 chars, not NULL-terminated */
665 {
666 unsigned int strLen;
667
668 if (eventDataLen < 4)
669 return -1;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000670 strLen = get4LE(eventData);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700671 eventData += 4;
672 eventDataLen -= 4;
673
674 if (eventDataLen < strLen)
675 return -1;
676
677 if (strLen < outBufLen) {
678 memcpy(outBuf, eventData, strLen);
679 outBuf += strLen;
680 outBufLen -= strLen;
681 } else if (outBufLen > 0) {
682 /* copy what we can */
683 memcpy(outBuf, eventData, outBufLen);
684 outBuf += outBufLen;
685 outBufLen -= outBufLen;
686 goto no_room;
687 }
688 eventData += strLen;
689 eventDataLen -= strLen;
690 break;
691 }
692 case EVENT_TYPE_LIST:
693 /* N items, all different types */
694 {
695 unsigned char count;
696 int i;
697
698 if (eventDataLen < 1)
699 return -1;
700
701 count = *eventData++;
702 eventDataLen--;
703
704 if (outBufLen > 0) {
705 *outBuf++ = '[';
706 outBufLen--;
707 } else {
708 goto no_room;
709 }
710
711 for (i = 0; i < count; i++) {
712 result = android_log_printBinaryEvent(&eventData, &eventDataLen,
713 &outBuf, &outBufLen);
714 if (result != 0)
715 goto bail;
716
717 if (i < count-1) {
718 if (outBufLen > 0) {
719 *outBuf++ = ',';
720 outBufLen--;
721 } else {
722 goto no_room;
723 }
724 }
725 }
726
727 if (outBufLen > 0) {
728 *outBuf++ = ']';
729 outBufLen--;
730 } else {
731 goto no_room;
732 }
733 }
734 break;
735 default:
736 fprintf(stderr, "Unknown binary event type %d\n", type);
737 return -1;
738 }
739
740bail:
741 *pEventData = eventData;
742 *pEventDataLen = eventDataLen;
743 *pOutBuf = outBuf;
744 *pOutBufLen = outBufLen;
745 return result;
746
747no_room:
748 result = 1;
749 goto bail;
750}
751
752/**
753 * Convert a binary log entry to ASCII form.
754 *
755 * For convenience we mimic the processLogBuffer API. There is no
756 * pre-defined output length for the binary data, since we're free to format
757 * it however we choose, which means we can't really use a fixed-size buffer
758 * here.
759 */
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800760LIBLOG_ABI_PUBLIC int android_log_processBinaryLogBuffer(
761 struct logger_entry *buf,
762 AndroidLogEntry *entry,
763 const EventTagMap *map,
764 char *messageBuf, int messageBufLen)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700765{
766 size_t inCount;
767 unsigned int tagIndex;
768 const unsigned char* eventData;
769
770 entry->tv_sec = buf->sec;
771 entry->tv_nsec = buf->nsec;
772 entry->priority = ANDROID_LOG_INFO;
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800773 entry->uid = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700774 entry->pid = buf->pid;
775 entry->tid = buf->tid;
776
777 /*
Mark Salyzyn7bc80232015-12-11 12:32:53 -0800778 * Pull the tag out, fill in some additional details based on incoming
779 * buffer version (v3 adds lid, v4 adds uid).
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700780 */
781 eventData = (const unsigned char*) buf->msg;
Mark Salyzyn40b21552013-12-18 12:59:01 -0800782 struct logger_entry_v2 *buf2 = (struct logger_entry_v2 *)buf;
783 if (buf2->hdr_size) {
Mark Salyzyn305374c2016-08-18 14:59:41 -0700784 if ((buf2->hdr_size < sizeof(((struct log_msg *)NULL)->entry_v1)) ||
785 (buf2->hdr_size > sizeof(((struct log_msg *)NULL)->entry))) {
786 fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
787 return -1;
788 }
Mark Salyzyn40b21552013-12-18 12:59:01 -0800789 eventData = ((unsigned char *)buf2) + buf2->hdr_size;
Mark Salyzyn7bc80232015-12-11 12:32:53 -0800790 if ((buf2->hdr_size >= sizeof(struct logger_entry_v3)) &&
791 (((struct logger_entry_v3 *)buf)->lid == LOG_ID_SECURITY)) {
792 entry->priority = ANDROID_LOG_WARN;
793 }
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800794 if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) {
795 entry->uid = ((struct logger_entry_v4 *)buf)->uid;
796 }
Mark Salyzyn40b21552013-12-18 12:59:01 -0800797 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700798 inCount = buf->len;
799 if (inCount < 4)
800 return -1;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000801 tagIndex = get4LE(eventData);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700802 eventData += 4;
803 inCount -= 4;
804
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700805 entry->tagLen = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700806 if (map != NULL) {
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700807 entry->tag = android_lookupEventTag_len(map, &entry->tagLen, tagIndex);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700808 } else {
809 entry->tag = NULL;
810 }
811
812 /*
813 * If we don't have a map, or didn't find the tag number in the map,
814 * stuff a generated tag value into the start of the output buffer and
815 * shift the buffer pointers down.
816 */
817 if (entry->tag == NULL) {
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700818 size_t tagLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700819
820 tagLen = snprintf(messageBuf, messageBufLen, "[%d]", tagIndex);
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700821 if (tagLen >= (size_t)messageBufLen) {
822 tagLen = messageBufLen - 1;
823 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700824 entry->tag = messageBuf;
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700825 entry->tagLen = tagLen;
826 messageBuf += tagLen + 1;
827 messageBufLen -= tagLen + 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700828 }
829
830 /*
831 * Format the event log data into the buffer.
832 */
833 char* outBuf = messageBuf;
834 size_t outRemaining = messageBufLen-1; /* leave one for nul byte */
835 int result;
836 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf,
837 &outRemaining);
838 if (result < 0) {
839 fprintf(stderr, "Binary log entry conversion failed\n");
840 return -1;
841 } else if (result == 1) {
842 if (outBuf > messageBuf) {
843 /* leave an indicator */
844 *(outBuf-1) = '!';
845 } else {
846 /* no room to output anything at all */
847 *outBuf++ = '!';
848 outRemaining--;
849 }
850 /* pretend we ate all the data */
851 inCount = 0;
852 }
853
854 /* eat the silly terminating '\n' */
855 if (inCount == 1 && *eventData == '\n') {
856 eventData++;
857 inCount--;
858 }
859
860 if (inCount != 0) {
861 fprintf(stderr,
Andrew Hsiehd2c8f522012-02-27 16:48:18 -0800862 "Warning: leftover binary log data (%zu bytes)\n", inCount);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700863 }
864
865 /*
866 * Terminate the buffer. The NUL byte does not count as part of
867 * entry->messageLen.
868 */
869 *outBuf = '\0';
870 entry->messageLen = outBuf - messageBuf;
871 assert(entry->messageLen == (messageBufLen-1) - outRemaining);
872
873 entry->message = messageBuf;
874
875 return 0;
876}
877
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700878/*
879 * One utf8 character at a time
880 *
881 * Returns the length of the utf8 character in the buffer,
882 * or -1 if illegal or truncated
883 *
884 * Open coded from libutils/Unicode.cpp, borrowed from utf8_length(),
885 * can not remove from here because of library circular dependencies.
886 * Expect one-day utf8_character_length with the same signature could
887 * _also_ be part of libutils/Unicode.cpp if its usefullness needs to
888 * propagate globally.
889 */
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800890LIBLOG_WEAK ssize_t utf8_character_length(const char *src, size_t len)
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700891{
892 const char *cur = src;
893 const char first_char = *cur++;
894 static const uint32_t kUnicodeMaxCodepoint = 0x0010FFFF;
895 int32_t mask, to_ignore_mask;
896 size_t num_to_read;
897 uint32_t utf32;
898
899 if ((first_char & 0x80) == 0) { /* ASCII */
Mark Salyzynfaa92e92015-09-08 07:57:27 -0700900 return first_char ? 1 : -1;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700901 }
902
903 /*
904 * (UTF-8's character must not be like 10xxxxxx,
905 * but 110xxxxx, 1110xxxx, ... or 1111110x)
906 */
907 if ((first_char & 0x40) == 0) {
908 return -1;
909 }
910
911 for (utf32 = 1, num_to_read = 1, mask = 0x40, to_ignore_mask = 0x80;
912 num_to_read < 5 && (first_char & mask);
913 num_to_read++, to_ignore_mask |= mask, mask >>= 1) {
914 if (num_to_read > len) {
915 return -1;
916 }
917 if ((*cur & 0xC0) != 0x80) { /* can not be 10xxxxxx? */
918 return -1;
919 }
920 utf32 = (utf32 << 6) + (*cur++ & 0b00111111);
921 }
922 /* "first_char" must be (110xxxxx - 11110xxx) */
923 if (num_to_read >= 5) {
924 return -1;
925 }
926 to_ignore_mask |= mask;
927 utf32 |= ((~to_ignore_mask) & first_char) << (6 * (num_to_read - 1));
928 if (utf32 > kUnicodeMaxCodepoint) {
929 return -1;
930 }
931 return num_to_read;
932}
933
934/*
935 * Convert to printable from message to p buffer, return string length. If p is
936 * NULL, do not copy, but still return the expected string length.
937 */
938static size_t convertPrintable(char *p, const char *message, size_t messageLen)
939{
940 char *begin = p;
941 bool print = p != NULL;
942
943 while (messageLen) {
944 char buf[6];
945 ssize_t len = sizeof(buf) - 1;
946 if ((size_t)len > messageLen) {
947 len = messageLen;
948 }
949 len = utf8_character_length(message, len);
950
951 if (len < 0) {
952 snprintf(buf, sizeof(buf),
953 ((messageLen > 1) && isdigit(message[1]))
954 ? "\\%03o"
955 : "\\%o",
956 *message & 0377);
957 len = 1;
958 } else {
959 buf[0] = '\0';
960 if (len == 1) {
961 if (*message == '\a') {
962 strcpy(buf, "\\a");
963 } else if (*message == '\b') {
964 strcpy(buf, "\\b");
965 } else if (*message == '\t') {
Mark Salyzyn65d5ca22015-11-18 09:58:00 -0800966 strcpy(buf, "\t"); // Do not escape tabs
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700967 } else if (*message == '\v') {
968 strcpy(buf, "\\v");
969 } else if (*message == '\f') {
970 strcpy(buf, "\\f");
971 } else if (*message == '\r') {
972 strcpy(buf, "\\r");
973 } else if (*message == '\\') {
974 strcpy(buf, "\\\\");
975 } else if ((*message < ' ') || (*message & 0x80)) {
976 snprintf(buf, sizeof(buf), "\\%o", *message & 0377);
977 }
978 }
979 if (!buf[0]) {
980 strncpy(buf, message, len);
981 buf[len] = '\0';
982 }
983 }
984 if (print) {
985 strcpy(p, buf);
986 }
987 p += strlen(buf);
988 message += len;
989 messageLen -= len;
990 }
991 return p - begin;
992}
993
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800994static char *readSeconds(char *e, struct timespec *t)
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700995{
996 unsigned long multiplier;
997 char *p;
998 t->tv_sec = strtoul(e, &p, 10);
999 if (*p != '.') {
1000 return NULL;
1001 }
1002 t->tv_nsec = 0;
1003 multiplier = NS_PER_SEC;
1004 while (isdigit(*++p) && (multiplier /= 10)) {
1005 t->tv_nsec += (*p - '0') * multiplier;
1006 }
1007 return p;
1008}
1009
1010static struct timespec *sumTimespec(struct timespec *left,
1011 struct timespec *right)
1012{
1013 left->tv_nsec += right->tv_nsec;
1014 left->tv_sec += right->tv_sec;
1015 if (left->tv_nsec >= (long)NS_PER_SEC) {
1016 left->tv_nsec -= NS_PER_SEC;
1017 left->tv_sec += 1;
1018 }
1019 return left;
1020}
1021
1022static struct timespec *subTimespec(struct timespec *result,
1023 struct timespec *left,
1024 struct timespec *right)
1025{
1026 result->tv_nsec = left->tv_nsec - right->tv_nsec;
1027 result->tv_sec = left->tv_sec - right->tv_sec;
1028 if (result->tv_nsec < 0) {
1029 result->tv_nsec += NS_PER_SEC;
1030 result->tv_sec -= 1;
1031 }
1032 return result;
1033}
1034
1035static long long nsecTimespec(struct timespec *now)
1036{
1037 return (long long)now->tv_sec * NS_PER_SEC + now->tv_nsec;
1038}
1039
1040static void convertMonotonic(struct timespec *result,
1041 const AndroidLogEntry *entry)
1042{
1043 struct listnode *node;
1044 struct conversionList {
1045 struct listnode node; /* first */
1046 struct timespec time;
1047 struct timespec convert;
1048 } *list, *next;
1049 struct timespec time, convert;
1050
1051 /* If we do not have a conversion list, build one up */
1052 if (list_empty(&convertHead)) {
1053 bool suspended_pending = false;
1054 struct timespec suspended_monotonic = { 0, 0 };
1055 struct timespec suspended_diff = { 0, 0 };
1056
1057 /*
1058 * Read dmesg for _some_ synchronization markers and insert
1059 * Anything in the Android Logger before the dmesg logging span will
1060 * be highly suspect regarding the monotonic time calculations.
1061 */
Mark Salyzyn78786da2016-04-28 16:06:24 -07001062 FILE *p = popen("/system/bin/dmesg", "re");
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001063 if (p) {
1064 char *line = NULL;
1065 size_t len = 0;
1066 while (getline(&line, &len, p) > 0) {
1067 static const char suspend[] = "PM: suspend entry ";
1068 static const char resume[] = "PM: suspend exit ";
1069 static const char healthd[] = "healthd";
1070 static const char battery[] = ": battery ";
1071 static const char suspended[] = "Suspended for ";
1072 struct timespec monotonic;
1073 struct tm tm;
1074 char *cp, *e = line;
1075 bool add_entry = true;
1076
1077 if (*e == '<') {
1078 while (*e && (*e != '>')) {
1079 ++e;
1080 }
1081 if (*e != '>') {
1082 continue;
1083 }
1084 }
1085 if (*e != '[') {
1086 continue;
1087 }
1088 while (*++e == ' ') {
1089 ;
1090 }
1091 e = readSeconds(e, &monotonic);
1092 if (!e || (*e != ']')) {
1093 continue;
1094 }
1095
1096 if ((e = strstr(e, suspend))) {
1097 e += sizeof(suspend) - 1;
1098 } else if ((e = strstr(line, resume))) {
1099 e += sizeof(resume) - 1;
1100 } else if (((e = strstr(line, healthd)))
1101 && ((e = strstr(e + sizeof(healthd) - 1, battery)))) {
1102 /* NB: healthd is roughly 150us late, worth the price to
1103 * deal with ntp-induced or hardware clock drift. */
1104 e += sizeof(battery) - 1;
1105 } else if ((e = strstr(line, suspended))) {
1106 e += sizeof(suspended) - 1;
1107 e = readSeconds(e, &time);
1108 if (!e) {
1109 continue;
1110 }
1111 add_entry = false;
1112 suspended_pending = true;
1113 suspended_monotonic = monotonic;
1114 suspended_diff = time;
1115 } else {
1116 continue;
1117 }
1118 if (add_entry) {
1119 /* look for "????-??-?? ??:??:??.????????? UTC" */
1120 cp = strstr(e, " UTC");
1121 if (!cp || ((cp - e) < 29) || (cp[-10] != '.')) {
1122 continue;
1123 }
1124 e = cp - 29;
1125 cp = readSeconds(cp - 10, &time);
1126 if (!cp) {
1127 continue;
1128 }
1129 cp = strptime(e, "%Y-%m-%d %H:%M:%S.", &tm);
1130 if (!cp) {
1131 continue;
1132 }
1133 cp = getenv(tz);
1134 if (cp) {
1135 cp = strdup(cp);
1136 }
1137 setenv(tz, utc, 1);
1138 time.tv_sec = mktime(&tm);
1139 if (cp) {
1140 setenv(tz, cp, 1);
1141 free(cp);
1142 } else {
1143 unsetenv(tz);
1144 }
1145 list = calloc(1, sizeof(struct conversionList));
1146 list_init(&list->node);
1147 list->time = time;
1148 subTimespec(&list->convert, &time, &monotonic);
1149 list_add_tail(&convertHead, &list->node);
1150 }
1151 if (suspended_pending && !list_empty(&convertHead)) {
1152 list = node_to_item(list_tail(&convertHead),
1153 struct conversionList, node);
1154 if (subTimespec(&time,
1155 subTimespec(&time,
1156 &list->time,
1157 &list->convert),
1158 &suspended_monotonic)->tv_sec > 0) {
1159 /* resume, what is convert factor before? */
1160 subTimespec(&convert, &list->convert, &suspended_diff);
1161 } else {
1162 /* suspend */
1163 convert = list->convert;
1164 }
1165 time = suspended_monotonic;
1166 sumTimespec(&time, &convert);
1167 /* breakpoint just before sleep */
1168 list = calloc(1, sizeof(struct conversionList));
1169 list_init(&list->node);
1170 list->time = time;
1171 list->convert = convert;
1172 list_add_tail(&convertHead, &list->node);
1173 /* breakpoint just after sleep */
1174 list = calloc(1, sizeof(struct conversionList));
1175 list_init(&list->node);
1176 list->time = time;
1177 sumTimespec(&list->time, &suspended_diff);
1178 list->convert = convert;
1179 sumTimespec(&list->convert, &suspended_diff);
1180 list_add_tail(&convertHead, &list->node);
1181 suspended_pending = false;
1182 }
1183 }
1184 pclose(p);
1185 }
1186 /* last entry is our current time conversion */
1187 list = calloc(1, sizeof(struct conversionList));
1188 list_init(&list->node);
1189 clock_gettime(CLOCK_REALTIME, &list->time);
1190 clock_gettime(CLOCK_MONOTONIC, &convert);
1191 clock_gettime(CLOCK_MONOTONIC, &time);
1192 /* Correct for instant clock_gettime latency (syscall or ~30ns) */
1193 subTimespec(&time, &convert, subTimespec(&time, &time, &convert));
1194 /* Calculate conversion factor */
1195 subTimespec(&list->convert, &list->time, &time);
1196 list_add_tail(&convertHead, &list->node);
1197 if (suspended_pending) {
1198 /* manufacture a suspend @ point before */
1199 subTimespec(&convert, &list->convert, &suspended_diff);
1200 time = suspended_monotonic;
1201 sumTimespec(&time, &convert);
1202 /* breakpoint just after sleep */
1203 list = calloc(1, sizeof(struct conversionList));
1204 list_init(&list->node);
1205 list->time = time;
1206 sumTimespec(&list->time, &suspended_diff);
1207 list->convert = convert;
1208 sumTimespec(&list->convert, &suspended_diff);
1209 list_add_head(&convertHead, &list->node);
1210 /* breakpoint just before sleep */
1211 list = calloc(1, sizeof(struct conversionList));
1212 list_init(&list->node);
1213 list->time = time;
1214 list->convert = convert;
1215 list_add_head(&convertHead, &list->node);
1216 }
1217 }
1218
1219 /* Find the breakpoint in the conversion list */
1220 list = node_to_item(list_head(&convertHead), struct conversionList, node);
1221 next = NULL;
1222 list_for_each(node, &convertHead) {
1223 next = node_to_item(node, struct conversionList, node);
1224 if (entry->tv_sec < next->time.tv_sec) {
1225 break;
1226 } else if (entry->tv_sec == next->time.tv_sec) {
1227 if (entry->tv_nsec < next->time.tv_nsec) {
1228 break;
1229 }
1230 }
1231 list = next;
1232 }
1233
1234 /* blend time from one breakpoint to the next */
1235 convert = list->convert;
1236 if (next) {
1237 unsigned long long total, run;
1238
1239 total = nsecTimespec(subTimespec(&time, &next->time, &list->time));
1240 time.tv_sec = entry->tv_sec;
1241 time.tv_nsec = entry->tv_nsec;
1242 run = nsecTimespec(subTimespec(&time, &time, &list->time));
1243 if (run < total) {
1244 long long crun;
1245
1246 float f = nsecTimespec(subTimespec(&time, &next->convert, &convert));
1247 f *= run;
1248 f /= total;
1249 crun = f;
1250 convert.tv_sec += crun / (long long)NS_PER_SEC;
1251 if (crun < 0) {
1252 convert.tv_nsec -= (-crun) % NS_PER_SEC;
1253 if (convert.tv_nsec < 0) {
1254 convert.tv_nsec += NS_PER_SEC;
1255 convert.tv_sec -= 1;
1256 }
1257 } else {
1258 convert.tv_nsec += crun % NS_PER_SEC;
1259 if (convert.tv_nsec >= (long)NS_PER_SEC) {
1260 convert.tv_nsec -= NS_PER_SEC;
1261 convert.tv_sec += 1;
1262 }
1263 }
1264 }
1265 }
1266
1267 /* Apply the correction factor */
1268 result->tv_sec = entry->tv_sec;
1269 result->tv_nsec = entry->tv_nsec;
1270 subTimespec(result, result, &convert);
1271}
1272
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001273/**
1274 * Formats a log message into a buffer
1275 *
1276 * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer
1277 * If return value != defaultBuffer, caller must call free()
1278 * Returns NULL on malloc error
1279 */
1280
Mark Salyzynbe1d3c22016-03-10 08:25:33 -08001281LIBLOG_ABI_PUBLIC char *android_log_formatLogLine (
1282 AndroidLogFormat *p_format,
1283 char *defaultBuffer,
1284 size_t defaultBufferSize,
1285 const AndroidLogEntry *entry,
1286 size_t *p_outLength)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001287{
Yabin Cui8a985352014-11-13 10:02:08 -08001288#if !defined(_WIN32)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001289 struct tm tmBuf;
1290#endif
1291 struct tm* ptm;
Mark Salyzynf28f6a92015-08-31 08:01:33 -07001292 char timeBuf[64]; /* good margin, 23+nul for msec, 26+nul for usec */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001293 char prefixBuf[128], suffixBuf[128];
1294 char priChar;
1295 int prefixSuffixIsHeaderFooter = 0;
Mark Salyzyn90e7af32015-12-07 16:52:42 -08001296 char *ret;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001297 time_t now;
1298 unsigned long nsec;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001299
1300 priChar = filterPriToChar(entry->priority);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001301 size_t prefixLen = 0, suffixLen = 0;
1302 size_t len;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001303
1304 /*
1305 * Get the current date/time in pretty form
1306 *
1307 * It's often useful when examining a log with "less" to jump to
1308 * a specific point in the file by searching for the date/time stamp.
1309 * For this reason it's very annoying to have regexp meta characters
1310 * in the time stamp. Don't use forward slashes, parenthesis,
1311 * brackets, asterisks, or other special chars here.
Mark Salyzynf28f6a92015-08-31 08:01:33 -07001312 *
1313 * The caller may have affected the timezone environment, this is
1314 * expected to be sensitive to that.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001315 */
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001316 now = entry->tv_sec;
1317 nsec = entry->tv_nsec;
1318 if (p_format->monotonic_output) {
Mark Salyzynb6bee332015-09-08 08:56:32 -07001319 // prevent convertMonotonic from being called if logd is monotonic
Mark Salyzynba7a9a02015-12-01 15:57:25 -08001320 if (android_log_clockid() != CLOCK_MONOTONIC) {
Mark Salyzynb6bee332015-09-08 08:56:32 -07001321 struct timespec time;
1322 convertMonotonic(&time, entry);
1323 now = time.tv_sec;
1324 nsec = time.tv_nsec;
1325 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001326 }
1327 if (now < 0) {
1328 nsec = NS_PER_SEC - nsec;
1329 }
1330 if (p_format->epoch_output || p_format->monotonic_output) {
1331 ptm = NULL;
1332 snprintf(timeBuf, sizeof(timeBuf),
1333 p_format->monotonic_output ? "%6lld" : "%19lld",
1334 (long long)now);
1335 } else {
Yabin Cui8a985352014-11-13 10:02:08 -08001336#if !defined(_WIN32)
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001337 ptm = localtime_r(&now, &tmBuf);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001338#else
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001339 ptm = localtime(&now);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001340#endif
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001341 strftime(timeBuf, sizeof(timeBuf),
1342 &"%Y-%m-%d %H:%M:%S"[p_format->year_output ? 0 : 3],
1343 ptm);
1344 }
Mark Salyzyne1f20042015-05-06 08:40:40 -07001345 len = strlen(timeBuf);
1346 if (p_format->usec_time_output) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -07001347 len += snprintf(timeBuf + len, sizeof(timeBuf) - len,
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001348 ".%06ld", nsec / US_PER_NSEC);
Mark Salyzyne1f20042015-05-06 08:40:40 -07001349 } else {
Mark Salyzynf28f6a92015-08-31 08:01:33 -07001350 len += snprintf(timeBuf + len, sizeof(timeBuf) - len,
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001351 ".%03ld", nsec / MS_PER_NSEC);
Mark Salyzynf28f6a92015-08-31 08:01:33 -07001352 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001353 if (p_format->zone_output && ptm) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -07001354 strftime(timeBuf + len, sizeof(timeBuf) - len, " %z", ptm);
Mark Salyzyne1f20042015-05-06 08:40:40 -07001355 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001356
1357 /*
1358 * Construct a buffer containing the log header and log message.
1359 */
Pierre Zurekead88fc2010-10-17 22:39:37 +02001360 if (p_format->colored_output) {
1361 prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm",
1362 colorFromPri(entry->priority));
1363 prefixLen = MIN(prefixLen, sizeof(prefixBuf));
1364 suffixLen = snprintf(suffixBuf, sizeof(suffixBuf), "\x1B[0m");
1365 suffixLen = MIN(suffixLen, sizeof(suffixBuf));
1366 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001367
Mark Salyzyn90e7af32015-12-07 16:52:42 -08001368 char uid[16];
1369 uid[0] = '\0';
1370 if (p_format->uid_output) {
1371 if (entry->uid >= 0) {
Mark Salyzyn2aa510e2015-12-08 09:15:06 -08001372
William Roberts8a5b9ca2016-04-08 12:13:17 -07001373 /*
1374 * This code is Android specific, bionic guarantees that
1375 * calls to non-reentrant getpwuid() are thread safe.
1376 */
1377#ifndef __BIONIC__
1378#warning "This code assumes that getpwuid is thread safe, only true with Bionic!"
1379#endif
1380 struct passwd* pwd = getpwuid(entry->uid);
1381 if (pwd && (strlen(pwd->pw_name) <= 5)) {
1382 snprintf(uid, sizeof(uid), "%5s:", pwd->pw_name);
Mark Salyzyn2aa510e2015-12-08 09:15:06 -08001383 } else {
1384 // Not worth parsing package list, names all longer than 5
1385 snprintf(uid, sizeof(uid), "%5d:", entry->uid);
1386 }
Mark Salyzyn90e7af32015-12-07 16:52:42 -08001387 } else {
1388 snprintf(uid, sizeof(uid), " ");
1389 }
1390 }
1391
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001392 switch (p_format->format) {
1393 case FORMAT_TAG:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001394 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001395 "%c/%-8.*s: ", priChar, (int)entry->tagLen, entry->tag);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001396 strcpy(suffixBuf + suffixLen, "\n");
1397 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001398 break;
1399 case FORMAT_PROCESS:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001400 len = snprintf(suffixBuf + suffixLen, sizeof(suffixBuf) - suffixLen,
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001401 " (%.*s)\n", (int)entry->tagLen, entry->tag);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001402 suffixLen += MIN(len, sizeof(suffixBuf) - suffixLen);
1403 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyn90e7af32015-12-07 16:52:42 -08001404 "%c(%s%5d) ", priChar, uid, entry->pid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001405 break;
1406 case FORMAT_THREAD:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001407 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyn90e7af32015-12-07 16:52:42 -08001408 "%c(%s%5d:%5d) ", priChar, uid, entry->pid, entry->tid);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001409 strcpy(suffixBuf + suffixLen, "\n");
1410 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001411 break;
1412 case FORMAT_RAW:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001413 prefixBuf[prefixLen] = 0;
1414 len = 0;
1415 strcpy(suffixBuf + suffixLen, "\n");
1416 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001417 break;
1418 case FORMAT_TIME:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001419 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001420 "%s %c/%-8.*s(%s%5d): ", timeBuf, priChar,
1421 (int)entry->tagLen, entry->tag, uid, entry->pid);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001422 strcpy(suffixBuf + suffixLen, "\n");
1423 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001424 break;
1425 case FORMAT_THREADTIME:
Mark Salyzyn90e7af32015-12-07 16:52:42 -08001426 ret = strchr(uid, ':');
1427 if (ret) {
1428 *ret = ' ';
1429 }
Pierre Zurekead88fc2010-10-17 22:39:37 +02001430 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001431 "%s %s%5d %5d %c %-8.*s: ", timeBuf, uid, entry->pid,
1432 entry->tid, priChar, (int)entry->tagLen, entry->tag);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001433 strcpy(suffixBuf + suffixLen, "\n");
1434 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001435 break;
1436 case FORMAT_LONG:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001437 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001438 "[ %s %s%5d:%5d %c/%-8.*s ]\n",
1439 timeBuf, uid, entry->pid, entry->tid, priChar,
1440 (int)entry->tagLen, entry->tag);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001441 strcpy(suffixBuf + suffixLen, "\n\n");
1442 suffixLen += 2;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001443 prefixSuffixIsHeaderFooter = 1;
1444 break;
1445 case FORMAT_BRIEF:
1446 default:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001447 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001448 "%c/%-8.*s(%s%5d): ", priChar, (int)entry->tagLen, entry->tag,
1449 uid, entry->pid);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001450 strcpy(suffixBuf + suffixLen, "\n");
1451 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001452 break;
1453 }
Pierre Zurekead88fc2010-10-17 22:39:37 +02001454
Keith Prestonb45b5c92010-02-11 15:12:53 -06001455 /* snprintf has a weird return value. It returns what would have been
1456 * written given a large enough buffer. In the case that the prefix is
1457 * longer then our buffer(128), it messes up the calculations below
1458 * possibly causing heap corruption. To avoid this we double check and
1459 * set the length at the maximum (size minus null byte)
1460 */
Mark Salyzyn2f83d672016-03-11 12:06:12 -08001461 prefixLen += len;
1462 if (prefixLen >= sizeof(prefixBuf)) {
1463 prefixLen = sizeof(prefixBuf) - 1;
1464 prefixBuf[sizeof(prefixBuf) - 1] = '\0';
1465 }
1466 if (suffixLen >= sizeof(suffixBuf)) {
1467 suffixLen = sizeof(suffixBuf) - 1;
1468 suffixBuf[sizeof(suffixBuf) - 2] = '\n';
1469 suffixBuf[sizeof(suffixBuf) - 1] = '\0';
1470 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001471
1472 /* the following code is tragically unreadable */
1473
1474 size_t numLines;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001475 char *p;
1476 size_t bufferSize;
1477 const char *pm;
1478
1479 if (prefixSuffixIsHeaderFooter) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001480 /* we're just wrapping message with a header/footer */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001481 numLines = 1;
1482 } else {
1483 pm = entry->message;
1484 numLines = 0;
1485
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001486 /*
1487 * The line-end finding here must match the line-end finding
1488 * in for ( ... numLines...) loop below
1489 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001490 while (pm < (entry->message + entry->messageLen)) {
1491 if (*pm++ == '\n') numLines++;
1492 }
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001493 /* plus one line for anything not newline-terminated at the end */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001494 if (pm > entry->message && *(pm-1) != '\n') numLines++;
1495 }
1496
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001497 /*
1498 * this is an upper bound--newlines in message may be counted
1499 * extraneously
1500 */
1501 bufferSize = (numLines * (prefixLen + suffixLen)) + 1;
1502 if (p_format->printable_output) {
1503 /* Calculate extra length to convert non-printable to printable */
1504 bufferSize += convertPrintable(NULL, entry->message, entry->messageLen);
1505 } else {
1506 bufferSize += entry->messageLen;
1507 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001508
1509 if (defaultBufferSize >= bufferSize) {
1510 ret = defaultBuffer;
1511 } else {
1512 ret = (char *)malloc(bufferSize);
1513
1514 if (ret == NULL) {
1515 return ret;
1516 }
1517 }
1518
1519 ret[0] = '\0'; /* to start strcat off */
1520
1521 p = ret;
1522 pm = entry->message;
1523
1524 if (prefixSuffixIsHeaderFooter) {
1525 strcat(p, prefixBuf);
1526 p += prefixLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001527 if (p_format->printable_output) {
1528 p += convertPrintable(p, entry->message, entry->messageLen);
1529 } else {
1530 strncat(p, entry->message, entry->messageLen);
1531 p += entry->messageLen;
1532 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001533 strcat(p, suffixBuf);
1534 p += suffixLen;
1535 } else {
Mark Salyzyn7cc80132016-01-20 13:52:46 -08001536 do {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001537 const char *lineStart;
1538 size_t lineLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001539 lineStart = pm;
1540
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001541 /* Find the next end-of-line in message */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001542 while (pm < (entry->message + entry->messageLen)
1543 && *pm != '\n') pm++;
1544 lineLen = pm - lineStart;
1545
1546 strcat(p, prefixBuf);
1547 p += prefixLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001548 if (p_format->printable_output) {
1549 p += convertPrintable(p, lineStart, lineLen);
1550 } else {
1551 strncat(p, lineStart, lineLen);
1552 p += lineLen;
1553 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001554 strcat(p, suffixBuf);
1555 p += suffixLen;
1556
1557 if (*pm == '\n') pm++;
Mark Salyzyn7cc80132016-01-20 13:52:46 -08001558 } while (pm < (entry->message + entry->messageLen));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001559 }
1560
1561 if (p_outLength != NULL) {
1562 *p_outLength = p - ret;
1563 }
1564
1565 return ret;
1566}
1567
1568/**
1569 * Either print or do not print log line, based on filter
1570 *
1571 * Returns count bytes written
1572 */
1573
Mark Salyzynbe1d3c22016-03-10 08:25:33 -08001574LIBLOG_ABI_PUBLIC int android_log_printLogLine(
1575 AndroidLogFormat *p_format,
1576 int fd,
1577 const AndroidLogEntry *entry)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001578{
1579 int ret;
1580 char defaultBuffer[512];
1581 char *outBuffer = NULL;
1582 size_t totalLen;
1583
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001584 outBuffer = android_log_formatLogLine(p_format, defaultBuffer,
1585 sizeof(defaultBuffer), entry, &totalLen);
1586
1587 if (!outBuffer)
1588 return -1;
1589
1590 do {
1591 ret = write(fd, outBuffer, totalLen);
1592 } while (ret < 0 && errno == EINTR);
1593
1594 if (ret < 0) {
1595 fprintf(stderr, "+++ LOG: write failed (errno=%d)\n", errno);
1596 ret = 0;
1597 goto done;
1598 }
1599
1600 if (((size_t)ret) < totalLen) {
1601 fprintf(stderr, "+++ LOG: write partial (%d of %d)\n", ret,
1602 (int)totalLen);
1603 goto done;
1604 }
1605
1606done:
1607 if (outBuffer != defaultBuffer) {
1608 free(outBuffer);
1609 }
1610
1611 return ret;
1612}