blob: 4421f83a33489564a88a4fc00dd3916af3b68ba4 [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 */
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080019#ifndef __MINGW32__
20#define HAVE_STRSEP
21#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070022
Mark Salyzyna04464a2014-04-30 08:50:53 -070023#include <assert.h>
24#include <ctype.h>
25#include <errno.h>
Mark Salyzyn4fd05072016-10-18 11:30:11 -070026#include <inttypes.h>
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080027#ifndef __MINGW32__
William Roberts8a5b9ca2016-04-08 12:13:17 -070028#include <pwd.h>
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080029#endif
Pierre Zurekead88fc2010-10-17 22:39:37 +020030#include <stdbool.h>
Mark Salyzyna04464a2014-04-30 08:50:53 -070031#include <stdint.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
Pierre Zurekead88fc2010-10-17 22:39:37 +020035#include <sys/param.h>
William Roberts8a5b9ca2016-04-08 12:13:17 -070036#include <sys/types.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070037
Mark Salyzyn4cbed022015-08-31 15:53:41 -070038#include <cutils/list.h>
Mark Salyzynaeaaf812016-09-30 13:30:33 -070039#include <log/log.h>
Colin Cross9227bd32013-07-23 16:59:20 -070040#include <log/logprint.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070041
Mark Salyzyn018a96d2016-03-01 13:45:42 -080042#include "log_portability.h"
Mark Salyzynbe1d3c22016-03-10 08:25:33 -080043
Mark Salyzyn4cbed022015-08-31 15:53:41 -070044#define MS_PER_NSEC 1000000
45#define US_PER_NSEC 1000
46
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080047#ifndef MIN
48#define MIN(a,b) (((a) < (b)) ? (a) : (b))
49#endif
50
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070051typedef struct FilterInfo_t {
52 char *mTag;
53 android_LogPriority mPri;
54 struct FilterInfo_t *p_next;
55} FilterInfo;
56
57struct AndroidLogFormat_t {
58 android_LogPriority global_pri;
59 FilterInfo *filters;
60 AndroidLogPrintFormat format;
Pierre Zurekead88fc2010-10-17 22:39:37 +020061 bool colored_output;
Mark Salyzyne1f20042015-05-06 08:40:40 -070062 bool usec_time_output;
Mark Salyzyn9b4d7e12017-01-30 09:16:09 -080063 bool nsec_time_output;
Mark Salyzynb932b2f2015-05-15 09:01:58 -070064 bool printable_output;
Mark Salyzynf28f6a92015-08-31 08:01:33 -070065 bool year_output;
66 bool zone_output;
Mark Salyzyn4cbed022015-08-31 15:53:41 -070067 bool epoch_output;
68 bool monotonic_output;
Mark Salyzyn90e7af32015-12-07 16:52:42 -080069 bool uid_output;
Mark Salyzyn4fd05072016-10-18 11:30:11 -070070 bool descriptive_output;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070071};
72
Pierre Zurekead88fc2010-10-17 22:39:37 +020073/*
Mark Salyzyn4fd05072016-10-18 11:30:11 -070074 * API issues prevent us from exposing "descriptive" in AndroidLogFormat_t
75 * during android_log_processBinaryLogBuffer(), so we break layering.
76 */
77static bool descriptive_output = false;
78
79/*
Pierre Zurekead88fc2010-10-17 22:39:37 +020080 * gnome-terminal color tags
81 * See http://misc.flogisoft.com/bash/tip_colors_and_formatting
82 * for ideas on how to set the forground color of the text for xterm.
83 * The color manipulation character stream is defined as:
84 * ESC [ 3 8 ; 5 ; <color#> m
85 */
86#define ANDROID_COLOR_BLUE 75
87#define ANDROID_COLOR_DEFAULT 231
88#define ANDROID_COLOR_GREEN 40
89#define ANDROID_COLOR_ORANGE 166
90#define ANDROID_COLOR_RED 196
91#define ANDROID_COLOR_YELLOW 226
92
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070093static FilterInfo * filterinfo_new(const char * tag, android_LogPriority pri)
94{
95 FilterInfo *p_ret;
96
97 p_ret = (FilterInfo *)calloc(1, sizeof(FilterInfo));
98 p_ret->mTag = strdup(tag);
99 p_ret->mPri = pri;
100
101 return p_ret;
102}
103
Mark Salyzyna04464a2014-04-30 08:50:53 -0700104/* balance to above, filterinfo_free left unimplemented */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700105
106/*
107 * Note: also accepts 0-9 priorities
108 * returns ANDROID_LOG_UNKNOWN if the character is unrecognized
109 */
110static android_LogPriority filterCharToPri (char c)
111{
112 android_LogPriority pri;
113
114 c = tolower(c);
115
116 if (c >= '0' && c <= '9') {
Mark Salyzynb1d150b2017-03-02 07:47:21 -0800117 if (c >= ('0' + ANDROID_LOG_SILENT)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700118 pri = ANDROID_LOG_VERBOSE;
119 } else {
120 pri = (android_LogPriority)(c - '0');
121 }
122 } else if (c == 'v') {
123 pri = ANDROID_LOG_VERBOSE;
124 } else if (c == 'd') {
125 pri = ANDROID_LOG_DEBUG;
126 } else if (c == 'i') {
127 pri = ANDROID_LOG_INFO;
128 } else if (c == 'w') {
129 pri = ANDROID_LOG_WARN;
130 } else if (c == 'e') {
131 pri = ANDROID_LOG_ERROR;
132 } else if (c == 'f') {
133 pri = ANDROID_LOG_FATAL;
134 } else if (c == 's') {
135 pri = ANDROID_LOG_SILENT;
136 } else if (c == '*') {
137 pri = ANDROID_LOG_DEFAULT;
138 } else {
139 pri = ANDROID_LOG_UNKNOWN;
140 }
141
142 return pri;
143}
144
145static char filterPriToChar (android_LogPriority pri)
146{
147 switch (pri) {
148 case ANDROID_LOG_VERBOSE: return 'V';
149 case ANDROID_LOG_DEBUG: return 'D';
150 case ANDROID_LOG_INFO: return 'I';
151 case ANDROID_LOG_WARN: return 'W';
152 case ANDROID_LOG_ERROR: return 'E';
153 case ANDROID_LOG_FATAL: return 'F';
154 case ANDROID_LOG_SILENT: return 'S';
155
156 case ANDROID_LOG_DEFAULT:
157 case ANDROID_LOG_UNKNOWN:
158 default: return '?';
159 }
160}
161
Pierre Zurekead88fc2010-10-17 22:39:37 +0200162static int colorFromPri (android_LogPriority pri)
163{
164 switch (pri) {
165 case ANDROID_LOG_VERBOSE: return ANDROID_COLOR_DEFAULT;
166 case ANDROID_LOG_DEBUG: return ANDROID_COLOR_BLUE;
167 case ANDROID_LOG_INFO: return ANDROID_COLOR_GREEN;
168 case ANDROID_LOG_WARN: return ANDROID_COLOR_ORANGE;
169 case ANDROID_LOG_ERROR: return ANDROID_COLOR_RED;
170 case ANDROID_LOG_FATAL: return ANDROID_COLOR_RED;
171 case ANDROID_LOG_SILENT: return ANDROID_COLOR_DEFAULT;
172
173 case ANDROID_LOG_DEFAULT:
174 case ANDROID_LOG_UNKNOWN:
175 default: return ANDROID_COLOR_DEFAULT;
176 }
177}
178
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700179static android_LogPriority filterPriForTag(
180 AndroidLogFormat *p_format, const char *tag)
181{
182 FilterInfo *p_curFilter;
183
184 for (p_curFilter = p_format->filters
185 ; p_curFilter != NULL
186 ; p_curFilter = p_curFilter->p_next
187 ) {
188 if (0 == strcmp(tag, p_curFilter->mTag)) {
189 if (p_curFilter->mPri == ANDROID_LOG_DEFAULT) {
190 return p_format->global_pri;
191 } else {
192 return p_curFilter->mPri;
193 }
194 }
195 }
196
197 return p_format->global_pri;
198}
199
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700200/**
201 * returns 1 if this log line should be printed based on its priority
202 * and tag, and 0 if it should not
203 */
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800204LIBLOG_ABI_PUBLIC int android_log_shouldPrintLine (
205 AndroidLogFormat *p_format,
206 const char *tag,
207 android_LogPriority pri)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700208{
209 return pri >= filterPriForTag(p_format, tag);
210}
211
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800212LIBLOG_ABI_PUBLIC AndroidLogFormat *android_log_format_new()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700213{
214 AndroidLogFormat *p_ret;
215
216 p_ret = calloc(1, sizeof(AndroidLogFormat));
217
218 p_ret->global_pri = ANDROID_LOG_VERBOSE;
219 p_ret->format = FORMAT_BRIEF;
Pierre Zurekead88fc2010-10-17 22:39:37 +0200220 p_ret->colored_output = false;
Mark Salyzyne1f20042015-05-06 08:40:40 -0700221 p_ret->usec_time_output = false;
Mark Salyzyn9b4d7e12017-01-30 09:16:09 -0800222 p_ret->nsec_time_output = false;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700223 p_ret->printable_output = false;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700224 p_ret->year_output = false;
225 p_ret->zone_output = false;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700226 p_ret->epoch_output = false;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800227#ifdef __ANDROID__
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800228 p_ret->monotonic_output = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800229#else
230 p_ret->monotonic_output = false;
231#endif
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800232 p_ret->uid_output = false;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700233 p_ret->descriptive_output = false;
234 descriptive_output = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700235
236 return p_ret;
237}
238
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700239static list_declare(convertHead);
240
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800241LIBLOG_ABI_PUBLIC void android_log_format_free(AndroidLogFormat *p_format)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700242{
243 FilterInfo *p_info, *p_info_old;
244
245 p_info = p_format->filters;
246
247 while (p_info != NULL) {
248 p_info_old = p_info;
249 p_info = p_info->p_next;
250
251 free(p_info_old);
252 }
253
254 free(p_format);
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700255
256 /* Free conversion resource, can always be reconstructed */
257 while (!list_empty(&convertHead)) {
258 struct listnode *node = list_head(&convertHead);
259 list_remove(node);
260 free(node);
261 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700262}
263
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800264LIBLOG_ABI_PUBLIC int android_log_setPrintFormat(
265 AndroidLogFormat *p_format,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700266 AndroidLogPrintFormat format)
267{
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700268 switch (format) {
269 case FORMAT_MODIFIER_COLOR:
Pierre Zurekead88fc2010-10-17 22:39:37 +0200270 p_format->colored_output = true;
Mark Salyzyne1f20042015-05-06 08:40:40 -0700271 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700272 case FORMAT_MODIFIER_TIME_USEC:
Mark Salyzyne1f20042015-05-06 08:40:40 -0700273 p_format->usec_time_output = true;
274 return 0;
Mark Salyzyn9b4d7e12017-01-30 09:16:09 -0800275 case FORMAT_MODIFIER_TIME_NSEC:
276 p_format->nsec_time_output = true;
277 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700278 case FORMAT_MODIFIER_PRINTABLE:
279 p_format->printable_output = true;
280 return 0;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700281 case FORMAT_MODIFIER_YEAR:
282 p_format->year_output = true;
283 return 0;
284 case FORMAT_MODIFIER_ZONE:
285 p_format->zone_output = !p_format->zone_output;
286 return 0;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700287 case FORMAT_MODIFIER_EPOCH:
288 p_format->epoch_output = true;
289 return 0;
290 case FORMAT_MODIFIER_MONOTONIC:
291 p_format->monotonic_output = true;
292 return 0;
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800293 case FORMAT_MODIFIER_UID:
294 p_format->uid_output = true;
295 return 0;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700296 case FORMAT_MODIFIER_DESCRIPT:
297 p_format->descriptive_output = true;
298 descriptive_output = true;
299 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700300 default:
301 break;
Mark Salyzyne1f20042015-05-06 08:40:40 -0700302 }
303 p_format->format = format;
304 return 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700305}
306
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700307static const char tz[] = "TZ";
308static const char utc[] = "UTC";
309
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700310/**
311 * Returns FORMAT_OFF on invalid string
312 */
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800313LIBLOG_ABI_PUBLIC AndroidLogPrintFormat android_log_formatFromString(
314 const char * formatString)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700315{
316 static AndroidLogPrintFormat format;
317
318 if (strcmp(formatString, "brief") == 0) format = FORMAT_BRIEF;
319 else if (strcmp(formatString, "process") == 0) format = FORMAT_PROCESS;
320 else if (strcmp(formatString, "tag") == 0) format = FORMAT_TAG;
321 else if (strcmp(formatString, "thread") == 0) format = FORMAT_THREAD;
322 else if (strcmp(formatString, "raw") == 0) format = FORMAT_RAW;
323 else if (strcmp(formatString, "time") == 0) format = FORMAT_TIME;
324 else if (strcmp(formatString, "threadtime") == 0) format = FORMAT_THREADTIME;
325 else if (strcmp(formatString, "long") == 0) format = FORMAT_LONG;
Mark Salyzyne1f20042015-05-06 08:40:40 -0700326 else if (strcmp(formatString, "color") == 0) format = FORMAT_MODIFIER_COLOR;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700327 else if (strcmp(formatString, "colour") == 0) format = FORMAT_MODIFIER_COLOR;
Mark Salyzyne1f20042015-05-06 08:40:40 -0700328 else if (strcmp(formatString, "usec") == 0) format = FORMAT_MODIFIER_TIME_USEC;
Mark Salyzyn9b4d7e12017-01-30 09:16:09 -0800329 else if (strcmp(formatString, "nsec") == 0) format = FORMAT_MODIFIER_TIME_NSEC;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700330 else if (strcmp(formatString, "printable") == 0) format = FORMAT_MODIFIER_PRINTABLE;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700331 else if (strcmp(formatString, "year") == 0) format = FORMAT_MODIFIER_YEAR;
332 else if (strcmp(formatString, "zone") == 0) format = FORMAT_MODIFIER_ZONE;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700333 else if (strcmp(formatString, "epoch") == 0) format = FORMAT_MODIFIER_EPOCH;
334 else if (strcmp(formatString, "monotonic") == 0) format = FORMAT_MODIFIER_MONOTONIC;
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800335 else if (strcmp(formatString, "uid") == 0) format = FORMAT_MODIFIER_UID;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700336 else if (strcmp(formatString, "descriptive") == 0) format = FORMAT_MODIFIER_DESCRIPT;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800337#ifndef __MINGW32__
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700338 else {
339 extern char *tzname[2];
340 static const char gmt[] = "GMT";
341 char *cp = getenv(tz);
342 if (cp) {
343 cp = strdup(cp);
344 }
345 setenv(tz, formatString, 1);
346 /*
347 * Run tzset here to determine if the timezone is legitimate. If the
348 * zone is GMT, check if that is what was asked for, if not then
349 * did not match any on the system; report an error to caller.
350 */
351 tzset();
352 if (!tzname[0]
353 || ((!strcmp(tzname[0], utc)
354 || !strcmp(tzname[0], gmt)) /* error? */
355 && strcasecmp(formatString, utc)
356 && strcasecmp(formatString, gmt))) { /* ok */
357 if (cp) {
358 setenv(tz, cp, 1);
359 } else {
360 unsetenv(tz);
361 }
362 tzset();
363 format = FORMAT_OFF;
364 } else {
365 format = FORMAT_MODIFIER_ZONE;
366 }
367 free(cp);
368 }
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800369#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700370
371 return format;
372}
373
374/**
375 * filterExpression: a single filter expression
376 * eg "AT:d"
377 *
378 * returns 0 on success and -1 on invalid expression
379 *
380 * Assumes single threaded execution
381 */
382
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800383LIBLOG_ABI_PUBLIC int android_log_addFilterRule(
384 AndroidLogFormat *p_format,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700385 const char *filterExpression)
386{
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700387 size_t tagNameLength;
388 android_LogPriority pri = ANDROID_LOG_DEFAULT;
389
390 tagNameLength = strcspn(filterExpression, ":");
391
392 if (tagNameLength == 0) {
393 goto error;
394 }
395
396 if(filterExpression[tagNameLength] == ':') {
Mark Salyzynb1d150b2017-03-02 07:47:21 -0800397 pri = filterCharToPri(filterExpression[tagNameLength + 1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700398
399 if (pri == ANDROID_LOG_UNKNOWN) {
400 goto error;
401 }
402 }
403
404 if(0 == strncmp("*", filterExpression, tagNameLength)) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700405 /*
406 * This filter expression refers to the global filter
407 * The default level for this is DEBUG if the priority
408 * is unspecified
409 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700410 if (pri == ANDROID_LOG_DEFAULT) {
411 pri = ANDROID_LOG_DEBUG;
412 }
413
414 p_format->global_pri = pri;
415 } else {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700416 /*
417 * for filter expressions that don't refer to the global
418 * filter, the default is verbose if the priority is unspecified
419 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700420 if (pri == ANDROID_LOG_DEFAULT) {
421 pri = ANDROID_LOG_VERBOSE;
422 }
423
424 char *tagName;
425
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700426/*
427 * Presently HAVE_STRNDUP is never defined, so the second case is always taken
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800428 * Darwin doesn't have strndup, everything else does
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700429 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700430#ifdef HAVE_STRNDUP
431 tagName = strndup(filterExpression, tagNameLength);
432#else
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700433 /* a few extra bytes copied... */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700434 tagName = strdup(filterExpression);
435 tagName[tagNameLength] = '\0';
436#endif /*HAVE_STRNDUP*/
437
438 FilterInfo *p_fi = filterinfo_new(tagName, pri);
439 free(tagName);
440
441 p_fi->p_next = p_format->filters;
442 p_format->filters = p_fi;
443 }
444
445 return 0;
446error:
447 return -1;
448}
449
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800450#ifndef HAVE_STRSEP
451/* KISS replacement helper for below */
452static char* strsep(char** stringp, const char* delim)
453{
454 char* token;
455 char* ret = *stringp;
456
457 if (!ret || !*ret) {
458 return NULL;
459 }
460 token = strpbrk(ret, delim);
461 if (token) {
462 *token = '\0';
463 ++token;
464 } else {
465 token = ret + strlen(ret);
466 }
467 *stringp = token;
468 return ret;
469}
470#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700471
472/**
473 * filterString: a comma/whitespace-separated set of filter expressions
474 *
475 * eg "AT:d *:i"
476 *
477 * returns 0 on success and -1 on invalid expression
478 *
479 * Assumes single threaded execution
480 *
481 */
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800482LIBLOG_ABI_PUBLIC int android_log_addFilterString(
483 AndroidLogFormat *p_format,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700484 const char *filterString)
485{
486 char *filterStringCopy = strdup (filterString);
487 char *p_cur = filterStringCopy;
488 char *p_ret;
489 int err;
490
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700491 /* Yes, I'm using strsep */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700492 while (NULL != (p_ret = strsep(&p_cur, " \t,"))) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700493 /* ignore whitespace-only entries */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700494 if(p_ret[0] != '\0') {
495 err = android_log_addFilterRule(p_format, p_ret);
496
497 if (err < 0) {
498 goto error;
499 }
500 }
501 }
502
503 free (filterStringCopy);
504 return 0;
505error:
506 free (filterStringCopy);
507 return -1;
508}
509
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700510/**
511 * Splits a wire-format buffer into an AndroidLogEntry
512 * entry allocated by caller. Pointers will point directly into buf
513 *
514 * Returns 0 on success and -1 on invalid wire format (entry will be
515 * in unspecified state)
516 */
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800517LIBLOG_ABI_PUBLIC int android_log_processLogBuffer(
518 struct logger_entry *buf,
519 AndroidLogEntry *entry)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700520{
Mark Salyzynb1d150b2017-03-02 07:47:21 -0800521 entry->message = NULL;
522 entry->messageLen = 0;
523
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700524 entry->tv_sec = buf->sec;
525 entry->tv_nsec = buf->nsec;
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800526 entry->uid = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700527 entry->pid = buf->pid;
528 entry->tid = buf->tid;
Kenny Root4bf3c022011-09-30 17:10:14 -0700529
530 /*
531 * format: <priority:1><tag:N>\0<message:N>\0
532 *
533 * tag str
Nick Kraleviche1ede152011-10-18 15:23:33 -0700534 * starts at buf->msg+1
Kenny Root4bf3c022011-09-30 17:10:14 -0700535 * msg
Nick Kraleviche1ede152011-10-18 15:23:33 -0700536 * starts at buf->msg+1+len(tag)+1
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700537 *
538 * The message may have been truncated by the kernel log driver.
539 * When that happens, we must null-terminate the message ourselves.
Kenny Root4bf3c022011-09-30 17:10:14 -0700540 */
Nick Kraleviche1ede152011-10-18 15:23:33 -0700541 if (buf->len < 3) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700542 /*
543 * An well-formed entry must consist of at least a priority
544 * and two null characters
545 */
Nick Kraleviche1ede152011-10-18 15:23:33 -0700546 fprintf(stderr, "+++ LOG: entry too small\n");
Kenny Root4bf3c022011-09-30 17:10:14 -0700547 return -1;
548 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700549
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700550 int msgStart = -1;
551 int msgEnd = -1;
552
Nick Kraleviche1ede152011-10-18 15:23:33 -0700553 int i;
Mark Salyzyn40b21552013-12-18 12:59:01 -0800554 char *msg = buf->msg;
555 struct logger_entry_v2 *buf2 = (struct logger_entry_v2 *)buf;
556 if (buf2->hdr_size) {
Mark Salyzyn305374c2016-08-18 14:59:41 -0700557 if ((buf2->hdr_size < sizeof(((struct log_msg *)NULL)->entry_v1)) ||
558 (buf2->hdr_size > sizeof(((struct log_msg *)NULL)->entry))) {
559 fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
560 return -1;
561 }
Mark Salyzyn40b21552013-12-18 12:59:01 -0800562 msg = ((char *)buf2) + buf2->hdr_size;
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800563 if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) {
564 entry->uid = ((struct logger_entry_v4 *)buf)->uid;
565 }
Mark Salyzyn40b21552013-12-18 12:59:01 -0800566 }
Nick Kraleviche1ede152011-10-18 15:23:33 -0700567 for (i = 1; i < buf->len; i++) {
Mark Salyzyn40b21552013-12-18 12:59:01 -0800568 if (msg[i] == '\0') {
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700569 if (msgStart == -1) {
570 msgStart = i + 1;
571 } else {
572 msgEnd = i;
573 break;
574 }
Nick Kraleviche1ede152011-10-18 15:23:33 -0700575 }
576 }
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700577
578 if (msgStart == -1) {
Mark Salyzyn083c5342016-03-21 09:45:34 -0700579 /* +++ LOG: malformed log message, DYB */
580 for (i = 1; i < buf->len; i++) {
581 /* odd characters in tag? */
582 if ((msg[i] <= ' ') || (msg[i] == ':') || (msg[i] >= 0x7f)) {
583 msg[i] = '\0';
584 msgStart = i + 1;
585 break;
586 }
587 }
588 if (msgStart == -1) {
589 msgStart = buf->len - 1; /* All tag, no message, print truncates */
590 }
Nick Kralevich63f4a842011-10-17 10:45:03 -0700591 }
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700592 if (msgEnd == -1) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700593 /* incoming message not null-terminated; force it */
Mark Salyzynd7bcf402015-12-04 09:24:15 -0800594 msgEnd = buf->len - 1; /* may result in msgEnd < msgStart */
Mark Salyzyn40b21552013-12-18 12:59:01 -0800595 msg[msgEnd] = '\0';
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700596 }
597
Mark Salyzyn40b21552013-12-18 12:59:01 -0800598 entry->priority = msg[0];
599 entry->tag = msg + 1;
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700600 entry->tagLen = msgStart - 1;
Mark Salyzyn40b21552013-12-18 12:59:01 -0800601 entry->message = msg + msgStart;
Mark Salyzynd7bcf402015-12-04 09:24:15 -0800602 entry->messageLen = (msgEnd < msgStart) ? 0 : (msgEnd - msgStart);
Nick Kralevich63f4a842011-10-17 10:45:03 -0700603
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700604 return 0;
605}
606
607/*
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000608 * Extract a 4-byte value from a byte stream.
609 */
610static inline uint32_t get4LE(const uint8_t* src)
611{
612 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
613}
614
615/*
616 * Extract an 8-byte value from a byte stream.
617 */
618static inline uint64_t get8LE(const uint8_t* src)
619{
620 uint32_t low, high;
621
622 low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
623 high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24);
Mark Salyzynb1d150b2017-03-02 07:47:21 -0800624 return ((uint64_t)high << 32) | (uint64_t)low;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000625}
626
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700627static bool findChar(const char** cp, size_t* len, int c) {
Mark Salyzyn8dcd94b2016-11-07 13:54:42 -0800628 while ((*len) && isspace(*(*cp))) {
629 ++(*cp);
630 --(*len);
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700631 }
632 if (c == INT_MAX) return *len;
Mark Salyzyn8dcd94b2016-11-07 13:54:42 -0800633 if ((*len) && (*(*cp) == c)) {
634 ++(*cp);
635 --(*len);
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700636 return true;
637 }
638 return false;
639}
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000640
641/*
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700642 * Recursively convert binary log data to printable form.
643 *
644 * This needs to be recursive because you can have lists of lists.
645 *
646 * If we run out of room, we stop processing immediately. It's important
647 * for us to check for space on every output element to avoid producing
648 * garbled output.
649 *
650 * Returns 0 on success, 1 on buffer full, -1 on failure.
651 */
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700652enum objectType {
653 TYPE_OBJECTS = '1',
654 TYPE_BYTES = '2',
655 TYPE_MILLISECONDS = '3',
656 TYPE_ALLOCATIONS = '4',
657 TYPE_ID = '5',
658 TYPE_PERCENT = '6'
659};
660
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700661static int android_log_printBinaryEvent(const unsigned char** pEventData,
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700662 size_t* pEventDataLen, char** pOutBuf, size_t* pOutBufLen,
663 const char** fmtStr, size_t* fmtLen)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700664{
665 const unsigned char* eventData = *pEventData;
666 size_t eventDataLen = *pEventDataLen;
667 char* outBuf = *pOutBuf;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700668 char* outBufSave = outBuf;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700669 size_t outBufLen = *pOutBufLen;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700670 size_t outBufLenSave = outBufLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700671 unsigned char type;
672 size_t outCount;
673 int result = 0;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700674 const char* cp;
675 size_t len;
676 int64_t lval;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700677
Mark Salyzyn1a57ae32016-11-11 14:41:30 -0800678 if (eventDataLen < 1) return -1;
679
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700680 type = *eventData++;
681 eventDataLen--;
682
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700683 cp = NULL;
684 len = 0;
685 if (fmtStr && *fmtStr && fmtLen && *fmtLen && **fmtStr) {
686 cp = *fmtStr;
687 len = *fmtLen;
688 }
689 /*
690 * event.logtag format specification:
691 *
692 * Optionally, after the tag names can be put a description for the value(s)
693 * of the tag. Description are in the format
694 * (<name>|data type[|data unit])
695 * Multiple values are separated by commas.
696 *
697 * The data type is a number from the following values:
698 * 1: int
699 * 2: long
700 * 3: string
701 * 4: list
702 * 5: float
703 *
704 * The data unit is a number taken from the following list:
705 * 1: Number of objects
706 * 2: Number of bytes
707 * 3: Number of milliseconds
708 * 4: Number of allocations
709 * 5: Id
710 * 6: Percent
711 * Default value for data of type int/long is 2 (bytes).
712 */
713 if (!cp || !findChar(&cp, &len, '(')) {
714 len = 0;
715 } else {
716 char* outBufLastSpace = NULL;
717
718 findChar(&cp, &len, INT_MAX);
719 while (len && *cp && (*cp != '|') && (*cp != ')')) {
720 if (outBufLen <= 0) {
721 /* halt output */
722 goto no_room;
723 }
724 outBufLastSpace = isspace(*cp) ? outBuf : NULL;
725 *outBuf = *cp;
726 ++outBuf;
727 ++cp;
728 --outBufLen;
729 --len;
730 }
731 if (outBufLastSpace) {
732 outBufLen += outBuf - outBufLastSpace;
733 outBuf = outBufLastSpace;
734 }
735 if (outBufLen <= 0) {
736 /* halt output */
737 goto no_room;
738 }
739 if (outBufSave != outBuf) {
740 *outBuf = '=';
741 ++outBuf;
742 --outBufLen;
743 }
744
745 if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) {
746 static const unsigned char typeTable[] = {
747 EVENT_TYPE_INT,
748 EVENT_TYPE_LONG,
749 EVENT_TYPE_STRING,
750 EVENT_TYPE_LIST,
751 EVENT_TYPE_FLOAT
752 };
753
754 if ((*cp >= '1') &&
755 (*cp < (char)('1' + (sizeof(typeTable) / sizeof(typeTable[0])))) &&
756 (type != typeTable[(size_t)(*cp - '1')])) len = 0;
757
758 if (len) {
759 ++cp;
760 --len;
761 } else {
762 /* reset the format */
763 outBuf = outBufSave;
764 outBufLen = outBufLenSave;
765 }
766 }
767 }
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800768 outCount = 0;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700769 lval = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700770 switch (type) {
771 case EVENT_TYPE_INT:
772 /* 32-bit signed int */
773 {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700774 int32_t ival;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700775
Mark Salyzyn1a57ae32016-11-11 14:41:30 -0800776 if (eventDataLen < 4) return -1;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000777 ival = get4LE(eventData);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700778 eventData += 4;
779 eventDataLen -= 4;
780
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700781 lval = ival;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700782 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700783 goto pr_lval;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700784 case EVENT_TYPE_LONG:
785 /* 64-bit signed long */
Mark Salyzyn1a57ae32016-11-11 14:41:30 -0800786 if (eventDataLen < 8) return -1;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700787 lval = get8LE(eventData);
788 eventData += 8;
789 eventDataLen -= 8;
790 pr_lval:
791 outCount = snprintf(outBuf, outBufLen, "%" PRId64, lval);
792 if (outCount < outBufLen) {
793 outBuf += outCount;
794 outBufLen -= outCount;
795 } else {
796 /* halt output */
797 goto no_room;
Jeff Brown44193d92015-04-28 12:47:02 -0700798 }
799 break;
800 case EVENT_TYPE_FLOAT:
801 /* float */
802 {
803 uint32_t ival;
804 float fval;
805
Mark Salyzyn1a57ae32016-11-11 14:41:30 -0800806 if (eventDataLen < 4) return -1;
Jeff Brown44193d92015-04-28 12:47:02 -0700807 ival = get4LE(eventData);
808 fval = *(float*)&ival;
809 eventData += 4;
810 eventDataLen -= 4;
811
812 outCount = snprintf(outBuf, outBufLen, "%f", fval);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700813 if (outCount < outBufLen) {
814 outBuf += outCount;
815 outBufLen -= outCount;
816 } else {
817 /* halt output */
818 goto no_room;
819 }
820 }
821 break;
822 case EVENT_TYPE_STRING:
823 /* UTF-8 chars, not NULL-terminated */
824 {
825 unsigned int strLen;
826
Mark Salyzyn1a57ae32016-11-11 14:41:30 -0800827 if (eventDataLen < 4) return -1;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000828 strLen = get4LE(eventData);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700829 eventData += 4;
830 eventDataLen -= 4;
831
Mark Salyzynb1d150b2017-03-02 07:47:21 -0800832 if (eventDataLen < strLen) {
833 result = -1; /* mark truncated */
834 strLen = eventDataLen;
835 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700836
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700837 if (cp && (strLen == 0)) {
838 /* reset the format if no content */
839 outBuf = outBufSave;
840 outBufLen = outBufLenSave;
841 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700842 if (strLen < outBufLen) {
843 memcpy(outBuf, eventData, strLen);
844 outBuf += strLen;
845 outBufLen -= strLen;
Mark Salyzynb1d150b2017-03-02 07:47:21 -0800846 } else {
847 if (outBufLen > 0) {
848 /* copy what we can */
849 memcpy(outBuf, eventData, outBufLen);
850 outBuf += outBufLen;
851 outBufLen -= outBufLen;
852 }
853 if (!result) result = 1; /* if not truncated, return no room */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700854 }
855 eventData += strLen;
856 eventDataLen -= strLen;
Mark Salyzynb1d150b2017-03-02 07:47:21 -0800857 if (result != 0) goto bail;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700858 break;
859 }
860 case EVENT_TYPE_LIST:
861 /* N items, all different types */
862 {
863 unsigned char count;
864 int i;
865
Mark Salyzyn1a57ae32016-11-11 14:41:30 -0800866 if (eventDataLen < 1) return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700867
868 count = *eventData++;
869 eventDataLen--;
870
Mark Salyzyn1a57ae32016-11-11 14:41:30 -0800871 if (outBufLen <= 0) goto no_room;
872
873 *outBuf++ = '[';
874 outBufLen--;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700875
876 for (i = 0; i < count; i++) {
877 result = android_log_printBinaryEvent(&eventData, &eventDataLen,
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700878 &outBuf, &outBufLen, fmtStr, fmtLen);
Mark Salyzyn1a57ae32016-11-11 14:41:30 -0800879 if (result != 0) goto bail;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700880
Mark Salyzyn1a57ae32016-11-11 14:41:30 -0800881 if (i < (count - 1)) {
882 if (outBufLen <= 0) goto no_room;
883 *outBuf++ = ',';
884 outBufLen--;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700885 }
886 }
887
Mark Salyzyn1a57ae32016-11-11 14:41:30 -0800888 if (outBufLen <= 0) goto no_room;
889
890 *outBuf++ = ']';
891 outBufLen--;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700892 }
893 break;
894 default:
895 fprintf(stderr, "Unknown binary event type %d\n", type);
896 return -1;
897 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700898 if (cp && len) {
899 if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) {
900 switch (*cp) {
901 case TYPE_OBJECTS:
902 outCount = 0;
903 /* outCount = snprintf(outBuf, outBufLen, " objects"); */
904 break;
905 case TYPE_BYTES:
906 if ((lval != 0) && ((lval % 1024) == 0)) {
907 /* repaint with multiplier */
908 static const char suffixTable[] = { 'K', 'M', 'G', 'T' };
909 size_t idx = 0;
910 outBuf -= outCount;
911 outBufLen += outCount;
912 do {
913 lval /= 1024;
914 if ((lval % 1024) != 0) break;
915 } while (++idx < ((sizeof(suffixTable) /
916 sizeof(suffixTable[0])) - 1));
917 outCount = snprintf(outBuf, outBufLen,
918 "%" PRId64 "%cB",
919 lval, suffixTable[idx]);
920 } else {
921 outCount = snprintf(outBuf, outBufLen, "B");
922 }
923 break;
924 case TYPE_MILLISECONDS:
925 if (((lval <= -1000) || (1000 <= lval)) &&
926 (outBufLen || (outBuf[-1] == '0'))) {
927 /* repaint as (fractional) seconds, possibly saving space */
928 if (outBufLen) outBuf[0] = outBuf[-1];
929 outBuf[-1] = outBuf[-2];
930 outBuf[-2] = outBuf[-3];
931 outBuf[-3] = '.';
932 while ((outBufLen == 0) || (*outBuf == '0')) {
933 --outBuf;
934 ++outBufLen;
935 }
936 if (*outBuf != '.') {
937 ++outBuf;
938 --outBufLen;
939 }
940 outCount = snprintf(outBuf, outBufLen, "s");
941 } else {
942 outCount = snprintf(outBuf, outBufLen, "ms");
943 }
944 break;
945 case TYPE_ALLOCATIONS:
946 outCount = 0;
947 /* outCount = snprintf(outBuf, outBufLen, " allocations"); */
948 break;
949 case TYPE_ID:
950 outCount = 0;
951 break;
952 case TYPE_PERCENT:
953 outCount = snprintf(outBuf, outBufLen, "%%");
954 break;
955 default: /* ? */
956 outCount = 0;
957 break;
958 }
959 ++cp;
960 --len;
961 if (outCount < outBufLen) {
962 outBuf += outCount;
963 outBufLen -= outCount;
964 } else if (outCount) {
965 /* halt output */
966 goto no_room;
967 }
968 }
969 if (!findChar(&cp, &len, ')')) len = 0;
970 if (!findChar(&cp, &len, ',')) len = 0;
971 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700972
973bail:
974 *pEventData = eventData;
975 *pEventDataLen = eventDataLen;
976 *pOutBuf = outBuf;
977 *pOutBufLen = outBufLen;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700978 if (cp) {
979 *fmtStr = cp;
980 *fmtLen = len;
981 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700982 return result;
983
984no_room:
985 result = 1;
986 goto bail;
987}
988
989/**
990 * Convert a binary log entry to ASCII form.
991 *
992 * For convenience we mimic the processLogBuffer API. There is no
993 * pre-defined output length for the binary data, since we're free to format
994 * it however we choose, which means we can't really use a fixed-size buffer
995 * here.
996 */
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800997LIBLOG_ABI_PUBLIC int android_log_processBinaryLogBuffer(
998 struct logger_entry *buf,
999 AndroidLogEntry *entry,
Mark Salyzynb1d150b2017-03-02 07:47:21 -08001000 const EventTagMap *map __unused, /* only on !__ANDROID__ */
Mark Salyzynbe1d3c22016-03-10 08:25:33 -08001001 char *messageBuf, int messageBufLen)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001002{
1003 size_t inCount;
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001004 uint32_t tagIndex;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001005 const unsigned char* eventData;
1006
Mark Salyzynb1d150b2017-03-02 07:47:21 -08001007 entry->message = NULL;
1008 entry->messageLen = 0;
1009
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001010 entry->tv_sec = buf->sec;
1011 entry->tv_nsec = buf->nsec;
1012 entry->priority = ANDROID_LOG_INFO;
Mark Salyzyn90e7af32015-12-07 16:52:42 -08001013 entry->uid = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001014 entry->pid = buf->pid;
1015 entry->tid = buf->tid;
1016
1017 /*
Mark Salyzyn7bc80232015-12-11 12:32:53 -08001018 * Pull the tag out, fill in some additional details based on incoming
1019 * buffer version (v3 adds lid, v4 adds uid).
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001020 */
Mark Salyzynb1d150b2017-03-02 07:47:21 -08001021 eventData = (const unsigned char*)buf->msg;
Mark Salyzyn40b21552013-12-18 12:59:01 -08001022 struct logger_entry_v2 *buf2 = (struct logger_entry_v2 *)buf;
1023 if (buf2->hdr_size) {
Mark Salyzyn305374c2016-08-18 14:59:41 -07001024 if ((buf2->hdr_size < sizeof(((struct log_msg *)NULL)->entry_v1)) ||
1025 (buf2->hdr_size > sizeof(((struct log_msg *)NULL)->entry))) {
1026 fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
1027 return -1;
1028 }
Mark Salyzyn40b21552013-12-18 12:59:01 -08001029 eventData = ((unsigned char *)buf2) + buf2->hdr_size;
Mark Salyzyn7bc80232015-12-11 12:32:53 -08001030 if ((buf2->hdr_size >= sizeof(struct logger_entry_v3)) &&
1031 (((struct logger_entry_v3 *)buf)->lid == LOG_ID_SECURITY)) {
1032 entry->priority = ANDROID_LOG_WARN;
1033 }
Mark Salyzyn90e7af32015-12-07 16:52:42 -08001034 if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) {
1035 entry->uid = ((struct logger_entry_v4 *)buf)->uid;
1036 }
Mark Salyzyn40b21552013-12-18 12:59:01 -08001037 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001038 inCount = buf->len;
Mark Salyzyn1a57ae32016-11-11 14:41:30 -08001039 if (inCount < 4) return -1;
Mark Salyzyn8470dad2015-03-06 20:42:57 +00001040 tagIndex = get4LE(eventData);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001041 eventData += 4;
1042 inCount -= 4;
1043
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001044 entry->tagLen = 0;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001045 entry->tag = NULL;
1046#ifdef __ANDROID__
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001047 if (map != NULL) {
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001048 entry->tag = android_lookupEventTag_len(map, &entry->tagLen, tagIndex);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001049 }
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001050#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001051
1052 /*
1053 * If we don't have a map, or didn't find the tag number in the map,
1054 * stuff a generated tag value into the start of the output buffer and
1055 * shift the buffer pointers down.
1056 */
1057 if (entry->tag == NULL) {
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001058 size_t tagLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001059
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001060 tagLen = snprintf(messageBuf, messageBufLen, "[%" PRIu32 "]", tagIndex);
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001061 if (tagLen >= (size_t)messageBufLen) {
1062 tagLen = messageBufLen - 1;
1063 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001064 entry->tag = messageBuf;
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001065 entry->tagLen = tagLen;
1066 messageBuf += tagLen + 1;
1067 messageBufLen -= tagLen + 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001068 }
1069
1070 /*
1071 * Format the event log data into the buffer.
1072 */
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001073 const char* fmtStr = NULL;
1074 size_t fmtLen = 0;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001075#ifdef __ANDROID__
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001076 if (descriptive_output && map) {
1077 fmtStr = android_lookupEventFormat_len(map, &fmtLen, tagIndex);
1078 }
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001079#endif
Mark Salyzyn1a57ae32016-11-11 14:41:30 -08001080
1081 char* outBuf = messageBuf;
1082 size_t outRemaining = messageBufLen - 1; /* leave one for nul byte */
1083 int result = 0;
1084
1085 if ((inCount > 0) || fmtLen) {
1086 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf,
1087 &outRemaining, &fmtStr, &fmtLen);
1088 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -07001089 if ((result == 1) && fmtStr) {
1090 /* We overflowed :-(, let's repaint the line w/o format dressings */
1091 eventData = (const unsigned char*)buf->msg;
1092 if (buf2->hdr_size) {
1093 eventData = ((unsigned char *)buf2) + buf2->hdr_size;
1094 }
1095 eventData += 4;
1096 outBuf = messageBuf;
1097 outRemaining = messageBufLen - 1;
1098 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf,
1099 &outRemaining, NULL, NULL);
1100 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001101 if (result < 0) {
1102 fprintf(stderr, "Binary log entry conversion failed\n");
Mark Salyzyn1a57ae32016-11-11 14:41:30 -08001103 }
1104 if (result) {
1105 if (!outRemaining) {
1106 /* make space to leave an indicator */
1107 --outBuf;
1108 ++outRemaining;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001109 }
Mark Salyzyn1a57ae32016-11-11 14:41:30 -08001110 *outBuf++ = (result < 0) ? '!' : '^'; /* Error or Truncation? */
1111 outRemaining--;
1112 /* pretend we ate all the data to prevent log stutter */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001113 inCount = 0;
Mark Salyzyn538bc122016-11-16 15:28:31 -08001114 if (result > 0) result = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001115 }
1116
1117 /* eat the silly terminating '\n' */
1118 if (inCount == 1 && *eventData == '\n') {
1119 eventData++;
1120 inCount--;
1121 }
1122
1123 if (inCount != 0) {
1124 fprintf(stderr,
Andrew Hsiehd2c8f522012-02-27 16:48:18 -08001125 "Warning: leftover binary log data (%zu bytes)\n", inCount);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001126 }
1127
1128 /*
1129 * Terminate the buffer. The NUL byte does not count as part of
1130 * entry->messageLen.
1131 */
1132 *outBuf = '\0';
1133 entry->messageLen = outBuf - messageBuf;
Mark Salyzynb1d150b2017-03-02 07:47:21 -08001134 assert(entry->messageLen == (messageBufLen - 1) - outRemaining);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001135
1136 entry->message = messageBuf;
1137
Mark Salyzyn538bc122016-11-16 15:28:31 -08001138 return result;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001139}
1140
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001141/*
1142 * One utf8 character at a time
1143 *
1144 * Returns the length of the utf8 character in the buffer,
1145 * or -1 if illegal or truncated
1146 *
1147 * Open coded from libutils/Unicode.cpp, borrowed from utf8_length(),
1148 * can not remove from here because of library circular dependencies.
1149 * Expect one-day utf8_character_length with the same signature could
1150 * _also_ be part of libutils/Unicode.cpp if its usefullness needs to
1151 * propagate globally.
1152 */
Mark Salyzynbe1d3c22016-03-10 08:25:33 -08001153LIBLOG_WEAK ssize_t utf8_character_length(const char *src, size_t len)
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001154{
1155 const char *cur = src;
1156 const char first_char = *cur++;
1157 static const uint32_t kUnicodeMaxCodepoint = 0x0010FFFF;
1158 int32_t mask, to_ignore_mask;
1159 size_t num_to_read;
1160 uint32_t utf32;
1161
1162 if ((first_char & 0x80) == 0) { /* ASCII */
Mark Salyzynfaa92e92015-09-08 07:57:27 -07001163 return first_char ? 1 : -1;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001164 }
1165
1166 /*
1167 * (UTF-8's character must not be like 10xxxxxx,
1168 * but 110xxxxx, 1110xxxx, ... or 1111110x)
1169 */
1170 if ((first_char & 0x40) == 0) {
1171 return -1;
1172 }
1173
1174 for (utf32 = 1, num_to_read = 1, mask = 0x40, to_ignore_mask = 0x80;
1175 num_to_read < 5 && (first_char & mask);
1176 num_to_read++, to_ignore_mask |= mask, mask >>= 1) {
1177 if (num_to_read > len) {
1178 return -1;
1179 }
1180 if ((*cur & 0xC0) != 0x80) { /* can not be 10xxxxxx? */
1181 return -1;
1182 }
1183 utf32 = (utf32 << 6) + (*cur++ & 0b00111111);
1184 }
1185 /* "first_char" must be (110xxxxx - 11110xxx) */
1186 if (num_to_read >= 5) {
1187 return -1;
1188 }
1189 to_ignore_mask |= mask;
1190 utf32 |= ((~to_ignore_mask) & first_char) << (6 * (num_to_read - 1));
1191 if (utf32 > kUnicodeMaxCodepoint) {
1192 return -1;
1193 }
1194 return num_to_read;
1195}
1196
1197/*
1198 * Convert to printable from message to p buffer, return string length. If p is
1199 * NULL, do not copy, but still return the expected string length.
1200 */
1201static size_t convertPrintable(char *p, const char *message, size_t messageLen)
1202{
1203 char *begin = p;
1204 bool print = p != NULL;
1205
1206 while (messageLen) {
1207 char buf[6];
1208 ssize_t len = sizeof(buf) - 1;
1209 if ((size_t)len > messageLen) {
1210 len = messageLen;
1211 }
1212 len = utf8_character_length(message, len);
1213
1214 if (len < 0) {
1215 snprintf(buf, sizeof(buf),
1216 ((messageLen > 1) && isdigit(message[1]))
1217 ? "\\%03o"
1218 : "\\%o",
1219 *message & 0377);
1220 len = 1;
1221 } else {
1222 buf[0] = '\0';
1223 if (len == 1) {
1224 if (*message == '\a') {
1225 strcpy(buf, "\\a");
1226 } else if (*message == '\b') {
1227 strcpy(buf, "\\b");
1228 } else if (*message == '\t') {
Mark Salyzynb1d150b2017-03-02 07:47:21 -08001229 strcpy(buf, "\t"); /* Do not escape tabs */
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001230 } else if (*message == '\v') {
1231 strcpy(buf, "\\v");
1232 } else if (*message == '\f') {
1233 strcpy(buf, "\\f");
1234 } else if (*message == '\r') {
1235 strcpy(buf, "\\r");
1236 } else if (*message == '\\') {
1237 strcpy(buf, "\\\\");
1238 } else if ((*message < ' ') || (*message & 0x80)) {
1239 snprintf(buf, sizeof(buf), "\\%o", *message & 0377);
1240 }
1241 }
1242 if (!buf[0]) {
1243 strncpy(buf, message, len);
1244 buf[len] = '\0';
1245 }
1246 }
1247 if (print) {
1248 strcpy(p, buf);
1249 }
1250 p += strlen(buf);
1251 message += len;
1252 messageLen -= len;
1253 }
1254 return p - begin;
1255}
1256
Mark Salyzynbe1d3c22016-03-10 08:25:33 -08001257static char *readSeconds(char *e, struct timespec *t)
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001258{
1259 unsigned long multiplier;
1260 char *p;
1261 t->tv_sec = strtoul(e, &p, 10);
1262 if (*p != '.') {
1263 return NULL;
1264 }
1265 t->tv_nsec = 0;
1266 multiplier = NS_PER_SEC;
1267 while (isdigit(*++p) && (multiplier /= 10)) {
1268 t->tv_nsec += (*p - '0') * multiplier;
1269 }
1270 return p;
1271}
1272
1273static struct timespec *sumTimespec(struct timespec *left,
1274 struct timespec *right)
1275{
1276 left->tv_nsec += right->tv_nsec;
1277 left->tv_sec += right->tv_sec;
1278 if (left->tv_nsec >= (long)NS_PER_SEC) {
1279 left->tv_nsec -= NS_PER_SEC;
1280 left->tv_sec += 1;
1281 }
1282 return left;
1283}
1284
1285static struct timespec *subTimespec(struct timespec *result,
1286 struct timespec *left,
1287 struct timespec *right)
1288{
1289 result->tv_nsec = left->tv_nsec - right->tv_nsec;
1290 result->tv_sec = left->tv_sec - right->tv_sec;
1291 if (result->tv_nsec < 0) {
1292 result->tv_nsec += NS_PER_SEC;
1293 result->tv_sec -= 1;
1294 }
1295 return result;
1296}
1297
1298static long long nsecTimespec(struct timespec *now)
1299{
1300 return (long long)now->tv_sec * NS_PER_SEC + now->tv_nsec;
1301}
1302
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001303#ifdef __ANDROID__
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001304static void convertMonotonic(struct timespec *result,
1305 const AndroidLogEntry *entry)
1306{
1307 struct listnode *node;
1308 struct conversionList {
1309 struct listnode node; /* first */
1310 struct timespec time;
1311 struct timespec convert;
1312 } *list, *next;
1313 struct timespec time, convert;
1314
1315 /* If we do not have a conversion list, build one up */
1316 if (list_empty(&convertHead)) {
1317 bool suspended_pending = false;
1318 struct timespec suspended_monotonic = { 0, 0 };
1319 struct timespec suspended_diff = { 0, 0 };
1320
1321 /*
1322 * Read dmesg for _some_ synchronization markers and insert
1323 * Anything in the Android Logger before the dmesg logging span will
1324 * be highly suspect regarding the monotonic time calculations.
1325 */
Mark Salyzyn78786da2016-04-28 16:06:24 -07001326 FILE *p = popen("/system/bin/dmesg", "re");
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001327 if (p) {
1328 char *line = NULL;
1329 size_t len = 0;
1330 while (getline(&line, &len, p) > 0) {
1331 static const char suspend[] = "PM: suspend entry ";
1332 static const char resume[] = "PM: suspend exit ";
1333 static const char healthd[] = "healthd";
1334 static const char battery[] = ": battery ";
1335 static const char suspended[] = "Suspended for ";
1336 struct timespec monotonic;
1337 struct tm tm;
1338 char *cp, *e = line;
1339 bool add_entry = true;
1340
1341 if (*e == '<') {
1342 while (*e && (*e != '>')) {
1343 ++e;
1344 }
1345 if (*e != '>') {
1346 continue;
1347 }
1348 }
1349 if (*e != '[') {
1350 continue;
1351 }
1352 while (*++e == ' ') {
1353 ;
1354 }
1355 e = readSeconds(e, &monotonic);
1356 if (!e || (*e != ']')) {
1357 continue;
1358 }
1359
1360 if ((e = strstr(e, suspend))) {
1361 e += sizeof(suspend) - 1;
1362 } else if ((e = strstr(line, resume))) {
1363 e += sizeof(resume) - 1;
1364 } else if (((e = strstr(line, healthd)))
1365 && ((e = strstr(e + sizeof(healthd) - 1, battery)))) {
1366 /* NB: healthd is roughly 150us late, worth the price to
1367 * deal with ntp-induced or hardware clock drift. */
1368 e += sizeof(battery) - 1;
1369 } else if ((e = strstr(line, suspended))) {
1370 e += sizeof(suspended) - 1;
1371 e = readSeconds(e, &time);
1372 if (!e) {
1373 continue;
1374 }
1375 add_entry = false;
1376 suspended_pending = true;
1377 suspended_monotonic = monotonic;
1378 suspended_diff = time;
1379 } else {
1380 continue;
1381 }
1382 if (add_entry) {
1383 /* look for "????-??-?? ??:??:??.????????? UTC" */
1384 cp = strstr(e, " UTC");
1385 if (!cp || ((cp - e) < 29) || (cp[-10] != '.')) {
1386 continue;
1387 }
1388 e = cp - 29;
1389 cp = readSeconds(cp - 10, &time);
1390 if (!cp) {
1391 continue;
1392 }
1393 cp = strptime(e, "%Y-%m-%d %H:%M:%S.", &tm);
1394 if (!cp) {
1395 continue;
1396 }
1397 cp = getenv(tz);
1398 if (cp) {
1399 cp = strdup(cp);
1400 }
1401 setenv(tz, utc, 1);
1402 time.tv_sec = mktime(&tm);
1403 if (cp) {
1404 setenv(tz, cp, 1);
1405 free(cp);
1406 } else {
1407 unsetenv(tz);
1408 }
1409 list = calloc(1, sizeof(struct conversionList));
1410 list_init(&list->node);
1411 list->time = time;
1412 subTimespec(&list->convert, &time, &monotonic);
1413 list_add_tail(&convertHead, &list->node);
1414 }
1415 if (suspended_pending && !list_empty(&convertHead)) {
1416 list = node_to_item(list_tail(&convertHead),
1417 struct conversionList, node);
1418 if (subTimespec(&time,
1419 subTimespec(&time,
1420 &list->time,
1421 &list->convert),
1422 &suspended_monotonic)->tv_sec > 0) {
1423 /* resume, what is convert factor before? */
1424 subTimespec(&convert, &list->convert, &suspended_diff);
1425 } else {
1426 /* suspend */
1427 convert = list->convert;
1428 }
1429 time = suspended_monotonic;
1430 sumTimespec(&time, &convert);
1431 /* breakpoint just before sleep */
1432 list = calloc(1, sizeof(struct conversionList));
1433 list_init(&list->node);
1434 list->time = time;
1435 list->convert = convert;
1436 list_add_tail(&convertHead, &list->node);
1437 /* breakpoint just after sleep */
1438 list = calloc(1, sizeof(struct conversionList));
1439 list_init(&list->node);
1440 list->time = time;
1441 sumTimespec(&list->time, &suspended_diff);
1442 list->convert = convert;
1443 sumTimespec(&list->convert, &suspended_diff);
1444 list_add_tail(&convertHead, &list->node);
1445 suspended_pending = false;
1446 }
1447 }
1448 pclose(p);
1449 }
1450 /* last entry is our current time conversion */
1451 list = calloc(1, sizeof(struct conversionList));
1452 list_init(&list->node);
1453 clock_gettime(CLOCK_REALTIME, &list->time);
1454 clock_gettime(CLOCK_MONOTONIC, &convert);
1455 clock_gettime(CLOCK_MONOTONIC, &time);
1456 /* Correct for instant clock_gettime latency (syscall or ~30ns) */
1457 subTimespec(&time, &convert, subTimespec(&time, &time, &convert));
1458 /* Calculate conversion factor */
1459 subTimespec(&list->convert, &list->time, &time);
1460 list_add_tail(&convertHead, &list->node);
1461 if (suspended_pending) {
1462 /* manufacture a suspend @ point before */
1463 subTimespec(&convert, &list->convert, &suspended_diff);
1464 time = suspended_monotonic;
1465 sumTimespec(&time, &convert);
1466 /* breakpoint just after sleep */
1467 list = calloc(1, sizeof(struct conversionList));
1468 list_init(&list->node);
1469 list->time = time;
1470 sumTimespec(&list->time, &suspended_diff);
1471 list->convert = convert;
1472 sumTimespec(&list->convert, &suspended_diff);
1473 list_add_head(&convertHead, &list->node);
1474 /* breakpoint just before sleep */
1475 list = calloc(1, sizeof(struct conversionList));
1476 list_init(&list->node);
1477 list->time = time;
1478 list->convert = convert;
1479 list_add_head(&convertHead, &list->node);
1480 }
1481 }
1482
1483 /* Find the breakpoint in the conversion list */
1484 list = node_to_item(list_head(&convertHead), struct conversionList, node);
1485 next = NULL;
1486 list_for_each(node, &convertHead) {
1487 next = node_to_item(node, struct conversionList, node);
1488 if (entry->tv_sec < next->time.tv_sec) {
1489 break;
1490 } else if (entry->tv_sec == next->time.tv_sec) {
1491 if (entry->tv_nsec < next->time.tv_nsec) {
1492 break;
1493 }
1494 }
1495 list = next;
1496 }
1497
1498 /* blend time from one breakpoint to the next */
1499 convert = list->convert;
1500 if (next) {
1501 unsigned long long total, run;
1502
1503 total = nsecTimespec(subTimespec(&time, &next->time, &list->time));
1504 time.tv_sec = entry->tv_sec;
1505 time.tv_nsec = entry->tv_nsec;
1506 run = nsecTimespec(subTimespec(&time, &time, &list->time));
1507 if (run < total) {
1508 long long crun;
1509
1510 float f = nsecTimespec(subTimespec(&time, &next->convert, &convert));
1511 f *= run;
1512 f /= total;
1513 crun = f;
1514 convert.tv_sec += crun / (long long)NS_PER_SEC;
1515 if (crun < 0) {
1516 convert.tv_nsec -= (-crun) % NS_PER_SEC;
1517 if (convert.tv_nsec < 0) {
1518 convert.tv_nsec += NS_PER_SEC;
1519 convert.tv_sec -= 1;
1520 }
1521 } else {
1522 convert.tv_nsec += crun % NS_PER_SEC;
1523 if (convert.tv_nsec >= (long)NS_PER_SEC) {
1524 convert.tv_nsec -= NS_PER_SEC;
1525 convert.tv_sec += 1;
1526 }
1527 }
1528 }
1529 }
1530
1531 /* Apply the correction factor */
1532 result->tv_sec = entry->tv_sec;
1533 result->tv_nsec = entry->tv_nsec;
1534 subTimespec(result, result, &convert);
1535}
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001536#endif
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001537
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001538/**
1539 * Formats a log message into a buffer
1540 *
1541 * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer
1542 * If return value != defaultBuffer, caller must call free()
1543 * Returns NULL on malloc error
1544 */
1545
Mark Salyzynbe1d3c22016-03-10 08:25:33 -08001546LIBLOG_ABI_PUBLIC char *android_log_formatLogLine (
1547 AndroidLogFormat *p_format,
1548 char *defaultBuffer,
1549 size_t defaultBufferSize,
1550 const AndroidLogEntry *entry,
1551 size_t *p_outLength)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001552{
Yabin Cui8a985352014-11-13 10:02:08 -08001553#if !defined(_WIN32)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001554 struct tm tmBuf;
1555#endif
1556 struct tm* ptm;
Mark Salyzyn9b4d7e12017-01-30 09:16:09 -08001557 /* good margin, 23+nul for msec, 26+nul for usec, 29+nul to nsec */
1558 char timeBuf[64];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001559 char prefixBuf[128], suffixBuf[128];
1560 char priChar;
1561 int prefixSuffixIsHeaderFooter = 0;
Mark Salyzyn90e7af32015-12-07 16:52:42 -08001562 char *ret;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001563 time_t now;
1564 unsigned long nsec;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001565
1566 priChar = filterPriToChar(entry->priority);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001567 size_t prefixLen = 0, suffixLen = 0;
1568 size_t len;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001569
1570 /*
1571 * Get the current date/time in pretty form
1572 *
1573 * It's often useful when examining a log with "less" to jump to
1574 * a specific point in the file by searching for the date/time stamp.
1575 * For this reason it's very annoying to have regexp meta characters
1576 * in the time stamp. Don't use forward slashes, parenthesis,
1577 * brackets, asterisks, or other special chars here.
Mark Salyzynf28f6a92015-08-31 08:01:33 -07001578 *
1579 * The caller may have affected the timezone environment, this is
1580 * expected to be sensitive to that.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001581 */
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001582 now = entry->tv_sec;
1583 nsec = entry->tv_nsec;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001584#if __ANDROID__
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001585 if (p_format->monotonic_output) {
Mark Salyzynb1d150b2017-03-02 07:47:21 -08001586 /* prevent convertMonotonic from being called if logd is monotonic */
Mark Salyzynba7a9a02015-12-01 15:57:25 -08001587 if (android_log_clockid() != CLOCK_MONOTONIC) {
Mark Salyzynb6bee332015-09-08 08:56:32 -07001588 struct timespec time;
1589 convertMonotonic(&time, entry);
1590 now = time.tv_sec;
1591 nsec = time.tv_nsec;
1592 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001593 }
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001594#endif
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001595 if (now < 0) {
1596 nsec = NS_PER_SEC - nsec;
1597 }
1598 if (p_format->epoch_output || p_format->monotonic_output) {
1599 ptm = NULL;
1600 snprintf(timeBuf, sizeof(timeBuf),
1601 p_format->monotonic_output ? "%6lld" : "%19lld",
1602 (long long)now);
1603 } else {
Yabin Cui8a985352014-11-13 10:02:08 -08001604#if !defined(_WIN32)
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001605 ptm = localtime_r(&now, &tmBuf);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001606#else
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001607 ptm = localtime(&now);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001608#endif
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001609 strftime(timeBuf, sizeof(timeBuf),
1610 &"%Y-%m-%d %H:%M:%S"[p_format->year_output ? 0 : 3],
1611 ptm);
1612 }
Mark Salyzyne1f20042015-05-06 08:40:40 -07001613 len = strlen(timeBuf);
Mark Salyzyn9b4d7e12017-01-30 09:16:09 -08001614 if (p_format->nsec_time_output) {
1615 len += snprintf(timeBuf + len, sizeof(timeBuf) - len,
1616 ".%09ld", nsec);
1617 } else if (p_format->usec_time_output) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -07001618 len += snprintf(timeBuf + len, sizeof(timeBuf) - len,
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001619 ".%06ld", nsec / US_PER_NSEC);
Mark Salyzyne1f20042015-05-06 08:40:40 -07001620 } else {
Mark Salyzynf28f6a92015-08-31 08:01:33 -07001621 len += snprintf(timeBuf + len, sizeof(timeBuf) - len,
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001622 ".%03ld", nsec / MS_PER_NSEC);
Mark Salyzynf28f6a92015-08-31 08:01:33 -07001623 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001624 if (p_format->zone_output && ptm) {
Mark Salyzynf28f6a92015-08-31 08:01:33 -07001625 strftime(timeBuf + len, sizeof(timeBuf) - len, " %z", ptm);
Mark Salyzyne1f20042015-05-06 08:40:40 -07001626 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001627
1628 /*
1629 * Construct a buffer containing the log header and log message.
1630 */
Pierre Zurekead88fc2010-10-17 22:39:37 +02001631 if (p_format->colored_output) {
1632 prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm",
1633 colorFromPri(entry->priority));
1634 prefixLen = MIN(prefixLen, sizeof(prefixBuf));
1635 suffixLen = snprintf(suffixBuf, sizeof(suffixBuf), "\x1B[0m");
1636 suffixLen = MIN(suffixLen, sizeof(suffixBuf));
1637 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001638
Mark Salyzyn90e7af32015-12-07 16:52:42 -08001639 char uid[16];
1640 uid[0] = '\0';
1641 if (p_format->uid_output) {
1642 if (entry->uid >= 0) {
Mark Salyzyn2aa510e2015-12-08 09:15:06 -08001643
William Roberts8a5b9ca2016-04-08 12:13:17 -07001644 /*
1645 * This code is Android specific, bionic guarantees that
1646 * calls to non-reentrant getpwuid() are thread safe.
1647 */
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001648#if !defined(__MINGW32__)
1649#if (FAKE_LOG_DEVICE == 0)
William Roberts8a5b9ca2016-04-08 12:13:17 -07001650#ifndef __BIONIC__
1651#warning "This code assumes that getpwuid is thread safe, only true with Bionic!"
1652#endif
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001653#endif
William Roberts8a5b9ca2016-04-08 12:13:17 -07001654 struct passwd* pwd = getpwuid(entry->uid);
1655 if (pwd && (strlen(pwd->pw_name) <= 5)) {
1656 snprintf(uid, sizeof(uid), "%5s:", pwd->pw_name);
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001657 } else
1658#endif
1659 {
Mark Salyzynb1d150b2017-03-02 07:47:21 -08001660 /* Not worth parsing package list, names all longer than 5 */
Mark Salyzyn2aa510e2015-12-08 09:15:06 -08001661 snprintf(uid, sizeof(uid), "%5d:", entry->uid);
1662 }
Mark Salyzyn90e7af32015-12-07 16:52:42 -08001663 } else {
1664 snprintf(uid, sizeof(uid), " ");
1665 }
1666 }
1667
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001668 switch (p_format->format) {
1669 case FORMAT_TAG:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001670 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001671 "%c/%-8.*s: ", priChar, (int)entry->tagLen, entry->tag);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001672 strcpy(suffixBuf + suffixLen, "\n");
1673 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001674 break;
1675 case FORMAT_PROCESS:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001676 len = snprintf(suffixBuf + suffixLen, sizeof(suffixBuf) - suffixLen,
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001677 " (%.*s)\n", (int)entry->tagLen, entry->tag);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001678 suffixLen += MIN(len, sizeof(suffixBuf) - suffixLen);
1679 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyn90e7af32015-12-07 16:52:42 -08001680 "%c(%s%5d) ", priChar, uid, entry->pid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001681 break;
1682 case FORMAT_THREAD:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001683 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyn90e7af32015-12-07 16:52:42 -08001684 "%c(%s%5d:%5d) ", priChar, uid, entry->pid, entry->tid);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001685 strcpy(suffixBuf + suffixLen, "\n");
1686 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001687 break;
1688 case FORMAT_RAW:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001689 prefixBuf[prefixLen] = 0;
1690 len = 0;
1691 strcpy(suffixBuf + suffixLen, "\n");
1692 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001693 break;
1694 case FORMAT_TIME:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001695 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001696 "%s %c/%-8.*s(%s%5d): ", timeBuf, priChar,
1697 (int)entry->tagLen, entry->tag, uid, entry->pid);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001698 strcpy(suffixBuf + suffixLen, "\n");
1699 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001700 break;
1701 case FORMAT_THREADTIME:
Mark Salyzyn90e7af32015-12-07 16:52:42 -08001702 ret = strchr(uid, ':');
1703 if (ret) {
1704 *ret = ' ';
1705 }
Pierre Zurekead88fc2010-10-17 22:39:37 +02001706 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001707 "%s %s%5d %5d %c %-8.*s: ", timeBuf, uid, entry->pid,
1708 entry->tid, priChar, (int)entry->tagLen, entry->tag);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001709 strcpy(suffixBuf + suffixLen, "\n");
1710 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001711 break;
1712 case FORMAT_LONG:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001713 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001714 "[ %s %s%5d:%5d %c/%-8.*s ]\n",
1715 timeBuf, uid, entry->pid, entry->tid, priChar,
1716 (int)entry->tagLen, entry->tag);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001717 strcpy(suffixBuf + suffixLen, "\n\n");
1718 suffixLen += 2;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001719 prefixSuffixIsHeaderFooter = 1;
1720 break;
1721 case FORMAT_BRIEF:
1722 default:
Pierre Zurekead88fc2010-10-17 22:39:37 +02001723 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Mark Salyzyn807e40e2016-09-22 09:56:51 -07001724 "%c/%-8.*s(%s%5d): ", priChar, (int)entry->tagLen, entry->tag,
1725 uid, entry->pid);
Pierre Zurekead88fc2010-10-17 22:39:37 +02001726 strcpy(suffixBuf + suffixLen, "\n");
1727 ++suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001728 break;
1729 }
Pierre Zurekead88fc2010-10-17 22:39:37 +02001730
Keith Prestonb45b5c92010-02-11 15:12:53 -06001731 /* snprintf has a weird return value. It returns what would have been
1732 * written given a large enough buffer. In the case that the prefix is
1733 * longer then our buffer(128), it messes up the calculations below
1734 * possibly causing heap corruption. To avoid this we double check and
1735 * set the length at the maximum (size minus null byte)
1736 */
Mark Salyzyn2f83d672016-03-11 12:06:12 -08001737 prefixLen += len;
1738 if (prefixLen >= sizeof(prefixBuf)) {
1739 prefixLen = sizeof(prefixBuf) - 1;
1740 prefixBuf[sizeof(prefixBuf) - 1] = '\0';
1741 }
1742 if (suffixLen >= sizeof(suffixBuf)) {
1743 suffixLen = sizeof(suffixBuf) - 1;
1744 suffixBuf[sizeof(suffixBuf) - 2] = '\n';
1745 suffixBuf[sizeof(suffixBuf) - 1] = '\0';
1746 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001747
1748 /* the following code is tragically unreadable */
1749
1750 size_t numLines;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001751 char *p;
1752 size_t bufferSize;
1753 const char *pm;
1754
1755 if (prefixSuffixIsHeaderFooter) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001756 /* we're just wrapping message with a header/footer */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001757 numLines = 1;
1758 } else {
1759 pm = entry->message;
1760 numLines = 0;
1761
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001762 /*
1763 * The line-end finding here must match the line-end finding
1764 * in for ( ... numLines...) loop below
1765 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001766 while (pm < (entry->message + entry->messageLen)) {
1767 if (*pm++ == '\n') numLines++;
1768 }
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001769 /* plus one line for anything not newline-terminated at the end */
Mark Salyzynb1d150b2017-03-02 07:47:21 -08001770 if (pm > entry->message && *(pm - 1) != '\n') numLines++;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001771 }
1772
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001773 /*
1774 * this is an upper bound--newlines in message may be counted
1775 * extraneously
1776 */
1777 bufferSize = (numLines * (prefixLen + suffixLen)) + 1;
1778 if (p_format->printable_output) {
1779 /* Calculate extra length to convert non-printable to printable */
1780 bufferSize += convertPrintable(NULL, entry->message, entry->messageLen);
1781 } else {
1782 bufferSize += entry->messageLen;
1783 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001784
1785 if (defaultBufferSize >= bufferSize) {
1786 ret = defaultBuffer;
1787 } else {
1788 ret = (char *)malloc(bufferSize);
1789
1790 if (ret == NULL) {
1791 return ret;
1792 }
1793 }
1794
1795 ret[0] = '\0'; /* to start strcat off */
1796
1797 p = ret;
1798 pm = entry->message;
1799
1800 if (prefixSuffixIsHeaderFooter) {
1801 strcat(p, prefixBuf);
1802 p += prefixLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001803 if (p_format->printable_output) {
1804 p += convertPrintable(p, entry->message, entry->messageLen);
1805 } else {
1806 strncat(p, entry->message, entry->messageLen);
1807 p += entry->messageLen;
1808 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001809 strcat(p, suffixBuf);
1810 p += suffixLen;
1811 } else {
Mark Salyzyn7cc80132016-01-20 13:52:46 -08001812 do {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001813 const char *lineStart;
1814 size_t lineLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001815 lineStart = pm;
1816
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001817 /* Find the next end-of-line in message */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001818 while (pm < (entry->message + entry->messageLen)
1819 && *pm != '\n') pm++;
1820 lineLen = pm - lineStart;
1821
1822 strcat(p, prefixBuf);
1823 p += prefixLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001824 if (p_format->printable_output) {
1825 p += convertPrintable(p, lineStart, lineLen);
1826 } else {
1827 strncat(p, lineStart, lineLen);
1828 p += lineLen;
1829 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001830 strcat(p, suffixBuf);
1831 p += suffixLen;
1832
1833 if (*pm == '\n') pm++;
Mark Salyzyn7cc80132016-01-20 13:52:46 -08001834 } while (pm < (entry->message + entry->messageLen));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001835 }
1836
1837 if (p_outLength != NULL) {
1838 *p_outLength = p - ret;
1839 }
1840
1841 return ret;
1842}
1843
1844/**
1845 * Either print or do not print log line, based on filter
1846 *
1847 * Returns count bytes written
1848 */
1849
Mark Salyzynbe1d3c22016-03-10 08:25:33 -08001850LIBLOG_ABI_PUBLIC int android_log_printLogLine(
1851 AndroidLogFormat *p_format,
1852 int fd,
1853 const AndroidLogEntry *entry)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001854{
1855 int ret;
1856 char defaultBuffer[512];
1857 char *outBuffer = NULL;
1858 size_t totalLen;
1859
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001860 outBuffer = android_log_formatLogLine(p_format, defaultBuffer,
1861 sizeof(defaultBuffer), entry, &totalLen);
1862
Mark Salyzyn1a57ae32016-11-11 14:41:30 -08001863 if (!outBuffer) return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001864
1865 do {
1866 ret = write(fd, outBuffer, totalLen);
1867 } while (ret < 0 && errno == EINTR);
1868
1869 if (ret < 0) {
1870 fprintf(stderr, "+++ LOG: write failed (errno=%d)\n", errno);
1871 ret = 0;
1872 goto done;
1873 }
1874
1875 if (((size_t)ret) < totalLen) {
1876 fprintf(stderr, "+++ LOG: write partial (%d of %d)\n", ret,
1877 (int)totalLen);
1878 goto done;
1879 }
1880
1881done:
1882 if (outBuffer != defaultBuffer) {
1883 free(outBuffer);
1884 }
1885
1886 return ret;
1887}