blob: 798b0890971515b0b47ee892a56b3fb2d9c16bd8 [file] [log] [blame]
Mark Salyzync73e2b12013-11-22 07:54:30 -08001/*
The Android Open Source Project32315d42008-10-21 07:00:00 -07002**
Mark Salyzyn64bf6682013-12-18 12:59:01 -08003** Copyright 2006-2014, The Android Open Source Project
The Android Open Source Project32315d42008-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
Mark Salyzyn7ad91922016-03-08 16:18:26 -080018#ifndef __MINGW32__
19#define HAVE_STRSEP
20#endif
The Android Open Source Project32315d42008-10-21 07:00:00 -070021
Mark Salyzyn3a5219e2014-04-30 08:50:53 -070022#include <assert.h>
23#include <ctype.h>
24#include <errno.h>
Mark Salyzyn604679a2016-10-18 11:30:11 -070025#include <inttypes.h>
Mark Salyzyn7ad91922016-03-08 16:18:26 -080026#ifndef __MINGW32__
William Roberts6877c6c2016-04-08 12:13:17 -070027#include <pwd.h>
Mark Salyzyn7ad91922016-03-08 16:18:26 -080028#endif
Pierre Zurek507ea562010-10-17 22:39:37 +020029#include <stdbool.h>
Mark Salyzyn3a5219e2014-04-30 08:50:53 -070030#include <stdint.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
Pierre Zurek507ea562010-10-17 22:39:37 +020034#include <sys/param.h>
William Roberts6877c6c2016-04-08 12:13:17 -070035#include <sys/types.h>
The Android Open Source Project32315d42008-10-21 07:00:00 -070036
Mark Salyzyn4d0473f2015-08-31 15:53:41 -070037#include <cutils/list.h>
Mark Salyzyn8b8bfd22016-09-30 13:30:33 -070038#include <log/log.h>
Colin Crosse355ded2013-07-23 16:59:20 -070039#include <log/logprint.h>
The Android Open Source Project32315d42008-10-21 07:00:00 -070040
Mark Salyzyndd9094a2016-03-01 13:45:42 -080041#include "log_portability.h"
Mark Salyzyna5c22172016-03-10 08:25:33 -080042
Mark Salyzyn4d0473f2015-08-31 15:53:41 -070043#define MS_PER_NSEC 1000000
44#define US_PER_NSEC 1000
45
Mark Salyzyn7ad91922016-03-08 16:18:26 -080046#ifndef MIN
Mark Salyzyn6e315682017-03-09 08:09:43 -080047#define MIN(a, b) (((a) < (b)) ? (a) : (b))
Mark Salyzyn7ad91922016-03-08 16:18:26 -080048#endif
49
The Android Open Source Project32315d42008-10-21 07:00:00 -070050typedef struct FilterInfo_t {
Mark Salyzyn6e315682017-03-09 08:09:43 -080051 char* mTag;
52 android_LogPriority mPri;
53 struct FilterInfo_t* p_next;
The Android Open Source Project32315d42008-10-21 07:00:00 -070054} FilterInfo;
55
56struct AndroidLogFormat_t {
Mark Salyzyn6e315682017-03-09 08:09:43 -080057 android_LogPriority global_pri;
58 FilterInfo* filters;
59 AndroidLogPrintFormat format;
60 bool colored_output;
61 bool usec_time_output;
62 bool nsec_time_output;
63 bool printable_output;
64 bool year_output;
65 bool zone_output;
66 bool epoch_output;
67 bool monotonic_output;
68 bool uid_output;
69 bool descriptive_output;
The Android Open Source Project32315d42008-10-21 07:00:00 -070070};
71
Pierre Zurek507ea562010-10-17 22:39:37 +020072/*
Mark Salyzyn604679a2016-10-18 11:30:11 -070073 * API issues prevent us from exposing "descriptive" in AndroidLogFormat_t
74 * during android_log_processBinaryLogBuffer(), so we break layering.
75 */
76static bool descriptive_output = false;
77
78/*
Pierre Zurek507ea562010-10-17 22:39:37 +020079 * gnome-terminal color tags
80 * See http://misc.flogisoft.com/bash/tip_colors_and_formatting
81 * for ideas on how to set the forground color of the text for xterm.
82 * The color manipulation character stream is defined as:
83 * ESC [ 3 8 ; 5 ; <color#> m
84 */
Mark Salyzyn6e315682017-03-09 08:09:43 -080085#define ANDROID_COLOR_BLUE 75
Pierre Zurek507ea562010-10-17 22:39:37 +020086#define ANDROID_COLOR_DEFAULT 231
Mark Salyzyn6e315682017-03-09 08:09:43 -080087#define ANDROID_COLOR_GREEN 40
88#define ANDROID_COLOR_ORANGE 166
89#define ANDROID_COLOR_RED 196
90#define ANDROID_COLOR_YELLOW 226
Pierre Zurek507ea562010-10-17 22:39:37 +020091
Mark Salyzyn6e315682017-03-09 08:09:43 -080092static FilterInfo* filterinfo_new(const char* tag, android_LogPriority pri) {
93 FilterInfo* p_ret;
The Android Open Source Project32315d42008-10-21 07:00:00 -070094
Mark Salyzyn6e315682017-03-09 08:09:43 -080095 p_ret = (FilterInfo*)calloc(1, sizeof(FilterInfo));
96 p_ret->mTag = strdup(tag);
97 p_ret->mPri = pri;
The Android Open Source Project32315d42008-10-21 07:00:00 -070098
Mark Salyzyn6e315682017-03-09 08:09:43 -080099 return p_ret;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700100}
101
Mark Salyzyn3a5219e2014-04-30 08:50:53 -0700102/* balance to above, filterinfo_free left unimplemented */
The Android Open Source Project32315d42008-10-21 07:00:00 -0700103
104/*
105 * Note: also accepts 0-9 priorities
106 * returns ANDROID_LOG_UNKNOWN if the character is unrecognized
107 */
Mark Salyzyn6e315682017-03-09 08:09:43 -0800108static android_LogPriority filterCharToPri(char c) {
109 android_LogPriority pri;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700110
Mark Salyzyn6e315682017-03-09 08:09:43 -0800111 c = tolower(c);
The Android Open Source Project32315d42008-10-21 07:00:00 -0700112
Mark Salyzyn6e315682017-03-09 08:09:43 -0800113 if (c >= '0' && c <= '9') {
114 if (c >= ('0' + ANDROID_LOG_SILENT)) {
115 pri = ANDROID_LOG_VERBOSE;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700116 } else {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800117 pri = (android_LogPriority)(c - '0');
The Android Open Source Project32315d42008-10-21 07:00:00 -0700118 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800119 } else if (c == 'v') {
120 pri = ANDROID_LOG_VERBOSE;
121 } else if (c == 'd') {
122 pri = ANDROID_LOG_DEBUG;
123 } else if (c == 'i') {
124 pri = ANDROID_LOG_INFO;
125 } else if (c == 'w') {
126 pri = ANDROID_LOG_WARN;
127 } else if (c == 'e') {
128 pri = ANDROID_LOG_ERROR;
129 } else if (c == 'f') {
130 pri = ANDROID_LOG_FATAL;
131 } else if (c == 's') {
132 pri = ANDROID_LOG_SILENT;
133 } else if (c == '*') {
134 pri = ANDROID_LOG_DEFAULT;
135 } else {
136 pri = ANDROID_LOG_UNKNOWN;
137 }
The Android Open Source Project32315d42008-10-21 07:00:00 -0700138
Mark Salyzyn6e315682017-03-09 08:09:43 -0800139 return pri;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700140}
141
Mark Salyzyn6e315682017-03-09 08:09:43 -0800142static char filterPriToChar(android_LogPriority pri) {
143 switch (pri) {
144 /* clang-format off */
145 case ANDROID_LOG_VERBOSE: return 'V';
146 case ANDROID_LOG_DEBUG: return 'D';
147 case ANDROID_LOG_INFO: return 'I';
148 case ANDROID_LOG_WARN: return 'W';
149 case ANDROID_LOG_ERROR: return 'E';
150 case ANDROID_LOG_FATAL: return 'F';
151 case ANDROID_LOG_SILENT: return 'S';
The Android Open Source Project32315d42008-10-21 07:00:00 -0700152
Mark Salyzyn6e315682017-03-09 08:09:43 -0800153 case ANDROID_LOG_DEFAULT:
154 case ANDROID_LOG_UNKNOWN:
155 default: return '?';
Tom Cherryf623f022019-01-10 10:37:36 -0800156 /* clang-format on */
Mark Salyzyn6e315682017-03-09 08:09:43 -0800157 }
The Android Open Source Project32315d42008-10-21 07:00:00 -0700158}
159
Mark Salyzyn6e315682017-03-09 08:09:43 -0800160static int colorFromPri(android_LogPriority pri) {
161 switch (pri) {
162 /* clang-format off */
163 case ANDROID_LOG_VERBOSE: return ANDROID_COLOR_DEFAULT;
164 case ANDROID_LOG_DEBUG: return ANDROID_COLOR_BLUE;
165 case ANDROID_LOG_INFO: return ANDROID_COLOR_GREEN;
166 case ANDROID_LOG_WARN: return ANDROID_COLOR_ORANGE;
167 case ANDROID_LOG_ERROR: return ANDROID_COLOR_RED;
168 case ANDROID_LOG_FATAL: return ANDROID_COLOR_RED;
169 case ANDROID_LOG_SILENT: return ANDROID_COLOR_DEFAULT;
Pierre Zurek507ea562010-10-17 22:39:37 +0200170
Mark Salyzyn6e315682017-03-09 08:09:43 -0800171 case ANDROID_LOG_DEFAULT:
172 case ANDROID_LOG_UNKNOWN:
173 default: return ANDROID_COLOR_DEFAULT;
Tom Cherryf623f022019-01-10 10:37:36 -0800174 /* clang-format on */
Mark Salyzyn6e315682017-03-09 08:09:43 -0800175 }
Pierre Zurek507ea562010-10-17 22:39:37 +0200176}
177
Tom Cherryf623f022019-01-10 10:37:36 -0800178static android_LogPriority filterPriForTag(AndroidLogFormat* p_format, const char* tag) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800179 FilterInfo* p_curFilter;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700180
Tom Cherryf623f022019-01-10 10:37:36 -0800181 for (p_curFilter = p_format->filters; p_curFilter != NULL; p_curFilter = p_curFilter->p_next) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800182 if (0 == strcmp(tag, p_curFilter->mTag)) {
183 if (p_curFilter->mPri == ANDROID_LOG_DEFAULT) {
184 return p_format->global_pri;
185 } else {
186 return p_curFilter->mPri;
187 }
The Android Open Source Project32315d42008-10-21 07:00:00 -0700188 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800189 }
The Android Open Source Project32315d42008-10-21 07:00:00 -0700190
Mark Salyzyn6e315682017-03-09 08:09:43 -0800191 return p_format->global_pri;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700192}
193
The Android Open Source Project32315d42008-10-21 07:00:00 -0700194/**
195 * returns 1 if this log line should be printed based on its priority
196 * and tag, and 0 if it should not
197 */
Tom Cherryf623f022019-01-10 10:37:36 -0800198LIBLOG_ABI_PUBLIC int android_log_shouldPrintLine(AndroidLogFormat* p_format, const char* tag,
Mark Salyzyn6e315682017-03-09 08:09:43 -0800199 android_LogPriority pri) {
200 return pri >= filterPriForTag(p_format, tag);
The Android Open Source Project32315d42008-10-21 07:00:00 -0700201}
202
Mark Salyzyn6e315682017-03-09 08:09:43 -0800203LIBLOG_ABI_PUBLIC AndroidLogFormat* android_log_format_new() {
204 AndroidLogFormat* p_ret;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700205
Tom Cherryf623f022019-01-10 10:37:36 -0800206 p_ret = static_cast<AndroidLogFormat*>(calloc(1, sizeof(AndroidLogFormat)));
The Android Open Source Project32315d42008-10-21 07:00:00 -0700207
Mark Salyzyn6e315682017-03-09 08:09:43 -0800208 p_ret->global_pri = ANDROID_LOG_VERBOSE;
209 p_ret->format = FORMAT_BRIEF;
210 p_ret->colored_output = false;
211 p_ret->usec_time_output = false;
212 p_ret->nsec_time_output = false;
213 p_ret->printable_output = false;
214 p_ret->year_output = false;
215 p_ret->zone_output = false;
216 p_ret->epoch_output = false;
Mark Salyzyn7ad91922016-03-08 16:18:26 -0800217#ifdef __ANDROID__
Mark Salyzyn6e315682017-03-09 08:09:43 -0800218 p_ret->monotonic_output = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzyn7ad91922016-03-08 16:18:26 -0800219#else
Mark Salyzyn6e315682017-03-09 08:09:43 -0800220 p_ret->monotonic_output = false;
Mark Salyzyn7ad91922016-03-08 16:18:26 -0800221#endif
Mark Salyzyn6e315682017-03-09 08:09:43 -0800222 p_ret->uid_output = false;
223 p_ret->descriptive_output = false;
224 descriptive_output = false;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700225
Mark Salyzyn6e315682017-03-09 08:09:43 -0800226 return p_ret;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700227}
228
Mark Salyzyn4d0473f2015-08-31 15:53:41 -0700229static list_declare(convertHead);
230
Mark Salyzyn6e315682017-03-09 08:09:43 -0800231LIBLOG_ABI_PUBLIC void android_log_format_free(AndroidLogFormat* p_format) {
232 FilterInfo *p_info, *p_info_old;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700233
Mark Salyzyn6e315682017-03-09 08:09:43 -0800234 p_info = p_format->filters;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700235
Mark Salyzyn6e315682017-03-09 08:09:43 -0800236 while (p_info != NULL) {
237 p_info_old = p_info;
238 p_info = p_info->p_next;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700239
Mark Salyzyn6e315682017-03-09 08:09:43 -0800240 free(p_info_old);
241 }
The Android Open Source Project32315d42008-10-21 07:00:00 -0700242
Mark Salyzyn6e315682017-03-09 08:09:43 -0800243 free(p_format);
Mark Salyzyn4d0473f2015-08-31 15:53:41 -0700244
Mark Salyzyn6e315682017-03-09 08:09:43 -0800245 /* Free conversion resource, can always be reconstructed */
246 while (!list_empty(&convertHead)) {
247 struct listnode* node = list_head(&convertHead);
248 list_remove(node);
Ting-Yuan Huangca6822f2017-08-15 17:01:33 -0700249 LOG_ALWAYS_FATAL_IF(node == list_head(&convertHead), "corrupted list");
Mark Salyzyn6e315682017-03-09 08:09:43 -0800250 free(node);
251 }
The Android Open Source Project32315d42008-10-21 07:00:00 -0700252}
253
Mark Salyzyn6e315682017-03-09 08:09:43 -0800254LIBLOG_ABI_PUBLIC int android_log_setPrintFormat(AndroidLogFormat* p_format,
255 AndroidLogPrintFormat format) {
256 switch (format) {
Mark Salyzyn1ebdb352015-05-15 09:01:58 -0700257 case FORMAT_MODIFIER_COLOR:
Mark Salyzyn6e315682017-03-09 08:09:43 -0800258 p_format->colored_output = true;
259 return 0;
Mark Salyzyn1ebdb352015-05-15 09:01:58 -0700260 case FORMAT_MODIFIER_TIME_USEC:
Mark Salyzyn6e315682017-03-09 08:09:43 -0800261 p_format->usec_time_output = true;
262 return 0;
Mark Salyzynf0050852017-01-30 09:16:09 -0800263 case FORMAT_MODIFIER_TIME_NSEC:
Mark Salyzyn6e315682017-03-09 08:09:43 -0800264 p_format->nsec_time_output = true;
265 return 0;
Mark Salyzyn1ebdb352015-05-15 09:01:58 -0700266 case FORMAT_MODIFIER_PRINTABLE:
Mark Salyzyn6e315682017-03-09 08:09:43 -0800267 p_format->printable_output = true;
268 return 0;
Mark Salyzyneb0456d2015-08-31 08:01:33 -0700269 case FORMAT_MODIFIER_YEAR:
Mark Salyzyn6e315682017-03-09 08:09:43 -0800270 p_format->year_output = true;
271 return 0;
Mark Salyzyneb0456d2015-08-31 08:01:33 -0700272 case FORMAT_MODIFIER_ZONE:
Mark Salyzyn6e315682017-03-09 08:09:43 -0800273 p_format->zone_output = !p_format->zone_output;
274 return 0;
Mark Salyzyn4d0473f2015-08-31 15:53:41 -0700275 case FORMAT_MODIFIER_EPOCH:
Mark Salyzyn6e315682017-03-09 08:09:43 -0800276 p_format->epoch_output = true;
277 return 0;
Mark Salyzyn4d0473f2015-08-31 15:53:41 -0700278 case FORMAT_MODIFIER_MONOTONIC:
Mark Salyzyn6e315682017-03-09 08:09:43 -0800279 p_format->monotonic_output = true;
280 return 0;
Mark Salyzynb71da912015-12-07 16:52:42 -0800281 case FORMAT_MODIFIER_UID:
Mark Salyzyn6e315682017-03-09 08:09:43 -0800282 p_format->uid_output = true;
283 return 0;
Mark Salyzyn604679a2016-10-18 11:30:11 -0700284 case FORMAT_MODIFIER_DESCRIPT:
Mark Salyzyn6e315682017-03-09 08:09:43 -0800285 p_format->descriptive_output = true;
286 descriptive_output = true;
287 return 0;
Mark Salyzyn1ebdb352015-05-15 09:01:58 -0700288 default:
Mark Salyzyn6e315682017-03-09 08:09:43 -0800289 break;
290 }
291 p_format->format = format;
292 return 1;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700293}
294
Mark Salyzyneb0456d2015-08-31 08:01:33 -0700295static const char tz[] = "TZ";
296static const char utc[] = "UTC";
297
The Android Open Source Project32315d42008-10-21 07:00:00 -0700298/**
299 * Returns FORMAT_OFF on invalid string
300 */
Tom Cherryf623f022019-01-10 10:37:36 -0800301LIBLOG_ABI_PUBLIC AndroidLogPrintFormat android_log_formatFromString(const char* formatString) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800302 static AndroidLogPrintFormat format;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700303
Mark Salyzyn6e315682017-03-09 08:09:43 -0800304 /* clang-format off */
305 if (!strcmp(formatString, "brief")) format = FORMAT_BRIEF;
306 else if (!strcmp(formatString, "process")) format = FORMAT_PROCESS;
307 else if (!strcmp(formatString, "tag")) format = FORMAT_TAG;
308 else if (!strcmp(formatString, "thread")) format = FORMAT_THREAD;
309 else if (!strcmp(formatString, "raw")) format = FORMAT_RAW;
310 else if (!strcmp(formatString, "time")) format = FORMAT_TIME;
311 else if (!strcmp(formatString, "threadtime")) format = FORMAT_THREADTIME;
312 else if (!strcmp(formatString, "long")) format = FORMAT_LONG;
313 else if (!strcmp(formatString, "color")) format = FORMAT_MODIFIER_COLOR;
314 else if (!strcmp(formatString, "colour")) format = FORMAT_MODIFIER_COLOR;
315 else if (!strcmp(formatString, "usec")) format = FORMAT_MODIFIER_TIME_USEC;
316 else if (!strcmp(formatString, "nsec")) format = FORMAT_MODIFIER_TIME_NSEC;
317 else if (!strcmp(formatString, "printable")) format = FORMAT_MODIFIER_PRINTABLE;
318 else if (!strcmp(formatString, "year")) format = FORMAT_MODIFIER_YEAR;
319 else if (!strcmp(formatString, "zone")) format = FORMAT_MODIFIER_ZONE;
320 else if (!strcmp(formatString, "epoch")) format = FORMAT_MODIFIER_EPOCH;
321 else if (!strcmp(formatString, "monotonic")) format = FORMAT_MODIFIER_MONOTONIC;
322 else if (!strcmp(formatString, "uid")) format = FORMAT_MODIFIER_UID;
323 else if (!strcmp(formatString, "descriptive")) format = FORMAT_MODIFIER_DESCRIPT;
Tom Cherryf623f022019-01-10 10:37:36 -0800324 /* clang-format on */
Mark Salyzyn07ff2122016-11-10 10:24:44 -0800325
Mark Salyzyn7ad91922016-03-08 16:18:26 -0800326#ifndef __MINGW32__
Mark Salyzyn6e315682017-03-09 08:09:43 -0800327 else {
328 extern char* tzname[2];
329 static const char gmt[] = "GMT";
330 char* cp = getenv(tz);
331 if (cp) {
332 cp = strdup(cp);
Mark Salyzyneb0456d2015-08-31 08:01:33 -0700333 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800334 setenv(tz, formatString, 1);
335 /*
336 * Run tzset here to determine if the timezone is legitimate. If the
337 * zone is GMT, check if that is what was asked for, if not then
338 * did not match any on the system; report an error to caller.
339 */
340 tzset();
341 if (!tzname[0] ||
Tom Cherryf623f022019-01-10 10:37:36 -0800342 ((!strcmp(tzname[0], utc) || !strcmp(tzname[0], gmt)) /* error? */
343 && strcasecmp(formatString, utc) && strcasecmp(formatString, gmt))) { /* ok */
Mark Salyzyn6e315682017-03-09 08:09:43 -0800344 if (cp) {
345 setenv(tz, cp, 1);
346 } else {
347 unsetenv(tz);
348 }
349 tzset();
350 format = FORMAT_OFF;
351 } else {
352 format = FORMAT_MODIFIER_ZONE;
353 }
354 free(cp);
355 }
Mark Salyzyn7ad91922016-03-08 16:18:26 -0800356#endif
The Android Open Source Project32315d42008-10-21 07:00:00 -0700357
Mark Salyzyn6e315682017-03-09 08:09:43 -0800358 return format;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700359}
360
361/**
362 * filterExpression: a single filter expression
363 * eg "AT:d"
364 *
365 * returns 0 on success and -1 on invalid expression
366 *
367 * Assumes single threaded execution
368 */
369
Mark Salyzyn6e315682017-03-09 08:09:43 -0800370LIBLOG_ABI_PUBLIC int android_log_addFilterRule(AndroidLogFormat* p_format,
371 const char* filterExpression) {
372 size_t tagNameLength;
373 android_LogPriority pri = ANDROID_LOG_DEFAULT;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700374
Mark Salyzyn6e315682017-03-09 08:09:43 -0800375 tagNameLength = strcspn(filterExpression, ":");
The Android Open Source Project32315d42008-10-21 07:00:00 -0700376
Mark Salyzyn6e315682017-03-09 08:09:43 -0800377 if (tagNameLength == 0) {
378 goto error;
379 }
380
381 if (filterExpression[tagNameLength] == ':') {
382 pri = filterCharToPri(filterExpression[tagNameLength + 1]);
383
384 if (pri == ANDROID_LOG_UNKNOWN) {
385 goto error;
386 }
387 }
388
389 if (0 == strncmp("*", filterExpression, tagNameLength)) {
390 /*
391 * This filter expression refers to the global filter
392 * The default level for this is DEBUG if the priority
393 * is unspecified
394 */
395 if (pri == ANDROID_LOG_DEFAULT) {
396 pri = ANDROID_LOG_DEBUG;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700397 }
398
Mark Salyzyn6e315682017-03-09 08:09:43 -0800399 p_format->global_pri = pri;
400 } else {
401 /*
402 * for filter expressions that don't refer to the global
403 * filter, the default is verbose if the priority is unspecified
404 */
405 if (pri == ANDROID_LOG_DEFAULT) {
406 pri = ANDROID_LOG_VERBOSE;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700407 }
408
Mark Salyzyn6e315682017-03-09 08:09:43 -0800409 char* tagName;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700410
Mark Salyzyn1ebdb352015-05-15 09:01:58 -0700411/*
412 * Presently HAVE_STRNDUP is never defined, so the second case is always taken
Mark Salyzyn7ad91922016-03-08 16:18:26 -0800413 * Darwin doesn't have strndup, everything else does
Mark Salyzyn1ebdb352015-05-15 09:01:58 -0700414 */
The Android Open Source Project32315d42008-10-21 07:00:00 -0700415#ifdef HAVE_STRNDUP
Mark Salyzyn6e315682017-03-09 08:09:43 -0800416 tagName = strndup(filterExpression, tagNameLength);
The Android Open Source Project32315d42008-10-21 07:00:00 -0700417#else
Mark Salyzyn6e315682017-03-09 08:09:43 -0800418 /* a few extra bytes copied... */
419 tagName = strdup(filterExpression);
420 tagName[tagNameLength] = '\0';
The Android Open Source Project32315d42008-10-21 07:00:00 -0700421#endif /*HAVE_STRNDUP*/
422
Mark Salyzyn6e315682017-03-09 08:09:43 -0800423 FilterInfo* p_fi = filterinfo_new(tagName, pri);
424 free(tagName);
The Android Open Source Project32315d42008-10-21 07:00:00 -0700425
Mark Salyzyn6e315682017-03-09 08:09:43 -0800426 p_fi->p_next = p_format->filters;
427 p_format->filters = p_fi;
428 }
The Android Open Source Project32315d42008-10-21 07:00:00 -0700429
Mark Salyzyn6e315682017-03-09 08:09:43 -0800430 return 0;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700431error:
Mark Salyzyn6e315682017-03-09 08:09:43 -0800432 return -1;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700433}
434
Mark Salyzyn7ad91922016-03-08 16:18:26 -0800435#ifndef HAVE_STRSEP
436/* KISS replacement helper for below */
Mark Salyzyn6e315682017-03-09 08:09:43 -0800437static char* strsep(char** stringp, const char* delim) {
438 char* token;
439 char* ret = *stringp;
Mark Salyzyn7ad91922016-03-08 16:18:26 -0800440
Mark Salyzyn6e315682017-03-09 08:09:43 -0800441 if (!ret || !*ret) {
442 return NULL;
443 }
444 token = strpbrk(ret, delim);
445 if (token) {
446 *token = '\0';
447 ++token;
448 } else {
449 token = ret + strlen(ret);
450 }
451 *stringp = token;
452 return ret;
Mark Salyzyn7ad91922016-03-08 16:18:26 -0800453}
454#endif
The Android Open Source Project32315d42008-10-21 07:00:00 -0700455
456/**
457 * filterString: a comma/whitespace-separated set of filter expressions
458 *
459 * eg "AT:d *:i"
460 *
461 * returns 0 on success and -1 on invalid expression
462 *
463 * Assumes single threaded execution
464 *
465 */
Mark Salyzyn6e315682017-03-09 08:09:43 -0800466LIBLOG_ABI_PUBLIC int android_log_addFilterString(AndroidLogFormat* p_format,
467 const char* filterString) {
468 char* filterStringCopy = strdup(filterString);
469 char* p_cur = filterStringCopy;
470 char* p_ret;
471 int err;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700472
Mark Salyzyn6e315682017-03-09 08:09:43 -0800473 /* Yes, I'm using strsep */
474 while (NULL != (p_ret = strsep(&p_cur, " \t,"))) {
475 /* ignore whitespace-only entries */
476 if (p_ret[0] != '\0') {
477 err = android_log_addFilterRule(p_format, p_ret);
The Android Open Source Project32315d42008-10-21 07:00:00 -0700478
Mark Salyzyn6e315682017-03-09 08:09:43 -0800479 if (err < 0) {
480 goto error;
481 }
The Android Open Source Project32315d42008-10-21 07:00:00 -0700482 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800483 }
The Android Open Source Project32315d42008-10-21 07:00:00 -0700484
Mark Salyzyn6e315682017-03-09 08:09:43 -0800485 free(filterStringCopy);
486 return 0;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700487error:
Mark Salyzyn6e315682017-03-09 08:09:43 -0800488 free(filterStringCopy);
489 return -1;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700490}
491
The Android Open Source Project32315d42008-10-21 07:00:00 -0700492/**
493 * Splits a wire-format buffer into an AndroidLogEntry
494 * entry allocated by caller. Pointers will point directly into buf
495 *
496 * Returns 0 on success and -1 on invalid wire format (entry will be
497 * in unspecified state)
498 */
Mark Salyzyn6e315682017-03-09 08:09:43 -0800499LIBLOG_ABI_PUBLIC int android_log_processLogBuffer(struct logger_entry* buf,
500 AndroidLogEntry* entry) {
501 entry->message = NULL;
502 entry->messageLen = 0;
Mark Salyzyned47c7b2017-03-02 07:47:21 -0800503
Mark Salyzyn6e315682017-03-09 08:09:43 -0800504 entry->tv_sec = buf->sec;
505 entry->tv_nsec = buf->nsec;
506 entry->uid = -1;
507 entry->pid = buf->pid;
508 entry->tid = buf->tid;
Kenny Rootfa7c09a2011-09-30 17:10:14 -0700509
Mark Salyzyn6e315682017-03-09 08:09:43 -0800510 /*
511 * format: <priority:1><tag:N>\0<message:N>\0
512 *
513 * tag str
514 * starts at buf->msg+1
515 * msg
516 * starts at buf->msg+1+len(tag)+1
517 *
518 * The message may have been truncated by the kernel log driver.
519 * When that happens, we must null-terminate the message ourselves.
520 */
521 if (buf->len < 3) {
Kenny Rootfa7c09a2011-09-30 17:10:14 -0700522 /*
Mark Salyzyn6e315682017-03-09 08:09:43 -0800523 * An well-formed entry must consist of at least a priority
524 * and two null characters
Kenny Rootfa7c09a2011-09-30 17:10:14 -0700525 */
Mark Salyzyn6e315682017-03-09 08:09:43 -0800526 fprintf(stderr, "+++ LOG: entry too small\n");
527 return -1;
528 }
The Android Open Source Project32315d42008-10-21 07:00:00 -0700529
Mark Salyzyn6e315682017-03-09 08:09:43 -0800530 int msgStart = -1;
531 int msgEnd = -1;
Jeff Sharkey059ac702011-10-26 18:40:39 -0700532
Mark Salyzyn6e315682017-03-09 08:09:43 -0800533 int i;
534 char* msg = buf->msg;
535 struct logger_entry_v2* buf2 = (struct logger_entry_v2*)buf;
536 if (buf2->hdr_size) {
537 if ((buf2->hdr_size < sizeof(((struct log_msg*)NULL)->entry_v1)) ||
538 (buf2->hdr_size > sizeof(((struct log_msg*)NULL)->entry))) {
539 fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
540 return -1;
Mark Salyzyn64bf6682013-12-18 12:59:01 -0800541 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800542 msg = ((char*)buf2) + buf2->hdr_size;
543 if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) {
544 entry->uid = ((struct logger_entry_v4*)buf)->uid;
545 }
546 }
547 for (i = 1; i < buf->len; i++) {
548 if (msg[i] == '\0') {
549 if (msgStart == -1) {
550 msgStart = i + 1;
551 } else {
552 msgEnd = i;
553 break;
554 }
555 }
556 }
557
558 if (msgStart == -1) {
559 /* +++ LOG: malformed log message, DYB */
Nick Kralevich2f2789a2011-10-18 15:23:33 -0700560 for (i = 1; i < buf->len; i++) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800561 /* odd characters in tag? */
562 if ((msg[i] <= ' ') || (msg[i] == ':') || (msg[i] >= 0x7f)) {
563 msg[i] = '\0';
564 msgStart = i + 1;
565 break;
566 }
Nick Kralevich2f2789a2011-10-18 15:23:33 -0700567 }
Jeff Sharkey059ac702011-10-26 18:40:39 -0700568 if (msgStart == -1) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800569 msgStart = buf->len - 1; /* All tag, no message, print truncates */
Nick Kralevich32be08b2011-10-17 10:45:03 -0700570 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800571 }
572 if (msgEnd == -1) {
573 /* incoming message not null-terminated; force it */
574 msgEnd = buf->len - 1; /* may result in msgEnd < msgStart */
575 msg[msgEnd] = '\0';
576 }
Jeff Sharkey059ac702011-10-26 18:40:39 -0700577
Tom Cherryf623f022019-01-10 10:37:36 -0800578 entry->priority = static_cast<android_LogPriority>(msg[0]);
Mark Salyzyn6e315682017-03-09 08:09:43 -0800579 entry->tag = msg + 1;
580 entry->tagLen = msgStart - 1;
581 entry->message = msg + msgStart;
582 entry->messageLen = (msgEnd < msgStart) ? 0 : (msgEnd - msgStart);
Nick Kralevich32be08b2011-10-17 10:45:03 -0700583
Mark Salyzyn6e315682017-03-09 08:09:43 -0800584 return 0;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700585}
586
587/*
Mark Salyzynbd8b8902015-03-06 20:42:57 +0000588 * Extract a 4-byte value from a byte stream.
589 */
Mark Salyzyn6e315682017-03-09 08:09:43 -0800590static inline uint32_t get4LE(const uint8_t* src) {
591 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
Mark Salyzynbd8b8902015-03-06 20:42:57 +0000592}
593
594/*
595 * Extract an 8-byte value from a byte stream.
596 */
Mark Salyzyn6e315682017-03-09 08:09:43 -0800597static inline uint64_t get8LE(const uint8_t* src) {
598 uint32_t low, high;
Mark Salyzynbd8b8902015-03-06 20:42:57 +0000599
Mark Salyzyn6e315682017-03-09 08:09:43 -0800600 low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
601 high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24);
602 return ((uint64_t)high << 32) | (uint64_t)low;
Mark Salyzynbd8b8902015-03-06 20:42:57 +0000603}
604
Mark Salyzyn604679a2016-10-18 11:30:11 -0700605static bool findChar(const char** cp, size_t* len, int c) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800606 while ((*len) && isspace(*(*cp))) {
607 ++(*cp);
608 --(*len);
609 }
610 if (c == INT_MAX) return *len;
611 if ((*len) && (*(*cp) == c)) {
612 ++(*cp);
613 --(*len);
614 return true;
615 }
616 return false;
Mark Salyzyn604679a2016-10-18 11:30:11 -0700617}
Mark Salyzynbd8b8902015-03-06 20:42:57 +0000618
619/*
The Android Open Source Project32315d42008-10-21 07:00:00 -0700620 * Recursively convert binary log data to printable form.
621 *
622 * This needs to be recursive because you can have lists of lists.
623 *
624 * If we run out of room, we stop processing immediately. It's important
625 * for us to check for space on every output element to avoid producing
626 * garbled output.
627 *
628 * Returns 0 on success, 1 on buffer full, -1 on failure.
629 */
Mark Salyzyn604679a2016-10-18 11:30:11 -0700630enum objectType {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800631 TYPE_OBJECTS = '1',
632 TYPE_BYTES = '2',
633 TYPE_MILLISECONDS = '3',
634 TYPE_ALLOCATIONS = '4',
635 TYPE_ID = '5',
Mark Salyzyn07ff2122016-11-10 10:24:44 -0800636 TYPE_PERCENT = '6',
637 TYPE_MONOTONIC = 's'
Mark Salyzyn604679a2016-10-18 11:30:11 -0700638};
639
Tom Cherryf623f022019-01-10 10:37:36 -0800640static int android_log_printBinaryEvent(const unsigned char** pEventData, size_t* pEventDataLen,
641 char** pOutBuf, size_t* pOutBufLen, const char** fmtStr,
Mark Salyzyn6e315682017-03-09 08:09:43 -0800642 size_t* fmtLen) {
643 const unsigned char* eventData = *pEventData;
644 size_t eventDataLen = *pEventDataLen;
645 char* outBuf = *pOutBuf;
646 char* outBufSave = outBuf;
647 size_t outBufLen = *pOutBufLen;
648 size_t outBufLenSave = outBufLen;
649 unsigned char type;
Mark Salyzyn07ff2122016-11-10 10:24:44 -0800650 size_t outCount = 0;
Mark Salyzyn6e315682017-03-09 08:09:43 -0800651 int result = 0;
652 const char* cp;
653 size_t len;
654 int64_t lval;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700655
Mark Salyzyn6e315682017-03-09 08:09:43 -0800656 if (eventDataLen < 1) return -1;
Mark Salyzyn555064b2016-11-11 14:41:30 -0800657
Mark Salyzyn6e315682017-03-09 08:09:43 -0800658 type = *eventData++;
659 eventDataLen--;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700660
Mark Salyzyn6e315682017-03-09 08:09:43 -0800661 cp = NULL;
662 len = 0;
663 if (fmtStr && *fmtStr && fmtLen && *fmtLen && **fmtStr) {
664 cp = *fmtStr;
665 len = *fmtLen;
666 }
667 /*
668 * event.logtag format specification:
669 *
670 * Optionally, after the tag names can be put a description for the value(s)
671 * of the tag. Description are in the format
672 * (<name>|data type[|data unit])
673 * Multiple values are separated by commas.
674 *
675 * The data type is a number from the following values:
676 * 1: int
677 * 2: long
678 * 3: string
679 * 4: list
680 * 5: float
681 *
682 * The data unit is a number taken from the following list:
683 * 1: Number of objects
684 * 2: Number of bytes
685 * 3: Number of milliseconds
686 * 4: Number of allocations
687 * 5: Id
688 * 6: Percent
Mark Salyzyn07ff2122016-11-10 10:24:44 -0800689 * s: Number of seconds (monotonic time)
Mark Salyzyn6e315682017-03-09 08:09:43 -0800690 * Default value for data of type int/long is 2 (bytes).
691 */
692 if (!cp || !findChar(&cp, &len, '(')) {
Mark Salyzyn604679a2016-10-18 11:30:11 -0700693 len = 0;
Mark Salyzyn6e315682017-03-09 08:09:43 -0800694 } else {
695 char* outBufLastSpace = NULL;
696
697 findChar(&cp, &len, INT_MAX);
698 while (len && *cp && (*cp != '|') && (*cp != ')')) {
699 if (outBufLen <= 0) {
700 /* halt output */
701 goto no_room;
702 }
703 outBufLastSpace = isspace(*cp) ? outBuf : NULL;
704 *outBuf = *cp;
705 ++outBuf;
706 ++cp;
707 --outBufLen;
708 --len;
Mark Salyzyn604679a2016-10-18 11:30:11 -0700709 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800710 if (outBufLastSpace) {
711 outBufLen += outBuf - outBufLastSpace;
712 outBuf = outBufLastSpace;
713 }
714 if (outBufLen <= 0) {
715 /* halt output */
716 goto no_room;
717 }
718 if (outBufSave != outBuf) {
719 *outBuf = '=';
720 ++outBuf;
721 --outBufLen;
722 }
723
724 if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) {
Tom Cherryf623f022019-01-10 10:37:36 -0800725 static const unsigned char typeTable[] = {EVENT_TYPE_INT, EVENT_TYPE_LONG, EVENT_TYPE_STRING,
726 EVENT_TYPE_LIST, EVENT_TYPE_FLOAT};
Mark Salyzyn6e315682017-03-09 08:09:43 -0800727
Tom Cherryf623f022019-01-10 10:37:36 -0800728 if ((*cp >= '1') && (*cp < (char)('1' + (sizeof(typeTable) / sizeof(typeTable[0])))) &&
Mark Salyzyn6e315682017-03-09 08:09:43 -0800729 (type != typeTable[(size_t)(*cp - '1')]))
Mark Salyzyn604679a2016-10-18 11:30:11 -0700730 len = 0;
Mark Salyzyn604679a2016-10-18 11:30:11 -0700731
Mark Salyzyn6e315682017-03-09 08:09:43 -0800732 if (len) {
733 ++cp;
734 --len;
735 } else {
736 /* reset the format */
737 outBuf = outBufSave;
738 outBufLen = outBufLenSave;
739 }
Mark Salyzyn604679a2016-10-18 11:30:11 -0700740 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800741 }
742 outCount = 0;
743 lval = 0;
744 switch (type) {
The Android Open Source Project32315d42008-10-21 07:00:00 -0700745 case EVENT_TYPE_INT:
Mark Salyzyn6e315682017-03-09 08:09:43 -0800746 /* 32-bit signed int */
747 {
748 int32_t ival;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700749
Mark Salyzyn6e315682017-03-09 08:09:43 -0800750 if (eventDataLen < 4) return -1;
751 ival = get4LE(eventData);
752 eventData += 4;
753 eventDataLen -= 4;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700754
Mark Salyzyn6e315682017-03-09 08:09:43 -0800755 lval = ival;
756 }
757 goto pr_lval;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700758 case EVENT_TYPE_LONG:
Mark Salyzyn6e315682017-03-09 08:09:43 -0800759 /* 64-bit signed long */
760 if (eventDataLen < 8) return -1;
761 lval = get8LE(eventData);
762 eventData += 8;
763 eventDataLen -= 8;
Mark Salyzyn604679a2016-10-18 11:30:11 -0700764 pr_lval:
Mark Salyzyn6e315682017-03-09 08:09:43 -0800765 outCount = snprintf(outBuf, outBufLen, "%" PRId64, lval);
766 if (outCount < outBufLen) {
767 outBuf += outCount;
768 outBufLen -= outCount;
769 } else {
770 /* halt output */
771 goto no_room;
772 }
773 break;
Jeff Brown9cac7fc2015-04-28 12:47:02 -0700774 case EVENT_TYPE_FLOAT:
Mark Salyzyn6e315682017-03-09 08:09:43 -0800775 /* float */
776 {
777 uint32_t ival;
778 float fval;
Jeff Brown9cac7fc2015-04-28 12:47:02 -0700779
Mark Salyzyn6e315682017-03-09 08:09:43 -0800780 if (eventDataLen < 4) return -1;
781 ival = get4LE(eventData);
782 fval = *(float*)&ival;
783 eventData += 4;
784 eventDataLen -= 4;
Jeff Brown9cac7fc2015-04-28 12:47:02 -0700785
Mark Salyzyn6e315682017-03-09 08:09:43 -0800786 outCount = snprintf(outBuf, outBufLen, "%f", fval);
787 if (outCount < outBufLen) {
788 outBuf += outCount;
789 outBufLen -= outCount;
790 } else {
791 /* halt output */
792 goto no_room;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700793 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800794 }
795 break;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700796 case EVENT_TYPE_STRING:
Mark Salyzyn6e315682017-03-09 08:09:43 -0800797 /* UTF-8 chars, not NULL-terminated */
798 {
799 unsigned int strLen;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700800
Mark Salyzyn6e315682017-03-09 08:09:43 -0800801 if (eventDataLen < 4) return -1;
802 strLen = get4LE(eventData);
803 eventData += 4;
804 eventDataLen -= 4;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700805
Mark Salyzyn6e315682017-03-09 08:09:43 -0800806 if (eventDataLen < strLen) {
807 result = -1; /* mark truncated */
808 strLen = eventDataLen;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700809 }
The Android Open Source Project32315d42008-10-21 07:00:00 -0700810
Mark Salyzyn6e315682017-03-09 08:09:43 -0800811 if (cp && (strLen == 0)) {
812 /* reset the format if no content */
813 outBuf = outBufSave;
814 outBufLen = outBufLenSave;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700815 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800816 if (strLen < outBufLen) {
817 memcpy(outBuf, eventData, strLen);
818 outBuf += strLen;
819 outBufLen -= strLen;
820 } else {
821 if (outBufLen > 0) {
822 /* copy what we can */
823 memcpy(outBuf, eventData, outBufLen);
824 outBuf += outBufLen;
825 outBufLen -= outBufLen;
826 }
827 if (!result) result = 1; /* if not truncated, return no room */
828 }
829 eventData += strLen;
830 eventDataLen -= strLen;
831 if (result != 0) goto bail;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700832 break;
Mark Salyzyn6e315682017-03-09 08:09:43 -0800833 }
834 case EVENT_TYPE_LIST:
835 /* N items, all different types */
836 {
837 unsigned char count;
838 int i;
839
840 if (eventDataLen < 1) return -1;
841
842 count = *eventData++;
843 eventDataLen--;
844
845 if (outBufLen <= 0) goto no_room;
846
847 *outBuf++ = '[';
848 outBufLen--;
849
850 for (i = 0; i < count; i++) {
Tom Cherryf623f022019-01-10 10:37:36 -0800851 result = android_log_printBinaryEvent(&eventData, &eventDataLen, &outBuf, &outBufLen,
852 fmtStr, fmtLen);
Mark Salyzyn6e315682017-03-09 08:09:43 -0800853 if (result != 0) goto bail;
854
855 if (i < (count - 1)) {
856 if (outBufLen <= 0) goto no_room;
857 *outBuf++ = ',';
858 outBufLen--;
859 }
Mark Salyzyn604679a2016-10-18 11:30:11 -0700860 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800861
862 if (outBufLen <= 0) goto no_room;
863
864 *outBuf++ = ']';
865 outBufLen--;
866 }
867 break;
868 default:
869 fprintf(stderr, "Unknown binary event type %d\n", type);
870 return -1;
871 }
872 if (cp && len) {
873 if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) {
874 switch (*cp) {
875 case TYPE_OBJECTS:
876 outCount = 0;
877 /* outCount = snprintf(outBuf, outBufLen, " objects"); */
878 break;
879 case TYPE_BYTES:
880 if ((lval != 0) && ((lval % 1024) == 0)) {
881 /* repaint with multiplier */
Tom Cherryf623f022019-01-10 10:37:36 -0800882 static const char suffixTable[] = {'K', 'M', 'G', 'T'};
Mark Salyzyn6e315682017-03-09 08:09:43 -0800883 size_t idx = 0;
884 outBuf -= outCount;
885 outBufLen += outCount;
886 do {
887 lval /= 1024;
888 if ((lval % 1024) != 0) break;
Tom Cherryf623f022019-01-10 10:37:36 -0800889 } while (++idx < ((sizeof(suffixTable) / sizeof(suffixTable[0])) - 1));
890 outCount = snprintf(outBuf, outBufLen, "%" PRId64 "%cB", lval, suffixTable[idx]);
Mark Salyzyn6e315682017-03-09 08:09:43 -0800891 } else {
892 outCount = snprintf(outBuf, outBufLen, "B");
893 }
894 break;
895 case TYPE_MILLISECONDS:
Tom Cherryf623f022019-01-10 10:37:36 -0800896 if (((lval <= -1000) || (1000 <= lval)) && (outBufLen || (outBuf[-1] == '0'))) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800897 /* repaint as (fractional) seconds, possibly saving space */
898 if (outBufLen) outBuf[0] = outBuf[-1];
899 outBuf[-1] = outBuf[-2];
900 outBuf[-2] = outBuf[-3];
901 outBuf[-3] = '.';
902 while ((outBufLen == 0) || (*outBuf == '0')) {
903 --outBuf;
904 ++outBufLen;
905 }
906 if (*outBuf != '.') {
907 ++outBuf;
908 --outBufLen;
909 }
910 outCount = snprintf(outBuf, outBufLen, "s");
911 } else {
912 outCount = snprintf(outBuf, outBufLen, "ms");
913 }
914 break;
Mark Salyzyn07ff2122016-11-10 10:24:44 -0800915 case TYPE_MONOTONIC: {
916 static const uint64_t minute = 60;
917 static const uint64_t hour = 60 * minute;
918 static const uint64_t day = 24 * hour;
919
920 /* Repaint as unsigned seconds, minutes, hours ... */
921 outBuf -= outCount;
922 outBufLen += outCount;
923 uint64_t val = lval;
924 if (val >= day) {
925 outCount = snprintf(outBuf, outBufLen, "%" PRIu64 "d ", val / day);
926 if (outCount >= outBufLen) break;
927 outBuf += outCount;
928 outBufLen -= outCount;
929 val = (val % day) + day;
930 }
931 if (val >= minute) {
932 if (val >= hour) {
Tom Cherryf623f022019-01-10 10:37:36 -0800933 outCount = snprintf(outBuf, outBufLen, "%" PRIu64 ":", (val / hour) % (day / hour));
Mark Salyzyn07ff2122016-11-10 10:24:44 -0800934 if (outCount >= outBufLen) break;
935 outBuf += outCount;
936 outBufLen -= outCount;
937 }
938 outCount =
Tom Cherryf623f022019-01-10 10:37:36 -0800939 snprintf(outBuf, outBufLen, (val >= hour) ? "%02" PRIu64 ":" : "%" PRIu64 ":",
Mark Salyzyn07ff2122016-11-10 10:24:44 -0800940 (val / minute) % (hour / minute));
941 if (outCount >= outBufLen) break;
942 outBuf += outCount;
943 outBufLen -= outCount;
944 }
Tom Cherryf623f022019-01-10 10:37:36 -0800945 outCount = snprintf(outBuf, outBufLen, (val >= minute) ? "%02" PRIu64 : "%" PRIu64 "s",
Mark Salyzyn07ff2122016-11-10 10:24:44 -0800946 val % minute);
947 } break;
Mark Salyzyn6e315682017-03-09 08:09:43 -0800948 case TYPE_ALLOCATIONS:
949 outCount = 0;
950 /* outCount = snprintf(outBuf, outBufLen, " allocations"); */
951 break;
952 case TYPE_ID:
953 outCount = 0;
954 break;
955 case TYPE_PERCENT:
956 outCount = snprintf(outBuf, outBufLen, "%%");
957 break;
958 default: /* ? */
959 outCount = 0;
960 break;
961 }
962 ++cp;
963 --len;
964 if (outCount < outBufLen) {
965 outBuf += outCount;
966 outBufLen -= outCount;
967 } else if (outCount) {
968 /* halt output */
969 goto no_room;
970 }
Mark Salyzyn604679a2016-10-18 11:30:11 -0700971 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800972 if (!findChar(&cp, &len, ')')) len = 0;
973 if (!findChar(&cp, &len, ',')) len = 0;
974 }
The Android Open Source Project32315d42008-10-21 07:00:00 -0700975
976bail:
Mark Salyzyn6e315682017-03-09 08:09:43 -0800977 *pEventData = eventData;
978 *pEventDataLen = eventDataLen;
979 *pOutBuf = outBuf;
980 *pOutBufLen = outBufLen;
981 if (cp) {
982 *fmtStr = cp;
983 *fmtLen = len;
984 }
985 return result;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700986
987no_room:
Mark Salyzyn6e315682017-03-09 08:09:43 -0800988 result = 1;
989 goto bail;
The Android Open Source Project32315d42008-10-21 07:00:00 -0700990}
991
992/**
993 * Convert a binary log entry to ASCII form.
994 *
995 * For convenience we mimic the processLogBuffer API. There is no
996 * pre-defined output length for the binary data, since we're free to format
997 * it however we choose, which means we can't really use a fixed-size buffer
998 * here.
999 */
Mark Salyzyna5c22172016-03-10 08:25:33 -08001000LIBLOG_ABI_PUBLIC int android_log_processBinaryLogBuffer(
Mark Salyzyn6e315682017-03-09 08:09:43 -08001001 struct logger_entry* buf, AndroidLogEntry* entry,
Tom Cherryf623f022019-01-10 10:37:36 -08001002 [[maybe_unused]] const EventTagMap* map, /* only on !__ANDROID__ */
Mark Salyzyn6e315682017-03-09 08:09:43 -08001003 char* messageBuf, int messageBufLen) {
1004 size_t inCount;
1005 uint32_t tagIndex;
1006 const unsigned char* eventData;
The Android Open Source Project32315d42008-10-21 07:00:00 -07001007
Mark Salyzyn6e315682017-03-09 08:09:43 -08001008 entry->message = NULL;
1009 entry->messageLen = 0;
Mark Salyzyned47c7b2017-03-02 07:47:21 -08001010
Mark Salyzyn6e315682017-03-09 08:09:43 -08001011 entry->tv_sec = buf->sec;
1012 entry->tv_nsec = buf->nsec;
1013 entry->priority = ANDROID_LOG_INFO;
1014 entry->uid = -1;
1015 entry->pid = buf->pid;
1016 entry->tid = buf->tid;
The Android Open Source Project32315d42008-10-21 07:00:00 -07001017
Mark Salyzyn6e315682017-03-09 08:09:43 -08001018 /*
1019 * Pull the tag out, fill in some additional details based on incoming
1020 * buffer version (v3 adds lid, v4 adds uid).
1021 */
1022 eventData = (const unsigned char*)buf->msg;
1023 struct logger_entry_v2* buf2 = (struct logger_entry_v2*)buf;
1024 if (buf2->hdr_size) {
1025 if ((buf2->hdr_size < sizeof(((struct log_msg*)NULL)->entry_v1)) ||
1026 (buf2->hdr_size > sizeof(((struct log_msg*)NULL)->entry))) {
1027 fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
1028 return -1;
1029 }
1030 eventData = ((unsigned char*)buf2) + buf2->hdr_size;
1031 if ((buf2->hdr_size >= sizeof(struct logger_entry_v3)) &&
1032 (((struct logger_entry_v3*)buf)->lid == LOG_ID_SECURITY)) {
1033 entry->priority = ANDROID_LOG_WARN;
1034 }
1035 if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) {
1036 entry->uid = ((struct logger_entry_v4*)buf)->uid;
1037 }
1038 }
1039 inCount = buf->len;
1040 if (inCount < 4) return -1;
1041 tagIndex = get4LE(eventData);
1042 eventData += 4;
1043 inCount -= 4;
1044
1045 entry->tagLen = 0;
1046 entry->tag = NULL;
1047#ifdef __ANDROID__
1048 if (map != NULL) {
1049 entry->tag = android_lookupEventTag_len(map, &entry->tagLen, tagIndex);
1050 }
1051#endif
1052
1053 /*
1054 * If we don't have a map, or didn't find the tag number in the map,
1055 * stuff a generated tag value into the start of the output buffer and
1056 * shift the buffer pointers down.
1057 */
1058 if (entry->tag == NULL) {
1059 size_t tagLen;
1060
1061 tagLen = snprintf(messageBuf, messageBufLen, "[%" PRIu32 "]", tagIndex);
1062 if (tagLen >= (size_t)messageBufLen) {
1063 tagLen = messageBufLen - 1;
1064 }
1065 entry->tag = messageBuf;
1066 entry->tagLen = tagLen;
1067 messageBuf += tagLen + 1;
1068 messageBufLen -= tagLen + 1;
1069 }
1070
1071 /*
1072 * Format the event log data into the buffer.
1073 */
1074 const char* fmtStr = NULL;
1075 size_t fmtLen = 0;
1076#ifdef __ANDROID__
1077 if (descriptive_output && map) {
1078 fmtStr = android_lookupEventFormat_len(map, &fmtLen, tagIndex);
1079 }
1080#endif
1081
1082 char* outBuf = messageBuf;
1083 size_t outRemaining = messageBufLen - 1; /* leave one for nul byte */
1084 int result = 0;
1085
1086 if ((inCount > 0) || fmtLen) {
Tom Cherryf623f022019-01-10 10:37:36 -08001087 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, &outRemaining, &fmtStr,
1088 &fmtLen);
Mark Salyzyn6e315682017-03-09 08:09:43 -08001089 }
1090 if ((result == 1) && fmtStr) {
1091 /* We overflowed :-(, let's repaint the line w/o format dressings */
Mark Salyzyned47c7b2017-03-02 07:47:21 -08001092 eventData = (const unsigned char*)buf->msg;
Mark Salyzyn64bf6682013-12-18 12:59:01 -08001093 if (buf2->hdr_size) {
Mark Salyzyn6e315682017-03-09 08:09:43 -08001094 eventData = ((unsigned char*)buf2) + buf2->hdr_size;
Mark Salyzyn64bf6682013-12-18 12:59:01 -08001095 }
The Android Open Source Project32315d42008-10-21 07:00:00 -07001096 eventData += 4;
Mark Salyzyn6e315682017-03-09 08:09:43 -08001097 outBuf = messageBuf;
1098 outRemaining = messageBufLen - 1;
Tom Cherryf623f022019-01-10 10:37:36 -08001099 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, &outRemaining, NULL, NULL);
Mark Salyzyn6e315682017-03-09 08:09:43 -08001100 }
1101 if (result < 0) {
1102 fprintf(stderr, "Binary log entry conversion failed\n");
1103 }
1104 if (result) {
1105 if (!outRemaining) {
1106 /* make space to leave an indicator */
1107 --outBuf;
1108 ++outRemaining;
The Android Open Source Project32315d42008-10-21 07:00:00 -07001109 }
Mark Salyzyn6e315682017-03-09 08:09:43 -08001110 *outBuf++ = (result < 0) ? '!' : '^'; /* Error or Truncation? */
1111 outRemaining--;
1112 /* pretend we ate all the data to prevent log stutter */
1113 inCount = 0;
1114 if (result > 0) result = 0;
1115 }
The Android Open Source Project32315d42008-10-21 07:00:00 -07001116
Mark Salyzyn6e315682017-03-09 08:09:43 -08001117 /* eat the silly terminating '\n' */
1118 if (inCount == 1 && *eventData == '\n') {
1119 eventData++;
1120 inCount--;
1121 }
The Android Open Source Project32315d42008-10-21 07:00:00 -07001122
Mark Salyzyn6e315682017-03-09 08:09:43 -08001123 if (inCount != 0) {
1124 fprintf(stderr, "Warning: leftover binary log data (%zu bytes)\n", inCount);
1125 }
The Android Open Source Project32315d42008-10-21 07:00:00 -07001126
Mark Salyzyn6e315682017-03-09 08:09:43 -08001127 /*
1128 * Terminate the buffer. The NUL byte does not count as part of
1129 * entry->messageLen.
1130 */
1131 *outBuf = '\0';
1132 entry->messageLen = outBuf - messageBuf;
1133 assert(entry->messageLen == (messageBufLen - 1) - outRemaining);
Mark Salyzyn555064b2016-11-11 14:41:30 -08001134
Mark Salyzyn6e315682017-03-09 08:09:43 -08001135 entry->message = messageBuf;
Mark Salyzyn555064b2016-11-11 14:41:30 -08001136
Mark Salyzyn6e315682017-03-09 08:09:43 -08001137 return result;
The Android Open Source Project32315d42008-10-21 07:00:00 -07001138}
1139
Mark Salyzyn1ebdb352015-05-15 09:01:58 -07001140/*
1141 * One utf8 character at a time
1142 *
1143 * Returns the length of the utf8 character in the buffer,
1144 * or -1 if illegal or truncated
1145 *
1146 * Open coded from libutils/Unicode.cpp, borrowed from utf8_length(),
1147 * can not remove from here because of library circular dependencies.
1148 * Expect one-day utf8_character_length with the same signature could
1149 * _also_ be part of libutils/Unicode.cpp if its usefullness needs to
1150 * propagate globally.
1151 */
Mark Salyzyn6e315682017-03-09 08:09:43 -08001152LIBLOG_WEAK ssize_t utf8_character_length(const char* src, size_t len) {
1153 const char* cur = src;
1154 const char first_char = *cur++;
1155 static const uint32_t kUnicodeMaxCodepoint = 0x0010FFFF;
1156 int32_t mask, to_ignore_mask;
1157 size_t num_to_read;
1158 uint32_t utf32;
Mark Salyzyn1ebdb352015-05-15 09:01:58 -07001159
Mark Salyzyn6e315682017-03-09 08:09:43 -08001160 if ((first_char & 0x80) == 0) { /* ASCII */
1161 return first_char ? 1 : -1;
1162 }
Mark Salyzyn1ebdb352015-05-15 09:01:58 -07001163
Mark Salyzyn6e315682017-03-09 08:09:43 -08001164 /*
1165 * (UTF-8's character must not be like 10xxxxxx,
1166 * but 110xxxxx, 1110xxxx, ... or 1111110x)
1167 */
1168 if ((first_char & 0x40) == 0) {
1169 return -1;
1170 }
Mark Salyzyn1ebdb352015-05-15 09:01:58 -07001171
Mark Salyzyn6e315682017-03-09 08:09:43 -08001172 for (utf32 = 1, num_to_read = 1, mask = 0x40, to_ignore_mask = 0x80;
Tom Cherryf623f022019-01-10 10:37:36 -08001173 num_to_read < 5 && (first_char & mask); num_to_read++, to_ignore_mask |= mask, mask >>= 1) {
Mark Salyzyn6e315682017-03-09 08:09:43 -08001174 if (num_to_read > len) {
1175 return -1;
Mark Salyzyn1ebdb352015-05-15 09:01:58 -07001176 }
Mark Salyzyn6e315682017-03-09 08:09:43 -08001177 if ((*cur & 0xC0) != 0x80) { /* can not be 10xxxxxx? */
1178 return -1;
Mark Salyzyn1ebdb352015-05-15 09:01:58 -07001179 }
Mark Salyzyn6e315682017-03-09 08:09:43 -08001180 utf32 = (utf32 << 6) + (*cur++ & 0b00111111);
1181 }
1182 /* "first_char" must be (110xxxxx - 11110xxx) */
1183 if (num_to_read >= 5) {
1184 return -1;
1185 }
1186 to_ignore_mask |= mask;
1187 utf32 |= ((~to_ignore_mask) & first_char) << (6 * (num_to_read - 1));
1188 if (utf32 > kUnicodeMaxCodepoint) {
1189 return -1;
1190 }
1191 return num_to_read;
Mark Salyzyn1ebdb352015-05-15 09:01:58 -07001192}
1193
1194/*
1195 * Convert to printable from message to p buffer, return string length. If p is
1196 * NULL, do not copy, but still return the expected string length.
1197 */
Mark Salyzyn6e315682017-03-09 08:09:43 -08001198static size_t convertPrintable(char* p, const char* message, size_t messageLen) {
1199 char* begin = p;
1200 bool print = p != NULL;
Mark Salyzyn1ebdb352015-05-15 09:01:58 -07001201
Mark Salyzyn6e315682017-03-09 08:09:43 -08001202 while (messageLen) {
1203 char buf[6];
1204 ssize_t len = sizeof(buf) - 1;
1205 if ((size_t)len > messageLen) {
1206 len = messageLen;
1207 }
1208 len = utf8_character_length(message, len);
1209
1210 if (len < 0) {
Tom Cherryf623f022019-01-10 10:37:36 -08001211 snprintf(buf, sizeof(buf), ((messageLen > 1) && isdigit(message[1])) ? "\\%03o" : "\\%o",
Mark Salyzyn6e315682017-03-09 08:09:43 -08001212 *message & 0377);
1213 len = 1;
1214 } else {
1215 buf[0] = '\0';
1216 if (len == 1) {
1217 if (*message == '\a') {
1218 strcpy(buf, "\\a");
1219 } else if (*message == '\b') {
1220 strcpy(buf, "\\b");
1221 } else if (*message == '\t') {
1222 strcpy(buf, "\t"); /* Do not escape tabs */
1223 } else if (*message == '\v') {
1224 strcpy(buf, "\\v");
1225 } else if (*message == '\f') {
1226 strcpy(buf, "\\f");
1227 } else if (*message == '\r') {
1228 strcpy(buf, "\\r");
1229 } else if (*message == '\\') {
1230 strcpy(buf, "\\\\");
1231 } else if ((*message < ' ') || (*message & 0x80)) {
1232 snprintf(buf, sizeof(buf), "\\%o", *message & 0377);
Mark Salyzyn1ebdb352015-05-15 09:01:58 -07001233 }
Mark Salyzyn6e315682017-03-09 08:09:43 -08001234 }
1235 if (!buf[0]) {
1236 strncpy(buf, message, len);
1237 buf[len] = '\0';
1238 }
Mark Salyzyn1ebdb352015-05-15 09:01:58 -07001239 }
Mark Salyzyn6e315682017-03-09 08:09:43 -08001240 if (print) {
1241 strcpy(p, buf);
1242 }
1243 p += strlen(buf);
1244 message += len;
1245 messageLen -= len;
1246 }
1247 return p - begin;
Mark Salyzyn1ebdb352015-05-15 09:01:58 -07001248}
1249
Mark Salyzyn6e315682017-03-09 08:09:43 -08001250static char* readSeconds(char* e, struct timespec* t) {
1251 unsigned long multiplier;
1252 char* p;
1253 t->tv_sec = strtoul(e, &p, 10);
1254 if (*p != '.') {
1255 return NULL;
1256 }
1257 t->tv_nsec = 0;
1258 multiplier = NS_PER_SEC;
1259 while (isdigit(*++p) && (multiplier /= 10)) {
1260 t->tv_nsec += (*p - '0') * multiplier;
1261 }
1262 return p;
Mark Salyzyn4d0473f2015-08-31 15:53:41 -07001263}
1264
Tom Cherryf623f022019-01-10 10:37:36 -08001265static struct timespec* sumTimespec(struct timespec* left, struct timespec* right) {
Mark Salyzyn6e315682017-03-09 08:09:43 -08001266 left->tv_nsec += right->tv_nsec;
1267 left->tv_sec += right->tv_sec;
1268 if (left->tv_nsec >= (long)NS_PER_SEC) {
1269 left->tv_nsec -= NS_PER_SEC;
1270 left->tv_sec += 1;
1271 }
1272 return left;
Mark Salyzyn4d0473f2015-08-31 15:53:41 -07001273}
1274
Tom Cherryf623f022019-01-10 10:37:36 -08001275static struct timespec* subTimespec(struct timespec* result, struct timespec* left,
Mark Salyzyn6e315682017-03-09 08:09:43 -08001276 struct timespec* right) {
1277 result->tv_nsec = left->tv_nsec - right->tv_nsec;
1278 result->tv_sec = left->tv_sec - right->tv_sec;
1279 if (result->tv_nsec < 0) {
1280 result->tv_nsec += NS_PER_SEC;
1281 result->tv_sec -= 1;
1282 }
1283 return result;
Mark Salyzyn4d0473f2015-08-31 15:53:41 -07001284}
1285
Mark Salyzyn6e315682017-03-09 08:09:43 -08001286static long long nsecTimespec(struct timespec* now) {
1287 return (long long)now->tv_sec * NS_PER_SEC + now->tv_nsec;
Mark Salyzyn4d0473f2015-08-31 15:53:41 -07001288}
1289
Mark Salyzyn7ad91922016-03-08 16:18:26 -08001290#ifdef __ANDROID__
Tom Cherryf623f022019-01-10 10:37:36 -08001291static void convertMonotonic(struct timespec* result, const AndroidLogEntry* entry) {
Mark Salyzyn6e315682017-03-09 08:09:43 -08001292 struct listnode* node;
1293 struct conversionList {
1294 struct listnode node; /* first */
1295 struct timespec time;
1296 struct timespec convert;
1297 } * list, *next;
1298 struct timespec time, convert;
Mark Salyzyn4d0473f2015-08-31 15:53:41 -07001299
Mark Salyzyn6e315682017-03-09 08:09:43 -08001300 /* If we do not have a conversion list, build one up */
1301 if (list_empty(&convertHead)) {
1302 bool suspended_pending = false;
Tom Cherryf623f022019-01-10 10:37:36 -08001303 struct timespec suspended_monotonic = {0, 0};
1304 struct timespec suspended_diff = {0, 0};
Mark Salyzyn4d0473f2015-08-31 15:53:41 -07001305
Mark Salyzyn6e315682017-03-09 08:09:43 -08001306 /*
1307 * Read dmesg for _some_ synchronization markers and insert
1308 * Anything in the Android Logger before the dmesg logging span will
1309 * be highly suspect regarding the monotonic time calculations.
1310 */
1311 FILE* p = popen("/system/bin/dmesg", "re");
1312 if (p) {
1313 char* line = NULL;
1314 size_t len = 0;
1315 while (getline(&line, &len, p) > 0) {
1316 static const char suspend[] = "PM: suspend entry ";
1317 static const char resume[] = "PM: suspend exit ";
1318 static const char healthd[] = "healthd";
1319 static const char battery[] = ": battery ";
1320 static const char suspended[] = "Suspended for ";
1321 struct timespec monotonic;
1322 struct tm tm;
1323 char *cp, *e = line;
1324 bool add_entry = true;
Mark Salyzyn4d0473f2015-08-31 15:53:41 -07001325
Mark Salyzyn6e315682017-03-09 08:09:43 -08001326 if (*e == '<') {
1327 while (*e && (*e != '>')) {
1328 ++e;
1329 }
1330 if (*e != '>') {
1331 continue;
1332 }
Mark Salyzyn4d0473f2015-08-31 15:53:41 -07001333 }
Mark Salyzyn6e315682017-03-09 08:09:43 -08001334 if (*e != '[') {
1335 continue;
1336 }
1337 while (*++e == ' ') {
1338 ;
1339 }
1340 e = readSeconds(e, &monotonic);
1341 if (!e || (*e != ']')) {
1342 continue;
1343 }
1344
1345 if ((e = strstr(e, suspend))) {
1346 e += sizeof(suspend) - 1;
1347 } else if ((e = strstr(line, resume))) {
1348 e += sizeof(resume) - 1;
1349 } else if (((e = strstr(line, healthd))) &&
1350 ((e = strstr(e + sizeof(healthd) - 1, battery)))) {
1351 /* NB: healthd is roughly 150us late, worth the price to
1352 * deal with ntp-induced or hardware clock drift. */
1353 e += sizeof(battery) - 1;
1354 } else if ((e = strstr(line, suspended))) {
1355 e += sizeof(suspended) - 1;
1356 e = readSeconds(e, &time);
1357 if (!e) {
1358 continue;
1359 }
1360 add_entry = false;
1361 suspended_pending = true;
1362 suspended_monotonic = monotonic;
1363 suspended_diff = time;
1364 } else {
1365 continue;
1366 }
1367 if (add_entry) {
1368 /* look for "????-??-?? ??:??:??.????????? UTC" */
1369 cp = strstr(e, " UTC");
1370 if (!cp || ((cp - e) < 29) || (cp[-10] != '.')) {
1371 continue;
1372 }
1373 e = cp - 29;
1374 cp = readSeconds(cp - 10, &time);
1375 if (!cp) {
1376 continue;
1377 }
1378 cp = strptime(e, "%Y-%m-%d %H:%M:%S.", &tm);
1379 if (!cp) {
1380 continue;
1381 }
1382 cp = getenv(tz);
1383 if (cp) {
1384 cp = strdup(cp);
1385 }
1386 setenv(tz, utc, 1);
1387 time.tv_sec = mktime(&tm);
1388 if (cp) {
1389 setenv(tz, cp, 1);
1390 free(cp);
1391 } else {
1392 unsetenv(tz);
1393 }
Tom Cherryf623f022019-01-10 10:37:36 -08001394 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn6e315682017-03-09 08:09:43 -08001395 list_init(&list->node);
1396 list->time = time;
1397 subTimespec(&list->convert, &time, &monotonic);
1398 list_add_tail(&convertHead, &list->node);
1399 }
1400 if (suspended_pending && !list_empty(&convertHead)) {
Tom Cherryf623f022019-01-10 10:37:36 -08001401 list = node_to_item(list_tail(&convertHead), struct conversionList, node);
Mark Salyzyn6e315682017-03-09 08:09:43 -08001402 if (subTimespec(&time, subTimespec(&time, &list->time, &list->convert),
1403 &suspended_monotonic)
1404 ->tv_sec > 0) {
1405 /* resume, what is convert factor before? */
Mark Salyzyn4d0473f2015-08-31 15:53:41 -07001406 subTimespec(&convert, &list->convert, &suspended_diff);
Mark Salyzyn6e315682017-03-09 08:09:43 -08001407 } else {
1408 /* suspend */
1409 convert = list->convert;
1410 }
1411 time = suspended_monotonic;
1412 sumTimespec(&time, &convert);
1413 /* breakpoint just before sleep */
Tom Cherryf623f022019-01-10 10:37:36 -08001414 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn6e315682017-03-09 08:09:43 -08001415 list_init(&list->node);
1416 list->time = time;
1417 list->convert = convert;
1418 list_add_tail(&convertHead, &list->node);
1419 /* breakpoint just after sleep */
Tom Cherryf623f022019-01-10 10:37:36 -08001420 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn6e315682017-03-09 08:09:43 -08001421 list_init(&list->node);
1422 list->time = time;
1423 sumTimespec(&list->time, &suspended_diff);
1424 list->convert = convert;
1425 sumTimespec(&list->convert, &suspended_diff);
1426 list_add_tail(&convertHead, &list->node);
1427 suspended_pending = false;
Mark Salyzyn4d0473f2015-08-31 15:53:41 -07001428 }
Mark Salyzyn6e315682017-03-09 08:09:43 -08001429 }
1430 pclose(p);
Mark Salyzyn4d0473f2015-08-31 15:53:41 -07001431 }
Mark Salyzyn6e315682017-03-09 08:09:43 -08001432 /* last entry is our current time conversion */
Tom Cherryf623f022019-01-10 10:37:36 -08001433 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn6e315682017-03-09 08:09:43 -08001434 list_init(&list->node);
1435 clock_gettime(CLOCK_REALTIME, &list->time);
1436 clock_gettime(CLOCK_MONOTONIC, &convert);
1437 clock_gettime(CLOCK_MONOTONIC, &time);
1438 /* Correct for instant clock_gettime latency (syscall or ~30ns) */
1439 subTimespec(&time, &convert, subTimespec(&time, &time, &convert));
1440 /* Calculate conversion factor */
1441 subTimespec(&list->convert, &list->time, &time);
1442 list_add_tail(&convertHead, &list->node);
1443 if (suspended_pending) {
1444 /* manufacture a suspend @ point before */
1445 subTimespec(&convert, &list->convert, &suspended_diff);
1446 time = suspended_monotonic;
1447 sumTimespec(&time, &convert);
1448 /* breakpoint just after sleep */
Tom Cherryf623f022019-01-10 10:37:36 -08001449 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn6e315682017-03-09 08:09:43 -08001450 list_init(&list->node);
1451 list->time = time;
1452 sumTimespec(&list->time, &suspended_diff);
1453 list->convert = convert;
1454 sumTimespec(&list->convert, &suspended_diff);
1455 list_add_head(&convertHead, &list->node);
1456 /* breakpoint just before sleep */
Tom Cherryf623f022019-01-10 10:37:36 -08001457 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn6e315682017-03-09 08:09:43 -08001458 list_init(&list->node);
1459 list->time = time;
1460 list->convert = convert;
1461 list_add_head(&convertHead, &list->node);
1462 }
1463 }
Mark Salyzyn4d0473f2015-08-31 15:53:41 -07001464
Mark Salyzyn6e315682017-03-09 08:09:43 -08001465 /* Find the breakpoint in the conversion list */
1466 list = node_to_item(list_head(&convertHead), struct conversionList, node);
1467 next = NULL;
1468 list_for_each(node, &convertHead) {
1469 next = node_to_item(node, struct conversionList, node);
1470 if (entry->tv_sec < next->time.tv_sec) {
1471 break;
1472 } else if (entry->tv_sec == next->time.tv_sec) {
1473 if (entry->tv_nsec < next->time.tv_nsec) {
1474 break;
1475 }
1476 }
1477 list = next;
1478 }
1479
1480 /* blend time from one breakpoint to the next */
1481 convert = list->convert;
1482 if (next) {
1483 unsigned long long total, run;
1484
1485 total = nsecTimespec(subTimespec(&time, &next->time, &list->time));
1486 time.tv_sec = entry->tv_sec;
1487 time.tv_nsec = entry->tv_nsec;
1488 run = nsecTimespec(subTimespec(&time, &time, &list->time));
1489 if (run < total) {
1490 long long crun;
1491
1492 float f = nsecTimespec(subTimespec(&time, &next->convert, &convert));
1493 f *= run;
1494 f /= total;
1495 crun = f;
1496 convert.tv_sec += crun / (long long)NS_PER_SEC;
1497 if (crun < 0) {
1498 convert.tv_nsec -= (-crun) % NS_PER_SEC;
1499 if (convert.tv_nsec < 0) {
1500 convert.tv_nsec += NS_PER_SEC;
1501 convert.tv_sec -= 1;
Mark Salyzyn4d0473f2015-08-31 15:53:41 -07001502 }
Mark Salyzyn6e315682017-03-09 08:09:43 -08001503 } else {
1504 convert.tv_nsec += crun % NS_PER_SEC;
1505 if (convert.tv_nsec >= (long)NS_PER_SEC) {
1506 convert.tv_nsec -= NS_PER_SEC;
1507 convert.tv_sec += 1;
Mark Salyzyn4d0473f2015-08-31 15:53:41 -07001508 }
Mark Salyzyn6e315682017-03-09 08:09:43 -08001509 }
Mark Salyzyn4d0473f2015-08-31 15:53:41 -07001510 }
Mark Salyzyn6e315682017-03-09 08:09:43 -08001511 }
Mark Salyzyn4d0473f2015-08-31 15:53:41 -07001512
Mark Salyzyn6e315682017-03-09 08:09:43 -08001513 /* Apply the correction factor */
1514 result->tv_sec = entry->tv_sec;
1515 result->tv_nsec = entry->tv_nsec;
1516 subTimespec(result, result, &convert);
Mark Salyzyn4d0473f2015-08-31 15:53:41 -07001517}
Mark Salyzyn7ad91922016-03-08 16:18:26 -08001518#endif
Mark Salyzyn4d0473f2015-08-31 15:53:41 -07001519
The Android Open Source Project32315d42008-10-21 07:00:00 -07001520/**
1521 * Formats a log message into a buffer
1522 *
1523 * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer
1524 * If return value != defaultBuffer, caller must call free()
1525 * Returns NULL on malloc error
1526 */
1527
Tom Cherryf623f022019-01-10 10:37:36 -08001528LIBLOG_ABI_PUBLIC char* android_log_formatLogLine(AndroidLogFormat* p_format, char* defaultBuffer,
Mark Salyzyn6e315682017-03-09 08:09:43 -08001529 size_t defaultBufferSize,
1530 const AndroidLogEntry* entry,
1531 size_t* p_outLength) {
Yabin Cui945b83f2014-11-13 10:02:08 -08001532#if !defined(_WIN32)
Mark Salyzyn6e315682017-03-09 08:09:43 -08001533 struct tm tmBuf;
The Android Open Source Project32315d42008-10-21 07:00:00 -07001534#endif
Mark Salyzyn6e315682017-03-09 08:09:43 -08001535 struct tm* ptm;
1536 /* good margin, 23+nul for msec, 26+nul for usec, 29+nul to nsec */
1537 char timeBuf[64];
1538 char prefixBuf[128], suffixBuf[128];
1539 char priChar;
1540 int prefixSuffixIsHeaderFooter = 0;
1541 char* ret;
1542 time_t now;
1543 unsigned long nsec;
The Android Open Source Project32315d42008-10-21 07:00:00 -07001544
Mark Salyzyn6e315682017-03-09 08:09:43 -08001545 priChar = filterPriToChar(entry->priority);
1546 size_t prefixLen = 0, suffixLen = 0;
1547 size_t len;
The Android Open Source Project32315d42008-10-21 07:00:00 -07001548
Mark Salyzyn6e315682017-03-09 08:09:43 -08001549 /*
1550 * Get the current date/time in pretty form
1551 *
1552 * It's often useful when examining a log with "less" to jump to
1553 * a specific point in the file by searching for the date/time stamp.
1554 * For this reason it's very annoying to have regexp meta characters
1555 * in the time stamp. Don't use forward slashes, parenthesis,
1556 * brackets, asterisks, or other special chars here.
1557 *
1558 * The caller may have affected the timezone environment, this is
1559 * expected to be sensitive to that.
1560 */
1561 now = entry->tv_sec;
1562 nsec = entry->tv_nsec;
Mark Salyzyn7ad91922016-03-08 16:18:26 -08001563#if __ANDROID__
Mark Salyzyn6e315682017-03-09 08:09:43 -08001564 if (p_format->monotonic_output) {
1565 /* prevent convertMonotonic from being called if logd is monotonic */
1566 if (android_log_clockid() != CLOCK_MONOTONIC) {
1567 struct timespec time;
1568 convertMonotonic(&time, entry);
1569 now = time.tv_sec;
1570 nsec = time.tv_nsec;
Mark Salyzyn4d0473f2015-08-31 15:53:41 -07001571 }
Mark Salyzyn6e315682017-03-09 08:09:43 -08001572 }
Mark Salyzyn7ad91922016-03-08 16:18:26 -08001573#endif
Mark Salyzyn6e315682017-03-09 08:09:43 -08001574 if (now < 0) {
1575 nsec = NS_PER_SEC - nsec;
1576 }
1577 if (p_format->epoch_output || p_format->monotonic_output) {
1578 ptm = NULL;
Tom Cherryf623f022019-01-10 10:37:36 -08001579 snprintf(timeBuf, sizeof(timeBuf), p_format->monotonic_output ? "%6lld" : "%19lld",
1580 (long long)now);
Mark Salyzyn6e315682017-03-09 08:09:43 -08001581 } else {
Yabin Cui945b83f2014-11-13 10:02:08 -08001582#if !defined(_WIN32)
Mark Salyzyn6e315682017-03-09 08:09:43 -08001583 ptm = localtime_r(&now, &tmBuf);
The Android Open Source Project32315d42008-10-21 07:00:00 -07001584#else
Mark Salyzyn6e315682017-03-09 08:09:43 -08001585 ptm = localtime(&now);
The Android Open Source Project32315d42008-10-21 07:00:00 -07001586#endif
Tom Cherryf623f022019-01-10 10:37:36 -08001587 strftime(timeBuf, sizeof(timeBuf), &"%Y-%m-%d %H:%M:%S"[p_format->year_output ? 0 : 3], ptm);
Mark Salyzyn6e315682017-03-09 08:09:43 -08001588 }
1589 len = strlen(timeBuf);
1590 if (p_format->nsec_time_output) {
1591 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%09ld", nsec);
1592 } else if (p_format->usec_time_output) {
Tom Cherryf623f022019-01-10 10:37:36 -08001593 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%06ld", nsec / US_PER_NSEC);
Mark Salyzyn6e315682017-03-09 08:09:43 -08001594 } else {
Tom Cherryf623f022019-01-10 10:37:36 -08001595 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%03ld", nsec / MS_PER_NSEC);
Mark Salyzyn6e315682017-03-09 08:09:43 -08001596 }
1597 if (p_format->zone_output && ptm) {
1598 strftime(timeBuf + len, sizeof(timeBuf) - len, " %z", ptm);
1599 }
The Android Open Source Project32315d42008-10-21 07:00:00 -07001600
Mark Salyzyn6e315682017-03-09 08:09:43 -08001601 /*
1602 * Construct a buffer containing the log header and log message.
1603 */
1604 if (p_format->colored_output) {
Tom Cherryf623f022019-01-10 10:37:36 -08001605 prefixLen =
1606 snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm", colorFromPri(entry->priority));
Mark Salyzyn6e315682017-03-09 08:09:43 -08001607 prefixLen = MIN(prefixLen, sizeof(prefixBuf));
George Burgess IV3d8ca6b2018-05-15 18:56:36 -07001608
1609 const char suffixContents[] = "\x1B[0m";
1610 strcpy(suffixBuf, suffixContents);
1611 suffixLen = strlen(suffixContents);
Mark Salyzyn6e315682017-03-09 08:09:43 -08001612 }
The Android Open Source Project32315d42008-10-21 07:00:00 -07001613
Mark Salyzyn6e315682017-03-09 08:09:43 -08001614 char uid[16];
1615 uid[0] = '\0';
1616 if (p_format->uid_output) {
1617 if (entry->uid >= 0) {
1618/*
1619 * This code is Android specific, bionic guarantees that
1620 * calls to non-reentrant getpwuid() are thread safe.
1621 */
Mark Salyzyn7ad91922016-03-08 16:18:26 -08001622#if !defined(__MINGW32__)
1623#if (FAKE_LOG_DEVICE == 0)
William Roberts6877c6c2016-04-08 12:13:17 -07001624#ifndef __BIONIC__
Tom Cherryf623f022019-01-10 10:37:36 -08001625#warning "This code assumes that getpwuid is thread safe, only true with Bionic!"
William Roberts6877c6c2016-04-08 12:13:17 -07001626#endif
Mark Salyzyn7ad91922016-03-08 16:18:26 -08001627#endif
Mark Salyzyn6e315682017-03-09 08:09:43 -08001628 struct passwd* pwd = getpwuid(entry->uid);
1629 if (pwd && (strlen(pwd->pw_name) <= 5)) {
1630 snprintf(uid, sizeof(uid), "%5s:", pwd->pw_name);
1631 } else
Mark Salyzyn7ad91922016-03-08 16:18:26 -08001632#endif
Mark Salyzyn6e315682017-03-09 08:09:43 -08001633 {
1634 /* Not worth parsing package list, names all longer than 5 */
1635 snprintf(uid, sizeof(uid), "%5d:", entry->uid);
1636 }
The Android Open Source Project32315d42008-10-21 07:00:00 -07001637 } else {
Mark Salyzyn6e315682017-03-09 08:09:43 -08001638 snprintf(uid, sizeof(uid), " ");
The Android Open Source Project32315d42008-10-21 07:00:00 -07001639 }
Mark Salyzyn6e315682017-03-09 08:09:43 -08001640 }
1641
1642 switch (p_format->format) {
1643 case FORMAT_TAG:
Tom Cherryf623f022019-01-10 10:37:36 -08001644 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c/%-8.*s: ", priChar,
1645 (int)entry->tagLen, entry->tag);
Mark Salyzyn6e315682017-03-09 08:09:43 -08001646 strcpy(suffixBuf + suffixLen, "\n");
1647 ++suffixLen;
1648 break;
1649 case FORMAT_PROCESS:
Tom Cherryf623f022019-01-10 10:37:36 -08001650 len = snprintf(suffixBuf + suffixLen, sizeof(suffixBuf) - suffixLen, " (%.*s)\n",
1651 (int)entry->tagLen, entry->tag);
Mark Salyzyn6e315682017-03-09 08:09:43 -08001652 suffixLen += MIN(len, sizeof(suffixBuf) - suffixLen);
Tom Cherryf623f022019-01-10 10:37:36 -08001653 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c(%s%5d) ", priChar,
1654 uid, entry->pid);
Mark Salyzyn6e315682017-03-09 08:09:43 -08001655 break;
1656 case FORMAT_THREAD:
Tom Cherryf623f022019-01-10 10:37:36 -08001657 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c(%s%5d:%5d) ",
1658 priChar, uid, entry->pid, entry->tid);
Mark Salyzyn6e315682017-03-09 08:09:43 -08001659 strcpy(suffixBuf + suffixLen, "\n");
1660 ++suffixLen;
1661 break;
1662 case FORMAT_RAW:
1663 prefixBuf[prefixLen] = 0;
1664 len = 0;
1665 strcpy(suffixBuf + suffixLen, "\n");
1666 ++suffixLen;
1667 break;
1668 case FORMAT_TIME:
1669 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherryf623f022019-01-10 10:37:36 -08001670 "%s %c/%-8.*s(%s%5d): ", timeBuf, priChar, (int)entry->tagLen, entry->tag, uid,
1671 entry->pid);
Mark Salyzyn6e315682017-03-09 08:09:43 -08001672 strcpy(suffixBuf + suffixLen, "\n");
1673 ++suffixLen;
1674 break;
1675 case FORMAT_THREADTIME:
1676 ret = strchr(uid, ':');
1677 if (ret) {
1678 *ret = ' ';
1679 }
1680 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherryf623f022019-01-10 10:37:36 -08001681 "%s %s%5d %5d %c %-8.*s: ", timeBuf, uid, entry->pid, entry->tid, priChar,
1682 (int)entry->tagLen, entry->tag);
Mark Salyzyn6e315682017-03-09 08:09:43 -08001683 strcpy(suffixBuf + suffixLen, "\n");
1684 ++suffixLen;
1685 break;
1686 case FORMAT_LONG:
1687 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherryf623f022019-01-10 10:37:36 -08001688 "[ %s %s%5d:%5d %c/%-8.*s ]\n", timeBuf, uid, entry->pid, entry->tid, priChar,
1689 (int)entry->tagLen, entry->tag);
Mark Salyzyn6e315682017-03-09 08:09:43 -08001690 strcpy(suffixBuf + suffixLen, "\n\n");
1691 suffixLen += 2;
1692 prefixSuffixIsHeaderFooter = 1;
1693 break;
1694 case FORMAT_BRIEF:
1695 default:
Tom Cherryf623f022019-01-10 10:37:36 -08001696 len =
1697 snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
1698 "%c/%-8.*s(%s%5d): ", priChar, (int)entry->tagLen, entry->tag, uid, entry->pid);
Mark Salyzyn6e315682017-03-09 08:09:43 -08001699 strcpy(suffixBuf + suffixLen, "\n");
1700 ++suffixLen;
1701 break;
1702 }
1703
1704 /* snprintf has a weird return value. It returns what would have been
1705 * written given a large enough buffer. In the case that the prefix is
1706 * longer then our buffer(128), it messes up the calculations below
1707 * possibly causing heap corruption. To avoid this we double check and
1708 * set the length at the maximum (size minus null byte)
1709 */
1710 prefixLen += len;
1711 if (prefixLen >= sizeof(prefixBuf)) {
1712 prefixLen = sizeof(prefixBuf) - 1;
1713 prefixBuf[sizeof(prefixBuf) - 1] = '\0';
1714 }
1715 if (suffixLen >= sizeof(suffixBuf)) {
1716 suffixLen = sizeof(suffixBuf) - 1;
1717 suffixBuf[sizeof(suffixBuf) - 2] = '\n';
1718 suffixBuf[sizeof(suffixBuf) - 1] = '\0';
1719 }
1720
1721 /* the following code is tragically unreadable */
1722
1723 size_t numLines;
1724 char* p;
1725 size_t bufferSize;
1726 const char* pm;
1727
1728 if (prefixSuffixIsHeaderFooter) {
1729 /* we're just wrapping message with a header/footer */
1730 numLines = 1;
1731 } else {
1732 pm = entry->message;
1733 numLines = 0;
The Android Open Source Project32315d42008-10-21 07:00:00 -07001734
Mark Salyzyn1ebdb352015-05-15 09:01:58 -07001735 /*
Mark Salyzyn6e315682017-03-09 08:09:43 -08001736 * The line-end finding here must match the line-end finding
1737 * in for ( ... numLines...) loop below
Mark Salyzyn1ebdb352015-05-15 09:01:58 -07001738 */
Mark Salyzyn6e315682017-03-09 08:09:43 -08001739 while (pm < (entry->message + entry->messageLen)) {
1740 if (*pm++ == '\n') numLines++;
1741 }
1742 /* plus one line for anything not newline-terminated at the end */
1743 if (pm > entry->message && *(pm - 1) != '\n') numLines++;
1744 }
1745
1746 /*
1747 * this is an upper bound--newlines in message may be counted
1748 * extraneously
1749 */
1750 bufferSize = (numLines * (prefixLen + suffixLen)) + 1;
1751 if (p_format->printable_output) {
1752 /* Calculate extra length to convert non-printable to printable */
1753 bufferSize += convertPrintable(NULL, entry->message, entry->messageLen);
1754 } else {
1755 bufferSize += entry->messageLen;
1756 }
1757
1758 if (defaultBufferSize >= bufferSize) {
1759 ret = defaultBuffer;
1760 } else {
1761 ret = (char*)malloc(bufferSize);
1762
1763 if (ret == NULL) {
1764 return ret;
1765 }
1766 }
1767
1768 ret[0] = '\0'; /* to start strcat off */
1769
1770 p = ret;
1771 pm = entry->message;
1772
1773 if (prefixSuffixIsHeaderFooter) {
1774 strcat(p, prefixBuf);
1775 p += prefixLen;
Mark Salyzyn1ebdb352015-05-15 09:01:58 -07001776 if (p_format->printable_output) {
Mark Salyzyn6e315682017-03-09 08:09:43 -08001777 p += convertPrintable(p, entry->message, entry->messageLen);
Mark Salyzyn1ebdb352015-05-15 09:01:58 -07001778 } else {
Mark Salyzyn6e315682017-03-09 08:09:43 -08001779 strncat(p, entry->message, entry->messageLen);
1780 p += entry->messageLen;
Mark Salyzyn1ebdb352015-05-15 09:01:58 -07001781 }
Mark Salyzyn6e315682017-03-09 08:09:43 -08001782 strcat(p, suffixBuf);
1783 p += suffixLen;
1784 } else {
1785 do {
1786 const char* lineStart;
1787 size_t lineLen;
1788 lineStart = pm;
The Android Open Source Project32315d42008-10-21 07:00:00 -07001789
Mark Salyzyn6e315682017-03-09 08:09:43 -08001790 /* Find the next end-of-line in message */
1791 while (pm < (entry->message + entry->messageLen) && *pm != '\n') pm++;
1792 lineLen = pm - lineStart;
The Android Open Source Project32315d42008-10-21 07:00:00 -07001793
Mark Salyzyn6e315682017-03-09 08:09:43 -08001794 strcat(p, prefixBuf);
1795 p += prefixLen;
1796 if (p_format->printable_output) {
1797 p += convertPrintable(p, lineStart, lineLen);
1798 } else {
1799 strncat(p, lineStart, lineLen);
1800 p += lineLen;
1801 }
1802 strcat(p, suffixBuf);
1803 p += suffixLen;
The Android Open Source Project32315d42008-10-21 07:00:00 -07001804
Mark Salyzyn6e315682017-03-09 08:09:43 -08001805 if (*pm == '\n') pm++;
1806 } while (pm < (entry->message + entry->messageLen));
1807 }
The Android Open Source Project32315d42008-10-21 07:00:00 -07001808
Mark Salyzyn6e315682017-03-09 08:09:43 -08001809 if (p_outLength != NULL) {
1810 *p_outLength = p - ret;
1811 }
The Android Open Source Project32315d42008-10-21 07:00:00 -07001812
Mark Salyzyn6e315682017-03-09 08:09:43 -08001813 return ret;
The Android Open Source Project32315d42008-10-21 07:00:00 -07001814}
1815
1816/**
1817 * Either print or do not print log line, based on filter
1818 *
1819 * Returns count bytes written
1820 */
1821
Tom Cherryf623f022019-01-10 10:37:36 -08001822LIBLOG_ABI_PUBLIC int android_log_printLogLine(AndroidLogFormat* p_format, int fd,
Mark Salyzyn6e315682017-03-09 08:09:43 -08001823 const AndroidLogEntry* entry) {
1824 int ret;
1825 char defaultBuffer[512];
1826 char* outBuffer = NULL;
1827 size_t totalLen;
The Android Open Source Project32315d42008-10-21 07:00:00 -07001828
Tom Cherryf623f022019-01-10 10:37:36 -08001829 outBuffer =
1830 android_log_formatLogLine(p_format, defaultBuffer, sizeof(defaultBuffer), entry, &totalLen);
The Android Open Source Project32315d42008-10-21 07:00:00 -07001831
Mark Salyzyn6e315682017-03-09 08:09:43 -08001832 if (!outBuffer) return -1;
The Android Open Source Project32315d42008-10-21 07:00:00 -07001833
Mark Salyzyn6e315682017-03-09 08:09:43 -08001834 do {
1835 ret = write(fd, outBuffer, totalLen);
1836 } while (ret < 0 && errno == EINTR);
The Android Open Source Project32315d42008-10-21 07:00:00 -07001837
Mark Salyzyn6e315682017-03-09 08:09:43 -08001838 if (ret < 0) {
1839 fprintf(stderr, "+++ LOG: write failed (errno=%d)\n", errno);
1840 ret = 0;
1841 goto done;
1842 }
The Android Open Source Project32315d42008-10-21 07:00:00 -07001843
Mark Salyzyn6e315682017-03-09 08:09:43 -08001844 if (((size_t)ret) < totalLen) {
1845 fprintf(stderr, "+++ LOG: write partial (%d of %d)\n", ret, (int)totalLen);
1846 goto done;
1847 }
The Android Open Source Project32315d42008-10-21 07:00:00 -07001848
1849done:
Mark Salyzyn6e315682017-03-09 08:09:43 -08001850 if (outBuffer != defaultBuffer) {
1851 free(outBuffer);
1852 }
The Android Open Source Project32315d42008-10-21 07:00:00 -07001853
Mark Salyzyn6e315682017-03-09 08:09:43 -08001854 return ret;
The Android Open Source Project32315d42008-10-21 07:00:00 -07001855}