blob: 238431f918350808d05f49410671b5e038bc736c [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
Tom Cherryc5c6d7d2020-04-17 09:38:55 -070022#include <log/logprint.h>
23
Mark Salyzyna04464a2014-04-30 08:50:53 -070024#include <assert.h>
25#include <ctype.h>
26#include <errno.h>
Mark Salyzyn4fd05072016-10-18 11:30:11 -070027#include <inttypes.h>
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080028#ifndef __MINGW32__
William Roberts8a5b9ca2016-04-08 12:13:17 -070029#include <pwd.h>
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080030#endif
Mark Salyzyna04464a2014-04-30 08:50:53 -070031#include <stdint.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
Pierre Zurekead88fc2010-10-17 22:39:37 +020035#include <sys/param.h>
William Roberts8a5b9ca2016-04-08 12:13:17 -070036#include <sys/types.h>
Tom Cherry91589842019-04-25 13:10:30 -070037#include <wchar.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070038
Mark Salyzyn4cbed022015-08-31 15:53:41 -070039#include <cutils/list.h>
Tom Cherrybbbf0892019-10-09 10:53:37 -070040
Mark Salyzynaeaaf812016-09-30 13:30:33 -070041#include <log/log.h>
Tom Cherryc5c6d7d2020-04-17 09:38:55 -070042#include <log/log_read.h>
Tom Cherrybbbf0892019-10-09 10:53:37 -070043#include <private/android_logger.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070044
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 Salyzyn2ed51d72017-03-09 08:09:43 -0800219 p_ret->monotonic_output = false;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800220 p_ret->uid_output = false;
221 p_ret->descriptive_output = false;
222 descriptive_output = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700223
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800224 return p_ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700225}
226
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700227static list_declare(convertHead);
228
Tom Cherry2d9779e2019-02-08 11:46:19 -0800229void android_log_format_free(AndroidLogFormat* p_format) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800230 FilterInfo *p_info, *p_info_old;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700231
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800232 p_info = p_format->filters;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700233
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800234 while (p_info != NULL) {
235 p_info_old = p_info;
236 p_info = p_info->p_next;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700237
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800238 free(p_info_old);
239 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700240
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800241 free(p_format);
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700242
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800243 /* Free conversion resource, can always be reconstructed */
244 while (!list_empty(&convertHead)) {
245 struct listnode* node = list_head(&convertHead);
246 list_remove(node);
Ting-Yuan Huang249bd052017-08-15 17:01:33 -0700247 LOG_ALWAYS_FATAL_IF(node == list_head(&convertHead), "corrupted list");
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800248 free(node);
249 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700250}
251
Tom Cherry2d9779e2019-02-08 11:46:19 -0800252int android_log_setPrintFormat(AndroidLogFormat* p_format, AndroidLogPrintFormat format) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800253 switch (format) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700254 case FORMAT_MODIFIER_COLOR:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800255 p_format->colored_output = true;
256 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700257 case FORMAT_MODIFIER_TIME_USEC:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800258 p_format->usec_time_output = true;
259 return 0;
Mark Salyzyn9b4d7e12017-01-30 09:16:09 -0800260 case FORMAT_MODIFIER_TIME_NSEC:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800261 p_format->nsec_time_output = true;
262 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700263 case FORMAT_MODIFIER_PRINTABLE:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800264 p_format->printable_output = true;
265 return 0;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700266 case FORMAT_MODIFIER_YEAR:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800267 p_format->year_output = true;
268 return 0;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700269 case FORMAT_MODIFIER_ZONE:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800270 p_format->zone_output = !p_format->zone_output;
271 return 0;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700272 case FORMAT_MODIFIER_EPOCH:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800273 p_format->epoch_output = true;
274 return 0;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700275 case FORMAT_MODIFIER_MONOTONIC:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800276 p_format->monotonic_output = true;
277 return 0;
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800278 case FORMAT_MODIFIER_UID:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800279 p_format->uid_output = true;
280 return 0;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700281 case FORMAT_MODIFIER_DESCRIPT:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800282 p_format->descriptive_output = true;
283 descriptive_output = true;
284 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700285 default:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800286 break;
287 }
288 p_format->format = format;
289 return 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700290}
291
Tom Cherry7d045f62019-09-30 12:58:55 -0700292#ifndef __MINGW32__
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700293static const char tz[] = "TZ";
294static const char utc[] = "UTC";
Tom Cherry7d045f62019-09-30 12:58:55 -0700295#endif
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700296
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700297/**
298 * Returns FORMAT_OFF on invalid string
299 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800300AndroidLogPrintFormat android_log_formatFromString(const char* formatString) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800301 static AndroidLogPrintFormat format;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700302
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800303 /* clang-format off */
304 if (!strcmp(formatString, "brief")) format = FORMAT_BRIEF;
305 else if (!strcmp(formatString, "process")) format = FORMAT_PROCESS;
306 else if (!strcmp(formatString, "tag")) format = FORMAT_TAG;
307 else if (!strcmp(formatString, "thread")) format = FORMAT_THREAD;
308 else if (!strcmp(formatString, "raw")) format = FORMAT_RAW;
309 else if (!strcmp(formatString, "time")) format = FORMAT_TIME;
310 else if (!strcmp(formatString, "threadtime")) format = FORMAT_THREADTIME;
311 else if (!strcmp(formatString, "long")) format = FORMAT_LONG;
312 else if (!strcmp(formatString, "color")) format = FORMAT_MODIFIER_COLOR;
313 else if (!strcmp(formatString, "colour")) format = FORMAT_MODIFIER_COLOR;
314 else if (!strcmp(formatString, "usec")) format = FORMAT_MODIFIER_TIME_USEC;
315 else if (!strcmp(formatString, "nsec")) format = FORMAT_MODIFIER_TIME_NSEC;
316 else if (!strcmp(formatString, "printable")) format = FORMAT_MODIFIER_PRINTABLE;
317 else if (!strcmp(formatString, "year")) format = FORMAT_MODIFIER_YEAR;
318 else if (!strcmp(formatString, "zone")) format = FORMAT_MODIFIER_ZONE;
319 else if (!strcmp(formatString, "epoch")) format = FORMAT_MODIFIER_EPOCH;
320 else if (!strcmp(formatString, "monotonic")) format = FORMAT_MODIFIER_MONOTONIC;
321 else if (!strcmp(formatString, "uid")) format = FORMAT_MODIFIER_UID;
322 else if (!strcmp(formatString, "descriptive")) format = FORMAT_MODIFIER_DESCRIPT;
Tom Cherry71ba1642019-01-10 10:37:36 -0800323 /* clang-format on */
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800324
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800325#ifndef __MINGW32__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800326 else {
327 extern char* tzname[2];
328 static const char gmt[] = "GMT";
329 char* cp = getenv(tz);
330 if (cp) {
331 cp = strdup(cp);
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700332 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800333 setenv(tz, formatString, 1);
334 /*
335 * Run tzset here to determine if the timezone is legitimate. If the
336 * zone is GMT, check if that is what was asked for, if not then
337 * did not match any on the system; report an error to caller.
338 */
339 tzset();
340 if (!tzname[0] ||
Tom Cherry71ba1642019-01-10 10:37:36 -0800341 ((!strcmp(tzname[0], utc) || !strcmp(tzname[0], gmt)) /* error? */
342 && strcasecmp(formatString, utc) && strcasecmp(formatString, gmt))) { /* ok */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800343 if (cp) {
344 setenv(tz, cp, 1);
345 } else {
346 unsetenv(tz);
347 }
348 tzset();
349 format = FORMAT_OFF;
350 } else {
351 format = FORMAT_MODIFIER_ZONE;
352 }
353 free(cp);
354 }
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800355#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700356
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800357 return format;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700358}
359
360/**
361 * filterExpression: a single filter expression
362 * eg "AT:d"
363 *
364 * returns 0 on success and -1 on invalid expression
365 *
366 * Assumes single threaded execution
367 */
368
Tom Cherry2d9779e2019-02-08 11:46:19 -0800369int android_log_addFilterRule(AndroidLogFormat* p_format, const char* filterExpression) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800370 size_t tagNameLength;
371 android_LogPriority pri = ANDROID_LOG_DEFAULT;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700372
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800373 tagNameLength = strcspn(filterExpression, ":");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700374
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800375 if (tagNameLength == 0) {
376 goto error;
377 }
378
379 if (filterExpression[tagNameLength] == ':') {
380 pri = filterCharToPri(filterExpression[tagNameLength + 1]);
381
382 if (pri == ANDROID_LOG_UNKNOWN) {
383 goto error;
384 }
385 }
386
387 if (0 == strncmp("*", filterExpression, tagNameLength)) {
388 /*
389 * This filter expression refers to the global filter
390 * The default level for this is DEBUG if the priority
391 * is unspecified
392 */
393 if (pri == ANDROID_LOG_DEFAULT) {
394 pri = ANDROID_LOG_DEBUG;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700395 }
396
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800397 p_format->global_pri = pri;
398 } else {
399 /*
400 * for filter expressions that don't refer to the global
401 * filter, the default is verbose if the priority is unspecified
402 */
403 if (pri == ANDROID_LOG_DEFAULT) {
404 pri = ANDROID_LOG_VERBOSE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700405 }
406
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800407 char* tagName;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700408
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700409/*
410 * Presently HAVE_STRNDUP is never defined, so the second case is always taken
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800411 * Darwin doesn't have strndup, everything else does
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700412 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700413#ifdef HAVE_STRNDUP
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800414 tagName = strndup(filterExpression, tagNameLength);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700415#else
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800416 /* a few extra bytes copied... */
417 tagName = strdup(filterExpression);
418 tagName[tagNameLength] = '\0';
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700419#endif /*HAVE_STRNDUP*/
420
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800421 FilterInfo* p_fi = filterinfo_new(tagName, pri);
422 free(tagName);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700423
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800424 p_fi->p_next = p_format->filters;
425 p_format->filters = p_fi;
426 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700427
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800428 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700429error:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800430 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700431}
432
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800433#ifndef HAVE_STRSEP
434/* KISS replacement helper for below */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800435static char* strsep(char** stringp, const char* delim) {
436 char* token;
437 char* ret = *stringp;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800438
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800439 if (!ret || !*ret) {
440 return NULL;
441 }
442 token = strpbrk(ret, delim);
443 if (token) {
444 *token = '\0';
445 ++token;
446 } else {
447 token = ret + strlen(ret);
448 }
449 *stringp = token;
450 return ret;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800451}
452#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700453
454/**
455 * filterString: a comma/whitespace-separated set of filter expressions
456 *
457 * eg "AT:d *:i"
458 *
459 * returns 0 on success and -1 on invalid expression
460 *
461 * Assumes single threaded execution
462 *
463 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800464int android_log_addFilterString(AndroidLogFormat* p_format, const char* filterString) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800465 char* filterStringCopy = strdup(filterString);
466 char* p_cur = filterStringCopy;
467 char* p_ret;
468 int err;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700469
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800470 /* Yes, I'm using strsep */
471 while (NULL != (p_ret = strsep(&p_cur, " \t,"))) {
472 /* ignore whitespace-only entries */
473 if (p_ret[0] != '\0') {
474 err = android_log_addFilterRule(p_format, p_ret);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700475
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800476 if (err < 0) {
477 goto error;
478 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700479 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800480 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700481
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800482 free(filterStringCopy);
483 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700484error:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800485 free(filterStringCopy);
486 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700487}
488
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700489/**
490 * Splits a wire-format buffer into an AndroidLogEntry
491 * entry allocated by caller. Pointers will point directly into buf
492 *
493 * Returns 0 on success and -1 on invalid wire format (entry will be
494 * in unspecified state)
495 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800496int android_log_processLogBuffer(struct logger_entry* buf, AndroidLogEntry* entry) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800497 entry->message = NULL;
498 entry->messageLen = 0;
Mark Salyzynb1d150b2017-03-02 07:47:21 -0800499
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800500 entry->tv_sec = buf->sec;
501 entry->tv_nsec = buf->nsec;
502 entry->uid = -1;
503 entry->pid = buf->pid;
504 entry->tid = buf->tid;
Kenny Root4bf3c022011-09-30 17:10:14 -0700505
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800506 /*
507 * format: <priority:1><tag:N>\0<message:N>\0
508 *
509 * tag str
Tom Cherryd3ecc662020-04-09 14:53:55 -0700510 * starts at buf + buf->hdr_size + 1
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800511 * msg
Tom Cherryd3ecc662020-04-09 14:53:55 -0700512 * starts at buf + buf->hdr_size + 1 + len(tag) + 1
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800513 *
Tom Cherryd3ecc662020-04-09 14:53:55 -0700514 * The message may have been truncated. When that happens, we must null-terminate the message
515 * ourselves.
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800516 */
517 if (buf->len < 3) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700518 /*
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800519 * An well-formed entry must consist of at least a priority
520 * and two null characters
Kenny Root4bf3c022011-09-30 17:10:14 -0700521 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800522 fprintf(stderr, "+++ LOG: entry too small\n");
523 return -1;
524 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700525
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800526 int msgStart = -1;
527 int msgEnd = -1;
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700528
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800529 int i;
Tom Cherryd3ecc662020-04-09 14:53:55 -0700530 if (buf->hdr_size < sizeof(logger_entry)) {
531 fprintf(stderr, "+++ LOG: hdr_size must be at least as big as struct logger_entry\n");
Tom Cherry441054a2019-10-15 16:53:11 -0700532 return -1;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800533 }
Tom Cherryd3ecc662020-04-09 14:53:55 -0700534 char* msg = reinterpret_cast<char*>(buf) + buf->hdr_size;
Tom Cherry441054a2019-10-15 16:53:11 -0700535 entry->uid = buf->uid;
536
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800537 for (i = 1; i < buf->len; i++) {
538 if (msg[i] == '\0') {
539 if (msgStart == -1) {
540 msgStart = i + 1;
541 } else {
542 msgEnd = i;
543 break;
544 }
545 }
546 }
547
548 if (msgStart == -1) {
549 /* +++ LOG: malformed log message, DYB */
Nick Kraleviche1ede152011-10-18 15:23:33 -0700550 for (i = 1; i < buf->len; i++) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800551 /* odd characters in tag? */
552 if ((msg[i] <= ' ') || (msg[i] == ':') || (msg[i] >= 0x7f)) {
553 msg[i] = '\0';
554 msgStart = i + 1;
555 break;
556 }
Nick Kraleviche1ede152011-10-18 15:23:33 -0700557 }
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700558 if (msgStart == -1) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800559 msgStart = buf->len - 1; /* All tag, no message, print truncates */
Nick Kralevich63f4a842011-10-17 10:45:03 -0700560 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800561 }
562 if (msgEnd == -1) {
563 /* incoming message not null-terminated; force it */
564 msgEnd = buf->len - 1; /* may result in msgEnd < msgStart */
565 msg[msgEnd] = '\0';
566 }
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700567
Tom Cherry71ba1642019-01-10 10:37:36 -0800568 entry->priority = static_cast<android_LogPriority>(msg[0]);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800569 entry->tag = msg + 1;
570 entry->tagLen = msgStart - 1;
571 entry->message = msg + msgStart;
572 entry->messageLen = (msgEnd < msgStart) ? 0 : (msgEnd - msgStart);
Nick Kralevich63f4a842011-10-17 10:45:03 -0700573
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800574 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700575}
576
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700577static bool findChar(const char** cp, size_t* len, int c) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800578 while ((*len) && isspace(*(*cp))) {
579 ++(*cp);
580 --(*len);
581 }
582 if (c == INT_MAX) return *len;
583 if ((*len) && (*(*cp) == c)) {
584 ++(*cp);
585 --(*len);
586 return true;
587 }
588 return false;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700589}
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000590
591/*
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700592 * Recursively convert binary log data to printable form.
593 *
594 * This needs to be recursive because you can have lists of lists.
595 *
596 * If we run out of room, we stop processing immediately. It's important
597 * for us to check for space on every output element to avoid producing
598 * garbled output.
599 *
600 * Returns 0 on success, 1 on buffer full, -1 on failure.
601 */
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700602enum objectType {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800603 TYPE_OBJECTS = '1',
604 TYPE_BYTES = '2',
605 TYPE_MILLISECONDS = '3',
606 TYPE_ALLOCATIONS = '4',
607 TYPE_ID = '5',
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800608 TYPE_PERCENT = '6',
609 TYPE_MONOTONIC = 's'
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700610};
611
Tom Cherry71ba1642019-01-10 10:37:36 -0800612static int android_log_printBinaryEvent(const unsigned char** pEventData, size_t* pEventDataLen,
613 char** pOutBuf, size_t* pOutBufLen, const char** fmtStr,
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800614 size_t* fmtLen) {
615 const unsigned char* eventData = *pEventData;
616 size_t eventDataLen = *pEventDataLen;
617 char* outBuf = *pOutBuf;
618 char* outBufSave = outBuf;
619 size_t outBufLen = *pOutBufLen;
620 size_t outBufLenSave = outBufLen;
621 unsigned char type;
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800622 size_t outCount = 0;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800623 int result = 0;
624 const char* cp;
625 size_t len;
626 int64_t lval;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700627
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800628 if (eventDataLen < 1) return -1;
Mark Salyzyn1a57ae32016-11-11 14:41:30 -0800629
Tom Cherrybbbf0892019-10-09 10:53:37 -0700630 type = *eventData;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700631
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800632 cp = NULL;
633 len = 0;
634 if (fmtStr && *fmtStr && fmtLen && *fmtLen && **fmtStr) {
635 cp = *fmtStr;
636 len = *fmtLen;
637 }
638 /*
639 * event.logtag format specification:
640 *
641 * Optionally, after the tag names can be put a description for the value(s)
642 * of the tag. Description are in the format
643 * (<name>|data type[|data unit])
644 * Multiple values are separated by commas.
645 *
646 * The data type is a number from the following values:
647 * 1: int
648 * 2: long
649 * 3: string
650 * 4: list
651 * 5: float
652 *
653 * The data unit is a number taken from the following list:
654 * 1: Number of objects
655 * 2: Number of bytes
656 * 3: Number of milliseconds
657 * 4: Number of allocations
658 * 5: Id
659 * 6: Percent
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800660 * s: Number of seconds (monotonic time)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800661 * Default value for data of type int/long is 2 (bytes).
662 */
663 if (!cp || !findChar(&cp, &len, '(')) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700664 len = 0;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800665 } else {
666 char* outBufLastSpace = NULL;
667
668 findChar(&cp, &len, INT_MAX);
669 while (len && *cp && (*cp != '|') && (*cp != ')')) {
670 if (outBufLen <= 0) {
671 /* halt output */
672 goto no_room;
673 }
674 outBufLastSpace = isspace(*cp) ? outBuf : NULL;
675 *outBuf = *cp;
676 ++outBuf;
677 ++cp;
678 --outBufLen;
679 --len;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700680 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800681 if (outBufLastSpace) {
682 outBufLen += outBuf - outBufLastSpace;
683 outBuf = outBufLastSpace;
684 }
685 if (outBufLen <= 0) {
686 /* halt output */
687 goto no_room;
688 }
689 if (outBufSave != outBuf) {
690 *outBuf = '=';
691 ++outBuf;
692 --outBufLen;
693 }
694
695 if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800696 static const unsigned char typeTable[] = {EVENT_TYPE_INT, EVENT_TYPE_LONG, EVENT_TYPE_STRING,
697 EVENT_TYPE_LIST, EVENT_TYPE_FLOAT};
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800698
Tom Cherry71ba1642019-01-10 10:37:36 -0800699 if ((*cp >= '1') && (*cp < (char)('1' + (sizeof(typeTable) / sizeof(typeTable[0])))) &&
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800700 (type != typeTable[(size_t)(*cp - '1')]))
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700701 len = 0;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700702
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800703 if (len) {
704 ++cp;
705 --len;
706 } else {
707 /* reset the format */
708 outBuf = outBufSave;
709 outBufLen = outBufLenSave;
710 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700711 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800712 }
713 outCount = 0;
714 lval = 0;
715 switch (type) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700716 case EVENT_TYPE_INT:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800717 /* 32-bit signed int */
718 {
Tom Cherrybbbf0892019-10-09 10:53:37 -0700719 if (eventDataLen < sizeof(android_event_int_t)) return -1;
720 auto* event_int = reinterpret_cast<const android_event_int_t*>(eventData);
721 lval = event_int->data;
722 eventData += sizeof(android_event_int_t);
723 eventDataLen -= sizeof(android_event_int_t);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800724 }
725 goto pr_lval;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700726 case EVENT_TYPE_LONG:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800727 /* 64-bit signed long */
Tom Cherrybbbf0892019-10-09 10:53:37 -0700728 if (eventDataLen < sizeof(android_event_long_t)) {
729 return -1;
730 }
731 {
732 auto* event_long = reinterpret_cast<const android_event_long_t*>(eventData);
733 lval = event_long->data;
734 }
735 eventData += sizeof(android_event_long_t);
736 eventDataLen -= sizeof(android_event_long_t);
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700737 pr_lval:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800738 outCount = snprintf(outBuf, outBufLen, "%" PRId64, lval);
739 if (outCount < outBufLen) {
740 outBuf += outCount;
741 outBufLen -= outCount;
742 } else {
743 /* halt output */
744 goto no_room;
745 }
746 break;
Jeff Brown44193d92015-04-28 12:47:02 -0700747 case EVENT_TYPE_FLOAT:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800748 /* float */
749 {
Tom Cherrybbbf0892019-10-09 10:53:37 -0700750 if (eventDataLen < sizeof(android_event_float_t)) return -1;
751 auto* event_float = reinterpret_cast<const android_event_float_t*>(eventData);
752 float fval = event_float->data;
753 eventData += sizeof(android_event_int_t);
754 eventDataLen -= sizeof(android_event_int_t);
Jeff Brown44193d92015-04-28 12:47:02 -0700755
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800756 outCount = snprintf(outBuf, outBufLen, "%f", fval);
757 if (outCount < outBufLen) {
758 outBuf += outCount;
759 outBufLen -= outCount;
760 } else {
761 /* halt output */
762 goto no_room;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700763 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800764 }
765 break;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700766 case EVENT_TYPE_STRING:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800767 /* UTF-8 chars, not NULL-terminated */
768 {
Tom Cherrybbbf0892019-10-09 10:53:37 -0700769 if (eventDataLen < sizeof(android_event_string_t)) return -1;
770 auto* event_string = reinterpret_cast<const android_event_string_t*>(eventData);
771 unsigned int strLen = event_string->length;
772 eventData += sizeof(android_event_string_t);
773 eventDataLen -= sizeof(android_event_string_t);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700774
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800775 if (eventDataLen < strLen) {
776 result = -1; /* mark truncated */
777 strLen = eventDataLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700778 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700779
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800780 if (cp && (strLen == 0)) {
781 /* reset the format if no content */
782 outBuf = outBufSave;
783 outBufLen = outBufLenSave;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700784 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800785 if (strLen < outBufLen) {
786 memcpy(outBuf, eventData, strLen);
787 outBuf += strLen;
788 outBufLen -= strLen;
789 } else {
790 if (outBufLen > 0) {
791 /* copy what we can */
792 memcpy(outBuf, eventData, outBufLen);
793 outBuf += outBufLen;
794 outBufLen -= outBufLen;
795 }
796 if (!result) result = 1; /* if not truncated, return no room */
797 }
798 eventData += strLen;
799 eventDataLen -= strLen;
800 if (result != 0) goto bail;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700801 break;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800802 }
803 case EVENT_TYPE_LIST:
804 /* N items, all different types */
805 {
Tom Cherrybbbf0892019-10-09 10:53:37 -0700806 if (eventDataLen < sizeof(android_event_list_t)) return -1;
807 auto* event_list = reinterpret_cast<const android_event_list_t*>(eventData);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800808
Tom Cherrybbbf0892019-10-09 10:53:37 -0700809 int8_t count = event_list->element_count;
810 eventData += sizeof(android_event_list_t);
811 eventDataLen -= sizeof(android_event_list_t);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800812
813 if (outBufLen <= 0) goto no_room;
814
815 *outBuf++ = '[';
816 outBufLen--;
817
Tom Cherrybbbf0892019-10-09 10:53:37 -0700818 for (int i = 0; i < count; i++) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800819 result = android_log_printBinaryEvent(&eventData, &eventDataLen, &outBuf, &outBufLen,
820 fmtStr, fmtLen);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800821 if (result != 0) goto bail;
822
823 if (i < (count - 1)) {
824 if (outBufLen <= 0) goto no_room;
825 *outBuf++ = ',';
826 outBufLen--;
827 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700828 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800829
830 if (outBufLen <= 0) goto no_room;
831
832 *outBuf++ = ']';
833 outBufLen--;
834 }
835 break;
836 default:
837 fprintf(stderr, "Unknown binary event type %d\n", type);
838 return -1;
839 }
840 if (cp && len) {
841 if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) {
842 switch (*cp) {
843 case TYPE_OBJECTS:
844 outCount = 0;
845 /* outCount = snprintf(outBuf, outBufLen, " objects"); */
846 break;
847 case TYPE_BYTES:
848 if ((lval != 0) && ((lval % 1024) == 0)) {
849 /* repaint with multiplier */
Tom Cherry71ba1642019-01-10 10:37:36 -0800850 static const char suffixTable[] = {'K', 'M', 'G', 'T'};
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800851 size_t idx = 0;
852 outBuf -= outCount;
853 outBufLen += outCount;
854 do {
855 lval /= 1024;
856 if ((lval % 1024) != 0) break;
Tom Cherry71ba1642019-01-10 10:37:36 -0800857 } while (++idx < ((sizeof(suffixTable) / sizeof(suffixTable[0])) - 1));
858 outCount = snprintf(outBuf, outBufLen, "%" PRId64 "%cB", lval, suffixTable[idx]);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800859 } else {
860 outCount = snprintf(outBuf, outBufLen, "B");
861 }
862 break;
863 case TYPE_MILLISECONDS:
Tom Cherry71ba1642019-01-10 10:37:36 -0800864 if (((lval <= -1000) || (1000 <= lval)) && (outBufLen || (outBuf[-1] == '0'))) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800865 /* repaint as (fractional) seconds, possibly saving space */
866 if (outBufLen) outBuf[0] = outBuf[-1];
867 outBuf[-1] = outBuf[-2];
868 outBuf[-2] = outBuf[-3];
869 outBuf[-3] = '.';
870 while ((outBufLen == 0) || (*outBuf == '0')) {
871 --outBuf;
872 ++outBufLen;
873 }
874 if (*outBuf != '.') {
875 ++outBuf;
876 --outBufLen;
877 }
878 outCount = snprintf(outBuf, outBufLen, "s");
879 } else {
880 outCount = snprintf(outBuf, outBufLen, "ms");
881 }
882 break;
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800883 case TYPE_MONOTONIC: {
884 static const uint64_t minute = 60;
885 static const uint64_t hour = 60 * minute;
886 static const uint64_t day = 24 * hour;
887
888 /* Repaint as unsigned seconds, minutes, hours ... */
889 outBuf -= outCount;
890 outBufLen += outCount;
891 uint64_t val = lval;
892 if (val >= day) {
893 outCount = snprintf(outBuf, outBufLen, "%" PRIu64 "d ", val / day);
894 if (outCount >= outBufLen) break;
895 outBuf += outCount;
896 outBufLen -= outCount;
897 val = (val % day) + day;
898 }
899 if (val >= minute) {
900 if (val >= hour) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800901 outCount = snprintf(outBuf, outBufLen, "%" PRIu64 ":", (val / hour) % (day / hour));
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800902 if (outCount >= outBufLen) break;
903 outBuf += outCount;
904 outBufLen -= outCount;
905 }
906 outCount =
Tom Cherry71ba1642019-01-10 10:37:36 -0800907 snprintf(outBuf, outBufLen, (val >= hour) ? "%02" PRIu64 ":" : "%" PRIu64 ":",
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800908 (val / minute) % (hour / minute));
909 if (outCount >= outBufLen) break;
910 outBuf += outCount;
911 outBufLen -= outCount;
912 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800913 outCount = snprintf(outBuf, outBufLen, (val >= minute) ? "%02" PRIu64 : "%" PRIu64 "s",
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800914 val % minute);
915 } break;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800916 case TYPE_ALLOCATIONS:
917 outCount = 0;
918 /* outCount = snprintf(outBuf, outBufLen, " allocations"); */
919 break;
920 case TYPE_ID:
921 outCount = 0;
922 break;
923 case TYPE_PERCENT:
924 outCount = snprintf(outBuf, outBufLen, "%%");
925 break;
926 default: /* ? */
927 outCount = 0;
928 break;
929 }
930 ++cp;
931 --len;
932 if (outCount < outBufLen) {
933 outBuf += outCount;
934 outBufLen -= outCount;
935 } else if (outCount) {
936 /* halt output */
937 goto no_room;
938 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700939 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800940 if (!findChar(&cp, &len, ')')) len = 0;
941 if (!findChar(&cp, &len, ',')) len = 0;
942 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700943
944bail:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800945 *pEventData = eventData;
946 *pEventDataLen = eventDataLen;
947 *pOutBuf = outBuf;
948 *pOutBufLen = outBufLen;
949 if (cp) {
950 *fmtStr = cp;
951 *fmtLen = len;
952 }
953 return result;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700954
955no_room:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800956 result = 1;
957 goto bail;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700958}
959
960/**
961 * Convert a binary log entry to ASCII form.
962 *
963 * For convenience we mimic the processLogBuffer API. There is no
964 * pre-defined output length for the binary data, since we're free to format
965 * it however we choose, which means we can't really use a fixed-size buffer
966 * here.
967 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800968int android_log_processBinaryLogBuffer(
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800969 struct logger_entry* buf, AndroidLogEntry* entry,
Tom Cherry71ba1642019-01-10 10:37:36 -0800970 [[maybe_unused]] const EventTagMap* map, /* only on !__ANDROID__ */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800971 char* messageBuf, int messageBufLen) {
972 size_t inCount;
973 uint32_t tagIndex;
974 const unsigned char* eventData;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700975
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800976 entry->message = NULL;
977 entry->messageLen = 0;
Mark Salyzynb1d150b2017-03-02 07:47:21 -0800978
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800979 entry->tv_sec = buf->sec;
980 entry->tv_nsec = buf->nsec;
981 entry->priority = ANDROID_LOG_INFO;
982 entry->uid = -1;
983 entry->pid = buf->pid;
984 entry->tid = buf->tid;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700985
Tom Cherryd3ecc662020-04-09 14:53:55 -0700986 if (buf->hdr_size < sizeof(logger_entry)) {
987 fprintf(stderr, "+++ LOG: hdr_size must be at least as big as struct logger_entry\n");
Tom Cherry441054a2019-10-15 16:53:11 -0700988 return -1;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800989 }
Tom Cherryd3ecc662020-04-09 14:53:55 -0700990 eventData = reinterpret_cast<unsigned char*>(buf) + buf->hdr_size;
Tom Cherry441054a2019-10-15 16:53:11 -0700991 if (buf->lid == LOG_ID_SECURITY) {
992 entry->priority = ANDROID_LOG_WARN;
993 }
994 entry->uid = buf->uid;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800995 inCount = buf->len;
Tom Cherrybbbf0892019-10-09 10:53:37 -0700996 if (inCount < sizeof(android_event_header_t)) return -1;
997 auto* event_header = reinterpret_cast<const android_event_header_t*>(eventData);
998 tagIndex = event_header->tag;
999 eventData += sizeof(android_event_header_t);
1000 inCount -= sizeof(android_event_header_t);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001001
1002 entry->tagLen = 0;
1003 entry->tag = NULL;
1004#ifdef __ANDROID__
1005 if (map != NULL) {
1006 entry->tag = android_lookupEventTag_len(map, &entry->tagLen, tagIndex);
1007 }
1008#endif
1009
1010 /*
1011 * If we don't have a map, or didn't find the tag number in the map,
1012 * stuff a generated tag value into the start of the output buffer and
1013 * shift the buffer pointers down.
1014 */
1015 if (entry->tag == NULL) {
1016 size_t tagLen;
1017
1018 tagLen = snprintf(messageBuf, messageBufLen, "[%" PRIu32 "]", tagIndex);
1019 if (tagLen >= (size_t)messageBufLen) {
1020 tagLen = messageBufLen - 1;
1021 }
1022 entry->tag = messageBuf;
1023 entry->tagLen = tagLen;
1024 messageBuf += tagLen + 1;
1025 messageBufLen -= tagLen + 1;
1026 }
1027
1028 /*
1029 * Format the event log data into the buffer.
1030 */
1031 const char* fmtStr = NULL;
1032 size_t fmtLen = 0;
1033#ifdef __ANDROID__
1034 if (descriptive_output && map) {
1035 fmtStr = android_lookupEventFormat_len(map, &fmtLen, tagIndex);
1036 }
1037#endif
1038
1039 char* outBuf = messageBuf;
1040 size_t outRemaining = messageBufLen - 1; /* leave one for nul byte */
1041 int result = 0;
1042
1043 if ((inCount > 0) || fmtLen) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001044 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, &outRemaining, &fmtStr,
1045 &fmtLen);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001046 }
1047 if ((result == 1) && fmtStr) {
1048 /* We overflowed :-(, let's repaint the line w/o format dressings */
Tom Cherryd3ecc662020-04-09 14:53:55 -07001049 eventData = reinterpret_cast<unsigned char*>(buf) + buf->hdr_size;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001050 eventData += 4;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001051 outBuf = messageBuf;
1052 outRemaining = messageBufLen - 1;
Tom Cherry71ba1642019-01-10 10:37:36 -08001053 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, &outRemaining, NULL, NULL);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001054 }
1055 if (result < 0) {
1056 fprintf(stderr, "Binary log entry conversion failed\n");
1057 }
1058 if (result) {
1059 if (!outRemaining) {
1060 /* make space to leave an indicator */
1061 --outBuf;
1062 ++outRemaining;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001063 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001064 *outBuf++ = (result < 0) ? '!' : '^'; /* Error or Truncation? */
1065 outRemaining--;
1066 /* pretend we ate all the data to prevent log stutter */
1067 inCount = 0;
1068 if (result > 0) result = 0;
1069 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001070
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001071 /* eat the silly terminating '\n' */
1072 if (inCount == 1 && *eventData == '\n') {
1073 eventData++;
1074 inCount--;
1075 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001076
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001077 if (inCount != 0) {
1078 fprintf(stderr, "Warning: leftover binary log data (%zu bytes)\n", inCount);
1079 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001080
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001081 /*
1082 * Terminate the buffer. The NUL byte does not count as part of
1083 * entry->messageLen.
1084 */
1085 *outBuf = '\0';
1086 entry->messageLen = outBuf - messageBuf;
1087 assert(entry->messageLen == (messageBufLen - 1) - outRemaining);
Mark Salyzyn1a57ae32016-11-11 14:41:30 -08001088
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001089 entry->message = messageBuf;
Mark Salyzyn1a57ae32016-11-11 14:41:30 -08001090
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001091 return result;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001092}
1093
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001094/*
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001095 * Convert to printable from message to p buffer, return string length. If p is
1096 * NULL, do not copy, but still return the expected string length.
1097 */
Tom Cherry91589842019-04-25 13:10:30 -07001098size_t convertPrintable(char* p, const char* message, size_t messageLen) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001099 char* begin = p;
1100 bool print = p != NULL;
Tom Cherry91589842019-04-25 13:10:30 -07001101 mbstate_t mb_state = {};
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001102
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001103 while (messageLen) {
1104 char buf[6];
1105 ssize_t len = sizeof(buf) - 1;
1106 if ((size_t)len > messageLen) {
1107 len = messageLen;
1108 }
Tom Cherry91589842019-04-25 13:10:30 -07001109 len = mbrtowc(nullptr, message, len, &mb_state);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001110
1111 if (len < 0) {
Tom Cherry91589842019-04-25 13:10:30 -07001112 snprintf(buf, sizeof(buf), "\\x%02X", static_cast<unsigned char>(*message));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001113 len = 1;
1114 } else {
1115 buf[0] = '\0';
1116 if (len == 1) {
1117 if (*message == '\a') {
1118 strcpy(buf, "\\a");
1119 } else if (*message == '\b') {
1120 strcpy(buf, "\\b");
1121 } else if (*message == '\t') {
1122 strcpy(buf, "\t"); /* Do not escape tabs */
1123 } else if (*message == '\v') {
1124 strcpy(buf, "\\v");
1125 } else if (*message == '\f') {
1126 strcpy(buf, "\\f");
1127 } else if (*message == '\r') {
1128 strcpy(buf, "\\r");
1129 } else if (*message == '\\') {
1130 strcpy(buf, "\\\\");
1131 } else if ((*message < ' ') || (*message & 0x80)) {
Tom Cherry91589842019-04-25 13:10:30 -07001132 snprintf(buf, sizeof(buf), "\\x%02X", static_cast<unsigned char>(*message));
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001133 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001134 }
1135 if (!buf[0]) {
1136 strncpy(buf, message, len);
1137 buf[len] = '\0';
1138 }
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001139 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001140 if (print) {
1141 strcpy(p, buf);
1142 }
1143 p += strlen(buf);
1144 message += len;
1145 messageLen -= len;
1146 }
1147 return p - begin;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001148}
1149
Tom Cherry7d045f62019-09-30 12:58:55 -07001150#ifdef __ANDROID__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001151static char* readSeconds(char* e, struct timespec* t) {
1152 unsigned long multiplier;
1153 char* p;
1154 t->tv_sec = strtoul(e, &p, 10);
1155 if (*p != '.') {
1156 return NULL;
1157 }
1158 t->tv_nsec = 0;
1159 multiplier = NS_PER_SEC;
1160 while (isdigit(*++p) && (multiplier /= 10)) {
1161 t->tv_nsec += (*p - '0') * multiplier;
1162 }
1163 return p;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001164}
1165
Tom Cherry71ba1642019-01-10 10:37:36 -08001166static struct timespec* sumTimespec(struct timespec* left, struct timespec* right) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001167 left->tv_nsec += right->tv_nsec;
1168 left->tv_sec += right->tv_sec;
1169 if (left->tv_nsec >= (long)NS_PER_SEC) {
1170 left->tv_nsec -= NS_PER_SEC;
1171 left->tv_sec += 1;
1172 }
1173 return left;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001174}
1175
Tom Cherry71ba1642019-01-10 10:37:36 -08001176static struct timespec* subTimespec(struct timespec* result, struct timespec* left,
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001177 struct timespec* right) {
1178 result->tv_nsec = left->tv_nsec - right->tv_nsec;
1179 result->tv_sec = left->tv_sec - right->tv_sec;
1180 if (result->tv_nsec < 0) {
1181 result->tv_nsec += NS_PER_SEC;
1182 result->tv_sec -= 1;
1183 }
1184 return result;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001185}
1186
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001187static long long nsecTimespec(struct timespec* now) {
1188 return (long long)now->tv_sec * NS_PER_SEC + now->tv_nsec;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001189}
1190
Tom Cherry71ba1642019-01-10 10:37:36 -08001191static void convertMonotonic(struct timespec* result, const AndroidLogEntry* entry) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001192 struct listnode* node;
1193 struct conversionList {
1194 struct listnode node; /* first */
1195 struct timespec time;
1196 struct timespec convert;
1197 } * list, *next;
1198 struct timespec time, convert;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001199
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001200 /* If we do not have a conversion list, build one up */
1201 if (list_empty(&convertHead)) {
1202 bool suspended_pending = false;
Tom Cherry71ba1642019-01-10 10:37:36 -08001203 struct timespec suspended_monotonic = {0, 0};
1204 struct timespec suspended_diff = {0, 0};
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001205
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001206 /*
1207 * Read dmesg for _some_ synchronization markers and insert
1208 * Anything in the Android Logger before the dmesg logging span will
1209 * be highly suspect regarding the monotonic time calculations.
1210 */
1211 FILE* p = popen("/system/bin/dmesg", "re");
1212 if (p) {
1213 char* line = NULL;
1214 size_t len = 0;
1215 while (getline(&line, &len, p) > 0) {
1216 static const char suspend[] = "PM: suspend entry ";
1217 static const char resume[] = "PM: suspend exit ";
1218 static const char healthd[] = "healthd";
1219 static const char battery[] = ": battery ";
1220 static const char suspended[] = "Suspended for ";
1221 struct timespec monotonic;
1222 struct tm tm;
1223 char *cp, *e = line;
1224 bool add_entry = true;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001225
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001226 if (*e == '<') {
1227 while (*e && (*e != '>')) {
1228 ++e;
1229 }
1230 if (*e != '>') {
1231 continue;
1232 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001233 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001234 if (*e != '[') {
1235 continue;
1236 }
1237 while (*++e == ' ') {
1238 ;
1239 }
1240 e = readSeconds(e, &monotonic);
1241 if (!e || (*e != ']')) {
1242 continue;
1243 }
1244
1245 if ((e = strstr(e, suspend))) {
1246 e += sizeof(suspend) - 1;
1247 } else if ((e = strstr(line, resume))) {
1248 e += sizeof(resume) - 1;
1249 } else if (((e = strstr(line, healthd))) &&
1250 ((e = strstr(e + sizeof(healthd) - 1, battery)))) {
1251 /* NB: healthd is roughly 150us late, worth the price to
1252 * deal with ntp-induced or hardware clock drift. */
1253 e += sizeof(battery) - 1;
1254 } else if ((e = strstr(line, suspended))) {
1255 e += sizeof(suspended) - 1;
1256 e = readSeconds(e, &time);
1257 if (!e) {
1258 continue;
1259 }
1260 add_entry = false;
1261 suspended_pending = true;
1262 suspended_monotonic = monotonic;
1263 suspended_diff = time;
1264 } else {
1265 continue;
1266 }
1267 if (add_entry) {
1268 /* look for "????-??-?? ??:??:??.????????? UTC" */
1269 cp = strstr(e, " UTC");
1270 if (!cp || ((cp - e) < 29) || (cp[-10] != '.')) {
1271 continue;
1272 }
1273 e = cp - 29;
1274 cp = readSeconds(cp - 10, &time);
1275 if (!cp) {
1276 continue;
1277 }
1278 cp = strptime(e, "%Y-%m-%d %H:%M:%S.", &tm);
1279 if (!cp) {
1280 continue;
1281 }
1282 cp = getenv(tz);
1283 if (cp) {
1284 cp = strdup(cp);
1285 }
1286 setenv(tz, utc, 1);
1287 time.tv_sec = mktime(&tm);
1288 if (cp) {
1289 setenv(tz, cp, 1);
1290 free(cp);
1291 } else {
1292 unsetenv(tz);
1293 }
Tom Cherry71ba1642019-01-10 10:37:36 -08001294 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001295 list_init(&list->node);
1296 list->time = time;
1297 subTimespec(&list->convert, &time, &monotonic);
1298 list_add_tail(&convertHead, &list->node);
1299 }
1300 if (suspended_pending && !list_empty(&convertHead)) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001301 list = node_to_item(list_tail(&convertHead), struct conversionList, node);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001302 if (subTimespec(&time, subTimespec(&time, &list->time, &list->convert),
1303 &suspended_monotonic)
1304 ->tv_sec > 0) {
1305 /* resume, what is convert factor before? */
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001306 subTimespec(&convert, &list->convert, &suspended_diff);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001307 } else {
1308 /* suspend */
1309 convert = list->convert;
1310 }
1311 time = suspended_monotonic;
1312 sumTimespec(&time, &convert);
1313 /* breakpoint just before sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001314 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001315 list_init(&list->node);
1316 list->time = time;
1317 list->convert = convert;
1318 list_add_tail(&convertHead, &list->node);
1319 /* breakpoint just after sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001320 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001321 list_init(&list->node);
1322 list->time = time;
1323 sumTimespec(&list->time, &suspended_diff);
1324 list->convert = convert;
1325 sumTimespec(&list->convert, &suspended_diff);
1326 list_add_tail(&convertHead, &list->node);
1327 suspended_pending = false;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001328 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001329 }
1330 pclose(p);
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001331 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001332 /* last entry is our current time conversion */
Tom Cherry71ba1642019-01-10 10:37:36 -08001333 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001334 list_init(&list->node);
1335 clock_gettime(CLOCK_REALTIME, &list->time);
1336 clock_gettime(CLOCK_MONOTONIC, &convert);
1337 clock_gettime(CLOCK_MONOTONIC, &time);
1338 /* Correct for instant clock_gettime latency (syscall or ~30ns) */
1339 subTimespec(&time, &convert, subTimespec(&time, &time, &convert));
1340 /* Calculate conversion factor */
1341 subTimespec(&list->convert, &list->time, &time);
1342 list_add_tail(&convertHead, &list->node);
1343 if (suspended_pending) {
1344 /* manufacture a suspend @ point before */
1345 subTimespec(&convert, &list->convert, &suspended_diff);
1346 time = suspended_monotonic;
1347 sumTimespec(&time, &convert);
1348 /* breakpoint just after sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001349 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001350 list_init(&list->node);
1351 list->time = time;
1352 sumTimespec(&list->time, &suspended_diff);
1353 list->convert = convert;
1354 sumTimespec(&list->convert, &suspended_diff);
1355 list_add_head(&convertHead, &list->node);
1356 /* breakpoint just before sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001357 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001358 list_init(&list->node);
1359 list->time = time;
1360 list->convert = convert;
1361 list_add_head(&convertHead, &list->node);
1362 }
1363 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001364
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001365 /* Find the breakpoint in the conversion list */
1366 list = node_to_item(list_head(&convertHead), struct conversionList, node);
1367 next = NULL;
1368 list_for_each(node, &convertHead) {
1369 next = node_to_item(node, struct conversionList, node);
1370 if (entry->tv_sec < next->time.tv_sec) {
1371 break;
1372 } else if (entry->tv_sec == next->time.tv_sec) {
1373 if (entry->tv_nsec < next->time.tv_nsec) {
1374 break;
1375 }
1376 }
1377 list = next;
1378 }
1379
1380 /* blend time from one breakpoint to the next */
1381 convert = list->convert;
1382 if (next) {
1383 unsigned long long total, run;
1384
1385 total = nsecTimespec(subTimespec(&time, &next->time, &list->time));
1386 time.tv_sec = entry->tv_sec;
1387 time.tv_nsec = entry->tv_nsec;
1388 run = nsecTimespec(subTimespec(&time, &time, &list->time));
1389 if (run < total) {
1390 long long crun;
1391
1392 float f = nsecTimespec(subTimespec(&time, &next->convert, &convert));
1393 f *= run;
1394 f /= total;
1395 crun = f;
1396 convert.tv_sec += crun / (long long)NS_PER_SEC;
1397 if (crun < 0) {
1398 convert.tv_nsec -= (-crun) % NS_PER_SEC;
1399 if (convert.tv_nsec < 0) {
1400 convert.tv_nsec += NS_PER_SEC;
1401 convert.tv_sec -= 1;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001402 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001403 } else {
1404 convert.tv_nsec += crun % NS_PER_SEC;
1405 if (convert.tv_nsec >= (long)NS_PER_SEC) {
1406 convert.tv_nsec -= NS_PER_SEC;
1407 convert.tv_sec += 1;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001408 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001409 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001410 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001411 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001412
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001413 /* Apply the correction factor */
1414 result->tv_sec = entry->tv_sec;
1415 result->tv_nsec = entry->tv_nsec;
1416 subTimespec(result, result, &convert);
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001417}
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001418#endif
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001419
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001420/**
1421 * Formats a log message into a buffer
1422 *
1423 * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer
1424 * If return value != defaultBuffer, caller must call free()
1425 * Returns NULL on malloc error
1426 */
1427
Tom Cherry2d9779e2019-02-08 11:46:19 -08001428char* android_log_formatLogLine(AndroidLogFormat* p_format, char* defaultBuffer,
1429 size_t defaultBufferSize, const AndroidLogEntry* entry,
1430 size_t* p_outLength) {
Yabin Cui8a985352014-11-13 10:02:08 -08001431#if !defined(_WIN32)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001432 struct tm tmBuf;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001433#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001434 struct tm* ptm;
1435 /* good margin, 23+nul for msec, 26+nul for usec, 29+nul to nsec */
1436 char timeBuf[64];
1437 char prefixBuf[128], suffixBuf[128];
1438 char priChar;
1439 int prefixSuffixIsHeaderFooter = 0;
1440 char* ret;
1441 time_t now;
1442 unsigned long nsec;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001443
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001444 priChar = filterPriToChar(entry->priority);
1445 size_t prefixLen = 0, suffixLen = 0;
1446 size_t len;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001447
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001448 /*
1449 * Get the current date/time in pretty form
1450 *
1451 * It's often useful when examining a log with "less" to jump to
1452 * a specific point in the file by searching for the date/time stamp.
1453 * For this reason it's very annoying to have regexp meta characters
1454 * in the time stamp. Don't use forward slashes, parenthesis,
1455 * brackets, asterisks, or other special chars here.
1456 *
1457 * The caller may have affected the timezone environment, this is
1458 * expected to be sensitive to that.
1459 */
1460 now = entry->tv_sec;
1461 nsec = entry->tv_nsec;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001462#if __ANDROID__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001463 if (p_format->monotonic_output) {
Tom Cherryf2c27462020-04-08 14:36:05 -07001464 struct timespec time;
1465 convertMonotonic(&time, entry);
1466 now = time.tv_sec;
1467 nsec = time.tv_nsec;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001468 }
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001469#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001470 if (now < 0) {
1471 nsec = NS_PER_SEC - nsec;
1472 }
1473 if (p_format->epoch_output || p_format->monotonic_output) {
1474 ptm = NULL;
Tom Cherry71ba1642019-01-10 10:37:36 -08001475 snprintf(timeBuf, sizeof(timeBuf), p_format->monotonic_output ? "%6lld" : "%19lld",
1476 (long long)now);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001477 } else {
Yabin Cui8a985352014-11-13 10:02:08 -08001478#if !defined(_WIN32)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001479 ptm = localtime_r(&now, &tmBuf);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001480#else
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001481 ptm = localtime(&now);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001482#endif
Tom Cherry71ba1642019-01-10 10:37:36 -08001483 strftime(timeBuf, sizeof(timeBuf), &"%Y-%m-%d %H:%M:%S"[p_format->year_output ? 0 : 3], ptm);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001484 }
1485 len = strlen(timeBuf);
1486 if (p_format->nsec_time_output) {
1487 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%09ld", nsec);
1488 } else if (p_format->usec_time_output) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001489 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%06ld", nsec / US_PER_NSEC);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001490 } else {
Tom Cherry71ba1642019-01-10 10:37:36 -08001491 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%03ld", nsec / MS_PER_NSEC);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001492 }
1493 if (p_format->zone_output && ptm) {
1494 strftime(timeBuf + len, sizeof(timeBuf) - len, " %z", ptm);
1495 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001496
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001497 /*
1498 * Construct a buffer containing the log header and log message.
1499 */
1500 if (p_format->colored_output) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001501 prefixLen =
1502 snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm", colorFromPri(entry->priority));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001503 prefixLen = MIN(prefixLen, sizeof(prefixBuf));
George Burgess IV487de272018-05-15 18:56:36 -07001504
1505 const char suffixContents[] = "\x1B[0m";
1506 strcpy(suffixBuf, suffixContents);
1507 suffixLen = strlen(suffixContents);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001508 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001509
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001510 char uid[16];
1511 uid[0] = '\0';
1512 if (p_format->uid_output) {
1513 if (entry->uid >= 0) {
1514/*
1515 * This code is Android specific, bionic guarantees that
1516 * calls to non-reentrant getpwuid() are thread safe.
1517 */
Tom Cherrye2187bf2020-01-27 15:45:52 -08001518#ifdef __ANDROID__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001519 struct passwd* pwd = getpwuid(entry->uid);
1520 if (pwd && (strlen(pwd->pw_name) <= 5)) {
1521 snprintf(uid, sizeof(uid), "%5s:", pwd->pw_name);
1522 } else
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001523#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001524 {
1525 /* Not worth parsing package list, names all longer than 5 */
1526 snprintf(uid, sizeof(uid), "%5d:", entry->uid);
1527 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001528 } else {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001529 snprintf(uid, sizeof(uid), " ");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001530 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001531 }
1532
1533 switch (p_format->format) {
1534 case FORMAT_TAG:
Tom Cherry71ba1642019-01-10 10:37:36 -08001535 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c/%-8.*s: ", priChar,
1536 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001537 strcpy(suffixBuf + suffixLen, "\n");
1538 ++suffixLen;
1539 break;
1540 case FORMAT_PROCESS:
Tom Cherry71ba1642019-01-10 10:37:36 -08001541 len = snprintf(suffixBuf + suffixLen, sizeof(suffixBuf) - suffixLen, " (%.*s)\n",
1542 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001543 suffixLen += MIN(len, sizeof(suffixBuf) - suffixLen);
Tom Cherry71ba1642019-01-10 10:37:36 -08001544 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c(%s%5d) ", priChar,
1545 uid, entry->pid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001546 break;
1547 case FORMAT_THREAD:
Tom Cherry71ba1642019-01-10 10:37:36 -08001548 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c(%s%5d:%5d) ",
1549 priChar, uid, entry->pid, entry->tid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001550 strcpy(suffixBuf + suffixLen, "\n");
1551 ++suffixLen;
1552 break;
1553 case FORMAT_RAW:
1554 prefixBuf[prefixLen] = 0;
1555 len = 0;
1556 strcpy(suffixBuf + suffixLen, "\n");
1557 ++suffixLen;
1558 break;
1559 case FORMAT_TIME:
1560 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherry71ba1642019-01-10 10:37:36 -08001561 "%s %c/%-8.*s(%s%5d): ", timeBuf, priChar, (int)entry->tagLen, entry->tag, uid,
1562 entry->pid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001563 strcpy(suffixBuf + suffixLen, "\n");
1564 ++suffixLen;
1565 break;
1566 case FORMAT_THREADTIME:
1567 ret = strchr(uid, ':');
1568 if (ret) {
1569 *ret = ' ';
1570 }
1571 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherry71ba1642019-01-10 10:37:36 -08001572 "%s %s%5d %5d %c %-8.*s: ", timeBuf, uid, entry->pid, entry->tid, priChar,
1573 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001574 strcpy(suffixBuf + suffixLen, "\n");
1575 ++suffixLen;
1576 break;
1577 case FORMAT_LONG:
1578 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherry71ba1642019-01-10 10:37:36 -08001579 "[ %s %s%5d:%5d %c/%-8.*s ]\n", timeBuf, uid, entry->pid, entry->tid, priChar,
1580 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001581 strcpy(suffixBuf + suffixLen, "\n\n");
1582 suffixLen += 2;
1583 prefixSuffixIsHeaderFooter = 1;
1584 break;
1585 case FORMAT_BRIEF:
1586 default:
Tom Cherry71ba1642019-01-10 10:37:36 -08001587 len =
1588 snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
1589 "%c/%-8.*s(%s%5d): ", priChar, (int)entry->tagLen, entry->tag, uid, entry->pid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001590 strcpy(suffixBuf + suffixLen, "\n");
1591 ++suffixLen;
1592 break;
1593 }
1594
1595 /* snprintf has a weird return value. It returns what would have been
1596 * written given a large enough buffer. In the case that the prefix is
1597 * longer then our buffer(128), it messes up the calculations below
1598 * possibly causing heap corruption. To avoid this we double check and
1599 * set the length at the maximum (size minus null byte)
1600 */
1601 prefixLen += len;
1602 if (prefixLen >= sizeof(prefixBuf)) {
1603 prefixLen = sizeof(prefixBuf) - 1;
1604 prefixBuf[sizeof(prefixBuf) - 1] = '\0';
1605 }
1606 if (suffixLen >= sizeof(suffixBuf)) {
1607 suffixLen = sizeof(suffixBuf) - 1;
1608 suffixBuf[sizeof(suffixBuf) - 2] = '\n';
1609 suffixBuf[sizeof(suffixBuf) - 1] = '\0';
1610 }
1611
1612 /* the following code is tragically unreadable */
1613
1614 size_t numLines;
1615 char* p;
1616 size_t bufferSize;
1617 const char* pm;
1618
1619 if (prefixSuffixIsHeaderFooter) {
1620 /* we're just wrapping message with a header/footer */
1621 numLines = 1;
1622 } else {
1623 pm = entry->message;
1624 numLines = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001625
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001626 /*
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001627 * The line-end finding here must match the line-end finding
1628 * in for ( ... numLines...) loop below
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001629 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001630 while (pm < (entry->message + entry->messageLen)) {
1631 if (*pm++ == '\n') numLines++;
1632 }
1633 /* plus one line for anything not newline-terminated at the end */
1634 if (pm > entry->message && *(pm - 1) != '\n') numLines++;
1635 }
1636
1637 /*
1638 * this is an upper bound--newlines in message may be counted
1639 * extraneously
1640 */
1641 bufferSize = (numLines * (prefixLen + suffixLen)) + 1;
1642 if (p_format->printable_output) {
1643 /* Calculate extra length to convert non-printable to printable */
1644 bufferSize += convertPrintable(NULL, entry->message, entry->messageLen);
1645 } else {
1646 bufferSize += entry->messageLen;
1647 }
1648
1649 if (defaultBufferSize >= bufferSize) {
1650 ret = defaultBuffer;
1651 } else {
1652 ret = (char*)malloc(bufferSize);
1653
1654 if (ret == NULL) {
1655 return ret;
1656 }
1657 }
1658
1659 ret[0] = '\0'; /* to start strcat off */
1660
1661 p = ret;
1662 pm = entry->message;
1663
1664 if (prefixSuffixIsHeaderFooter) {
1665 strcat(p, prefixBuf);
1666 p += prefixLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001667 if (p_format->printable_output) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001668 p += convertPrintable(p, entry->message, entry->messageLen);
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001669 } else {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001670 strncat(p, entry->message, entry->messageLen);
1671 p += entry->messageLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001672 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001673 strcat(p, suffixBuf);
1674 p += suffixLen;
1675 } else {
1676 do {
1677 const char* lineStart;
1678 size_t lineLen;
1679 lineStart = pm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001680
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001681 /* Find the next end-of-line in message */
1682 while (pm < (entry->message + entry->messageLen) && *pm != '\n') pm++;
1683 lineLen = pm - lineStart;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001684
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001685 strcat(p, prefixBuf);
1686 p += prefixLen;
1687 if (p_format->printable_output) {
1688 p += convertPrintable(p, lineStart, lineLen);
1689 } else {
1690 strncat(p, lineStart, lineLen);
1691 p += lineLen;
1692 }
1693 strcat(p, suffixBuf);
1694 p += suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001695
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001696 if (*pm == '\n') pm++;
1697 } while (pm < (entry->message + entry->messageLen));
1698 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001699
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001700 if (p_outLength != NULL) {
1701 *p_outLength = p - ret;
1702 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001703
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001704 return ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001705}
1706
1707/**
1708 * Either print or do not print log line, based on filter
1709 *
1710 * Returns count bytes written
1711 */
1712
Tom Cherry2d9779e2019-02-08 11:46:19 -08001713int android_log_printLogLine(AndroidLogFormat* p_format, int fd, const AndroidLogEntry* entry) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001714 int ret;
1715 char defaultBuffer[512];
1716 char* outBuffer = NULL;
1717 size_t totalLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001718
Tom Cherry71ba1642019-01-10 10:37:36 -08001719 outBuffer =
1720 android_log_formatLogLine(p_format, defaultBuffer, sizeof(defaultBuffer), entry, &totalLen);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001721
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001722 if (!outBuffer) return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001723
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001724 do {
1725 ret = write(fd, outBuffer, totalLen);
1726 } while (ret < 0 && errno == EINTR);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001727
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001728 if (ret < 0) {
1729 fprintf(stderr, "+++ LOG: write failed (errno=%d)\n", errno);
1730 ret = 0;
1731 goto done;
1732 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001733
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001734 if (((size_t)ret) < totalLen) {
1735 fprintf(stderr, "+++ LOG: write partial (%d of %d)\n", ret, (int)totalLen);
1736 goto done;
1737 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001738
1739done:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001740 if (outBuffer != defaultBuffer) {
1741 free(outBuffer);
1742 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001743
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001744 return ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001745}