blob: f9b14bd6eea8d578c6db10a512ba60b0a92ef5da [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>
Mark Salyzyn37c94512016-10-04 08:54:28 -070036#include <log/logger.h>
Colin Cross9227bd32013-07-23 16:59:20 -070037#include <log/logprint.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070038
Mark Salyzyn018a96d2016-03-01 13:45:42 -080039#include "log_portability.h"
Mark Salyzynbe1d3c22016-03-10 08:25:33 -080040
Mark Salyzyn4cbed022015-08-31 15:53:41 -070041#define MS_PER_NSEC 1000000
42#define US_PER_NSEC 1000
43
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070044typedef struct FilterInfo_t {
45 char *mTag;
46 android_LogPriority mPri;
47 struct FilterInfo_t *p_next;
48} FilterInfo;
49
50struct AndroidLogFormat_t {
51 android_LogPriority global_pri;
52 FilterInfo *filters;
53 AndroidLogPrintFormat format;
Pierre Zurekead88fc2010-10-17 22:39:37 +020054 bool colored_output;
Mark Salyzyne1f20042015-05-06 08:40:40 -070055 bool usec_time_output;
Mark Salyzynb932b2f2015-05-15 09:01:58 -070056 bool printable_output;
Mark Salyzynf28f6a92015-08-31 08:01:33 -070057 bool year_output;
58 bool zone_output;
Mark Salyzyn4cbed022015-08-31 15:53:41 -070059 bool epoch_output;
60 bool monotonic_output;
Mark Salyzyn90e7af32015-12-07 16:52:42 -080061 bool uid_output;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070062};
63
Pierre Zurekead88fc2010-10-17 22:39:37 +020064/*
65 * gnome-terminal color tags
66 * See http://misc.flogisoft.com/bash/tip_colors_and_formatting
67 * for ideas on how to set the forground color of the text for xterm.
68 * The color manipulation character stream is defined as:
69 * ESC [ 3 8 ; 5 ; <color#> m
70 */
71#define ANDROID_COLOR_BLUE 75
72#define ANDROID_COLOR_DEFAULT 231
73#define ANDROID_COLOR_GREEN 40
74#define ANDROID_COLOR_ORANGE 166
75#define ANDROID_COLOR_RED 196
76#define ANDROID_COLOR_YELLOW 226
77
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070078static FilterInfo * filterinfo_new(const char * tag, android_LogPriority pri)
79{
80 FilterInfo *p_ret;
81
82 p_ret = (FilterInfo *)calloc(1, sizeof(FilterInfo));
83 p_ret->mTag = strdup(tag);
84 p_ret->mPri = pri;
85
86 return p_ret;
87}
88
Mark Salyzyna04464a2014-04-30 08:50:53 -070089/* balance to above, filterinfo_free left unimplemented */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070090
91/*
92 * Note: also accepts 0-9 priorities
93 * returns ANDROID_LOG_UNKNOWN if the character is unrecognized
94 */
95static android_LogPriority filterCharToPri (char c)
96{
97 android_LogPriority pri;
98
99 c = tolower(c);
100
101 if (c >= '0' && c <= '9') {
102 if (c >= ('0'+ANDROID_LOG_SILENT)) {
103 pri = ANDROID_LOG_VERBOSE;
104 } else {
105 pri = (android_LogPriority)(c - '0');
106 }
107 } else if (c == 'v') {
108 pri = ANDROID_LOG_VERBOSE;
109 } else if (c == 'd') {
110 pri = ANDROID_LOG_DEBUG;
111 } else if (c == 'i') {
112 pri = ANDROID_LOG_INFO;
113 } else if (c == 'w') {
114 pri = ANDROID_LOG_WARN;
115 } else if (c == 'e') {
116 pri = ANDROID_LOG_ERROR;
117 } else if (c == 'f') {
118 pri = ANDROID_LOG_FATAL;
119 } else if (c == 's') {
120 pri = ANDROID_LOG_SILENT;
121 } else if (c == '*') {
122 pri = ANDROID_LOG_DEFAULT;
123 } else {
124 pri = ANDROID_LOG_UNKNOWN;
125 }
126
127 return pri;
128}
129
130static char filterPriToChar (android_LogPriority pri)
131{
132 switch (pri) {
133 case ANDROID_LOG_VERBOSE: return 'V';
134 case ANDROID_LOG_DEBUG: return 'D';
135 case ANDROID_LOG_INFO: return 'I';
136 case ANDROID_LOG_WARN: return 'W';
137 case ANDROID_LOG_ERROR: return 'E';
138 case ANDROID_LOG_FATAL: return 'F';
139 case ANDROID_LOG_SILENT: return 'S';
140
141 case ANDROID_LOG_DEFAULT:
142 case ANDROID_LOG_UNKNOWN:
143 default: return '?';
144 }
145}
146
Pierre Zurekead88fc2010-10-17 22:39:37 +0200147static int colorFromPri (android_LogPriority pri)
148{
149 switch (pri) {
150 case ANDROID_LOG_VERBOSE: return ANDROID_COLOR_DEFAULT;
151 case ANDROID_LOG_DEBUG: return ANDROID_COLOR_BLUE;
152 case ANDROID_LOG_INFO: return ANDROID_COLOR_GREEN;
153 case ANDROID_LOG_WARN: return ANDROID_COLOR_ORANGE;
154 case ANDROID_LOG_ERROR: return ANDROID_COLOR_RED;
155 case ANDROID_LOG_FATAL: return ANDROID_COLOR_RED;
156 case ANDROID_LOG_SILENT: return ANDROID_COLOR_DEFAULT;
157
158 case ANDROID_LOG_DEFAULT:
159 case ANDROID_LOG_UNKNOWN:
160 default: return ANDROID_COLOR_DEFAULT;
161 }
162}
163
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700164static android_LogPriority filterPriForTag(
165 AndroidLogFormat *p_format, const char *tag)
166{
167 FilterInfo *p_curFilter;
168
169 for (p_curFilter = p_format->filters
170 ; p_curFilter != NULL
171 ; p_curFilter = p_curFilter->p_next
172 ) {
173 if (0 == strcmp(tag, p_curFilter->mTag)) {
174 if (p_curFilter->mPri == ANDROID_LOG_DEFAULT) {
175 return p_format->global_pri;
176 } else {
177 return p_curFilter->mPri;
178 }
179 }
180 }
181
182 return p_format->global_pri;
183}
184
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700185/**
186 * returns 1 if this log line should be printed based on its priority
187 * and tag, and 0 if it should not
188 */
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800189LIBLOG_ABI_PUBLIC int android_log_shouldPrintLine (
190 AndroidLogFormat *p_format,
191 const char *tag,
192 android_LogPriority pri)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700193{
194 return pri >= filterPriForTag(p_format, tag);
195}
196
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800197LIBLOG_ABI_PUBLIC AndroidLogFormat *android_log_format_new()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700198{
199 AndroidLogFormat *p_ret;
200
201 p_ret = calloc(1, sizeof(AndroidLogFormat));
202
203 p_ret->global_pri = ANDROID_LOG_VERBOSE;
204 p_ret->format = FORMAT_BRIEF;
Pierre Zurekead88fc2010-10-17 22:39:37 +0200205 p_ret->colored_output = false;
Mark Salyzyne1f20042015-05-06 08:40:40 -0700206 p_ret->usec_time_output = false;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700207 p_ret->printable_output = false;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700208 p_ret->year_output = false;
209 p_ret->zone_output = false;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700210 p_ret->epoch_output = false;
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800211 p_ret->monotonic_output = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800212 p_ret->uid_output = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700213
214 return p_ret;
215}
216
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700217static list_declare(convertHead);
218
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800219LIBLOG_ABI_PUBLIC void android_log_format_free(AndroidLogFormat *p_format)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700220{
221 FilterInfo *p_info, *p_info_old;
222
223 p_info = p_format->filters;
224
225 while (p_info != NULL) {
226 p_info_old = p_info;
227 p_info = p_info->p_next;
228
229 free(p_info_old);
230 }
231
232 free(p_format);
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700233
234 /* Free conversion resource, can always be reconstructed */
235 while (!list_empty(&convertHead)) {
236 struct listnode *node = list_head(&convertHead);
237 list_remove(node);
238 free(node);
239 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700240}
241
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800242LIBLOG_ABI_PUBLIC int android_log_setPrintFormat(
243 AndroidLogFormat *p_format,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700244 AndroidLogPrintFormat format)
245{
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700246 switch (format) {
247 case FORMAT_MODIFIER_COLOR:
Pierre Zurekead88fc2010-10-17 22:39:37 +0200248 p_format->colored_output = true;
Mark Salyzyne1f20042015-05-06 08:40:40 -0700249 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700250 case FORMAT_MODIFIER_TIME_USEC:
Mark Salyzyne1f20042015-05-06 08:40:40 -0700251 p_format->usec_time_output = true;
252 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700253 case FORMAT_MODIFIER_PRINTABLE:
254 p_format->printable_output = true;
255 return 0;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700256 case FORMAT_MODIFIER_YEAR:
257 p_format->year_output = true;
258 return 0;
259 case FORMAT_MODIFIER_ZONE:
260 p_format->zone_output = !p_format->zone_output;
261 return 0;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700262 case FORMAT_MODIFIER_EPOCH:
263 p_format->epoch_output = true;
264 return 0;
265 case FORMAT_MODIFIER_MONOTONIC:
266 p_format->monotonic_output = true;
267 return 0;
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800268 case FORMAT_MODIFIER_UID:
269 p_format->uid_output = true;
270 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700271 default:
272 break;
Mark Salyzyne1f20042015-05-06 08:40:40 -0700273 }
274 p_format->format = format;
275 return 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700276}
277
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700278static const char tz[] = "TZ";
279static const char utc[] = "UTC";
280
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700281/**
282 * Returns FORMAT_OFF on invalid string
283 */
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800284LIBLOG_ABI_PUBLIC AndroidLogPrintFormat android_log_formatFromString(
285 const char * formatString)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700286{
287 static AndroidLogPrintFormat format;
288
289 if (strcmp(formatString, "brief") == 0) format = FORMAT_BRIEF;
290 else if (strcmp(formatString, "process") == 0) format = FORMAT_PROCESS;
291 else if (strcmp(formatString, "tag") == 0) format = FORMAT_TAG;
292 else if (strcmp(formatString, "thread") == 0) format = FORMAT_THREAD;
293 else if (strcmp(formatString, "raw") == 0) format = FORMAT_RAW;
294 else if (strcmp(formatString, "time") == 0) format = FORMAT_TIME;
295 else if (strcmp(formatString, "threadtime") == 0) format = FORMAT_THREADTIME;
296 else if (strcmp(formatString, "long") == 0) format = FORMAT_LONG;
Mark Salyzyne1f20042015-05-06 08:40:40 -0700297 else if (strcmp(formatString, "color") == 0) format = FORMAT_MODIFIER_COLOR;
298 else if (strcmp(formatString, "usec") == 0) format = FORMAT_MODIFIER_TIME_USEC;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700299 else if (strcmp(formatString, "printable") == 0) format = FORMAT_MODIFIER_PRINTABLE;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700300 else if (strcmp(formatString, "year") == 0) format = FORMAT_MODIFIER_YEAR;
301 else if (strcmp(formatString, "zone") == 0) format = FORMAT_MODIFIER_ZONE;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700302 else if (strcmp(formatString, "epoch") == 0) format = FORMAT_MODIFIER_EPOCH;
303 else if (strcmp(formatString, "monotonic") == 0) format = FORMAT_MODIFIER_MONOTONIC;
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800304 else if (strcmp(formatString, "uid") == 0) format = FORMAT_MODIFIER_UID;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700305 else {
306 extern char *tzname[2];
307 static const char gmt[] = "GMT";
308 char *cp = getenv(tz);
309 if (cp) {
310 cp = strdup(cp);
311 }
312 setenv(tz, formatString, 1);
313 /*
314 * Run tzset here to determine if the timezone is legitimate. If the
315 * zone is GMT, check if that is what was asked for, if not then
316 * did not match any on the system; report an error to caller.
317 */
318 tzset();
319 if (!tzname[0]
320 || ((!strcmp(tzname[0], utc)
321 || !strcmp(tzname[0], gmt)) /* error? */
322 && strcasecmp(formatString, utc)
323 && strcasecmp(formatString, gmt))) { /* ok */
324 if (cp) {
325 setenv(tz, cp, 1);
326 } else {
327 unsetenv(tz);
328 }
329 tzset();
330 format = FORMAT_OFF;
331 } else {
332 format = FORMAT_MODIFIER_ZONE;
333 }
334 free(cp);
335 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700336
337 return format;
338}
339
340/**
341 * filterExpression: a single filter expression
342 * eg "AT:d"
343 *
344 * returns 0 on success and -1 on invalid expression
345 *
346 * Assumes single threaded execution
347 */
348
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800349LIBLOG_ABI_PUBLIC int android_log_addFilterRule(
350 AndroidLogFormat *p_format,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700351 const char *filterExpression)
352{
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700353 size_t tagNameLength;
354 android_LogPriority pri = ANDROID_LOG_DEFAULT;
355
356 tagNameLength = strcspn(filterExpression, ":");
357
358 if (tagNameLength == 0) {
359 goto error;
360 }
361
362 if(filterExpression[tagNameLength] == ':') {
363 pri = filterCharToPri(filterExpression[tagNameLength+1]);
364
365 if (pri == ANDROID_LOG_UNKNOWN) {
366 goto error;
367 }
368 }
369
370 if(0 == strncmp("*", filterExpression, tagNameLength)) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700371 /*
372 * This filter expression refers to the global filter
373 * The default level for this is DEBUG if the priority
374 * is unspecified
375 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700376 if (pri == ANDROID_LOG_DEFAULT) {
377 pri = ANDROID_LOG_DEBUG;
378 }
379
380 p_format->global_pri = pri;
381 } else {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700382 /*
383 * for filter expressions that don't refer to the global
384 * filter, the default is verbose if the priority is unspecified
385 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700386 if (pri == ANDROID_LOG_DEFAULT) {
387 pri = ANDROID_LOG_VERBOSE;
388 }
389
390 char *tagName;
391
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700392/*
393 * Presently HAVE_STRNDUP is never defined, so the second case is always taken
394 * Darwin doesn't have strnup, everything else does
395 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700396#ifdef HAVE_STRNDUP
397 tagName = strndup(filterExpression, tagNameLength);
398#else
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700399 /* a few extra bytes copied... */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700400 tagName = strdup(filterExpression);
401 tagName[tagNameLength] = '\0';
402#endif /*HAVE_STRNDUP*/
403
404 FilterInfo *p_fi = filterinfo_new(tagName, pri);
405 free(tagName);
406
407 p_fi->p_next = p_format->filters;
408 p_format->filters = p_fi;
409 }
410
411 return 0;
412error:
413 return -1;
414}
415
416
417/**
418 * filterString: a comma/whitespace-separated set of filter expressions
419 *
420 * eg "AT:d *:i"
421 *
422 * returns 0 on success and -1 on invalid expression
423 *
424 * Assumes single threaded execution
425 *
426 */
427
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800428LIBLOG_ABI_PUBLIC int android_log_addFilterString(
429 AndroidLogFormat *p_format,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700430 const char *filterString)
431{
432 char *filterStringCopy = strdup (filterString);
433 char *p_cur = filterStringCopy;
434 char *p_ret;
435 int err;
436
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700437 /* Yes, I'm using strsep */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700438 while (NULL != (p_ret = strsep(&p_cur, " \t,"))) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700439 /* ignore whitespace-only entries */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700440 if(p_ret[0] != '\0') {
441 err = android_log_addFilterRule(p_format, p_ret);
442
443 if (err < 0) {
444 goto error;
445 }
446 }
447 }
448
449 free (filterStringCopy);
450 return 0;
451error:
452 free (filterStringCopy);
453 return -1;
454}
455
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700456/**
457 * Splits a wire-format buffer into an AndroidLogEntry
458 * entry allocated by caller. Pointers will point directly into buf
459 *
460 * Returns 0 on success and -1 on invalid wire format (entry will be
461 * in unspecified state)
462 */
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800463LIBLOG_ABI_PUBLIC int android_log_processLogBuffer(
464 struct logger_entry *buf,
465 AndroidLogEntry *entry)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700466{
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700467 entry->tv_sec = buf->sec;
468 entry->tv_nsec = buf->nsec;
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800469 entry->uid = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700470 entry->pid = buf->pid;
471 entry->tid = buf->tid;
Kenny Root4bf3c022011-09-30 17:10:14 -0700472
473 /*
474 * format: <priority:1><tag:N>\0<message:N>\0
475 *
476 * tag str
Nick Kraleviche1ede152011-10-18 15:23:33 -0700477 * starts at buf->msg+1
Kenny Root4bf3c022011-09-30 17:10:14 -0700478 * msg
Nick Kraleviche1ede152011-10-18 15:23:33 -0700479 * starts at buf->msg+1+len(tag)+1
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700480 *
481 * The message may have been truncated by the kernel log driver.
482 * When that happens, we must null-terminate the message ourselves.
Kenny Root4bf3c022011-09-30 17:10:14 -0700483 */
Nick Kraleviche1ede152011-10-18 15:23:33 -0700484 if (buf->len < 3) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700485 /*
486 * An well-formed entry must consist of at least a priority
487 * and two null characters
488 */
Nick Kraleviche1ede152011-10-18 15:23:33 -0700489 fprintf(stderr, "+++ LOG: entry too small\n");
Kenny Root4bf3c022011-09-30 17:10:14 -0700490 return -1;
491 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700492
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700493 int msgStart = -1;
494 int msgEnd = -1;
495
Nick Kraleviche1ede152011-10-18 15:23:33 -0700496 int i;
Mark Salyzyn40b21552013-12-18 12:59:01 -0800497 char *msg = buf->msg;
498 struct logger_entry_v2 *buf2 = (struct logger_entry_v2 *)buf;
499 if (buf2->hdr_size) {
Mark Salyzyn305374c2016-08-18 14:59:41 -0700500 if ((buf2->hdr_size < sizeof(((struct log_msg *)NULL)->entry_v1)) ||
501 (buf2->hdr_size > sizeof(((struct log_msg *)NULL)->entry))) {
502 fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
503 return -1;
504 }
Mark Salyzyn40b21552013-12-18 12:59:01 -0800505 msg = ((char *)buf2) + buf2->hdr_size;
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800506 if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) {
507 entry->uid = ((struct logger_entry_v4 *)buf)->uid;
508 }
Mark Salyzyn40b21552013-12-18 12:59:01 -0800509 }
Nick Kraleviche1ede152011-10-18 15:23:33 -0700510 for (i = 1; i < buf->len; i++) {
Mark Salyzyn40b21552013-12-18 12:59:01 -0800511 if (msg[i] == '\0') {
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700512 if (msgStart == -1) {
513 msgStart = i + 1;
514 } else {
515 msgEnd = i;
516 break;
517 }
Nick Kraleviche1ede152011-10-18 15:23:33 -0700518 }
519 }
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700520
521 if (msgStart == -1) {
Mark Salyzyn083c5342016-03-21 09:45:34 -0700522 /* +++ LOG: malformed log message, DYB */
523 for (i = 1; i < buf->len; i++) {
524 /* odd characters in tag? */
525 if ((msg[i] <= ' ') || (msg[i] == ':') || (msg[i] >= 0x7f)) {
526 msg[i] = '\0';
527 msgStart = i + 1;
528 break;
529 }
530 }
531 if (msgStart == -1) {
532 msgStart = buf->len - 1; /* All tag, no message, print truncates */
533 }
Nick Kralevich63f4a842011-10-17 10:45:03 -0700534 }
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700535 if (msgEnd == -1) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700536 /* incoming message not null-terminated; force it */
Mark Salyzynd7bcf402015-12-04 09:24:15 -0800537 msgEnd = buf->len - 1; /* may result in msgEnd < msgStart */
Mark Salyzyn40b21552013-12-18 12:59:01 -0800538 msg[msgEnd] = '\0';
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700539 }
540
Mark Salyzyn40b21552013-12-18 12:59:01 -0800541 entry->priority = msg[0];
542 entry->tag = msg + 1;
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700543 entry->tagLen = msgStart - 1;
Mark Salyzyn40b21552013-12-18 12:59:01 -0800544 entry->message = msg + msgStart;
Mark Salyzynd7bcf402015-12-04 09:24:15 -0800545 entry->messageLen = (msgEnd < msgStart) ? 0 : (msgEnd - msgStart);
Nick Kralevich63f4a842011-10-17 10:45:03 -0700546
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700547 return 0;
548}
549
550/*
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000551 * Extract a 4-byte value from a byte stream.
552 */
553static inline uint32_t get4LE(const uint8_t* src)
554{
555 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
556}
557
558/*
559 * Extract an 8-byte value from a byte stream.
560 */
561static inline uint64_t get8LE(const uint8_t* src)
562{
563 uint32_t low, high;
564
565 low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
566 high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24);
Jeff Brown44193d92015-04-28 12:47:02 -0700567 return ((uint64_t) high << 32) | (uint64_t) low;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000568}
569
570
571/*
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700572 * Recursively convert binary log data to printable form.
573 *
574 * This needs to be recursive because you can have lists of lists.
575 *
576 * If we run out of room, we stop processing immediately. It's important
577 * for us to check for space on every output element to avoid producing
578 * garbled output.
579 *
580 * Returns 0 on success, 1 on buffer full, -1 on failure.
581 */
582static int android_log_printBinaryEvent(const unsigned char** pEventData,
583 size_t* pEventDataLen, char** pOutBuf, size_t* pOutBufLen)
584{
585 const unsigned char* eventData = *pEventData;
586 size_t eventDataLen = *pEventDataLen;
587 char* outBuf = *pOutBuf;
588 size_t outBufLen = *pOutBufLen;
589 unsigned char type;
590 size_t outCount;
591 int result = 0;
592
593 if (eventDataLen < 1)
594 return -1;
595 type = *eventData++;
596 eventDataLen--;
597
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700598 switch (type) {
599 case EVENT_TYPE_INT:
600 /* 32-bit signed int */
601 {
602 int ival;
603
604 if (eventDataLen < 4)
605 return -1;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000606 ival = get4LE(eventData);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700607 eventData += 4;
608 eventDataLen -= 4;
609
610 outCount = snprintf(outBuf, outBufLen, "%d", ival);
611 if (outCount < outBufLen) {
612 outBuf += outCount;
613 outBufLen -= outCount;
614 } else {
615 /* halt output */
616 goto no_room;
617 }
618 }
619 break;
620 case EVENT_TYPE_LONG:
621 /* 64-bit signed long */
622 {
Jeff Brown44193d92015-04-28 12:47:02 -0700623 uint64_t lval;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700624
625 if (eventDataLen < 8)
626 return -1;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000627 lval = get8LE(eventData);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700628 eventData += 8;
629 eventDataLen -= 8;
630
Jeff Brown44193d92015-04-28 12:47:02 -0700631 outCount = snprintf(outBuf, outBufLen, "%" PRId64, lval);
632 if (outCount < outBufLen) {
633 outBuf += outCount;
634 outBufLen -= outCount;
635 } else {
636 /* halt output */
637 goto no_room;
638 }
639 }
640 break;
641 case EVENT_TYPE_FLOAT:
642 /* float */
643 {
644 uint32_t ival;
645 float fval;
646
647 if (eventDataLen < 4)
648 return -1;
649 ival = get4LE(eventData);
650 fval = *(float*)&ival;
651 eventData += 4;
652 eventDataLen -= 4;
653
654 outCount = snprintf(outBuf, outBufLen, "%f", fval);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700655 if (outCount < outBufLen) {
656 outBuf += outCount;
657 outBufLen -= outCount;
658 } else {
659 /* halt output */
660 goto no_room;
661 }
662 }
663 break;
664 case EVENT_TYPE_STRING:
665 /* UTF-8 chars, not NULL-terminated */
666 {
667 unsigned int strLen;
668
669 if (eventDataLen < 4)
670 return -1;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000671 strLen = get4LE(eventData);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700672 eventData += 4;
673 eventDataLen -= 4;
674
675 if (eventDataLen < strLen)
676 return -1;
677
678 if (strLen < outBufLen) {
679 memcpy(outBuf, eventData, strLen);
680 outBuf += strLen;
681 outBufLen -= strLen;
682 } else if (outBufLen > 0) {
683 /* copy what we can */
684 memcpy(outBuf, eventData, outBufLen);
685 outBuf += outBufLen;
686 outBufLen -= outBufLen;
687 goto no_room;
688 }
689 eventData += strLen;
690 eventDataLen -= strLen;
691 break;
692 }
693 case EVENT_TYPE_LIST:
694 /* N items, all different types */
695 {
696 unsigned char count;
697 int i;
698
699 if (eventDataLen < 1)
700 return -1;
701
702 count = *eventData++;
703 eventDataLen--;
704
705 if (outBufLen > 0) {
706 *outBuf++ = '[';
707 outBufLen--;
708 } else {
709 goto no_room;
710 }
711
712 for (i = 0; i < count; i++) {
713 result = android_log_printBinaryEvent(&eventData, &eventDataLen,
714 &outBuf, &outBufLen);
715 if (result != 0)
716 goto bail;
717
718 if (i < count-1) {
719 if (outBufLen > 0) {
720 *outBuf++ = ',';
721 outBufLen--;
722 } else {
723 goto no_room;
724 }
725 }
726 }
727
728 if (outBufLen > 0) {
729 *outBuf++ = ']';
730 outBufLen--;
731 } else {
732 goto no_room;
733 }
734 }
735 break;
736 default:
737 fprintf(stderr, "Unknown binary event type %d\n", type);
738 return -1;
739 }
740
741bail:
742 *pEventData = eventData;
743 *pEventDataLen = eventDataLen;
744 *pOutBuf = outBuf;
745 *pOutBufLen = outBufLen;
746 return result;
747
748no_room:
749 result = 1;
750 goto bail;
751}
752
753/**
754 * Convert a binary log entry to ASCII form.
755 *
756 * For convenience we mimic the processLogBuffer API. There is no
757 * pre-defined output length for the binary data, since we're free to format
758 * it however we choose, which means we can't really use a fixed-size buffer
759 * here.
760 */
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800761LIBLOG_ABI_PUBLIC int android_log_processBinaryLogBuffer(
762 struct logger_entry *buf,
763 AndroidLogEntry *entry,
764 const EventTagMap *map,
765 char *messageBuf, int messageBufLen)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700766{
767 size_t inCount;
768 unsigned int tagIndex;
769 const unsigned char* eventData;
770
771 entry->tv_sec = buf->sec;
772 entry->tv_nsec = buf->nsec;
773 entry->priority = ANDROID_LOG_INFO;
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800774 entry->uid = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700775 entry->pid = buf->pid;
776 entry->tid = buf->tid;
777
778 /*
Mark Salyzyn7bc80232015-12-11 12:32:53 -0800779 * Pull the tag out, fill in some additional details based on incoming
780 * buffer version (v3 adds lid, v4 adds uid).
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700781 */
782 eventData = (const unsigned char*) buf->msg;
Mark Salyzyn40b21552013-12-18 12:59:01 -0800783 struct logger_entry_v2 *buf2 = (struct logger_entry_v2 *)buf;
784 if (buf2->hdr_size) {
Mark Salyzyn305374c2016-08-18 14:59:41 -0700785 if ((buf2->hdr_size < sizeof(((struct log_msg *)NULL)->entry_v1)) ||
786 (buf2->hdr_size > sizeof(((struct log_msg *)NULL)->entry))) {
787 fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
788 return -1;
789 }
Mark Salyzyn40b21552013-12-18 12:59:01 -0800790 eventData = ((unsigned char *)buf2) + buf2->hdr_size;
Mark Salyzyn7bc80232015-12-11 12:32:53 -0800791 if ((buf2->hdr_size >= sizeof(struct logger_entry_v3)) &&
792 (((struct logger_entry_v3 *)buf)->lid == LOG_ID_SECURITY)) {
793 entry->priority = ANDROID_LOG_WARN;
794 }
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800795 if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) {
796 entry->uid = ((struct logger_entry_v4 *)buf)->uid;
797 }
Mark Salyzyn40b21552013-12-18 12:59:01 -0800798 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700799 inCount = buf->len;
800 if (inCount < 4)
801 return -1;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000802 tagIndex = get4LE(eventData);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700803 eventData += 4;
804 inCount -= 4;
805
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700806 entry->tagLen = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700807 if (map != NULL) {
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700808 entry->tag = android_lookupEventTag_len(map, &entry->tagLen, tagIndex);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700809 } else {
810 entry->tag = NULL;
811 }
812
813 /*
814 * If we don't have a map, or didn't find the tag number in the map,
815 * stuff a generated tag value into the start of the output buffer and
816 * shift the buffer pointers down.
817 */
818 if (entry->tag == NULL) {
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700819 size_t tagLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700820
821 tagLen = snprintf(messageBuf, messageBufLen, "[%d]", tagIndex);
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700822 if (tagLen >= (size_t)messageBufLen) {
823 tagLen = messageBufLen - 1;
824 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700825 entry->tag = messageBuf;
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700826 entry->tagLen = tagLen;
827 messageBuf += tagLen + 1;
828 messageBufLen -= tagLen + 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700829 }
830
831 /*
832 * Format the event log data into the buffer.
833 */
834 char* outBuf = messageBuf;
835 size_t outRemaining = messageBufLen-1; /* leave one for nul byte */
836 int result;
837 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf,
838 &outRemaining);
839 if (result < 0) {
840 fprintf(stderr, "Binary log entry conversion failed\n");
841 return -1;
842 } else if (result == 1) {
843 if (outBuf > messageBuf) {
844 /* leave an indicator */
845 *(outBuf-1) = '!';
846 } else {
847 /* no room to output anything at all */
848 *outBuf++ = '!';
849 outRemaining--;
850 }
851 /* pretend we ate all the data */
852 inCount = 0;
853 }
854
855 /* eat the silly terminating '\n' */
856 if (inCount == 1 && *eventData == '\n') {
857 eventData++;
858 inCount--;
859 }
860
861 if (inCount != 0) {
862 fprintf(stderr,
Andrew Hsiehd2c8f522012-02-27 16:48:18 -0800863 "Warning: leftover binary log data (%zu bytes)\n", inCount);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700864 }
865
866 /*
867 * Terminate the buffer. The NUL byte does not count as part of
868 * entry->messageLen.
869 */
870 *outBuf = '\0';
871 entry->messageLen = outBuf - messageBuf;
872 assert(entry->messageLen == (messageBufLen-1) - outRemaining);
873
874 entry->message = messageBuf;
875
876 return 0;
877}
878
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700879/*
880 * One utf8 character at a time
881 *
882 * Returns the length of the utf8 character in the buffer,
883 * or -1 if illegal or truncated
884 *
885 * Open coded from libutils/Unicode.cpp, borrowed from utf8_length(),
886 * can not remove from here because of library circular dependencies.
887 * Expect one-day utf8_character_length with the same signature could
888 * _also_ be part of libutils/Unicode.cpp if its usefullness needs to
889 * propagate globally.
890 */
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800891LIBLOG_WEAK ssize_t utf8_character_length(const char *src, size_t len)
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700892{
893 const char *cur = src;
894 const char first_char = *cur++;
895 static const uint32_t kUnicodeMaxCodepoint = 0x0010FFFF;
896 int32_t mask, to_ignore_mask;
897 size_t num_to_read;
898 uint32_t utf32;
899
900 if ((first_char & 0x80) == 0) { /* ASCII */
Mark Salyzynfaa92e92015-09-08 07:57:27 -0700901 return first_char ? 1 : -1;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700902 }
903
904 /*
905 * (UTF-8's character must not be like 10xxxxxx,
906 * but 110xxxxx, 1110xxxx, ... or 1111110x)
907 */
908 if ((first_char & 0x40) == 0) {
909 return -1;
910 }
911
912 for (utf32 = 1, num_to_read = 1, mask = 0x40, to_ignore_mask = 0x80;
913 num_to_read < 5 && (first_char & mask);
914 num_to_read++, to_ignore_mask |= mask, mask >>= 1) {
915 if (num_to_read > len) {
916 return -1;
917 }
918 if ((*cur & 0xC0) != 0x80) { /* can not be 10xxxxxx? */
919 return -1;
920 }
921 utf32 = (utf32 << 6) + (*cur++ & 0b00111111);
922 }
923 /* "first_char" must be (110xxxxx - 11110xxx) */
924 if (num_to_read >= 5) {
925 return -1;
926 }
927 to_ignore_mask |= mask;
928 utf32 |= ((~to_ignore_mask) & first_char) << (6 * (num_to_read - 1));
929 if (utf32 > kUnicodeMaxCodepoint) {
930 return -1;
931 }
932 return num_to_read;
933}
934
935/*
936 * Convert to printable from message to p buffer, return string length. If p is
937 * NULL, do not copy, but still return the expected string length.
938 */
939static size_t convertPrintable(char *p, const char *message, size_t messageLen)
940{
941 char *begin = p;
942 bool print = p != NULL;
943
944 while (messageLen) {
945 char buf[6];
946 ssize_t len = sizeof(buf) - 1;
947 if ((size_t)len > messageLen) {
948 len = messageLen;
949 }
950 len = utf8_character_length(message, len);
951
952 if (len < 0) {
953 snprintf(buf, sizeof(buf),
954 ((messageLen > 1) && isdigit(message[1]))
955 ? "\\%03o"
956 : "\\%o",
957 *message & 0377);
958 len = 1;
959 } else {
960 buf[0] = '\0';
961 if (len == 1) {
962 if (*message == '\a') {
963 strcpy(buf, "\\a");
964 } else if (*message == '\b') {
965 strcpy(buf, "\\b");
966 } else if (*message == '\t') {
Mark Salyzyn65d5ca22015-11-18 09:58:00 -0800967 strcpy(buf, "\t"); // Do not escape tabs
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700968 } else if (*message == '\v') {
969 strcpy(buf, "\\v");
970 } else if (*message == '\f') {
971 strcpy(buf, "\\f");
972 } else if (*message == '\r') {
973 strcpy(buf, "\\r");
974 } else if (*message == '\\') {
975 strcpy(buf, "\\\\");
976 } else if ((*message < ' ') || (*message & 0x80)) {
977 snprintf(buf, sizeof(buf), "\\%o", *message & 0377);
978 }
979 }
980 if (!buf[0]) {
981 strncpy(buf, message, len);
982 buf[len] = '\0';
983 }
984 }
985 if (print) {
986 strcpy(p, buf);
987 }
988 p += strlen(buf);
989 message += len;
990 messageLen -= len;
991 }
992 return p - begin;
993}
994
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800995static char *readSeconds(char *e, struct timespec *t)
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700996{
997 unsigned long multiplier;
998 char *p;
999 t->tv_sec = strtoul(e, &p, 10);
1000 if (*p != '.') {
1001 return NULL;
1002 }
1003 t->tv_nsec = 0;
1004 multiplier = NS_PER_SEC;
1005 while (isdigit(*++p) && (multiplier /= 10)) {
1006 t->tv_nsec += (*p - '0') * multiplier;
1007 }
1008 return p;
1009}
1010
1011static struct timespec *sumTimespec(struct timespec *left,
1012 struct timespec *right)
1013{
1014 left->tv_nsec += right->tv_nsec;
1015 left->tv_sec += right->tv_sec;
1016 if (left->tv_nsec >= (long)NS_PER_SEC) {
1017 left->tv_nsec -= NS_PER_SEC;
1018 left->tv_sec += 1;
1019 }
1020 return left;
1021}
1022
1023static struct timespec *subTimespec(struct timespec *result,
1024 struct timespec *left,
1025 struct timespec *right)
1026{
1027 result->tv_nsec = left->tv_nsec - right->tv_nsec;
1028 result->tv_sec = left->tv_sec - right->tv_sec;
1029 if (result->tv_nsec < 0) {
1030 result->tv_nsec += NS_PER_SEC;
1031 result->tv_sec -= 1;
1032 }
1033 return result;
1034}
1035
1036static long long nsecTimespec(struct timespec *now)
1037{
1038 return (long long)now->tv_sec * NS_PER_SEC + now->tv_nsec;
1039}
1040
1041static void convertMonotonic(struct timespec *result,
1042 const AndroidLogEntry *entry)
1043{
1044 struct listnode *node;
1045 struct conversionList {
1046 struct listnode node; /* first */
1047 struct timespec time;
1048 struct timespec convert;
1049 } *list, *next;
1050 struct timespec time, convert;
1051
1052 /* If we do not have a conversion list, build one up */
1053 if (list_empty(&convertHead)) {
1054 bool suspended_pending = false;
1055 struct timespec suspended_monotonic = { 0, 0 };
1056 struct timespec suspended_diff = { 0, 0 };
1057
1058 /*
1059 * Read dmesg for _some_ synchronization markers and insert
1060 * Anything in the Android Logger before the dmesg logging span will
1061 * be highly suspect regarding the monotonic time calculations.
1062 */
Mark Salyzyn78786da2016-04-28 16:06:24 -07001063 FILE *p = popen("/system/bin/dmesg", "re");
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001064 if (p) {
1065 char *line = NULL;
1066 size_t len = 0;
1067 while (getline(&line, &len, p) > 0) {
1068 static const char suspend[] = "PM: suspend entry ";
1069 static const char resume[] = "PM: suspend exit ";
1070 static const char healthd[] = "healthd";
1071 static const char battery[] = ": battery ";
1072 static const char suspended[] = "Suspended for ";
1073 struct timespec monotonic;
1074 struct tm tm;
1075 char *cp, *e = line;
1076 bool add_entry = true;
1077
1078 if (*e == '<') {
1079 while (*e && (*e != '>')) {
1080 ++e;
1081 }
1082 if (*e != '>') {
1083 continue;
1084 }
1085 }
1086 if (*e != '[') {
1087 continue;
1088 }
1089 while (*++e == ' ') {
1090 ;
1091 }
1092 e = readSeconds(e, &monotonic);
1093 if (!e || (*e != ']')) {
1094 continue;
1095 }
1096
1097 if ((e = strstr(e, suspend))) {
1098 e += sizeof(suspend) - 1;
1099 } else if ((e = strstr(line, resume))) {
1100 e += sizeof(resume) - 1;
1101 } else if (((e = strstr(line, healthd)))
1102 && ((e = strstr(e + sizeof(healthd) - 1, battery)))) {
1103 /* NB: healthd is roughly 150us late, worth the price to
1104 * deal with ntp-induced or hardware clock drift. */
1105 e += sizeof(battery) - 1;
1106 } else if ((e = strstr(line, suspended))) {
1107 e += sizeof(suspended) - 1;
1108 e = readSeconds(e, &time);
1109 if (!e) {
1110 continue;
1111 }
1112 add_entry = false;
1113 suspended_pending = true;
1114 suspended_monotonic = monotonic;
1115 suspended_diff = time;
1116 } else {
1117 continue;
1118 }
1119 if (add_entry) {
1120 /* look for "????-??-?? ??:??:??.????????? UTC" */
1121 cp = strstr(e, " UTC");
1122 if (!cp || ((cp - e) < 29) || (cp[-10] != '.')) {
1123 continue;
1124 }
1125 e = cp - 29;
1126 cp = readSeconds(cp - 10, &time);
1127 if (!cp) {
1128 continue;
1129 }
1130 cp = strptime(e, "%Y-%m-%d %H:%M:%S.", &tm);
1131 if (!cp) {
1132 continue;
1133 }
1134 cp = getenv(tz);
1135 if (cp) {
1136 cp = strdup(cp);
1137 }
1138 setenv(tz, utc, 1);
1139 time.tv_sec = mktime(&tm);
1140 if (cp) {
1141 setenv(tz, cp, 1);
1142 free(cp);
1143 } else {
1144 unsetenv(tz);
1145 }
1146 list = calloc(1, sizeof(struct conversionList));
1147 list_init(&list->node);
1148 list->time = time;
1149 subTimespec(&list->convert, &time, &monotonic);
1150 list_add_tail(&convertHead, &list->node);
1151 }
1152 if (suspended_pending && !list_empty(&convertHead)) {
1153 list = node_to_item(list_tail(&convertHead),
1154 struct conversionList, node);
1155 if (subTimespec(&time,
1156 subTimespec(&time,
1157 &list->time,
1158 &list->convert),
1159 &suspended_monotonic)->tv_sec > 0) {
1160 /* resume, what is convert factor before? */
1161 subTimespec(&convert, &list->convert, &suspended_diff);
1162 } else {
1163 /* suspend */
1164 convert = list->convert;
1165 }
1166 time = suspended_monotonic;
1167 sumTimespec(&time, &convert);
1168 /* breakpoint just before sleep */
1169 list = calloc(1, sizeof(struct conversionList));
1170 list_init(&list->node);
1171 list->time = time;
1172 list->convert = convert;
1173 list_add_tail(&convertHead, &list->node);
1174 /* breakpoint just after sleep */
1175 list = calloc(1, sizeof(struct conversionList));
1176 list_init(&list->node);
1177 list->time = time;
1178 sumTimespec(&list->time, &suspended_diff);
1179 list->convert = convert;
1180 sumTimespec(&list->convert, &suspended_diff);
1181 list_add_tail(&convertHead, &list->node);
1182 suspended_pending = false;
1183 }
1184 }
1185 pclose(p);
1186 }
1187 /* last entry is our current time conversion */
1188 list = calloc(1, sizeof(struct conversionList));
1189 list_init(&list->node);
1190 clock_gettime(CLOCK_REALTIME, &list->time);
1191 clock_gettime(CLOCK_MONOTONIC, &convert);
1192 clock_gettime(CLOCK_MONOTONIC, &time);
1193 /* Correct for instant clock_gettime latency (syscall or ~30ns) */
1194 subTimespec(&time, &convert, subTimespec(&time, &time, &convert));
1195 /* Calculate conversion factor */
1196 subTimespec(&list->convert, &list->time, &time);
1197 list_add_tail(&convertHead, &list->node);
1198 if (suspended_pending) {
1199 /* manufacture a suspend @ point before */
1200 subTimespec(&convert, &list->convert, &suspended_diff);
1201 time = suspended_monotonic;
1202 sumTimespec(&time, &convert);
1203 /* breakpoint just after sleep */
1204 list = calloc(1, sizeof(struct conversionList));
1205 list_init(&list->node);
1206 list->time = time;
1207 sumTimespec(&list->time, &suspended_diff);
1208 list->convert = convert;
1209 sumTimespec(&list->convert, &suspended_diff);
1210 list_add_head(&convertHead, &list->node);
1211 /* breakpoint just before sleep */
1212 list = calloc(1, sizeof(struct conversionList));
1213 list_init(&list->node);
1214 list->time = time;
1215 list->convert = convert;
1216 list_add_head(&convertHead, &list->node);
1217 }
1218 }
1219
1220 /* Find the breakpoint in the conversion list */
1221 list = node_to_item(list_head(&convertHead), struct conversionList, node);
1222 next = NULL;
1223 list_for_each(node, &convertHead) {
1224 next = node_to_item(node, struct conversionList, node);
1225 if (entry->tv_sec < next->time.tv_sec) {
1226 break;
1227 } else if (entry->tv_sec == next->time.tv_sec) {
1228 if (entry->tv_nsec < next->time.tv_nsec) {
1229 break;
1230 }
1231 }
1232 list = next;
1233 }
1234
1235 /* blend time from one breakpoint to the next */
1236 convert = list->convert;
1237 if (next) {
1238 unsigned long long total, run;
1239
1240 total = nsecTimespec(subTimespec(&time, &next->time, &list->time));
1241 time.tv_sec = entry->tv_sec;
1242 time.tv_nsec = entry->tv_nsec;
1243 run = nsecTimespec(subTimespec(&time, &time, &list->time));
1244 if (run < total) {
1245 long long crun;
1246
1247 float f = nsecTimespec(subTimespec(&time, &next->convert, &convert));
1248 f *= run;
1249 f /= total;
1250 crun = f;
1251 convert.tv_sec += crun / (long long)NS_PER_SEC;
1252 if (crun < 0) {
1253 convert.tv_nsec -= (-crun) % NS_PER_SEC;
1254 if (convert.tv_nsec < 0) {
1255 convert.tv_nsec += NS_PER_SEC;
1256 convert.tv_sec -= 1;
1257 }
1258 } else {
1259 convert.tv_nsec += crun % NS_PER_SEC;
1260 if (convert.tv_nsec >= (long)NS_PER_SEC) {
1261 convert.tv_nsec -= NS_PER_SEC;
1262 convert.tv_sec += 1;
1263 }
1264 }
1265 }
1266 }
1267
1268 /* Apply the correction factor */
1269 result->tv_sec = entry->tv_sec;
1270 result->tv_nsec = entry->tv_nsec;
1271 subTimespec(result, result, &convert);
1272}
1273
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001274/**
1275 * Formats a log message into a buffer
1276 *
1277 * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer
1278 * If return value != defaultBuffer, caller must call free()
1279 * Returns NULL on malloc error
1280 */
1281
Mark Salyzynbe1d3c22016-03-10 08:25:33 -08001282LIBLOG_ABI_PUBLIC char *android_log_formatLogLine (
1283 AndroidLogFormat *p_format,
1284 char *defaultBuffer,
1285 size_t defaultBufferSize,
1286 const AndroidLogEntry *entry,
1287 size_t *p_outLength)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001288{
Yabin Cui8a985352014-11-13 10:02:08 -08001289#if !defined(_WIN32)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001290 struct tm tmBuf;
1291#endif
1292 struct tm* ptm;
Mark Salyzynf28f6a92015-08-31 08:01:33 -07001293 char timeBuf[64]; /* good margin, 23+nul for msec, 26+nul for usec */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001294 char prefixBuf[128], suffixBuf[128];
1295 char priChar;
1296 int prefixSuffixIsHeaderFooter = 0;
Mark Salyzyn90e7af32015-12-07 16:52:42 -08001297 char *ret;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001298 time_t now;
1299 unsigned long nsec;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001300
1301 priChar = filterPriToChar(entry->priority);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001302 size_t prefixLen = 0, suffixLen = 0;
1303 size_t len;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001304
1305 /*
1306 * Get the current date/time in pretty form
1307 *
1308 * It's often useful when examining a log with "less" to jump to
1309 * a specific point in the file by searching for the date/time stamp.
1310 * For this reason it's very annoying to have regexp meta characters
1311 * in the time stamp. Don't use forward slashes, parenthesis,
1312 * brackets, asterisks, or other special chars here.
Mark Salyzynf28f6a92015-08-31 08:01:33 -07001313 *
1314 * The caller may have affected the timezone environment, this is
1315 * expected to be sensitive to that.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001316 */
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001317 now = entry->tv_sec;
1318 nsec = entry->tv_nsec;
1319 if (p_format->monotonic_output) {
Mark Salyzynb6bee332015-09-08 08:56:32 -07001320 // prevent convertMonotonic from being called if logd is monotonic
Mark Salyzynba7a9a02015-12-01 15:57:25 -08001321 if (android_log_clockid() != CLOCK_MONOTONIC) {
Mark Salyzynb6bee332015-09-08 08:56:32 -07001322 struct timespec time;
1323 convertMonotonic(&time, entry);
1324 now = time.tv_sec;
1325 nsec = time.tv_nsec;
1326 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001327 }
1328 if (now < 0) {
1329 nsec = NS_PER_SEC - nsec;
1330 }
1331 if (p_format->epoch_output || p_format->monotonic_output) {
1332 ptm = NULL;
1333 snprintf(timeBuf, sizeof(timeBuf),
1334 p_format->monotonic_output ? "%6lld" : "%19lld",
1335 (long long)now);
1336 } else {
Yabin Cui8a985352014-11-13 10:02:08 -08001337#if !defined(_WIN32)
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001338 ptm = localtime_r(&now, &tmBuf);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001339#else
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001340 ptm = localtime(&now);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001341#endif
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001342 strftime(timeBuf, sizeof(timeBuf),
1343 &"%Y-%m-%d %H:%M:%S"[p_format->year_output ? 0 : 3],
1344 ptm);
1345 }
Mark Salyzyne1f20042015-05-06 08:40:40 -07001346 len = strlen(timeBuf);
1347 if (p_format->usec_time_output) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -07001348 len += snprintf(timeBuf + len, sizeof(timeBuf) - len,
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001349 ".%06ld", nsec / US_PER_NSEC);
Mark Salyzyne1f20042015-05-06 08:40:40 -07001350 } else {
Mark Salyzynf28f6a92015-08-31 08:01:33 -07001351 len += snprintf(timeBuf + len, sizeof(timeBuf) - len,
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001352 ".%03ld", nsec / MS_PER_NSEC);
Mark Salyzynf28f6a92015-08-31 08:01:33 -07001353 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001354 if (p_format->zone_output && ptm) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -07001355 strftime(timeBuf + len, sizeof(timeBuf) - len, " %z", ptm);
Mark Salyzyne1f20042015-05-06 08:40:40 -07001356 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001357
1358 /*
1359 * Construct a buffer containing the log header and log message.
1360 */
Pierre Zurekead88fc2010-10-17 22:39:37 +02001361 if (p_format->colored_output) {
1362 prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm",
1363 colorFromPri(entry->priority));
1364 prefixLen = MIN(prefixLen, sizeof(prefixBuf));
1365 suffixLen = snprintf(suffixBuf, sizeof(suffixBuf), "\x1B[0m");
1366 suffixLen = MIN(suffixLen, sizeof(suffixBuf));
1367 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001368
Mark Salyzyn90e7af32015-12-07 16:52:42 -08001369 char uid[16];
1370 uid[0] = '\0';
1371 if (p_format->uid_output) {
1372 if (entry->uid >= 0) {
Mark Salyzyn2aa510e2015-12-08 09:15:06 -08001373
William Roberts8a5b9ca2016-04-08 12:13:17 -07001374 /*
1375 * This code is Android specific, bionic guarantees that
1376 * calls to non-reentrant getpwuid() are thread safe.
1377 */
1378#ifndef __BIONIC__
1379#warning "This code assumes that getpwuid is thread safe, only true with Bionic!"
1380#endif
1381 struct passwd* pwd = getpwuid(entry->uid);
1382 if (pwd && (strlen(pwd->pw_name) <= 5)) {
1383 snprintf(uid, sizeof(uid), "%5s:", pwd->pw_name);
Mark Salyzyn2aa510e2015-12-08 09:15:06 -08001384 } else {
1385 // Not worth parsing package list, names all longer than 5
1386 snprintf(uid, sizeof(uid), "%5d:", entry->uid);
1387 }
Mark Salyzyn90e7af32015-12-07 16:52:42 -08001388 } else {
1389 snprintf(uid, sizeof(uid), " ");
1390 }
1391 }
1392
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001393 switch (p_format->format) {
1394 case FORMAT_TAG:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001395 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001396 "%c/%-8.*s: ", priChar, (int)entry->tagLen, entry->tag);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001397 strcpy(suffixBuf + suffixLen, "\n");
1398 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001399 break;
1400 case FORMAT_PROCESS:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001401 len = snprintf(suffixBuf + suffixLen, sizeof(suffixBuf) - suffixLen,
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001402 " (%.*s)\n", (int)entry->tagLen, entry->tag);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001403 suffixLen += MIN(len, sizeof(suffixBuf) - suffixLen);
1404 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyn90e7af32015-12-07 16:52:42 -08001405 "%c(%s%5d) ", priChar, uid, entry->pid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001406 break;
1407 case FORMAT_THREAD:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001408 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyn90e7af32015-12-07 16:52:42 -08001409 "%c(%s%5d:%5d) ", priChar, uid, entry->pid, entry->tid);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001410 strcpy(suffixBuf + suffixLen, "\n");
1411 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001412 break;
1413 case FORMAT_RAW:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001414 prefixBuf[prefixLen] = 0;
1415 len = 0;
1416 strcpy(suffixBuf + suffixLen, "\n");
1417 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001418 break;
1419 case FORMAT_TIME:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001420 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001421 "%s %c/%-8.*s(%s%5d): ", timeBuf, priChar,
1422 (int)entry->tagLen, entry->tag, uid, entry->pid);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001423 strcpy(suffixBuf + suffixLen, "\n");
1424 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001425 break;
1426 case FORMAT_THREADTIME:
Mark Salyzyn90e7af32015-12-07 16:52:42 -08001427 ret = strchr(uid, ':');
1428 if (ret) {
1429 *ret = ' ';
1430 }
Pierre Zurekead88fc2010-10-17 22:39:37 +02001431 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001432 "%s %s%5d %5d %c %-8.*s: ", timeBuf, uid, entry->pid,
1433 entry->tid, priChar, (int)entry->tagLen, entry->tag);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001434 strcpy(suffixBuf + suffixLen, "\n");
1435 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001436 break;
1437 case FORMAT_LONG:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001438 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001439 "[ %s %s%5d:%5d %c/%-8.*s ]\n",
1440 timeBuf, uid, entry->pid, entry->tid, priChar,
1441 (int)entry->tagLen, entry->tag);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001442 strcpy(suffixBuf + suffixLen, "\n\n");
1443 suffixLen += 2;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001444 prefixSuffixIsHeaderFooter = 1;
1445 break;
1446 case FORMAT_BRIEF:
1447 default:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001448 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001449 "%c/%-8.*s(%s%5d): ", priChar, (int)entry->tagLen, entry->tag,
1450 uid, entry->pid);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001451 strcpy(suffixBuf + suffixLen, "\n");
1452 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001453 break;
1454 }
Pierre Zurekead88fc2010-10-17 22:39:37 +02001455
Keith Prestonb45b5c92010-02-11 15:12:53 -06001456 /* snprintf has a weird return value. It returns what would have been
1457 * written given a large enough buffer. In the case that the prefix is
1458 * longer then our buffer(128), it messes up the calculations below
1459 * possibly causing heap corruption. To avoid this we double check and
1460 * set the length at the maximum (size minus null byte)
1461 */
Mark Salyzyn2f83d672016-03-11 12:06:12 -08001462 prefixLen += len;
1463 if (prefixLen >= sizeof(prefixBuf)) {
1464 prefixLen = sizeof(prefixBuf) - 1;
1465 prefixBuf[sizeof(prefixBuf) - 1] = '\0';
1466 }
1467 if (suffixLen >= sizeof(suffixBuf)) {
1468 suffixLen = sizeof(suffixBuf) - 1;
1469 suffixBuf[sizeof(suffixBuf) - 2] = '\n';
1470 suffixBuf[sizeof(suffixBuf) - 1] = '\0';
1471 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001472
1473 /* the following code is tragically unreadable */
1474
1475 size_t numLines;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001476 char *p;
1477 size_t bufferSize;
1478 const char *pm;
1479
1480 if (prefixSuffixIsHeaderFooter) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001481 /* we're just wrapping message with a header/footer */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001482 numLines = 1;
1483 } else {
1484 pm = entry->message;
1485 numLines = 0;
1486
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001487 /*
1488 * The line-end finding here must match the line-end finding
1489 * in for ( ... numLines...) loop below
1490 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001491 while (pm < (entry->message + entry->messageLen)) {
1492 if (*pm++ == '\n') numLines++;
1493 }
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001494 /* plus one line for anything not newline-terminated at the end */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001495 if (pm > entry->message && *(pm-1) != '\n') numLines++;
1496 }
1497
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001498 /*
1499 * this is an upper bound--newlines in message may be counted
1500 * extraneously
1501 */
1502 bufferSize = (numLines * (prefixLen + suffixLen)) + 1;
1503 if (p_format->printable_output) {
1504 /* Calculate extra length to convert non-printable to printable */
1505 bufferSize += convertPrintable(NULL, entry->message, entry->messageLen);
1506 } else {
1507 bufferSize += entry->messageLen;
1508 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001509
1510 if (defaultBufferSize >= bufferSize) {
1511 ret = defaultBuffer;
1512 } else {
1513 ret = (char *)malloc(bufferSize);
1514
1515 if (ret == NULL) {
1516 return ret;
1517 }
1518 }
1519
1520 ret[0] = '\0'; /* to start strcat off */
1521
1522 p = ret;
1523 pm = entry->message;
1524
1525 if (prefixSuffixIsHeaderFooter) {
1526 strcat(p, prefixBuf);
1527 p += prefixLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001528 if (p_format->printable_output) {
1529 p += convertPrintable(p, entry->message, entry->messageLen);
1530 } else {
1531 strncat(p, entry->message, entry->messageLen);
1532 p += entry->messageLen;
1533 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001534 strcat(p, suffixBuf);
1535 p += suffixLen;
1536 } else {
Mark Salyzyn7cc80132016-01-20 13:52:46 -08001537 do {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001538 const char *lineStart;
1539 size_t lineLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001540 lineStart = pm;
1541
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001542 /* Find the next end-of-line in message */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001543 while (pm < (entry->message + entry->messageLen)
1544 && *pm != '\n') pm++;
1545 lineLen = pm - lineStart;
1546
1547 strcat(p, prefixBuf);
1548 p += prefixLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001549 if (p_format->printable_output) {
1550 p += convertPrintable(p, lineStart, lineLen);
1551 } else {
1552 strncat(p, lineStart, lineLen);
1553 p += lineLen;
1554 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001555 strcat(p, suffixBuf);
1556 p += suffixLen;
1557
1558 if (*pm == '\n') pm++;
Mark Salyzyn7cc80132016-01-20 13:52:46 -08001559 } while (pm < (entry->message + entry->messageLen));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001560 }
1561
1562 if (p_outLength != NULL) {
1563 *p_outLength = p - ret;
1564 }
1565
1566 return ret;
1567}
1568
1569/**
1570 * Either print or do not print log line, based on filter
1571 *
1572 * Returns count bytes written
1573 */
1574
Mark Salyzynbe1d3c22016-03-10 08:25:33 -08001575LIBLOG_ABI_PUBLIC int android_log_printLogLine(
1576 AndroidLogFormat *p_format,
1577 int fd,
1578 const AndroidLogEntry *entry)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001579{
1580 int ret;
1581 char defaultBuffer[512];
1582 char *outBuffer = NULL;
1583 size_t totalLen;
1584
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001585 outBuffer = android_log_formatLogLine(p_format, defaultBuffer,
1586 sizeof(defaultBuffer), entry, &totalLen);
1587
1588 if (!outBuffer)
1589 return -1;
1590
1591 do {
1592 ret = write(fd, outBuffer, totalLen);
1593 } while (ret < 0 && errno == EINTR);
1594
1595 if (ret < 0) {
1596 fprintf(stderr, "+++ LOG: write failed (errno=%d)\n", errno);
1597 ret = 0;
1598 goto done;
1599 }
1600
1601 if (((size_t)ret) < totalLen) {
1602 fprintf(stderr, "+++ LOG: write partial (%d of %d)\n", ret,
1603 (int)totalLen);
1604 goto done;
1605 }
1606
1607done:
1608 if (outBuffer != defaultBuffer) {
1609 free(outBuffer);
1610 }
1611
1612 return ret;
1613}