blob: 82fbafd70dc761c7e7cf18dabeb3408cba2bd6a8 [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
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080018#ifndef __MINGW32__
19#define HAVE_STRSEP
20#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070021
Mark Salyzyna04464a2014-04-30 08:50:53 -070022#include <assert.h>
23#include <ctype.h>
24#include <errno.h>
Mark Salyzyn4fd05072016-10-18 11:30:11 -070025#include <inttypes.h>
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080026#ifndef __MINGW32__
William Roberts8a5b9ca2016-04-08 12:13:17 -070027#include <pwd.h>
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080028#endif
Mark Salyzyna04464a2014-04-30 08:50:53 -070029#include <stdint.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
Pierre Zurekead88fc2010-10-17 22:39:37 +020033#include <sys/param.h>
William Roberts8a5b9ca2016-04-08 12:13:17 -070034#include <sys/types.h>
Tom Cherry91589842019-04-25 13:10:30 -070035#include <wchar.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070036
Mark Salyzyn4cbed022015-08-31 15:53:41 -070037#include <cutils/list.h>
Tom Cherrybbbf0892019-10-09 10:53:37 -070038
Mark Salyzynaeaaf812016-09-30 13:30:33 -070039#include <log/log.h>
Colin Cross9227bd32013-07-23 16:59:20 -070040#include <log/logprint.h>
Tom Cherrybbbf0892019-10-09 10:53:37 -070041#include <private/android_logger.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070042
Mark Salyzyn018a96d2016-03-01 13:45:42 -080043#include "log_portability.h"
Mark Salyzynbe1d3c22016-03-10 08:25:33 -080044
Mark Salyzyn4cbed022015-08-31 15:53:41 -070045#define MS_PER_NSEC 1000000
46#define US_PER_NSEC 1000
47
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080048#ifndef MIN
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080049#define MIN(a, b) (((a) < (b)) ? (a) : (b))
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080050#endif
51
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070052typedef struct FilterInfo_t {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080053 char* mTag;
54 android_LogPriority mPri;
55 struct FilterInfo_t* p_next;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070056} FilterInfo;
57
58struct AndroidLogFormat_t {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080059 android_LogPriority global_pri;
60 FilterInfo* filters;
61 AndroidLogPrintFormat format;
62 bool colored_output;
63 bool usec_time_output;
64 bool nsec_time_output;
65 bool printable_output;
66 bool year_output;
67 bool zone_output;
68 bool epoch_output;
69 bool monotonic_output;
70 bool uid_output;
71 bool descriptive_output;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072};
73
Pierre Zurekead88fc2010-10-17 22:39:37 +020074/*
Mark Salyzyn4fd05072016-10-18 11:30:11 -070075 * API issues prevent us from exposing "descriptive" in AndroidLogFormat_t
76 * during android_log_processBinaryLogBuffer(), so we break layering.
77 */
78static bool descriptive_output = false;
79
80/*
Pierre Zurekead88fc2010-10-17 22:39:37 +020081 * gnome-terminal color tags
82 * See http://misc.flogisoft.com/bash/tip_colors_and_formatting
83 * for ideas on how to set the forground color of the text for xterm.
84 * The color manipulation character stream is defined as:
85 * ESC [ 3 8 ; 5 ; <color#> m
86 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080087#define ANDROID_COLOR_BLUE 75
Pierre Zurekead88fc2010-10-17 22:39:37 +020088#define ANDROID_COLOR_DEFAULT 231
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080089#define ANDROID_COLOR_GREEN 40
90#define ANDROID_COLOR_ORANGE 166
91#define ANDROID_COLOR_RED 196
92#define ANDROID_COLOR_YELLOW 226
Pierre Zurekead88fc2010-10-17 22:39:37 +020093
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080094static FilterInfo* filterinfo_new(const char* tag, android_LogPriority pri) {
95 FilterInfo* p_ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070096
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080097 p_ret = (FilterInfo*)calloc(1, sizeof(FilterInfo));
98 p_ret->mTag = strdup(tag);
99 p_ret->mPri = pri;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700100
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800101 return p_ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700102}
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 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800110static android_LogPriority filterCharToPri(char c) {
111 android_LogPriority pri;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700112
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800113 c = tolower(c);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700114
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800115 if (c >= '0' && c <= '9') {
116 if (c >= ('0' + ANDROID_LOG_SILENT)) {
117 pri = ANDROID_LOG_VERBOSE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700118 } else {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800119 pri = (android_LogPriority)(c - '0');
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700120 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800121 } else if (c == 'v') {
122 pri = ANDROID_LOG_VERBOSE;
123 } else if (c == 'd') {
124 pri = ANDROID_LOG_DEBUG;
125 } else if (c == 'i') {
126 pri = ANDROID_LOG_INFO;
127 } else if (c == 'w') {
128 pri = ANDROID_LOG_WARN;
129 } else if (c == 'e') {
130 pri = ANDROID_LOG_ERROR;
131 } else if (c == 'f') {
132 pri = ANDROID_LOG_FATAL;
133 } else if (c == 's') {
134 pri = ANDROID_LOG_SILENT;
135 } else if (c == '*') {
136 pri = ANDROID_LOG_DEFAULT;
137 } else {
138 pri = ANDROID_LOG_UNKNOWN;
139 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700140
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800141 return pri;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700142}
143
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800144static char filterPriToChar(android_LogPriority pri) {
145 switch (pri) {
146 /* clang-format off */
147 case ANDROID_LOG_VERBOSE: return 'V';
148 case ANDROID_LOG_DEBUG: return 'D';
149 case ANDROID_LOG_INFO: return 'I';
150 case ANDROID_LOG_WARN: return 'W';
151 case ANDROID_LOG_ERROR: return 'E';
152 case ANDROID_LOG_FATAL: return 'F';
153 case ANDROID_LOG_SILENT: return 'S';
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700154
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800155 case ANDROID_LOG_DEFAULT:
156 case ANDROID_LOG_UNKNOWN:
157 default: return '?';
Tom Cherry71ba1642019-01-10 10:37:36 -0800158 /* clang-format on */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800159 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700160}
161
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800162static int colorFromPri(android_LogPriority pri) {
163 switch (pri) {
164 /* clang-format off */
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;
Pierre Zurekead88fc2010-10-17 22:39:37 +0200172
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800173 case ANDROID_LOG_DEFAULT:
174 case ANDROID_LOG_UNKNOWN:
175 default: return ANDROID_COLOR_DEFAULT;
Tom Cherry71ba1642019-01-10 10:37:36 -0800176 /* clang-format on */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800177 }
Pierre Zurekead88fc2010-10-17 22:39:37 +0200178}
179
Tom Cherry71ba1642019-01-10 10:37:36 -0800180static android_LogPriority filterPriForTag(AndroidLogFormat* p_format, const char* tag) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800181 FilterInfo* p_curFilter;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700182
Tom Cherry71ba1642019-01-10 10:37:36 -0800183 for (p_curFilter = p_format->filters; p_curFilter != NULL; p_curFilter = p_curFilter->p_next) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800184 if (0 == strcmp(tag, p_curFilter->mTag)) {
185 if (p_curFilter->mPri == ANDROID_LOG_DEFAULT) {
186 return p_format->global_pri;
187 } else {
188 return p_curFilter->mPri;
189 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700190 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800191 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700192
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800193 return p_format->global_pri;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700194}
195
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700196/**
197 * returns 1 if this log line should be printed based on its priority
198 * and tag, and 0 if it should not
199 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800200int android_log_shouldPrintLine(AndroidLogFormat* p_format, const char* tag,
201 android_LogPriority pri) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800202 return pri >= filterPriForTag(p_format, tag);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700203}
204
Tom Cherry2d9779e2019-02-08 11:46:19 -0800205AndroidLogFormat* android_log_format_new() {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800206 AndroidLogFormat* p_ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700207
Tom Cherry71ba1642019-01-10 10:37:36 -0800208 p_ret = static_cast<AndroidLogFormat*>(calloc(1, sizeof(AndroidLogFormat)));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700209
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800210 p_ret->global_pri = ANDROID_LOG_VERBOSE;
211 p_ret->format = FORMAT_BRIEF;
212 p_ret->colored_output = false;
213 p_ret->usec_time_output = false;
214 p_ret->nsec_time_output = false;
215 p_ret->printable_output = false;
216 p_ret->year_output = false;
217 p_ret->zone_output = false;
218 p_ret->epoch_output = false;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800219#ifdef __ANDROID__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800220 p_ret->monotonic_output = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800221#else
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800222 p_ret->monotonic_output = false;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800223#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800224 p_ret->uid_output = false;
225 p_ret->descriptive_output = false;
226 descriptive_output = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700227
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800228 return p_ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700229}
230
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700231static list_declare(convertHead);
232
Tom Cherry2d9779e2019-02-08 11:46:19 -0800233void android_log_format_free(AndroidLogFormat* p_format) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800234 FilterInfo *p_info, *p_info_old;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700235
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800236 p_info = p_format->filters;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700237
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800238 while (p_info != NULL) {
239 p_info_old = p_info;
240 p_info = p_info->p_next;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700241
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800242 free(p_info_old);
243 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700244
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800245 free(p_format);
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700246
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800247 /* Free conversion resource, can always be reconstructed */
248 while (!list_empty(&convertHead)) {
249 struct listnode* node = list_head(&convertHead);
250 list_remove(node);
Ting-Yuan Huang249bd052017-08-15 17:01:33 -0700251 LOG_ALWAYS_FATAL_IF(node == list_head(&convertHead), "corrupted list");
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800252 free(node);
253 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700254}
255
Tom Cherry2d9779e2019-02-08 11:46:19 -0800256int android_log_setPrintFormat(AndroidLogFormat* p_format, AndroidLogPrintFormat format) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800257 switch (format) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700258 case FORMAT_MODIFIER_COLOR:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800259 p_format->colored_output = true;
260 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700261 case FORMAT_MODIFIER_TIME_USEC:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800262 p_format->usec_time_output = true;
263 return 0;
Mark Salyzyn9b4d7e12017-01-30 09:16:09 -0800264 case FORMAT_MODIFIER_TIME_NSEC:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800265 p_format->nsec_time_output = true;
266 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700267 case FORMAT_MODIFIER_PRINTABLE:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800268 p_format->printable_output = true;
269 return 0;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700270 case FORMAT_MODIFIER_YEAR:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800271 p_format->year_output = true;
272 return 0;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700273 case FORMAT_MODIFIER_ZONE:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800274 p_format->zone_output = !p_format->zone_output;
275 return 0;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700276 case FORMAT_MODIFIER_EPOCH:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800277 p_format->epoch_output = true;
278 return 0;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700279 case FORMAT_MODIFIER_MONOTONIC:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800280 p_format->monotonic_output = true;
281 return 0;
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800282 case FORMAT_MODIFIER_UID:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800283 p_format->uid_output = true;
284 return 0;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700285 case FORMAT_MODIFIER_DESCRIPT:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800286 p_format->descriptive_output = true;
287 descriptive_output = true;
288 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700289 default:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800290 break;
291 }
292 p_format->format = format;
293 return 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700294}
295
Tom Cherry7d045f62019-09-30 12:58:55 -0700296#ifndef __MINGW32__
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700297static const char tz[] = "TZ";
298static const char utc[] = "UTC";
Tom Cherry7d045f62019-09-30 12:58:55 -0700299#endif
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700300
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301/**
302 * Returns FORMAT_OFF on invalid string
303 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800304AndroidLogPrintFormat android_log_formatFromString(const char* formatString) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800305 static AndroidLogPrintFormat format;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700306
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800307 /* clang-format off */
308 if (!strcmp(formatString, "brief")) format = FORMAT_BRIEF;
309 else if (!strcmp(formatString, "process")) format = FORMAT_PROCESS;
310 else if (!strcmp(formatString, "tag")) format = FORMAT_TAG;
311 else if (!strcmp(formatString, "thread")) format = FORMAT_THREAD;
312 else if (!strcmp(formatString, "raw")) format = FORMAT_RAW;
313 else if (!strcmp(formatString, "time")) format = FORMAT_TIME;
314 else if (!strcmp(formatString, "threadtime")) format = FORMAT_THREADTIME;
315 else if (!strcmp(formatString, "long")) format = FORMAT_LONG;
316 else if (!strcmp(formatString, "color")) format = FORMAT_MODIFIER_COLOR;
317 else if (!strcmp(formatString, "colour")) format = FORMAT_MODIFIER_COLOR;
318 else if (!strcmp(formatString, "usec")) format = FORMAT_MODIFIER_TIME_USEC;
319 else if (!strcmp(formatString, "nsec")) format = FORMAT_MODIFIER_TIME_NSEC;
320 else if (!strcmp(formatString, "printable")) format = FORMAT_MODIFIER_PRINTABLE;
321 else if (!strcmp(formatString, "year")) format = FORMAT_MODIFIER_YEAR;
322 else if (!strcmp(formatString, "zone")) format = FORMAT_MODIFIER_ZONE;
323 else if (!strcmp(formatString, "epoch")) format = FORMAT_MODIFIER_EPOCH;
324 else if (!strcmp(formatString, "monotonic")) format = FORMAT_MODIFIER_MONOTONIC;
325 else if (!strcmp(formatString, "uid")) format = FORMAT_MODIFIER_UID;
326 else if (!strcmp(formatString, "descriptive")) format = FORMAT_MODIFIER_DESCRIPT;
Tom Cherry71ba1642019-01-10 10:37:36 -0800327 /* clang-format on */
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800328
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800329#ifndef __MINGW32__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800330 else {
331 extern char* tzname[2];
332 static const char gmt[] = "GMT";
333 char* cp = getenv(tz);
334 if (cp) {
335 cp = strdup(cp);
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700336 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800337 setenv(tz, formatString, 1);
338 /*
339 * Run tzset here to determine if the timezone is legitimate. If the
340 * zone is GMT, check if that is what was asked for, if not then
341 * did not match any on the system; report an error to caller.
342 */
343 tzset();
344 if (!tzname[0] ||
Tom Cherry71ba1642019-01-10 10:37:36 -0800345 ((!strcmp(tzname[0], utc) || !strcmp(tzname[0], gmt)) /* error? */
346 && strcasecmp(formatString, utc) && strcasecmp(formatString, gmt))) { /* ok */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800347 if (cp) {
348 setenv(tz, cp, 1);
349 } else {
350 unsetenv(tz);
351 }
352 tzset();
353 format = FORMAT_OFF;
354 } else {
355 format = FORMAT_MODIFIER_ZONE;
356 }
357 free(cp);
358 }
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800359#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700360
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800361 return format;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700362}
363
364/**
365 * filterExpression: a single filter expression
366 * eg "AT:d"
367 *
368 * returns 0 on success and -1 on invalid expression
369 *
370 * Assumes single threaded execution
371 */
372
Tom Cherry2d9779e2019-02-08 11:46:19 -0800373int android_log_addFilterRule(AndroidLogFormat* p_format, const char* filterExpression) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800374 size_t tagNameLength;
375 android_LogPriority pri = ANDROID_LOG_DEFAULT;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700376
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800377 tagNameLength = strcspn(filterExpression, ":");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700378
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800379 if (tagNameLength == 0) {
380 goto error;
381 }
382
383 if (filterExpression[tagNameLength] == ':') {
384 pri = filterCharToPri(filterExpression[tagNameLength + 1]);
385
386 if (pri == ANDROID_LOG_UNKNOWN) {
387 goto error;
388 }
389 }
390
391 if (0 == strncmp("*", filterExpression, tagNameLength)) {
392 /*
393 * This filter expression refers to the global filter
394 * The default level for this is DEBUG if the priority
395 * is unspecified
396 */
397 if (pri == ANDROID_LOG_DEFAULT) {
398 pri = ANDROID_LOG_DEBUG;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700399 }
400
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800401 p_format->global_pri = pri;
402 } else {
403 /*
404 * for filter expressions that don't refer to the global
405 * filter, the default is verbose if the priority is unspecified
406 */
407 if (pri == ANDROID_LOG_DEFAULT) {
408 pri = ANDROID_LOG_VERBOSE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700409 }
410
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800411 char* tagName;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700412
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700413/*
414 * Presently HAVE_STRNDUP is never defined, so the second case is always taken
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800415 * Darwin doesn't have strndup, everything else does
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700416 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700417#ifdef HAVE_STRNDUP
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800418 tagName = strndup(filterExpression, tagNameLength);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700419#else
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800420 /* a few extra bytes copied... */
421 tagName = strdup(filterExpression);
422 tagName[tagNameLength] = '\0';
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700423#endif /*HAVE_STRNDUP*/
424
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800425 FilterInfo* p_fi = filterinfo_new(tagName, pri);
426 free(tagName);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700427
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800428 p_fi->p_next = p_format->filters;
429 p_format->filters = p_fi;
430 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700431
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800432 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700433error:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800434 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700435}
436
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800437#ifndef HAVE_STRSEP
438/* KISS replacement helper for below */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800439static char* strsep(char** stringp, const char* delim) {
440 char* token;
441 char* ret = *stringp;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800442
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800443 if (!ret || !*ret) {
444 return NULL;
445 }
446 token = strpbrk(ret, delim);
447 if (token) {
448 *token = '\0';
449 ++token;
450 } else {
451 token = ret + strlen(ret);
452 }
453 *stringp = token;
454 return ret;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800455}
456#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700457
458/**
459 * filterString: a comma/whitespace-separated set of filter expressions
460 *
461 * eg "AT:d *:i"
462 *
463 * returns 0 on success and -1 on invalid expression
464 *
465 * Assumes single threaded execution
466 *
467 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800468int android_log_addFilterString(AndroidLogFormat* p_format, const char* filterString) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800469 char* filterStringCopy = strdup(filterString);
470 char* p_cur = filterStringCopy;
471 char* p_ret;
472 int err;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700473
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800474 /* Yes, I'm using strsep */
475 while (NULL != (p_ret = strsep(&p_cur, " \t,"))) {
476 /* ignore whitespace-only entries */
477 if (p_ret[0] != '\0') {
478 err = android_log_addFilterRule(p_format, p_ret);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700479
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800480 if (err < 0) {
481 goto error;
482 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700483 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800484 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700485
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800486 free(filterStringCopy);
487 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700488error:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800489 free(filterStringCopy);
490 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700491}
492
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700493/**
494 * Splits a wire-format buffer into an AndroidLogEntry
495 * entry allocated by caller. Pointers will point directly into buf
496 *
497 * Returns 0 on success and -1 on invalid wire format (entry will be
498 * in unspecified state)
499 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800500int android_log_processLogBuffer(struct logger_entry* buf, AndroidLogEntry* entry) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800501 entry->message = NULL;
502 entry->messageLen = 0;
Mark Salyzynb1d150b2017-03-02 07:47:21 -0800503
Mark Salyzyn2ed51d72017-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 Root4bf3c022011-09-30 17:10:14 -0700509
Mark Salyzyn2ed51d72017-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 Root4bf3c022011-09-30 17:10:14 -0700522 /*
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800523 * An well-formed entry must consist of at least a priority
524 * and two null characters
Kenny Root4bf3c022011-09-30 17:10:14 -0700525 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800526 fprintf(stderr, "+++ LOG: entry too small\n");
527 return -1;
528 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700529
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800530 int msgStart = -1;
531 int msgEnd = -1;
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700532
Mark Salyzyn2ed51d72017-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 Salyzyn40b21552013-12-18 12:59:01 -0800541 }
Mark Salyzyn2ed51d72017-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 Kraleviche1ede152011-10-18 15:23:33 -0700560 for (i = 1; i < buf->len; i++) {
Mark Salyzyn2ed51d72017-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 Kraleviche1ede152011-10-18 15:23:33 -0700567 }
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700568 if (msgStart == -1) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800569 msgStart = buf->len - 1; /* All tag, no message, print truncates */
Nick Kralevich63f4a842011-10-17 10:45:03 -0700570 }
Mark Salyzyn2ed51d72017-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 Sharkeya820a0e2011-10-26 18:40:39 -0700577
Tom Cherry71ba1642019-01-10 10:37:36 -0800578 entry->priority = static_cast<android_LogPriority>(msg[0]);
Mark Salyzyn2ed51d72017-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 Kralevich63f4a842011-10-17 10:45:03 -0700583
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800584 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700585}
586
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700587static bool findChar(const char** cp, size_t* len, int c) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800588 while ((*len) && isspace(*(*cp))) {
589 ++(*cp);
590 --(*len);
591 }
592 if (c == INT_MAX) return *len;
593 if ((*len) && (*(*cp) == c)) {
594 ++(*cp);
595 --(*len);
596 return true;
597 }
598 return false;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700599}
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000600
601/*
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700602 * Recursively convert binary log data to printable form.
603 *
604 * This needs to be recursive because you can have lists of lists.
605 *
606 * If we run out of room, we stop processing immediately. It's important
607 * for us to check for space on every output element to avoid producing
608 * garbled output.
609 *
610 * Returns 0 on success, 1 on buffer full, -1 on failure.
611 */
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700612enum objectType {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800613 TYPE_OBJECTS = '1',
614 TYPE_BYTES = '2',
615 TYPE_MILLISECONDS = '3',
616 TYPE_ALLOCATIONS = '4',
617 TYPE_ID = '5',
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800618 TYPE_PERCENT = '6',
619 TYPE_MONOTONIC = 's'
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700620};
621
Tom Cherry71ba1642019-01-10 10:37:36 -0800622static int android_log_printBinaryEvent(const unsigned char** pEventData, size_t* pEventDataLen,
623 char** pOutBuf, size_t* pOutBufLen, const char** fmtStr,
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800624 size_t* fmtLen) {
625 const unsigned char* eventData = *pEventData;
626 size_t eventDataLen = *pEventDataLen;
627 char* outBuf = *pOutBuf;
628 char* outBufSave = outBuf;
629 size_t outBufLen = *pOutBufLen;
630 size_t outBufLenSave = outBufLen;
631 unsigned char type;
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800632 size_t outCount = 0;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800633 int result = 0;
634 const char* cp;
635 size_t len;
636 int64_t lval;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700637
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800638 if (eventDataLen < 1) return -1;
Mark Salyzyn1a57ae32016-11-11 14:41:30 -0800639
Tom Cherrybbbf0892019-10-09 10:53:37 -0700640 type = *eventData;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700641
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800642 cp = NULL;
643 len = 0;
644 if (fmtStr && *fmtStr && fmtLen && *fmtLen && **fmtStr) {
645 cp = *fmtStr;
646 len = *fmtLen;
647 }
648 /*
649 * event.logtag format specification:
650 *
651 * Optionally, after the tag names can be put a description for the value(s)
652 * of the tag. Description are in the format
653 * (<name>|data type[|data unit])
654 * Multiple values are separated by commas.
655 *
656 * The data type is a number from the following values:
657 * 1: int
658 * 2: long
659 * 3: string
660 * 4: list
661 * 5: float
662 *
663 * The data unit is a number taken from the following list:
664 * 1: Number of objects
665 * 2: Number of bytes
666 * 3: Number of milliseconds
667 * 4: Number of allocations
668 * 5: Id
669 * 6: Percent
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800670 * s: Number of seconds (monotonic time)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800671 * Default value for data of type int/long is 2 (bytes).
672 */
673 if (!cp || !findChar(&cp, &len, '(')) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700674 len = 0;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800675 } else {
676 char* outBufLastSpace = NULL;
677
678 findChar(&cp, &len, INT_MAX);
679 while (len && *cp && (*cp != '|') && (*cp != ')')) {
680 if (outBufLen <= 0) {
681 /* halt output */
682 goto no_room;
683 }
684 outBufLastSpace = isspace(*cp) ? outBuf : NULL;
685 *outBuf = *cp;
686 ++outBuf;
687 ++cp;
688 --outBufLen;
689 --len;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700690 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800691 if (outBufLastSpace) {
692 outBufLen += outBuf - outBufLastSpace;
693 outBuf = outBufLastSpace;
694 }
695 if (outBufLen <= 0) {
696 /* halt output */
697 goto no_room;
698 }
699 if (outBufSave != outBuf) {
700 *outBuf = '=';
701 ++outBuf;
702 --outBufLen;
703 }
704
705 if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800706 static const unsigned char typeTable[] = {EVENT_TYPE_INT, EVENT_TYPE_LONG, EVENT_TYPE_STRING,
707 EVENT_TYPE_LIST, EVENT_TYPE_FLOAT};
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800708
Tom Cherry71ba1642019-01-10 10:37:36 -0800709 if ((*cp >= '1') && (*cp < (char)('1' + (sizeof(typeTable) / sizeof(typeTable[0])))) &&
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800710 (type != typeTable[(size_t)(*cp - '1')]))
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700711 len = 0;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700712
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800713 if (len) {
714 ++cp;
715 --len;
716 } else {
717 /* reset the format */
718 outBuf = outBufSave;
719 outBufLen = outBufLenSave;
720 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700721 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800722 }
723 outCount = 0;
724 lval = 0;
725 switch (type) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700726 case EVENT_TYPE_INT:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800727 /* 32-bit signed int */
728 {
Tom Cherrybbbf0892019-10-09 10:53:37 -0700729 if (eventDataLen < sizeof(android_event_int_t)) return -1;
730 auto* event_int = reinterpret_cast<const android_event_int_t*>(eventData);
731 lval = event_int->data;
732 eventData += sizeof(android_event_int_t);
733 eventDataLen -= sizeof(android_event_int_t);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800734 }
735 goto pr_lval;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700736 case EVENT_TYPE_LONG:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800737 /* 64-bit signed long */
Tom Cherrybbbf0892019-10-09 10:53:37 -0700738 if (eventDataLen < sizeof(android_event_long_t)) {
739 return -1;
740 }
741 {
742 auto* event_long = reinterpret_cast<const android_event_long_t*>(eventData);
743 lval = event_long->data;
744 }
745 eventData += sizeof(android_event_long_t);
746 eventDataLen -= sizeof(android_event_long_t);
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700747 pr_lval:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800748 outCount = snprintf(outBuf, outBufLen, "%" PRId64, lval);
749 if (outCount < outBufLen) {
750 outBuf += outCount;
751 outBufLen -= outCount;
752 } else {
753 /* halt output */
754 goto no_room;
755 }
756 break;
Jeff Brown44193d92015-04-28 12:47:02 -0700757 case EVENT_TYPE_FLOAT:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800758 /* float */
759 {
Tom Cherrybbbf0892019-10-09 10:53:37 -0700760 if (eventDataLen < sizeof(android_event_float_t)) return -1;
761 auto* event_float = reinterpret_cast<const android_event_float_t*>(eventData);
762 float fval = event_float->data;
763 eventData += sizeof(android_event_int_t);
764 eventDataLen -= sizeof(android_event_int_t);
Jeff Brown44193d92015-04-28 12:47:02 -0700765
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800766 outCount = snprintf(outBuf, outBufLen, "%f", fval);
767 if (outCount < outBufLen) {
768 outBuf += outCount;
769 outBufLen -= outCount;
770 } else {
771 /* halt output */
772 goto no_room;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700773 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800774 }
775 break;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700776 case EVENT_TYPE_STRING:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800777 /* UTF-8 chars, not NULL-terminated */
778 {
Tom Cherrybbbf0892019-10-09 10:53:37 -0700779 if (eventDataLen < sizeof(android_event_string_t)) return -1;
780 auto* event_string = reinterpret_cast<const android_event_string_t*>(eventData);
781 unsigned int strLen = event_string->length;
782 eventData += sizeof(android_event_string_t);
783 eventDataLen -= sizeof(android_event_string_t);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700784
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800785 if (eventDataLen < strLen) {
786 result = -1; /* mark truncated */
787 strLen = eventDataLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700788 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700789
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800790 if (cp && (strLen == 0)) {
791 /* reset the format if no content */
792 outBuf = outBufSave;
793 outBufLen = outBufLenSave;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700794 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800795 if (strLen < outBufLen) {
796 memcpy(outBuf, eventData, strLen);
797 outBuf += strLen;
798 outBufLen -= strLen;
799 } else {
800 if (outBufLen > 0) {
801 /* copy what we can */
802 memcpy(outBuf, eventData, outBufLen);
803 outBuf += outBufLen;
804 outBufLen -= outBufLen;
805 }
806 if (!result) result = 1; /* if not truncated, return no room */
807 }
808 eventData += strLen;
809 eventDataLen -= strLen;
810 if (result != 0) goto bail;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700811 break;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800812 }
813 case EVENT_TYPE_LIST:
814 /* N items, all different types */
815 {
Tom Cherrybbbf0892019-10-09 10:53:37 -0700816 if (eventDataLen < sizeof(android_event_list_t)) return -1;
817 auto* event_list = reinterpret_cast<const android_event_list_t*>(eventData);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800818
Tom Cherrybbbf0892019-10-09 10:53:37 -0700819 int8_t count = event_list->element_count;
820 eventData += sizeof(android_event_list_t);
821 eventDataLen -= sizeof(android_event_list_t);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800822
823 if (outBufLen <= 0) goto no_room;
824
825 *outBuf++ = '[';
826 outBufLen--;
827
Tom Cherrybbbf0892019-10-09 10:53:37 -0700828 for (int i = 0; i < count; i++) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800829 result = android_log_printBinaryEvent(&eventData, &eventDataLen, &outBuf, &outBufLen,
830 fmtStr, fmtLen);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800831 if (result != 0) goto bail;
832
833 if (i < (count - 1)) {
834 if (outBufLen <= 0) goto no_room;
835 *outBuf++ = ',';
836 outBufLen--;
837 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700838 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800839
840 if (outBufLen <= 0) goto no_room;
841
842 *outBuf++ = ']';
843 outBufLen--;
844 }
845 break;
846 default:
847 fprintf(stderr, "Unknown binary event type %d\n", type);
848 return -1;
849 }
850 if (cp && len) {
851 if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) {
852 switch (*cp) {
853 case TYPE_OBJECTS:
854 outCount = 0;
855 /* outCount = snprintf(outBuf, outBufLen, " objects"); */
856 break;
857 case TYPE_BYTES:
858 if ((lval != 0) && ((lval % 1024) == 0)) {
859 /* repaint with multiplier */
Tom Cherry71ba1642019-01-10 10:37:36 -0800860 static const char suffixTable[] = {'K', 'M', 'G', 'T'};
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800861 size_t idx = 0;
862 outBuf -= outCount;
863 outBufLen += outCount;
864 do {
865 lval /= 1024;
866 if ((lval % 1024) != 0) break;
Tom Cherry71ba1642019-01-10 10:37:36 -0800867 } while (++idx < ((sizeof(suffixTable) / sizeof(suffixTable[0])) - 1));
868 outCount = snprintf(outBuf, outBufLen, "%" PRId64 "%cB", lval, suffixTable[idx]);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800869 } else {
870 outCount = snprintf(outBuf, outBufLen, "B");
871 }
872 break;
873 case TYPE_MILLISECONDS:
Tom Cherry71ba1642019-01-10 10:37:36 -0800874 if (((lval <= -1000) || (1000 <= lval)) && (outBufLen || (outBuf[-1] == '0'))) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800875 /* repaint as (fractional) seconds, possibly saving space */
876 if (outBufLen) outBuf[0] = outBuf[-1];
877 outBuf[-1] = outBuf[-2];
878 outBuf[-2] = outBuf[-3];
879 outBuf[-3] = '.';
880 while ((outBufLen == 0) || (*outBuf == '0')) {
881 --outBuf;
882 ++outBufLen;
883 }
884 if (*outBuf != '.') {
885 ++outBuf;
886 --outBufLen;
887 }
888 outCount = snprintf(outBuf, outBufLen, "s");
889 } else {
890 outCount = snprintf(outBuf, outBufLen, "ms");
891 }
892 break;
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800893 case TYPE_MONOTONIC: {
894 static const uint64_t minute = 60;
895 static const uint64_t hour = 60 * minute;
896 static const uint64_t day = 24 * hour;
897
898 /* Repaint as unsigned seconds, minutes, hours ... */
899 outBuf -= outCount;
900 outBufLen += outCount;
901 uint64_t val = lval;
902 if (val >= day) {
903 outCount = snprintf(outBuf, outBufLen, "%" PRIu64 "d ", val / day);
904 if (outCount >= outBufLen) break;
905 outBuf += outCount;
906 outBufLen -= outCount;
907 val = (val % day) + day;
908 }
909 if (val >= minute) {
910 if (val >= hour) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800911 outCount = snprintf(outBuf, outBufLen, "%" PRIu64 ":", (val / hour) % (day / hour));
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800912 if (outCount >= outBufLen) break;
913 outBuf += outCount;
914 outBufLen -= outCount;
915 }
916 outCount =
Tom Cherry71ba1642019-01-10 10:37:36 -0800917 snprintf(outBuf, outBufLen, (val >= hour) ? "%02" PRIu64 ":" : "%" PRIu64 ":",
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800918 (val / minute) % (hour / minute));
919 if (outCount >= outBufLen) break;
920 outBuf += outCount;
921 outBufLen -= outCount;
922 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800923 outCount = snprintf(outBuf, outBufLen, (val >= minute) ? "%02" PRIu64 : "%" PRIu64 "s",
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800924 val % minute);
925 } break;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800926 case TYPE_ALLOCATIONS:
927 outCount = 0;
928 /* outCount = snprintf(outBuf, outBufLen, " allocations"); */
929 break;
930 case TYPE_ID:
931 outCount = 0;
932 break;
933 case TYPE_PERCENT:
934 outCount = snprintf(outBuf, outBufLen, "%%");
935 break;
936 default: /* ? */
937 outCount = 0;
938 break;
939 }
940 ++cp;
941 --len;
942 if (outCount < outBufLen) {
943 outBuf += outCount;
944 outBufLen -= outCount;
945 } else if (outCount) {
946 /* halt output */
947 goto no_room;
948 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700949 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800950 if (!findChar(&cp, &len, ')')) len = 0;
951 if (!findChar(&cp, &len, ',')) len = 0;
952 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700953
954bail:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800955 *pEventData = eventData;
956 *pEventDataLen = eventDataLen;
957 *pOutBuf = outBuf;
958 *pOutBufLen = outBufLen;
959 if (cp) {
960 *fmtStr = cp;
961 *fmtLen = len;
962 }
963 return result;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700964
965no_room:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800966 result = 1;
967 goto bail;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700968}
969
970/**
971 * Convert a binary log entry to ASCII form.
972 *
973 * For convenience we mimic the processLogBuffer API. There is no
974 * pre-defined output length for the binary data, since we're free to format
975 * it however we choose, which means we can't really use a fixed-size buffer
976 * here.
977 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800978int android_log_processBinaryLogBuffer(
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800979 struct logger_entry* buf, AndroidLogEntry* entry,
Tom Cherry71ba1642019-01-10 10:37:36 -0800980 [[maybe_unused]] const EventTagMap* map, /* only on !__ANDROID__ */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800981 char* messageBuf, int messageBufLen) {
982 size_t inCount;
983 uint32_t tagIndex;
984 const unsigned char* eventData;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700985
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800986 entry->message = NULL;
987 entry->messageLen = 0;
Mark Salyzynb1d150b2017-03-02 07:47:21 -0800988
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800989 entry->tv_sec = buf->sec;
990 entry->tv_nsec = buf->nsec;
991 entry->priority = ANDROID_LOG_INFO;
992 entry->uid = -1;
993 entry->pid = buf->pid;
994 entry->tid = buf->tid;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700995
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800996 /*
997 * Pull the tag out, fill in some additional details based on incoming
998 * buffer version (v3 adds lid, v4 adds uid).
999 */
1000 eventData = (const unsigned char*)buf->msg;
1001 struct logger_entry_v2* buf2 = (struct logger_entry_v2*)buf;
1002 if (buf2->hdr_size) {
1003 if ((buf2->hdr_size < sizeof(((struct log_msg*)NULL)->entry_v1)) ||
1004 (buf2->hdr_size > sizeof(((struct log_msg*)NULL)->entry))) {
1005 fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
1006 return -1;
1007 }
1008 eventData = ((unsigned char*)buf2) + buf2->hdr_size;
1009 if ((buf2->hdr_size >= sizeof(struct logger_entry_v3)) &&
1010 (((struct logger_entry_v3*)buf)->lid == LOG_ID_SECURITY)) {
1011 entry->priority = ANDROID_LOG_WARN;
1012 }
1013 if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) {
1014 entry->uid = ((struct logger_entry_v4*)buf)->uid;
1015 }
1016 }
1017 inCount = buf->len;
Tom Cherrybbbf0892019-10-09 10:53:37 -07001018 if (inCount < sizeof(android_event_header_t)) return -1;
1019 auto* event_header = reinterpret_cast<const android_event_header_t*>(eventData);
1020 tagIndex = event_header->tag;
1021 eventData += sizeof(android_event_header_t);
1022 inCount -= sizeof(android_event_header_t);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001023
1024 entry->tagLen = 0;
1025 entry->tag = NULL;
1026#ifdef __ANDROID__
1027 if (map != NULL) {
1028 entry->tag = android_lookupEventTag_len(map, &entry->tagLen, tagIndex);
1029 }
1030#endif
1031
1032 /*
1033 * If we don't have a map, or didn't find the tag number in the map,
1034 * stuff a generated tag value into the start of the output buffer and
1035 * shift the buffer pointers down.
1036 */
1037 if (entry->tag == NULL) {
1038 size_t tagLen;
1039
1040 tagLen = snprintf(messageBuf, messageBufLen, "[%" PRIu32 "]", tagIndex);
1041 if (tagLen >= (size_t)messageBufLen) {
1042 tagLen = messageBufLen - 1;
1043 }
1044 entry->tag = messageBuf;
1045 entry->tagLen = tagLen;
1046 messageBuf += tagLen + 1;
1047 messageBufLen -= tagLen + 1;
1048 }
1049
1050 /*
1051 * Format the event log data into the buffer.
1052 */
1053 const char* fmtStr = NULL;
1054 size_t fmtLen = 0;
1055#ifdef __ANDROID__
1056 if (descriptive_output && map) {
1057 fmtStr = android_lookupEventFormat_len(map, &fmtLen, tagIndex);
1058 }
1059#endif
1060
1061 char* outBuf = messageBuf;
1062 size_t outRemaining = messageBufLen - 1; /* leave one for nul byte */
1063 int result = 0;
1064
1065 if ((inCount > 0) || fmtLen) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001066 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, &outRemaining, &fmtStr,
1067 &fmtLen);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001068 }
1069 if ((result == 1) && fmtStr) {
1070 /* We overflowed :-(, let's repaint the line w/o format dressings */
Mark Salyzynb1d150b2017-03-02 07:47:21 -08001071 eventData = (const unsigned char*)buf->msg;
Mark Salyzyn40b21552013-12-18 12:59:01 -08001072 if (buf2->hdr_size) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001073 eventData = ((unsigned char*)buf2) + buf2->hdr_size;
Mark Salyzyn40b21552013-12-18 12:59:01 -08001074 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001075 eventData += 4;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001076 outBuf = messageBuf;
1077 outRemaining = messageBufLen - 1;
Tom Cherry71ba1642019-01-10 10:37:36 -08001078 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, &outRemaining, NULL, NULL);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001079 }
1080 if (result < 0) {
1081 fprintf(stderr, "Binary log entry conversion failed\n");
1082 }
1083 if (result) {
1084 if (!outRemaining) {
1085 /* make space to leave an indicator */
1086 --outBuf;
1087 ++outRemaining;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001088 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001089 *outBuf++ = (result < 0) ? '!' : '^'; /* Error or Truncation? */
1090 outRemaining--;
1091 /* pretend we ate all the data to prevent log stutter */
1092 inCount = 0;
1093 if (result > 0) result = 0;
1094 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001095
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001096 /* eat the silly terminating '\n' */
1097 if (inCount == 1 && *eventData == '\n') {
1098 eventData++;
1099 inCount--;
1100 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001101
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001102 if (inCount != 0) {
1103 fprintf(stderr, "Warning: leftover binary log data (%zu bytes)\n", inCount);
1104 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001105
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001106 /*
1107 * Terminate the buffer. The NUL byte does not count as part of
1108 * entry->messageLen.
1109 */
1110 *outBuf = '\0';
1111 entry->messageLen = outBuf - messageBuf;
1112 assert(entry->messageLen == (messageBufLen - 1) - outRemaining);
Mark Salyzyn1a57ae32016-11-11 14:41:30 -08001113
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001114 entry->message = messageBuf;
Mark Salyzyn1a57ae32016-11-11 14:41:30 -08001115
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001116 return result;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001117}
1118
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001119/*
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001120 * Convert to printable from message to p buffer, return string length. If p is
1121 * NULL, do not copy, but still return the expected string length.
1122 */
Tom Cherry91589842019-04-25 13:10:30 -07001123size_t convertPrintable(char* p, const char* message, size_t messageLen) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001124 char* begin = p;
1125 bool print = p != NULL;
Tom Cherry91589842019-04-25 13:10:30 -07001126 mbstate_t mb_state = {};
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001127
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001128 while (messageLen) {
1129 char buf[6];
1130 ssize_t len = sizeof(buf) - 1;
1131 if ((size_t)len > messageLen) {
1132 len = messageLen;
1133 }
Tom Cherry91589842019-04-25 13:10:30 -07001134 len = mbrtowc(nullptr, message, len, &mb_state);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001135
1136 if (len < 0) {
Tom Cherry91589842019-04-25 13:10:30 -07001137 snprintf(buf, sizeof(buf), "\\x%02X", static_cast<unsigned char>(*message));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001138 len = 1;
1139 } else {
1140 buf[0] = '\0';
1141 if (len == 1) {
1142 if (*message == '\a') {
1143 strcpy(buf, "\\a");
1144 } else if (*message == '\b') {
1145 strcpy(buf, "\\b");
1146 } else if (*message == '\t') {
1147 strcpy(buf, "\t"); /* Do not escape tabs */
1148 } else if (*message == '\v') {
1149 strcpy(buf, "\\v");
1150 } else if (*message == '\f') {
1151 strcpy(buf, "\\f");
1152 } else if (*message == '\r') {
1153 strcpy(buf, "\\r");
1154 } else if (*message == '\\') {
1155 strcpy(buf, "\\\\");
1156 } else if ((*message < ' ') || (*message & 0x80)) {
Tom Cherry91589842019-04-25 13:10:30 -07001157 snprintf(buf, sizeof(buf), "\\x%02X", static_cast<unsigned char>(*message));
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001158 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001159 }
1160 if (!buf[0]) {
1161 strncpy(buf, message, len);
1162 buf[len] = '\0';
1163 }
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001164 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001165 if (print) {
1166 strcpy(p, buf);
1167 }
1168 p += strlen(buf);
1169 message += len;
1170 messageLen -= len;
1171 }
1172 return p - begin;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001173}
1174
Tom Cherry7d045f62019-09-30 12:58:55 -07001175#ifdef __ANDROID__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001176static char* readSeconds(char* e, struct timespec* t) {
1177 unsigned long multiplier;
1178 char* p;
1179 t->tv_sec = strtoul(e, &p, 10);
1180 if (*p != '.') {
1181 return NULL;
1182 }
1183 t->tv_nsec = 0;
1184 multiplier = NS_PER_SEC;
1185 while (isdigit(*++p) && (multiplier /= 10)) {
1186 t->tv_nsec += (*p - '0') * multiplier;
1187 }
1188 return p;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001189}
1190
Tom Cherry71ba1642019-01-10 10:37:36 -08001191static struct timespec* sumTimespec(struct timespec* left, struct timespec* right) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001192 left->tv_nsec += right->tv_nsec;
1193 left->tv_sec += right->tv_sec;
1194 if (left->tv_nsec >= (long)NS_PER_SEC) {
1195 left->tv_nsec -= NS_PER_SEC;
1196 left->tv_sec += 1;
1197 }
1198 return left;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001199}
1200
Tom Cherry71ba1642019-01-10 10:37:36 -08001201static struct timespec* subTimespec(struct timespec* result, struct timespec* left,
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001202 struct timespec* right) {
1203 result->tv_nsec = left->tv_nsec - right->tv_nsec;
1204 result->tv_sec = left->tv_sec - right->tv_sec;
1205 if (result->tv_nsec < 0) {
1206 result->tv_nsec += NS_PER_SEC;
1207 result->tv_sec -= 1;
1208 }
1209 return result;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001210}
1211
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001212static long long nsecTimespec(struct timespec* now) {
1213 return (long long)now->tv_sec * NS_PER_SEC + now->tv_nsec;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001214}
1215
Tom Cherry71ba1642019-01-10 10:37:36 -08001216static void convertMonotonic(struct timespec* result, const AndroidLogEntry* entry) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001217 struct listnode* node;
1218 struct conversionList {
1219 struct listnode node; /* first */
1220 struct timespec time;
1221 struct timespec convert;
1222 } * list, *next;
1223 struct timespec time, convert;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001224
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001225 /* If we do not have a conversion list, build one up */
1226 if (list_empty(&convertHead)) {
1227 bool suspended_pending = false;
Tom Cherry71ba1642019-01-10 10:37:36 -08001228 struct timespec suspended_monotonic = {0, 0};
1229 struct timespec suspended_diff = {0, 0};
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001230
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001231 /*
1232 * Read dmesg for _some_ synchronization markers and insert
1233 * Anything in the Android Logger before the dmesg logging span will
1234 * be highly suspect regarding the monotonic time calculations.
1235 */
1236 FILE* p = popen("/system/bin/dmesg", "re");
1237 if (p) {
1238 char* line = NULL;
1239 size_t len = 0;
1240 while (getline(&line, &len, p) > 0) {
1241 static const char suspend[] = "PM: suspend entry ";
1242 static const char resume[] = "PM: suspend exit ";
1243 static const char healthd[] = "healthd";
1244 static const char battery[] = ": battery ";
1245 static const char suspended[] = "Suspended for ";
1246 struct timespec monotonic;
1247 struct tm tm;
1248 char *cp, *e = line;
1249 bool add_entry = true;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001250
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001251 if (*e == '<') {
1252 while (*e && (*e != '>')) {
1253 ++e;
1254 }
1255 if (*e != '>') {
1256 continue;
1257 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001258 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001259 if (*e != '[') {
1260 continue;
1261 }
1262 while (*++e == ' ') {
1263 ;
1264 }
1265 e = readSeconds(e, &monotonic);
1266 if (!e || (*e != ']')) {
1267 continue;
1268 }
1269
1270 if ((e = strstr(e, suspend))) {
1271 e += sizeof(suspend) - 1;
1272 } else if ((e = strstr(line, resume))) {
1273 e += sizeof(resume) - 1;
1274 } else if (((e = strstr(line, healthd))) &&
1275 ((e = strstr(e + sizeof(healthd) - 1, battery)))) {
1276 /* NB: healthd is roughly 150us late, worth the price to
1277 * deal with ntp-induced or hardware clock drift. */
1278 e += sizeof(battery) - 1;
1279 } else if ((e = strstr(line, suspended))) {
1280 e += sizeof(suspended) - 1;
1281 e = readSeconds(e, &time);
1282 if (!e) {
1283 continue;
1284 }
1285 add_entry = false;
1286 suspended_pending = true;
1287 suspended_monotonic = monotonic;
1288 suspended_diff = time;
1289 } else {
1290 continue;
1291 }
1292 if (add_entry) {
1293 /* look for "????-??-?? ??:??:??.????????? UTC" */
1294 cp = strstr(e, " UTC");
1295 if (!cp || ((cp - e) < 29) || (cp[-10] != '.')) {
1296 continue;
1297 }
1298 e = cp - 29;
1299 cp = readSeconds(cp - 10, &time);
1300 if (!cp) {
1301 continue;
1302 }
1303 cp = strptime(e, "%Y-%m-%d %H:%M:%S.", &tm);
1304 if (!cp) {
1305 continue;
1306 }
1307 cp = getenv(tz);
1308 if (cp) {
1309 cp = strdup(cp);
1310 }
1311 setenv(tz, utc, 1);
1312 time.tv_sec = mktime(&tm);
1313 if (cp) {
1314 setenv(tz, cp, 1);
1315 free(cp);
1316 } else {
1317 unsetenv(tz);
1318 }
Tom Cherry71ba1642019-01-10 10:37:36 -08001319 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001320 list_init(&list->node);
1321 list->time = time;
1322 subTimespec(&list->convert, &time, &monotonic);
1323 list_add_tail(&convertHead, &list->node);
1324 }
1325 if (suspended_pending && !list_empty(&convertHead)) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001326 list = node_to_item(list_tail(&convertHead), struct conversionList, node);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001327 if (subTimespec(&time, subTimespec(&time, &list->time, &list->convert),
1328 &suspended_monotonic)
1329 ->tv_sec > 0) {
1330 /* resume, what is convert factor before? */
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001331 subTimespec(&convert, &list->convert, &suspended_diff);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001332 } else {
1333 /* suspend */
1334 convert = list->convert;
1335 }
1336 time = suspended_monotonic;
1337 sumTimespec(&time, &convert);
1338 /* breakpoint just before sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001339 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001340 list_init(&list->node);
1341 list->time = time;
1342 list->convert = convert;
1343 list_add_tail(&convertHead, &list->node);
1344 /* breakpoint just after sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001345 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001346 list_init(&list->node);
1347 list->time = time;
1348 sumTimespec(&list->time, &suspended_diff);
1349 list->convert = convert;
1350 sumTimespec(&list->convert, &suspended_diff);
1351 list_add_tail(&convertHead, &list->node);
1352 suspended_pending = false;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001353 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001354 }
1355 pclose(p);
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001356 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001357 /* last entry is our current time conversion */
Tom Cherry71ba1642019-01-10 10:37:36 -08001358 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001359 list_init(&list->node);
1360 clock_gettime(CLOCK_REALTIME, &list->time);
1361 clock_gettime(CLOCK_MONOTONIC, &convert);
1362 clock_gettime(CLOCK_MONOTONIC, &time);
1363 /* Correct for instant clock_gettime latency (syscall or ~30ns) */
1364 subTimespec(&time, &convert, subTimespec(&time, &time, &convert));
1365 /* Calculate conversion factor */
1366 subTimespec(&list->convert, &list->time, &time);
1367 list_add_tail(&convertHead, &list->node);
1368 if (suspended_pending) {
1369 /* manufacture a suspend @ point before */
1370 subTimespec(&convert, &list->convert, &suspended_diff);
1371 time = suspended_monotonic;
1372 sumTimespec(&time, &convert);
1373 /* breakpoint just after sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001374 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001375 list_init(&list->node);
1376 list->time = time;
1377 sumTimespec(&list->time, &suspended_diff);
1378 list->convert = convert;
1379 sumTimespec(&list->convert, &suspended_diff);
1380 list_add_head(&convertHead, &list->node);
1381 /* breakpoint just before sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001382 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001383 list_init(&list->node);
1384 list->time = time;
1385 list->convert = convert;
1386 list_add_head(&convertHead, &list->node);
1387 }
1388 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001389
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001390 /* Find the breakpoint in the conversion list */
1391 list = node_to_item(list_head(&convertHead), struct conversionList, node);
1392 next = NULL;
1393 list_for_each(node, &convertHead) {
1394 next = node_to_item(node, struct conversionList, node);
1395 if (entry->tv_sec < next->time.tv_sec) {
1396 break;
1397 } else if (entry->tv_sec == next->time.tv_sec) {
1398 if (entry->tv_nsec < next->time.tv_nsec) {
1399 break;
1400 }
1401 }
1402 list = next;
1403 }
1404
1405 /* blend time from one breakpoint to the next */
1406 convert = list->convert;
1407 if (next) {
1408 unsigned long long total, run;
1409
1410 total = nsecTimespec(subTimespec(&time, &next->time, &list->time));
1411 time.tv_sec = entry->tv_sec;
1412 time.tv_nsec = entry->tv_nsec;
1413 run = nsecTimespec(subTimespec(&time, &time, &list->time));
1414 if (run < total) {
1415 long long crun;
1416
1417 float f = nsecTimespec(subTimespec(&time, &next->convert, &convert));
1418 f *= run;
1419 f /= total;
1420 crun = f;
1421 convert.tv_sec += crun / (long long)NS_PER_SEC;
1422 if (crun < 0) {
1423 convert.tv_nsec -= (-crun) % NS_PER_SEC;
1424 if (convert.tv_nsec < 0) {
1425 convert.tv_nsec += NS_PER_SEC;
1426 convert.tv_sec -= 1;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001427 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001428 } else {
1429 convert.tv_nsec += crun % NS_PER_SEC;
1430 if (convert.tv_nsec >= (long)NS_PER_SEC) {
1431 convert.tv_nsec -= NS_PER_SEC;
1432 convert.tv_sec += 1;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001433 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001434 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001435 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001436 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001437
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001438 /* Apply the correction factor */
1439 result->tv_sec = entry->tv_sec;
1440 result->tv_nsec = entry->tv_nsec;
1441 subTimespec(result, result, &convert);
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001442}
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001443#endif
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001444
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001445/**
1446 * Formats a log message into a buffer
1447 *
1448 * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer
1449 * If return value != defaultBuffer, caller must call free()
1450 * Returns NULL on malloc error
1451 */
1452
Tom Cherry2d9779e2019-02-08 11:46:19 -08001453char* android_log_formatLogLine(AndroidLogFormat* p_format, char* defaultBuffer,
1454 size_t defaultBufferSize, const AndroidLogEntry* entry,
1455 size_t* p_outLength) {
Yabin Cui8a985352014-11-13 10:02:08 -08001456#if !defined(_WIN32)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001457 struct tm tmBuf;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001458#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001459 struct tm* ptm;
1460 /* good margin, 23+nul for msec, 26+nul for usec, 29+nul to nsec */
1461 char timeBuf[64];
1462 char prefixBuf[128], suffixBuf[128];
1463 char priChar;
1464 int prefixSuffixIsHeaderFooter = 0;
1465 char* ret;
1466 time_t now;
1467 unsigned long nsec;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001468
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001469 priChar = filterPriToChar(entry->priority);
1470 size_t prefixLen = 0, suffixLen = 0;
1471 size_t len;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001472
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001473 /*
1474 * Get the current date/time in pretty form
1475 *
1476 * It's often useful when examining a log with "less" to jump to
1477 * a specific point in the file by searching for the date/time stamp.
1478 * For this reason it's very annoying to have regexp meta characters
1479 * in the time stamp. Don't use forward slashes, parenthesis,
1480 * brackets, asterisks, or other special chars here.
1481 *
1482 * The caller may have affected the timezone environment, this is
1483 * expected to be sensitive to that.
1484 */
1485 now = entry->tv_sec;
1486 nsec = entry->tv_nsec;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001487#if __ANDROID__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001488 if (p_format->monotonic_output) {
1489 /* prevent convertMonotonic from being called if logd is monotonic */
1490 if (android_log_clockid() != CLOCK_MONOTONIC) {
1491 struct timespec time;
1492 convertMonotonic(&time, entry);
1493 now = time.tv_sec;
1494 nsec = time.tv_nsec;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001495 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001496 }
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001497#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001498 if (now < 0) {
1499 nsec = NS_PER_SEC - nsec;
1500 }
1501 if (p_format->epoch_output || p_format->monotonic_output) {
1502 ptm = NULL;
Tom Cherry71ba1642019-01-10 10:37:36 -08001503 snprintf(timeBuf, sizeof(timeBuf), p_format->monotonic_output ? "%6lld" : "%19lld",
1504 (long long)now);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001505 } else {
Yabin Cui8a985352014-11-13 10:02:08 -08001506#if !defined(_WIN32)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001507 ptm = localtime_r(&now, &tmBuf);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001508#else
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001509 ptm = localtime(&now);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001510#endif
Tom Cherry71ba1642019-01-10 10:37:36 -08001511 strftime(timeBuf, sizeof(timeBuf), &"%Y-%m-%d %H:%M:%S"[p_format->year_output ? 0 : 3], ptm);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001512 }
1513 len = strlen(timeBuf);
1514 if (p_format->nsec_time_output) {
1515 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%09ld", nsec);
1516 } else if (p_format->usec_time_output) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001517 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%06ld", nsec / US_PER_NSEC);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001518 } else {
Tom Cherry71ba1642019-01-10 10:37:36 -08001519 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%03ld", nsec / MS_PER_NSEC);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001520 }
1521 if (p_format->zone_output && ptm) {
1522 strftime(timeBuf + len, sizeof(timeBuf) - len, " %z", ptm);
1523 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001524
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001525 /*
1526 * Construct a buffer containing the log header and log message.
1527 */
1528 if (p_format->colored_output) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001529 prefixLen =
1530 snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm", colorFromPri(entry->priority));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001531 prefixLen = MIN(prefixLen, sizeof(prefixBuf));
George Burgess IV487de272018-05-15 18:56:36 -07001532
1533 const char suffixContents[] = "\x1B[0m";
1534 strcpy(suffixBuf, suffixContents);
1535 suffixLen = strlen(suffixContents);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001536 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001537
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001538 char uid[16];
1539 uid[0] = '\0';
1540 if (p_format->uid_output) {
1541 if (entry->uid >= 0) {
1542/*
1543 * This code is Android specific, bionic guarantees that
1544 * calls to non-reentrant getpwuid() are thread safe.
1545 */
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001546#if !defined(__MINGW32__)
1547#if (FAKE_LOG_DEVICE == 0)
William Roberts8a5b9ca2016-04-08 12:13:17 -07001548#ifndef __BIONIC__
Tom Cherry71ba1642019-01-10 10:37:36 -08001549#warning "This code assumes that getpwuid is thread safe, only true with Bionic!"
William Roberts8a5b9ca2016-04-08 12:13:17 -07001550#endif
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001551#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001552 struct passwd* pwd = getpwuid(entry->uid);
1553 if (pwd && (strlen(pwd->pw_name) <= 5)) {
1554 snprintf(uid, sizeof(uid), "%5s:", pwd->pw_name);
1555 } else
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001556#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001557 {
1558 /* Not worth parsing package list, names all longer than 5 */
1559 snprintf(uid, sizeof(uid), "%5d:", entry->uid);
1560 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001561 } else {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001562 snprintf(uid, sizeof(uid), " ");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001563 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001564 }
1565
1566 switch (p_format->format) {
1567 case FORMAT_TAG:
Tom Cherry71ba1642019-01-10 10:37:36 -08001568 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c/%-8.*s: ", priChar,
1569 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001570 strcpy(suffixBuf + suffixLen, "\n");
1571 ++suffixLen;
1572 break;
1573 case FORMAT_PROCESS:
Tom Cherry71ba1642019-01-10 10:37:36 -08001574 len = snprintf(suffixBuf + suffixLen, sizeof(suffixBuf) - suffixLen, " (%.*s)\n",
1575 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001576 suffixLen += MIN(len, sizeof(suffixBuf) - suffixLen);
Tom Cherry71ba1642019-01-10 10:37:36 -08001577 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c(%s%5d) ", priChar,
1578 uid, entry->pid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001579 break;
1580 case FORMAT_THREAD:
Tom Cherry71ba1642019-01-10 10:37:36 -08001581 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c(%s%5d:%5d) ",
1582 priChar, uid, entry->pid, entry->tid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001583 strcpy(suffixBuf + suffixLen, "\n");
1584 ++suffixLen;
1585 break;
1586 case FORMAT_RAW:
1587 prefixBuf[prefixLen] = 0;
1588 len = 0;
1589 strcpy(suffixBuf + suffixLen, "\n");
1590 ++suffixLen;
1591 break;
1592 case FORMAT_TIME:
1593 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherry71ba1642019-01-10 10:37:36 -08001594 "%s %c/%-8.*s(%s%5d): ", timeBuf, priChar, (int)entry->tagLen, entry->tag, uid,
1595 entry->pid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001596 strcpy(suffixBuf + suffixLen, "\n");
1597 ++suffixLen;
1598 break;
1599 case FORMAT_THREADTIME:
1600 ret = strchr(uid, ':');
1601 if (ret) {
1602 *ret = ' ';
1603 }
1604 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherry71ba1642019-01-10 10:37:36 -08001605 "%s %s%5d %5d %c %-8.*s: ", timeBuf, uid, entry->pid, entry->tid, priChar,
1606 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001607 strcpy(suffixBuf + suffixLen, "\n");
1608 ++suffixLen;
1609 break;
1610 case FORMAT_LONG:
1611 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherry71ba1642019-01-10 10:37:36 -08001612 "[ %s %s%5d:%5d %c/%-8.*s ]\n", timeBuf, uid, entry->pid, entry->tid, priChar,
1613 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001614 strcpy(suffixBuf + suffixLen, "\n\n");
1615 suffixLen += 2;
1616 prefixSuffixIsHeaderFooter = 1;
1617 break;
1618 case FORMAT_BRIEF:
1619 default:
Tom Cherry71ba1642019-01-10 10:37:36 -08001620 len =
1621 snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
1622 "%c/%-8.*s(%s%5d): ", priChar, (int)entry->tagLen, entry->tag, uid, entry->pid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001623 strcpy(suffixBuf + suffixLen, "\n");
1624 ++suffixLen;
1625 break;
1626 }
1627
1628 /* snprintf has a weird return value. It returns what would have been
1629 * written given a large enough buffer. In the case that the prefix is
1630 * longer then our buffer(128), it messes up the calculations below
1631 * possibly causing heap corruption. To avoid this we double check and
1632 * set the length at the maximum (size minus null byte)
1633 */
1634 prefixLen += len;
1635 if (prefixLen >= sizeof(prefixBuf)) {
1636 prefixLen = sizeof(prefixBuf) - 1;
1637 prefixBuf[sizeof(prefixBuf) - 1] = '\0';
1638 }
1639 if (suffixLen >= sizeof(suffixBuf)) {
1640 suffixLen = sizeof(suffixBuf) - 1;
1641 suffixBuf[sizeof(suffixBuf) - 2] = '\n';
1642 suffixBuf[sizeof(suffixBuf) - 1] = '\0';
1643 }
1644
1645 /* the following code is tragically unreadable */
1646
1647 size_t numLines;
1648 char* p;
1649 size_t bufferSize;
1650 const char* pm;
1651
1652 if (prefixSuffixIsHeaderFooter) {
1653 /* we're just wrapping message with a header/footer */
1654 numLines = 1;
1655 } else {
1656 pm = entry->message;
1657 numLines = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001658
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001659 /*
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001660 * The line-end finding here must match the line-end finding
1661 * in for ( ... numLines...) loop below
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001662 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001663 while (pm < (entry->message + entry->messageLen)) {
1664 if (*pm++ == '\n') numLines++;
1665 }
1666 /* plus one line for anything not newline-terminated at the end */
1667 if (pm > entry->message && *(pm - 1) != '\n') numLines++;
1668 }
1669
1670 /*
1671 * this is an upper bound--newlines in message may be counted
1672 * extraneously
1673 */
1674 bufferSize = (numLines * (prefixLen + suffixLen)) + 1;
1675 if (p_format->printable_output) {
1676 /* Calculate extra length to convert non-printable to printable */
1677 bufferSize += convertPrintable(NULL, entry->message, entry->messageLen);
1678 } else {
1679 bufferSize += entry->messageLen;
1680 }
1681
1682 if (defaultBufferSize >= bufferSize) {
1683 ret = defaultBuffer;
1684 } else {
1685 ret = (char*)malloc(bufferSize);
1686
1687 if (ret == NULL) {
1688 return ret;
1689 }
1690 }
1691
1692 ret[0] = '\0'; /* to start strcat off */
1693
1694 p = ret;
1695 pm = entry->message;
1696
1697 if (prefixSuffixIsHeaderFooter) {
1698 strcat(p, prefixBuf);
1699 p += prefixLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001700 if (p_format->printable_output) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001701 p += convertPrintable(p, entry->message, entry->messageLen);
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001702 } else {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001703 strncat(p, entry->message, entry->messageLen);
1704 p += entry->messageLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001705 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001706 strcat(p, suffixBuf);
1707 p += suffixLen;
1708 } else {
1709 do {
1710 const char* lineStart;
1711 size_t lineLen;
1712 lineStart = pm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001713
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001714 /* Find the next end-of-line in message */
1715 while (pm < (entry->message + entry->messageLen) && *pm != '\n') pm++;
1716 lineLen = pm - lineStart;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001717
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001718 strcat(p, prefixBuf);
1719 p += prefixLen;
1720 if (p_format->printable_output) {
1721 p += convertPrintable(p, lineStart, lineLen);
1722 } else {
1723 strncat(p, lineStart, lineLen);
1724 p += lineLen;
1725 }
1726 strcat(p, suffixBuf);
1727 p += suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001728
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001729 if (*pm == '\n') pm++;
1730 } while (pm < (entry->message + entry->messageLen));
1731 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001732
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001733 if (p_outLength != NULL) {
1734 *p_outLength = p - ret;
1735 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001736
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001737 return ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001738}
1739
1740/**
1741 * Either print or do not print log line, based on filter
1742 *
1743 * Returns count bytes written
1744 */
1745
Tom Cherry2d9779e2019-02-08 11:46:19 -08001746int android_log_printLogLine(AndroidLogFormat* p_format, int fd, const AndroidLogEntry* entry) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001747 int ret;
1748 char defaultBuffer[512];
1749 char* outBuffer = NULL;
1750 size_t totalLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001751
Tom Cherry71ba1642019-01-10 10:37:36 -08001752 outBuffer =
1753 android_log_formatLogLine(p_format, defaultBuffer, sizeof(defaultBuffer), entry, &totalLen);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001754
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001755 if (!outBuffer) return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001756
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001757 do {
1758 ret = write(fd, outBuffer, totalLen);
1759 } while (ret < 0 && errno == EINTR);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001760
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001761 if (ret < 0) {
1762 fprintf(stderr, "+++ LOG: write failed (errno=%d)\n", errno);
1763 ret = 0;
1764 goto done;
1765 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001766
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001767 if (((size_t)ret) < totalLen) {
1768 fprintf(stderr, "+++ LOG: write partial (%d of %d)\n", ret, (int)totalLen);
1769 goto done;
1770 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001771
1772done:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001773 if (outBuffer != defaultBuffer) {
1774 free(outBuffer);
1775 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001776
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001777 return ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001778}