blob: 4b61828b2291471f95a0360dad1103285e780291 [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;
Tom Cherry441054a2019-10-15 16:53:11 -0700535 if (buf->hdr_size != sizeof(struct logger_entry)) {
536 fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
537 return -1;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800538 }
Tom Cherry441054a2019-10-15 16:53:11 -0700539 entry->uid = buf->uid;
540
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800541 for (i = 1; i < buf->len; i++) {
542 if (msg[i] == '\0') {
543 if (msgStart == -1) {
544 msgStart = i + 1;
545 } else {
546 msgEnd = i;
547 break;
548 }
549 }
550 }
551
552 if (msgStart == -1) {
553 /* +++ LOG: malformed log message, DYB */
Nick Kraleviche1ede152011-10-18 15:23:33 -0700554 for (i = 1; i < buf->len; i++) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800555 /* odd characters in tag? */
556 if ((msg[i] <= ' ') || (msg[i] == ':') || (msg[i] >= 0x7f)) {
557 msg[i] = '\0';
558 msgStart = i + 1;
559 break;
560 }
Nick Kraleviche1ede152011-10-18 15:23:33 -0700561 }
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700562 if (msgStart == -1) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800563 msgStart = buf->len - 1; /* All tag, no message, print truncates */
Nick Kralevich63f4a842011-10-17 10:45:03 -0700564 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800565 }
566 if (msgEnd == -1) {
567 /* incoming message not null-terminated; force it */
568 msgEnd = buf->len - 1; /* may result in msgEnd < msgStart */
569 msg[msgEnd] = '\0';
570 }
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700571
Tom Cherry71ba1642019-01-10 10:37:36 -0800572 entry->priority = static_cast<android_LogPriority>(msg[0]);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800573 entry->tag = msg + 1;
574 entry->tagLen = msgStart - 1;
575 entry->message = msg + msgStart;
576 entry->messageLen = (msgEnd < msgStart) ? 0 : (msgEnd - msgStart);
Nick Kralevich63f4a842011-10-17 10:45:03 -0700577
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800578 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700579}
580
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700581static bool findChar(const char** cp, size_t* len, int c) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800582 while ((*len) && isspace(*(*cp))) {
583 ++(*cp);
584 --(*len);
585 }
586 if (c == INT_MAX) return *len;
587 if ((*len) && (*(*cp) == c)) {
588 ++(*cp);
589 --(*len);
590 return true;
591 }
592 return false;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700593}
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000594
595/*
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700596 * Recursively convert binary log data to printable form.
597 *
598 * This needs to be recursive because you can have lists of lists.
599 *
600 * If we run out of room, we stop processing immediately. It's important
601 * for us to check for space on every output element to avoid producing
602 * garbled output.
603 *
604 * Returns 0 on success, 1 on buffer full, -1 on failure.
605 */
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700606enum objectType {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800607 TYPE_OBJECTS = '1',
608 TYPE_BYTES = '2',
609 TYPE_MILLISECONDS = '3',
610 TYPE_ALLOCATIONS = '4',
611 TYPE_ID = '5',
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800612 TYPE_PERCENT = '6',
613 TYPE_MONOTONIC = 's'
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700614};
615
Tom Cherry71ba1642019-01-10 10:37:36 -0800616static int android_log_printBinaryEvent(const unsigned char** pEventData, size_t* pEventDataLen,
617 char** pOutBuf, size_t* pOutBufLen, const char** fmtStr,
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800618 size_t* fmtLen) {
619 const unsigned char* eventData = *pEventData;
620 size_t eventDataLen = *pEventDataLen;
621 char* outBuf = *pOutBuf;
622 char* outBufSave = outBuf;
623 size_t outBufLen = *pOutBufLen;
624 size_t outBufLenSave = outBufLen;
625 unsigned char type;
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800626 size_t outCount = 0;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800627 int result = 0;
628 const char* cp;
629 size_t len;
630 int64_t lval;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700631
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800632 if (eventDataLen < 1) return -1;
Mark Salyzyn1a57ae32016-11-11 14:41:30 -0800633
Tom Cherrybbbf0892019-10-09 10:53:37 -0700634 type = *eventData;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700635
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800636 cp = NULL;
637 len = 0;
638 if (fmtStr && *fmtStr && fmtLen && *fmtLen && **fmtStr) {
639 cp = *fmtStr;
640 len = *fmtLen;
641 }
642 /*
643 * event.logtag format specification:
644 *
645 * Optionally, after the tag names can be put a description for the value(s)
646 * of the tag. Description are in the format
647 * (<name>|data type[|data unit])
648 * Multiple values are separated by commas.
649 *
650 * The data type is a number from the following values:
651 * 1: int
652 * 2: long
653 * 3: string
654 * 4: list
655 * 5: float
656 *
657 * The data unit is a number taken from the following list:
658 * 1: Number of objects
659 * 2: Number of bytes
660 * 3: Number of milliseconds
661 * 4: Number of allocations
662 * 5: Id
663 * 6: Percent
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800664 * s: Number of seconds (monotonic time)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800665 * Default value for data of type int/long is 2 (bytes).
666 */
667 if (!cp || !findChar(&cp, &len, '(')) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700668 len = 0;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800669 } else {
670 char* outBufLastSpace = NULL;
671
672 findChar(&cp, &len, INT_MAX);
673 while (len && *cp && (*cp != '|') && (*cp != ')')) {
674 if (outBufLen <= 0) {
675 /* halt output */
676 goto no_room;
677 }
678 outBufLastSpace = isspace(*cp) ? outBuf : NULL;
679 *outBuf = *cp;
680 ++outBuf;
681 ++cp;
682 --outBufLen;
683 --len;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700684 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800685 if (outBufLastSpace) {
686 outBufLen += outBuf - outBufLastSpace;
687 outBuf = outBufLastSpace;
688 }
689 if (outBufLen <= 0) {
690 /* halt output */
691 goto no_room;
692 }
693 if (outBufSave != outBuf) {
694 *outBuf = '=';
695 ++outBuf;
696 --outBufLen;
697 }
698
699 if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800700 static const unsigned char typeTable[] = {EVENT_TYPE_INT, EVENT_TYPE_LONG, EVENT_TYPE_STRING,
701 EVENT_TYPE_LIST, EVENT_TYPE_FLOAT};
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800702
Tom Cherry71ba1642019-01-10 10:37:36 -0800703 if ((*cp >= '1') && (*cp < (char)('1' + (sizeof(typeTable) / sizeof(typeTable[0])))) &&
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800704 (type != typeTable[(size_t)(*cp - '1')]))
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700705 len = 0;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700706
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800707 if (len) {
708 ++cp;
709 --len;
710 } else {
711 /* reset the format */
712 outBuf = outBufSave;
713 outBufLen = outBufLenSave;
714 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700715 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800716 }
717 outCount = 0;
718 lval = 0;
719 switch (type) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700720 case EVENT_TYPE_INT:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800721 /* 32-bit signed int */
722 {
Tom Cherrybbbf0892019-10-09 10:53:37 -0700723 if (eventDataLen < sizeof(android_event_int_t)) return -1;
724 auto* event_int = reinterpret_cast<const android_event_int_t*>(eventData);
725 lval = event_int->data;
726 eventData += sizeof(android_event_int_t);
727 eventDataLen -= sizeof(android_event_int_t);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800728 }
729 goto pr_lval;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700730 case EVENT_TYPE_LONG:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800731 /* 64-bit signed long */
Tom Cherrybbbf0892019-10-09 10:53:37 -0700732 if (eventDataLen < sizeof(android_event_long_t)) {
733 return -1;
734 }
735 {
736 auto* event_long = reinterpret_cast<const android_event_long_t*>(eventData);
737 lval = event_long->data;
738 }
739 eventData += sizeof(android_event_long_t);
740 eventDataLen -= sizeof(android_event_long_t);
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700741 pr_lval:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800742 outCount = snprintf(outBuf, outBufLen, "%" PRId64, lval);
743 if (outCount < outBufLen) {
744 outBuf += outCount;
745 outBufLen -= outCount;
746 } else {
747 /* halt output */
748 goto no_room;
749 }
750 break;
Jeff Brown44193d92015-04-28 12:47:02 -0700751 case EVENT_TYPE_FLOAT:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800752 /* float */
753 {
Tom Cherrybbbf0892019-10-09 10:53:37 -0700754 if (eventDataLen < sizeof(android_event_float_t)) return -1;
755 auto* event_float = reinterpret_cast<const android_event_float_t*>(eventData);
756 float fval = event_float->data;
757 eventData += sizeof(android_event_int_t);
758 eventDataLen -= sizeof(android_event_int_t);
Jeff Brown44193d92015-04-28 12:47:02 -0700759
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800760 outCount = snprintf(outBuf, outBufLen, "%f", fval);
761 if (outCount < outBufLen) {
762 outBuf += outCount;
763 outBufLen -= outCount;
764 } else {
765 /* halt output */
766 goto no_room;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700767 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800768 }
769 break;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700770 case EVENT_TYPE_STRING:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800771 /* UTF-8 chars, not NULL-terminated */
772 {
Tom Cherrybbbf0892019-10-09 10:53:37 -0700773 if (eventDataLen < sizeof(android_event_string_t)) return -1;
774 auto* event_string = reinterpret_cast<const android_event_string_t*>(eventData);
775 unsigned int strLen = event_string->length;
776 eventData += sizeof(android_event_string_t);
777 eventDataLen -= sizeof(android_event_string_t);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700778
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800779 if (eventDataLen < strLen) {
780 result = -1; /* mark truncated */
781 strLen = eventDataLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700782 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700783
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800784 if (cp && (strLen == 0)) {
785 /* reset the format if no content */
786 outBuf = outBufSave;
787 outBufLen = outBufLenSave;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700788 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800789 if (strLen < outBufLen) {
790 memcpy(outBuf, eventData, strLen);
791 outBuf += strLen;
792 outBufLen -= strLen;
793 } else {
794 if (outBufLen > 0) {
795 /* copy what we can */
796 memcpy(outBuf, eventData, outBufLen);
797 outBuf += outBufLen;
798 outBufLen -= outBufLen;
799 }
800 if (!result) result = 1; /* if not truncated, return no room */
801 }
802 eventData += strLen;
803 eventDataLen -= strLen;
804 if (result != 0) goto bail;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700805 break;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800806 }
807 case EVENT_TYPE_LIST:
808 /* N items, all different types */
809 {
Tom Cherrybbbf0892019-10-09 10:53:37 -0700810 if (eventDataLen < sizeof(android_event_list_t)) return -1;
811 auto* event_list = reinterpret_cast<const android_event_list_t*>(eventData);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800812
Tom Cherrybbbf0892019-10-09 10:53:37 -0700813 int8_t count = event_list->element_count;
814 eventData += sizeof(android_event_list_t);
815 eventDataLen -= sizeof(android_event_list_t);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800816
817 if (outBufLen <= 0) goto no_room;
818
819 *outBuf++ = '[';
820 outBufLen--;
821
Tom Cherrybbbf0892019-10-09 10:53:37 -0700822 for (int i = 0; i < count; i++) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800823 result = android_log_printBinaryEvent(&eventData, &eventDataLen, &outBuf, &outBufLen,
824 fmtStr, fmtLen);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800825 if (result != 0) goto bail;
826
827 if (i < (count - 1)) {
828 if (outBufLen <= 0) goto no_room;
829 *outBuf++ = ',';
830 outBufLen--;
831 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700832 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800833
834 if (outBufLen <= 0) goto no_room;
835
836 *outBuf++ = ']';
837 outBufLen--;
838 }
839 break;
840 default:
841 fprintf(stderr, "Unknown binary event type %d\n", type);
842 return -1;
843 }
844 if (cp && len) {
845 if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) {
846 switch (*cp) {
847 case TYPE_OBJECTS:
848 outCount = 0;
849 /* outCount = snprintf(outBuf, outBufLen, " objects"); */
850 break;
851 case TYPE_BYTES:
852 if ((lval != 0) && ((lval % 1024) == 0)) {
853 /* repaint with multiplier */
Tom Cherry71ba1642019-01-10 10:37:36 -0800854 static const char suffixTable[] = {'K', 'M', 'G', 'T'};
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800855 size_t idx = 0;
856 outBuf -= outCount;
857 outBufLen += outCount;
858 do {
859 lval /= 1024;
860 if ((lval % 1024) != 0) break;
Tom Cherry71ba1642019-01-10 10:37:36 -0800861 } while (++idx < ((sizeof(suffixTable) / sizeof(suffixTable[0])) - 1));
862 outCount = snprintf(outBuf, outBufLen, "%" PRId64 "%cB", lval, suffixTable[idx]);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800863 } else {
864 outCount = snprintf(outBuf, outBufLen, "B");
865 }
866 break;
867 case TYPE_MILLISECONDS:
Tom Cherry71ba1642019-01-10 10:37:36 -0800868 if (((lval <= -1000) || (1000 <= lval)) && (outBufLen || (outBuf[-1] == '0'))) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800869 /* repaint as (fractional) seconds, possibly saving space */
870 if (outBufLen) outBuf[0] = outBuf[-1];
871 outBuf[-1] = outBuf[-2];
872 outBuf[-2] = outBuf[-3];
873 outBuf[-3] = '.';
874 while ((outBufLen == 0) || (*outBuf == '0')) {
875 --outBuf;
876 ++outBufLen;
877 }
878 if (*outBuf != '.') {
879 ++outBuf;
880 --outBufLen;
881 }
882 outCount = snprintf(outBuf, outBufLen, "s");
883 } else {
884 outCount = snprintf(outBuf, outBufLen, "ms");
885 }
886 break;
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800887 case TYPE_MONOTONIC: {
888 static const uint64_t minute = 60;
889 static const uint64_t hour = 60 * minute;
890 static const uint64_t day = 24 * hour;
891
892 /* Repaint as unsigned seconds, minutes, hours ... */
893 outBuf -= outCount;
894 outBufLen += outCount;
895 uint64_t val = lval;
896 if (val >= day) {
897 outCount = snprintf(outBuf, outBufLen, "%" PRIu64 "d ", val / day);
898 if (outCount >= outBufLen) break;
899 outBuf += outCount;
900 outBufLen -= outCount;
901 val = (val % day) + day;
902 }
903 if (val >= minute) {
904 if (val >= hour) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800905 outCount = snprintf(outBuf, outBufLen, "%" PRIu64 ":", (val / hour) % (day / hour));
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800906 if (outCount >= outBufLen) break;
907 outBuf += outCount;
908 outBufLen -= outCount;
909 }
910 outCount =
Tom Cherry71ba1642019-01-10 10:37:36 -0800911 snprintf(outBuf, outBufLen, (val >= hour) ? "%02" PRIu64 ":" : "%" PRIu64 ":",
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800912 (val / minute) % (hour / minute));
913 if (outCount >= outBufLen) break;
914 outBuf += outCount;
915 outBufLen -= outCount;
916 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800917 outCount = snprintf(outBuf, outBufLen, (val >= minute) ? "%02" PRIu64 : "%" PRIu64 "s",
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800918 val % minute);
919 } break;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800920 case TYPE_ALLOCATIONS:
921 outCount = 0;
922 /* outCount = snprintf(outBuf, outBufLen, " allocations"); */
923 break;
924 case TYPE_ID:
925 outCount = 0;
926 break;
927 case TYPE_PERCENT:
928 outCount = snprintf(outBuf, outBufLen, "%%");
929 break;
930 default: /* ? */
931 outCount = 0;
932 break;
933 }
934 ++cp;
935 --len;
936 if (outCount < outBufLen) {
937 outBuf += outCount;
938 outBufLen -= outCount;
939 } else if (outCount) {
940 /* halt output */
941 goto no_room;
942 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700943 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800944 if (!findChar(&cp, &len, ')')) len = 0;
945 if (!findChar(&cp, &len, ',')) len = 0;
946 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700947
948bail:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800949 *pEventData = eventData;
950 *pEventDataLen = eventDataLen;
951 *pOutBuf = outBuf;
952 *pOutBufLen = outBufLen;
953 if (cp) {
954 *fmtStr = cp;
955 *fmtLen = len;
956 }
957 return result;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700958
959no_room:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800960 result = 1;
961 goto bail;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700962}
963
964/**
965 * Convert a binary log entry to ASCII form.
966 *
967 * For convenience we mimic the processLogBuffer API. There is no
968 * pre-defined output length for the binary data, since we're free to format
969 * it however we choose, which means we can't really use a fixed-size buffer
970 * here.
971 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800972int android_log_processBinaryLogBuffer(
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800973 struct logger_entry* buf, AndroidLogEntry* entry,
Tom Cherry71ba1642019-01-10 10:37:36 -0800974 [[maybe_unused]] const EventTagMap* map, /* only on !__ANDROID__ */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800975 char* messageBuf, int messageBufLen) {
976 size_t inCount;
977 uint32_t tagIndex;
978 const unsigned char* eventData;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700979
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800980 entry->message = NULL;
981 entry->messageLen = 0;
Mark Salyzynb1d150b2017-03-02 07:47:21 -0800982
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800983 entry->tv_sec = buf->sec;
984 entry->tv_nsec = buf->nsec;
985 entry->priority = ANDROID_LOG_INFO;
986 entry->uid = -1;
987 entry->pid = buf->pid;
988 entry->tid = buf->tid;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700989
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800990 eventData = (const unsigned char*)buf->msg;
Tom Cherry441054a2019-10-15 16:53:11 -0700991 if (buf->hdr_size != sizeof(struct logger_entry)) {
992 fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
993 return -1;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800994 }
Tom Cherry441054a2019-10-15 16:53:11 -0700995 if (buf->lid == LOG_ID_SECURITY) {
996 entry->priority = ANDROID_LOG_WARN;
997 }
998 entry->uid = buf->uid;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800999 inCount = buf->len;
Tom Cherrybbbf0892019-10-09 10:53:37 -07001000 if (inCount < sizeof(android_event_header_t)) return -1;
1001 auto* event_header = reinterpret_cast<const android_event_header_t*>(eventData);
1002 tagIndex = event_header->tag;
1003 eventData += sizeof(android_event_header_t);
1004 inCount -= sizeof(android_event_header_t);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001005
1006 entry->tagLen = 0;
1007 entry->tag = NULL;
1008#ifdef __ANDROID__
1009 if (map != NULL) {
1010 entry->tag = android_lookupEventTag_len(map, &entry->tagLen, tagIndex);
1011 }
1012#endif
1013
1014 /*
1015 * If we don't have a map, or didn't find the tag number in the map,
1016 * stuff a generated tag value into the start of the output buffer and
1017 * shift the buffer pointers down.
1018 */
1019 if (entry->tag == NULL) {
1020 size_t tagLen;
1021
1022 tagLen = snprintf(messageBuf, messageBufLen, "[%" PRIu32 "]", tagIndex);
1023 if (tagLen >= (size_t)messageBufLen) {
1024 tagLen = messageBufLen - 1;
1025 }
1026 entry->tag = messageBuf;
1027 entry->tagLen = tagLen;
1028 messageBuf += tagLen + 1;
1029 messageBufLen -= tagLen + 1;
1030 }
1031
1032 /*
1033 * Format the event log data into the buffer.
1034 */
1035 const char* fmtStr = NULL;
1036 size_t fmtLen = 0;
1037#ifdef __ANDROID__
1038 if (descriptive_output && map) {
1039 fmtStr = android_lookupEventFormat_len(map, &fmtLen, tagIndex);
1040 }
1041#endif
1042
1043 char* outBuf = messageBuf;
1044 size_t outRemaining = messageBufLen - 1; /* leave one for nul byte */
1045 int result = 0;
1046
1047 if ((inCount > 0) || fmtLen) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001048 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, &outRemaining, &fmtStr,
1049 &fmtLen);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001050 }
1051 if ((result == 1) && fmtStr) {
1052 /* We overflowed :-(, let's repaint the line w/o format dressings */
Mark Salyzynb1d150b2017-03-02 07:47:21 -08001053 eventData = (const unsigned char*)buf->msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001054 eventData += 4;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001055 outBuf = messageBuf;
1056 outRemaining = messageBufLen - 1;
Tom Cherry71ba1642019-01-10 10:37:36 -08001057 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, &outRemaining, NULL, NULL);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001058 }
1059 if (result < 0) {
1060 fprintf(stderr, "Binary log entry conversion failed\n");
1061 }
1062 if (result) {
1063 if (!outRemaining) {
1064 /* make space to leave an indicator */
1065 --outBuf;
1066 ++outRemaining;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001067 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001068 *outBuf++ = (result < 0) ? '!' : '^'; /* Error or Truncation? */
1069 outRemaining--;
1070 /* pretend we ate all the data to prevent log stutter */
1071 inCount = 0;
1072 if (result > 0) result = 0;
1073 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001074
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001075 /* eat the silly terminating '\n' */
1076 if (inCount == 1 && *eventData == '\n') {
1077 eventData++;
1078 inCount--;
1079 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001080
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001081 if (inCount != 0) {
1082 fprintf(stderr, "Warning: leftover binary log data (%zu bytes)\n", inCount);
1083 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001084
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001085 /*
1086 * Terminate the buffer. The NUL byte does not count as part of
1087 * entry->messageLen.
1088 */
1089 *outBuf = '\0';
1090 entry->messageLen = outBuf - messageBuf;
1091 assert(entry->messageLen == (messageBufLen - 1) - outRemaining);
Mark Salyzyn1a57ae32016-11-11 14:41:30 -08001092
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001093 entry->message = messageBuf;
Mark Salyzyn1a57ae32016-11-11 14:41:30 -08001094
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001095 return result;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001096}
1097
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001098/*
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001099 * Convert to printable from message to p buffer, return string length. If p is
1100 * NULL, do not copy, but still return the expected string length.
1101 */
Tom Cherry91589842019-04-25 13:10:30 -07001102size_t convertPrintable(char* p, const char* message, size_t messageLen) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001103 char* begin = p;
1104 bool print = p != NULL;
Tom Cherry91589842019-04-25 13:10:30 -07001105 mbstate_t mb_state = {};
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001106
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001107 while (messageLen) {
1108 char buf[6];
1109 ssize_t len = sizeof(buf) - 1;
1110 if ((size_t)len > messageLen) {
1111 len = messageLen;
1112 }
Tom Cherry91589842019-04-25 13:10:30 -07001113 len = mbrtowc(nullptr, message, len, &mb_state);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001114
1115 if (len < 0) {
Tom Cherry91589842019-04-25 13:10:30 -07001116 snprintf(buf, sizeof(buf), "\\x%02X", static_cast<unsigned char>(*message));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001117 len = 1;
1118 } else {
1119 buf[0] = '\0';
1120 if (len == 1) {
1121 if (*message == '\a') {
1122 strcpy(buf, "\\a");
1123 } else if (*message == '\b') {
1124 strcpy(buf, "\\b");
1125 } else if (*message == '\t') {
1126 strcpy(buf, "\t"); /* Do not escape tabs */
1127 } else if (*message == '\v') {
1128 strcpy(buf, "\\v");
1129 } else if (*message == '\f') {
1130 strcpy(buf, "\\f");
1131 } else if (*message == '\r') {
1132 strcpy(buf, "\\r");
1133 } else if (*message == '\\') {
1134 strcpy(buf, "\\\\");
1135 } else if ((*message < ' ') || (*message & 0x80)) {
Tom Cherry91589842019-04-25 13:10:30 -07001136 snprintf(buf, sizeof(buf), "\\x%02X", static_cast<unsigned char>(*message));
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001137 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001138 }
1139 if (!buf[0]) {
1140 strncpy(buf, message, len);
1141 buf[len] = '\0';
1142 }
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001143 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001144 if (print) {
1145 strcpy(p, buf);
1146 }
1147 p += strlen(buf);
1148 message += len;
1149 messageLen -= len;
1150 }
1151 return p - begin;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001152}
1153
Tom Cherry7d045f62019-09-30 12:58:55 -07001154#ifdef __ANDROID__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001155static char* readSeconds(char* e, struct timespec* t) {
1156 unsigned long multiplier;
1157 char* p;
1158 t->tv_sec = strtoul(e, &p, 10);
1159 if (*p != '.') {
1160 return NULL;
1161 }
1162 t->tv_nsec = 0;
1163 multiplier = NS_PER_SEC;
1164 while (isdigit(*++p) && (multiplier /= 10)) {
1165 t->tv_nsec += (*p - '0') * multiplier;
1166 }
1167 return p;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001168}
1169
Tom Cherry71ba1642019-01-10 10:37:36 -08001170static struct timespec* sumTimespec(struct timespec* left, struct timespec* right) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001171 left->tv_nsec += right->tv_nsec;
1172 left->tv_sec += right->tv_sec;
1173 if (left->tv_nsec >= (long)NS_PER_SEC) {
1174 left->tv_nsec -= NS_PER_SEC;
1175 left->tv_sec += 1;
1176 }
1177 return left;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001178}
1179
Tom Cherry71ba1642019-01-10 10:37:36 -08001180static struct timespec* subTimespec(struct timespec* result, struct timespec* left,
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001181 struct timespec* right) {
1182 result->tv_nsec = left->tv_nsec - right->tv_nsec;
1183 result->tv_sec = left->tv_sec - right->tv_sec;
1184 if (result->tv_nsec < 0) {
1185 result->tv_nsec += NS_PER_SEC;
1186 result->tv_sec -= 1;
1187 }
1188 return result;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001189}
1190
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001191static long long nsecTimespec(struct timespec* now) {
1192 return (long long)now->tv_sec * NS_PER_SEC + now->tv_nsec;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001193}
1194
Tom Cherry71ba1642019-01-10 10:37:36 -08001195static void convertMonotonic(struct timespec* result, const AndroidLogEntry* entry) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001196 struct listnode* node;
1197 struct conversionList {
1198 struct listnode node; /* first */
1199 struct timespec time;
1200 struct timespec convert;
1201 } * list, *next;
1202 struct timespec time, convert;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001203
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001204 /* If we do not have a conversion list, build one up */
1205 if (list_empty(&convertHead)) {
1206 bool suspended_pending = false;
Tom Cherry71ba1642019-01-10 10:37:36 -08001207 struct timespec suspended_monotonic = {0, 0};
1208 struct timespec suspended_diff = {0, 0};
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001209
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001210 /*
1211 * Read dmesg for _some_ synchronization markers and insert
1212 * Anything in the Android Logger before the dmesg logging span will
1213 * be highly suspect regarding the monotonic time calculations.
1214 */
1215 FILE* p = popen("/system/bin/dmesg", "re");
1216 if (p) {
1217 char* line = NULL;
1218 size_t len = 0;
1219 while (getline(&line, &len, p) > 0) {
1220 static const char suspend[] = "PM: suspend entry ";
1221 static const char resume[] = "PM: suspend exit ";
1222 static const char healthd[] = "healthd";
1223 static const char battery[] = ": battery ";
1224 static const char suspended[] = "Suspended for ";
1225 struct timespec monotonic;
1226 struct tm tm;
1227 char *cp, *e = line;
1228 bool add_entry = true;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001229
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001230 if (*e == '<') {
1231 while (*e && (*e != '>')) {
1232 ++e;
1233 }
1234 if (*e != '>') {
1235 continue;
1236 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001237 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001238 if (*e != '[') {
1239 continue;
1240 }
1241 while (*++e == ' ') {
1242 ;
1243 }
1244 e = readSeconds(e, &monotonic);
1245 if (!e || (*e != ']')) {
1246 continue;
1247 }
1248
1249 if ((e = strstr(e, suspend))) {
1250 e += sizeof(suspend) - 1;
1251 } else if ((e = strstr(line, resume))) {
1252 e += sizeof(resume) - 1;
1253 } else if (((e = strstr(line, healthd))) &&
1254 ((e = strstr(e + sizeof(healthd) - 1, battery)))) {
1255 /* NB: healthd is roughly 150us late, worth the price to
1256 * deal with ntp-induced or hardware clock drift. */
1257 e += sizeof(battery) - 1;
1258 } else if ((e = strstr(line, suspended))) {
1259 e += sizeof(suspended) - 1;
1260 e = readSeconds(e, &time);
1261 if (!e) {
1262 continue;
1263 }
1264 add_entry = false;
1265 suspended_pending = true;
1266 suspended_monotonic = monotonic;
1267 suspended_diff = time;
1268 } else {
1269 continue;
1270 }
1271 if (add_entry) {
1272 /* look for "????-??-?? ??:??:??.????????? UTC" */
1273 cp = strstr(e, " UTC");
1274 if (!cp || ((cp - e) < 29) || (cp[-10] != '.')) {
1275 continue;
1276 }
1277 e = cp - 29;
1278 cp = readSeconds(cp - 10, &time);
1279 if (!cp) {
1280 continue;
1281 }
1282 cp = strptime(e, "%Y-%m-%d %H:%M:%S.", &tm);
1283 if (!cp) {
1284 continue;
1285 }
1286 cp = getenv(tz);
1287 if (cp) {
1288 cp = strdup(cp);
1289 }
1290 setenv(tz, utc, 1);
1291 time.tv_sec = mktime(&tm);
1292 if (cp) {
1293 setenv(tz, cp, 1);
1294 free(cp);
1295 } else {
1296 unsetenv(tz);
1297 }
Tom Cherry71ba1642019-01-10 10:37:36 -08001298 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001299 list_init(&list->node);
1300 list->time = time;
1301 subTimespec(&list->convert, &time, &monotonic);
1302 list_add_tail(&convertHead, &list->node);
1303 }
1304 if (suspended_pending && !list_empty(&convertHead)) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001305 list = node_to_item(list_tail(&convertHead), struct conversionList, node);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001306 if (subTimespec(&time, subTimespec(&time, &list->time, &list->convert),
1307 &suspended_monotonic)
1308 ->tv_sec > 0) {
1309 /* resume, what is convert factor before? */
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001310 subTimespec(&convert, &list->convert, &suspended_diff);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001311 } else {
1312 /* suspend */
1313 convert = list->convert;
1314 }
1315 time = suspended_monotonic;
1316 sumTimespec(&time, &convert);
1317 /* breakpoint just before sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001318 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001319 list_init(&list->node);
1320 list->time = time;
1321 list->convert = convert;
1322 list_add_tail(&convertHead, &list->node);
1323 /* breakpoint just after sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001324 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001325 list_init(&list->node);
1326 list->time = time;
1327 sumTimespec(&list->time, &suspended_diff);
1328 list->convert = convert;
1329 sumTimespec(&list->convert, &suspended_diff);
1330 list_add_tail(&convertHead, &list->node);
1331 suspended_pending = false;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001332 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001333 }
1334 pclose(p);
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001335 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001336 /* last entry is our current time conversion */
Tom Cherry71ba1642019-01-10 10:37:36 -08001337 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001338 list_init(&list->node);
1339 clock_gettime(CLOCK_REALTIME, &list->time);
1340 clock_gettime(CLOCK_MONOTONIC, &convert);
1341 clock_gettime(CLOCK_MONOTONIC, &time);
1342 /* Correct for instant clock_gettime latency (syscall or ~30ns) */
1343 subTimespec(&time, &convert, subTimespec(&time, &time, &convert));
1344 /* Calculate conversion factor */
1345 subTimespec(&list->convert, &list->time, &time);
1346 list_add_tail(&convertHead, &list->node);
1347 if (suspended_pending) {
1348 /* manufacture a suspend @ point before */
1349 subTimespec(&convert, &list->convert, &suspended_diff);
1350 time = suspended_monotonic;
1351 sumTimespec(&time, &convert);
1352 /* breakpoint just after sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001353 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001354 list_init(&list->node);
1355 list->time = time;
1356 sumTimespec(&list->time, &suspended_diff);
1357 list->convert = convert;
1358 sumTimespec(&list->convert, &suspended_diff);
1359 list_add_head(&convertHead, &list->node);
1360 /* breakpoint just before sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001361 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001362 list_init(&list->node);
1363 list->time = time;
1364 list->convert = convert;
1365 list_add_head(&convertHead, &list->node);
1366 }
1367 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001368
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001369 /* Find the breakpoint in the conversion list */
1370 list = node_to_item(list_head(&convertHead), struct conversionList, node);
1371 next = NULL;
1372 list_for_each(node, &convertHead) {
1373 next = node_to_item(node, struct conversionList, node);
1374 if (entry->tv_sec < next->time.tv_sec) {
1375 break;
1376 } else if (entry->tv_sec == next->time.tv_sec) {
1377 if (entry->tv_nsec < next->time.tv_nsec) {
1378 break;
1379 }
1380 }
1381 list = next;
1382 }
1383
1384 /* blend time from one breakpoint to the next */
1385 convert = list->convert;
1386 if (next) {
1387 unsigned long long total, run;
1388
1389 total = nsecTimespec(subTimespec(&time, &next->time, &list->time));
1390 time.tv_sec = entry->tv_sec;
1391 time.tv_nsec = entry->tv_nsec;
1392 run = nsecTimespec(subTimespec(&time, &time, &list->time));
1393 if (run < total) {
1394 long long crun;
1395
1396 float f = nsecTimespec(subTimespec(&time, &next->convert, &convert));
1397 f *= run;
1398 f /= total;
1399 crun = f;
1400 convert.tv_sec += crun / (long long)NS_PER_SEC;
1401 if (crun < 0) {
1402 convert.tv_nsec -= (-crun) % NS_PER_SEC;
1403 if (convert.tv_nsec < 0) {
1404 convert.tv_nsec += NS_PER_SEC;
1405 convert.tv_sec -= 1;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001406 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001407 } else {
1408 convert.tv_nsec += crun % NS_PER_SEC;
1409 if (convert.tv_nsec >= (long)NS_PER_SEC) {
1410 convert.tv_nsec -= NS_PER_SEC;
1411 convert.tv_sec += 1;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001412 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001413 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001414 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001415 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001416
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001417 /* Apply the correction factor */
1418 result->tv_sec = entry->tv_sec;
1419 result->tv_nsec = entry->tv_nsec;
1420 subTimespec(result, result, &convert);
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001421}
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001422#endif
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001423
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001424/**
1425 * Formats a log message into a buffer
1426 *
1427 * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer
1428 * If return value != defaultBuffer, caller must call free()
1429 * Returns NULL on malloc error
1430 */
1431
Tom Cherry2d9779e2019-02-08 11:46:19 -08001432char* android_log_formatLogLine(AndroidLogFormat* p_format, char* defaultBuffer,
1433 size_t defaultBufferSize, const AndroidLogEntry* entry,
1434 size_t* p_outLength) {
Yabin Cui8a985352014-11-13 10:02:08 -08001435#if !defined(_WIN32)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001436 struct tm tmBuf;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001437#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001438 struct tm* ptm;
1439 /* good margin, 23+nul for msec, 26+nul for usec, 29+nul to nsec */
1440 char timeBuf[64];
1441 char prefixBuf[128], suffixBuf[128];
1442 char priChar;
1443 int prefixSuffixIsHeaderFooter = 0;
1444 char* ret;
1445 time_t now;
1446 unsigned long nsec;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001447
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001448 priChar = filterPriToChar(entry->priority);
1449 size_t prefixLen = 0, suffixLen = 0;
1450 size_t len;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001451
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001452 /*
1453 * Get the current date/time in pretty form
1454 *
1455 * It's often useful when examining a log with "less" to jump to
1456 * a specific point in the file by searching for the date/time stamp.
1457 * For this reason it's very annoying to have regexp meta characters
1458 * in the time stamp. Don't use forward slashes, parenthesis,
1459 * brackets, asterisks, or other special chars here.
1460 *
1461 * The caller may have affected the timezone environment, this is
1462 * expected to be sensitive to that.
1463 */
1464 now = entry->tv_sec;
1465 nsec = entry->tv_nsec;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001466#if __ANDROID__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001467 if (p_format->monotonic_output) {
1468 /* prevent convertMonotonic from being called if logd is monotonic */
1469 if (android_log_clockid() != CLOCK_MONOTONIC) {
1470 struct timespec time;
1471 convertMonotonic(&time, entry);
1472 now = time.tv_sec;
1473 nsec = time.tv_nsec;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001474 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001475 }
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001476#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001477 if (now < 0) {
1478 nsec = NS_PER_SEC - nsec;
1479 }
1480 if (p_format->epoch_output || p_format->monotonic_output) {
1481 ptm = NULL;
Tom Cherry71ba1642019-01-10 10:37:36 -08001482 snprintf(timeBuf, sizeof(timeBuf), p_format->monotonic_output ? "%6lld" : "%19lld",
1483 (long long)now);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001484 } else {
Yabin Cui8a985352014-11-13 10:02:08 -08001485#if !defined(_WIN32)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001486 ptm = localtime_r(&now, &tmBuf);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001487#else
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001488 ptm = localtime(&now);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001489#endif
Tom Cherry71ba1642019-01-10 10:37:36 -08001490 strftime(timeBuf, sizeof(timeBuf), &"%Y-%m-%d %H:%M:%S"[p_format->year_output ? 0 : 3], ptm);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001491 }
1492 len = strlen(timeBuf);
1493 if (p_format->nsec_time_output) {
1494 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%09ld", nsec);
1495 } else if (p_format->usec_time_output) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001496 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%06ld", nsec / US_PER_NSEC);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001497 } else {
Tom Cherry71ba1642019-01-10 10:37:36 -08001498 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%03ld", nsec / MS_PER_NSEC);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001499 }
1500 if (p_format->zone_output && ptm) {
1501 strftime(timeBuf + len, sizeof(timeBuf) - len, " %z", ptm);
1502 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001503
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001504 /*
1505 * Construct a buffer containing the log header and log message.
1506 */
1507 if (p_format->colored_output) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001508 prefixLen =
1509 snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm", colorFromPri(entry->priority));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001510 prefixLen = MIN(prefixLen, sizeof(prefixBuf));
George Burgess IV487de272018-05-15 18:56:36 -07001511
1512 const char suffixContents[] = "\x1B[0m";
1513 strcpy(suffixBuf, suffixContents);
1514 suffixLen = strlen(suffixContents);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001515 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001516
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001517 char uid[16];
1518 uid[0] = '\0';
1519 if (p_format->uid_output) {
1520 if (entry->uid >= 0) {
1521/*
1522 * This code is Android specific, bionic guarantees that
1523 * calls to non-reentrant getpwuid() are thread safe.
1524 */
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001525#if !defined(__MINGW32__)
1526#if (FAKE_LOG_DEVICE == 0)
William Roberts8a5b9ca2016-04-08 12:13:17 -07001527#ifndef __BIONIC__
Tom Cherry71ba1642019-01-10 10:37:36 -08001528#warning "This code assumes that getpwuid is thread safe, only true with Bionic!"
William Roberts8a5b9ca2016-04-08 12:13:17 -07001529#endif
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001530#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001531 struct passwd* pwd = getpwuid(entry->uid);
1532 if (pwd && (strlen(pwd->pw_name) <= 5)) {
1533 snprintf(uid, sizeof(uid), "%5s:", pwd->pw_name);
1534 } else
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001535#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001536 {
1537 /* Not worth parsing package list, names all longer than 5 */
1538 snprintf(uid, sizeof(uid), "%5d:", entry->uid);
1539 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001540 } else {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001541 snprintf(uid, sizeof(uid), " ");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001542 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001543 }
1544
1545 switch (p_format->format) {
1546 case FORMAT_TAG:
Tom Cherry71ba1642019-01-10 10:37:36 -08001547 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c/%-8.*s: ", priChar,
1548 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001549 strcpy(suffixBuf + suffixLen, "\n");
1550 ++suffixLen;
1551 break;
1552 case FORMAT_PROCESS:
Tom Cherry71ba1642019-01-10 10:37:36 -08001553 len = snprintf(suffixBuf + suffixLen, sizeof(suffixBuf) - suffixLen, " (%.*s)\n",
1554 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001555 suffixLen += MIN(len, sizeof(suffixBuf) - suffixLen);
Tom Cherry71ba1642019-01-10 10:37:36 -08001556 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c(%s%5d) ", priChar,
1557 uid, entry->pid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001558 break;
1559 case FORMAT_THREAD:
Tom Cherry71ba1642019-01-10 10:37:36 -08001560 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c(%s%5d:%5d) ",
1561 priChar, uid, entry->pid, entry->tid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001562 strcpy(suffixBuf + suffixLen, "\n");
1563 ++suffixLen;
1564 break;
1565 case FORMAT_RAW:
1566 prefixBuf[prefixLen] = 0;
1567 len = 0;
1568 strcpy(suffixBuf + suffixLen, "\n");
1569 ++suffixLen;
1570 break;
1571 case FORMAT_TIME:
1572 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherry71ba1642019-01-10 10:37:36 -08001573 "%s %c/%-8.*s(%s%5d): ", timeBuf, priChar, (int)entry->tagLen, entry->tag, uid,
1574 entry->pid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001575 strcpy(suffixBuf + suffixLen, "\n");
1576 ++suffixLen;
1577 break;
1578 case FORMAT_THREADTIME:
1579 ret = strchr(uid, ':');
1580 if (ret) {
1581 *ret = ' ';
1582 }
1583 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherry71ba1642019-01-10 10:37:36 -08001584 "%s %s%5d %5d %c %-8.*s: ", timeBuf, uid, entry->pid, entry->tid, priChar,
1585 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001586 strcpy(suffixBuf + suffixLen, "\n");
1587 ++suffixLen;
1588 break;
1589 case FORMAT_LONG:
1590 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherry71ba1642019-01-10 10:37:36 -08001591 "[ %s %s%5d:%5d %c/%-8.*s ]\n", timeBuf, uid, entry->pid, entry->tid, priChar,
1592 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001593 strcpy(suffixBuf + suffixLen, "\n\n");
1594 suffixLen += 2;
1595 prefixSuffixIsHeaderFooter = 1;
1596 break;
1597 case FORMAT_BRIEF:
1598 default:
Tom Cherry71ba1642019-01-10 10:37:36 -08001599 len =
1600 snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
1601 "%c/%-8.*s(%s%5d): ", priChar, (int)entry->tagLen, entry->tag, uid, entry->pid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001602 strcpy(suffixBuf + suffixLen, "\n");
1603 ++suffixLen;
1604 break;
1605 }
1606
1607 /* snprintf has a weird return value. It returns what would have been
1608 * written given a large enough buffer. In the case that the prefix is
1609 * longer then our buffer(128), it messes up the calculations below
1610 * possibly causing heap corruption. To avoid this we double check and
1611 * set the length at the maximum (size minus null byte)
1612 */
1613 prefixLen += len;
1614 if (prefixLen >= sizeof(prefixBuf)) {
1615 prefixLen = sizeof(prefixBuf) - 1;
1616 prefixBuf[sizeof(prefixBuf) - 1] = '\0';
1617 }
1618 if (suffixLen >= sizeof(suffixBuf)) {
1619 suffixLen = sizeof(suffixBuf) - 1;
1620 suffixBuf[sizeof(suffixBuf) - 2] = '\n';
1621 suffixBuf[sizeof(suffixBuf) - 1] = '\0';
1622 }
1623
1624 /* the following code is tragically unreadable */
1625
1626 size_t numLines;
1627 char* p;
1628 size_t bufferSize;
1629 const char* pm;
1630
1631 if (prefixSuffixIsHeaderFooter) {
1632 /* we're just wrapping message with a header/footer */
1633 numLines = 1;
1634 } else {
1635 pm = entry->message;
1636 numLines = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001637
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001638 /*
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001639 * The line-end finding here must match the line-end finding
1640 * in for ( ... numLines...) loop below
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001641 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001642 while (pm < (entry->message + entry->messageLen)) {
1643 if (*pm++ == '\n') numLines++;
1644 }
1645 /* plus one line for anything not newline-terminated at the end */
1646 if (pm > entry->message && *(pm - 1) != '\n') numLines++;
1647 }
1648
1649 /*
1650 * this is an upper bound--newlines in message may be counted
1651 * extraneously
1652 */
1653 bufferSize = (numLines * (prefixLen + suffixLen)) + 1;
1654 if (p_format->printable_output) {
1655 /* Calculate extra length to convert non-printable to printable */
1656 bufferSize += convertPrintable(NULL, entry->message, entry->messageLen);
1657 } else {
1658 bufferSize += entry->messageLen;
1659 }
1660
1661 if (defaultBufferSize >= bufferSize) {
1662 ret = defaultBuffer;
1663 } else {
1664 ret = (char*)malloc(bufferSize);
1665
1666 if (ret == NULL) {
1667 return ret;
1668 }
1669 }
1670
1671 ret[0] = '\0'; /* to start strcat off */
1672
1673 p = ret;
1674 pm = entry->message;
1675
1676 if (prefixSuffixIsHeaderFooter) {
1677 strcat(p, prefixBuf);
1678 p += prefixLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001679 if (p_format->printable_output) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001680 p += convertPrintable(p, entry->message, entry->messageLen);
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001681 } else {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001682 strncat(p, entry->message, entry->messageLen);
1683 p += entry->messageLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001684 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001685 strcat(p, suffixBuf);
1686 p += suffixLen;
1687 } else {
1688 do {
1689 const char* lineStart;
1690 size_t lineLen;
1691 lineStart = pm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001692
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001693 /* Find the next end-of-line in message */
1694 while (pm < (entry->message + entry->messageLen) && *pm != '\n') pm++;
1695 lineLen = pm - lineStart;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001696
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001697 strcat(p, prefixBuf);
1698 p += prefixLen;
1699 if (p_format->printable_output) {
1700 p += convertPrintable(p, lineStart, lineLen);
1701 } else {
1702 strncat(p, lineStart, lineLen);
1703 p += lineLen;
1704 }
1705 strcat(p, suffixBuf);
1706 p += suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001707
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001708 if (*pm == '\n') pm++;
1709 } while (pm < (entry->message + entry->messageLen));
1710 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001711
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001712 if (p_outLength != NULL) {
1713 *p_outLength = p - ret;
1714 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001715
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001716 return ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001717}
1718
1719/**
1720 * Either print or do not print log line, based on filter
1721 *
1722 * Returns count bytes written
1723 */
1724
Tom Cherry2d9779e2019-02-08 11:46:19 -08001725int android_log_printLogLine(AndroidLogFormat* p_format, int fd, const AndroidLogEntry* entry) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001726 int ret;
1727 char defaultBuffer[512];
1728 char* outBuffer = NULL;
1729 size_t totalLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001730
Tom Cherry71ba1642019-01-10 10:37:36 -08001731 outBuffer =
1732 android_log_formatLogLine(p_format, defaultBuffer, sizeof(defaultBuffer), entry, &totalLen);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001733
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001734 if (!outBuffer) return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001735
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001736 do {
1737 ret = write(fd, outBuffer, totalLen);
1738 } while (ret < 0 && errno == EINTR);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001739
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001740 if (ret < 0) {
1741 fprintf(stderr, "+++ LOG: write failed (errno=%d)\n", errno);
1742 ret = 0;
1743 goto done;
1744 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001745
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001746 if (((size_t)ret) < totalLen) {
1747 fprintf(stderr, "+++ LOG: write partial (%d of %d)\n", ret, (int)totalLen);
1748 goto done;
1749 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001750
1751done:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001752 if (outBuffer != defaultBuffer) {
1753 free(outBuffer);
1754 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001755
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001756 return ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001757}