blob: dd2c797c38e659083519229e3501d6287b4bf8e7 [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>
Mark Salyzynaeaaf812016-09-30 13:30:33 -070038#include <log/log.h>
Colin Cross9227bd32013-07-23 16:59:20 -070039#include <log/logprint.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070040
Mark Salyzyn018a96d2016-03-01 13:45:42 -080041#include "log_portability.h"
Mark Salyzynbe1d3c22016-03-10 08:25:33 -080042
Mark Salyzyn4cbed022015-08-31 15:53:41 -070043#define MS_PER_NSEC 1000000
44#define US_PER_NSEC 1000
45
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080046#ifndef MIN
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080047#define MIN(a, b) (((a) < (b)) ? (a) : (b))
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080048#endif
49
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070050typedef struct FilterInfo_t {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080051 char* mTag;
52 android_LogPriority mPri;
53 struct FilterInfo_t* p_next;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054} FilterInfo;
55
56struct AndroidLogFormat_t {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080057 android_LogPriority global_pri;
58 FilterInfo* filters;
59 AndroidLogPrintFormat format;
60 bool colored_output;
61 bool usec_time_output;
62 bool nsec_time_output;
63 bool printable_output;
64 bool year_output;
65 bool zone_output;
66 bool epoch_output;
67 bool monotonic_output;
68 bool uid_output;
69 bool descriptive_output;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070070};
71
Pierre Zurekead88fc2010-10-17 22:39:37 +020072/*
Mark Salyzyn4fd05072016-10-18 11:30:11 -070073 * API issues prevent us from exposing "descriptive" in AndroidLogFormat_t
74 * during android_log_processBinaryLogBuffer(), so we break layering.
75 */
76static bool descriptive_output = false;
77
78/*
Pierre Zurekead88fc2010-10-17 22:39:37 +020079 * gnome-terminal color tags
80 * See http://misc.flogisoft.com/bash/tip_colors_and_formatting
81 * for ideas on how to set the forground color of the text for xterm.
82 * The color manipulation character stream is defined as:
83 * ESC [ 3 8 ; 5 ; <color#> m
84 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080085#define ANDROID_COLOR_BLUE 75
Pierre Zurekead88fc2010-10-17 22:39:37 +020086#define ANDROID_COLOR_DEFAULT 231
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080087#define ANDROID_COLOR_GREEN 40
88#define ANDROID_COLOR_ORANGE 166
89#define ANDROID_COLOR_RED 196
90#define ANDROID_COLOR_YELLOW 226
Pierre Zurekead88fc2010-10-17 22:39:37 +020091
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080092static FilterInfo* filterinfo_new(const char* tag, android_LogPriority pri) {
93 FilterInfo* p_ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070094
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080095 p_ret = (FilterInfo*)calloc(1, sizeof(FilterInfo));
96 p_ret->mTag = strdup(tag);
97 p_ret->mPri = pri;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070098
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080099 return p_ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700100}
101
Mark Salyzyna04464a2014-04-30 08:50:53 -0700102/* balance to above, filterinfo_free left unimplemented */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700103
104/*
105 * Note: also accepts 0-9 priorities
106 * returns ANDROID_LOG_UNKNOWN if the character is unrecognized
107 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800108static android_LogPriority filterCharToPri(char c) {
109 android_LogPriority pri;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700110
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800111 c = tolower(c);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700112
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800113 if (c >= '0' && c <= '9') {
114 if (c >= ('0' + ANDROID_LOG_SILENT)) {
115 pri = ANDROID_LOG_VERBOSE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700116 } else {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800117 pri = (android_LogPriority)(c - '0');
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700118 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800119 } else if (c == 'v') {
120 pri = ANDROID_LOG_VERBOSE;
121 } else if (c == 'd') {
122 pri = ANDROID_LOG_DEBUG;
123 } else if (c == 'i') {
124 pri = ANDROID_LOG_INFO;
125 } else if (c == 'w') {
126 pri = ANDROID_LOG_WARN;
127 } else if (c == 'e') {
128 pri = ANDROID_LOG_ERROR;
129 } else if (c == 'f') {
130 pri = ANDROID_LOG_FATAL;
131 } else if (c == 's') {
132 pri = ANDROID_LOG_SILENT;
133 } else if (c == '*') {
134 pri = ANDROID_LOG_DEFAULT;
135 } else {
136 pri = ANDROID_LOG_UNKNOWN;
137 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700138
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800139 return pri;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700140}
141
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800142static char filterPriToChar(android_LogPriority pri) {
143 switch (pri) {
144 /* clang-format off */
145 case ANDROID_LOG_VERBOSE: return 'V';
146 case ANDROID_LOG_DEBUG: return 'D';
147 case ANDROID_LOG_INFO: return 'I';
148 case ANDROID_LOG_WARN: return 'W';
149 case ANDROID_LOG_ERROR: return 'E';
150 case ANDROID_LOG_FATAL: return 'F';
151 case ANDROID_LOG_SILENT: return 'S';
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700152
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800153 case ANDROID_LOG_DEFAULT:
154 case ANDROID_LOG_UNKNOWN:
155 default: return '?';
Tom Cherry71ba1642019-01-10 10:37:36 -0800156 /* clang-format on */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800157 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700158}
159
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800160static int colorFromPri(android_LogPriority pri) {
161 switch (pri) {
162 /* clang-format off */
163 case ANDROID_LOG_VERBOSE: return ANDROID_COLOR_DEFAULT;
164 case ANDROID_LOG_DEBUG: return ANDROID_COLOR_BLUE;
165 case ANDROID_LOG_INFO: return ANDROID_COLOR_GREEN;
166 case ANDROID_LOG_WARN: return ANDROID_COLOR_ORANGE;
167 case ANDROID_LOG_ERROR: return ANDROID_COLOR_RED;
168 case ANDROID_LOG_FATAL: return ANDROID_COLOR_RED;
169 case ANDROID_LOG_SILENT: return ANDROID_COLOR_DEFAULT;
Pierre Zurekead88fc2010-10-17 22:39:37 +0200170
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800171 case ANDROID_LOG_DEFAULT:
172 case ANDROID_LOG_UNKNOWN:
173 default: return ANDROID_COLOR_DEFAULT;
Tom Cherry71ba1642019-01-10 10:37:36 -0800174 /* clang-format on */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800175 }
Pierre Zurekead88fc2010-10-17 22:39:37 +0200176}
177
Tom Cherry71ba1642019-01-10 10:37:36 -0800178static android_LogPriority filterPriForTag(AndroidLogFormat* p_format, const char* tag) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800179 FilterInfo* p_curFilter;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700180
Tom Cherry71ba1642019-01-10 10:37:36 -0800181 for (p_curFilter = p_format->filters; p_curFilter != NULL; p_curFilter = p_curFilter->p_next) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800182 if (0 == strcmp(tag, p_curFilter->mTag)) {
183 if (p_curFilter->mPri == ANDROID_LOG_DEFAULT) {
184 return p_format->global_pri;
185 } else {
186 return p_curFilter->mPri;
187 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700188 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800189 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700190
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800191 return p_format->global_pri;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700192}
193
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700194/**
195 * returns 1 if this log line should be printed based on its priority
196 * and tag, and 0 if it should not
197 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800198int android_log_shouldPrintLine(AndroidLogFormat* p_format, const char* tag,
199 android_LogPriority pri) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800200 return pri >= filterPriForTag(p_format, tag);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700201}
202
Tom Cherry2d9779e2019-02-08 11:46:19 -0800203AndroidLogFormat* android_log_format_new() {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800204 AndroidLogFormat* p_ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700205
Tom Cherry71ba1642019-01-10 10:37:36 -0800206 p_ret = static_cast<AndroidLogFormat*>(calloc(1, sizeof(AndroidLogFormat)));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700207
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800208 p_ret->global_pri = ANDROID_LOG_VERBOSE;
209 p_ret->format = FORMAT_BRIEF;
210 p_ret->colored_output = false;
211 p_ret->usec_time_output = false;
212 p_ret->nsec_time_output = false;
213 p_ret->printable_output = false;
214 p_ret->year_output = false;
215 p_ret->zone_output = false;
216 p_ret->epoch_output = false;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800217#ifdef __ANDROID__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800218 p_ret->monotonic_output = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800219#else
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800220 p_ret->monotonic_output = false;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800221#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800222 p_ret->uid_output = false;
223 p_ret->descriptive_output = false;
224 descriptive_output = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700225
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800226 return p_ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700227}
228
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700229static list_declare(convertHead);
230
Tom Cherry2d9779e2019-02-08 11:46:19 -0800231void android_log_format_free(AndroidLogFormat* p_format) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800232 FilterInfo *p_info, *p_info_old;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700233
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800234 p_info = p_format->filters;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700235
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800236 while (p_info != NULL) {
237 p_info_old = p_info;
238 p_info = p_info->p_next;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700239
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800240 free(p_info_old);
241 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700242
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800243 free(p_format);
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700244
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800245 /* Free conversion resource, can always be reconstructed */
246 while (!list_empty(&convertHead)) {
247 struct listnode* node = list_head(&convertHead);
248 list_remove(node);
Ting-Yuan Huang249bd052017-08-15 17:01:33 -0700249 LOG_ALWAYS_FATAL_IF(node == list_head(&convertHead), "corrupted list");
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800250 free(node);
251 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700252}
253
Tom Cherry2d9779e2019-02-08 11:46:19 -0800254int android_log_setPrintFormat(AndroidLogFormat* p_format, AndroidLogPrintFormat format) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800255 switch (format) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700256 case FORMAT_MODIFIER_COLOR:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800257 p_format->colored_output = true;
258 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700259 case FORMAT_MODIFIER_TIME_USEC:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800260 p_format->usec_time_output = true;
261 return 0;
Mark Salyzyn9b4d7e12017-01-30 09:16:09 -0800262 case FORMAT_MODIFIER_TIME_NSEC:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800263 p_format->nsec_time_output = true;
264 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700265 case FORMAT_MODIFIER_PRINTABLE:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800266 p_format->printable_output = true;
267 return 0;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700268 case FORMAT_MODIFIER_YEAR:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800269 p_format->year_output = true;
270 return 0;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700271 case FORMAT_MODIFIER_ZONE:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800272 p_format->zone_output = !p_format->zone_output;
273 return 0;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700274 case FORMAT_MODIFIER_EPOCH:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800275 p_format->epoch_output = true;
276 return 0;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700277 case FORMAT_MODIFIER_MONOTONIC:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800278 p_format->monotonic_output = true;
279 return 0;
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800280 case FORMAT_MODIFIER_UID:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800281 p_format->uid_output = true;
282 return 0;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700283 case FORMAT_MODIFIER_DESCRIPT:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800284 p_format->descriptive_output = true;
285 descriptive_output = true;
286 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700287 default:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800288 break;
289 }
290 p_format->format = format;
291 return 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700292}
293
Tom Cherry7d045f62019-09-30 12:58:55 -0700294#ifndef __MINGW32__
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700295static const char tz[] = "TZ";
296static const char utc[] = "UTC";
Tom Cherry7d045f62019-09-30 12:58:55 -0700297#endif
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700298
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700299/**
300 * Returns FORMAT_OFF on invalid string
301 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800302AndroidLogPrintFormat android_log_formatFromString(const char* formatString) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800303 static AndroidLogPrintFormat format;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700304
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800305 /* clang-format off */
306 if (!strcmp(formatString, "brief")) format = FORMAT_BRIEF;
307 else if (!strcmp(formatString, "process")) format = FORMAT_PROCESS;
308 else if (!strcmp(formatString, "tag")) format = FORMAT_TAG;
309 else if (!strcmp(formatString, "thread")) format = FORMAT_THREAD;
310 else if (!strcmp(formatString, "raw")) format = FORMAT_RAW;
311 else if (!strcmp(formatString, "time")) format = FORMAT_TIME;
312 else if (!strcmp(formatString, "threadtime")) format = FORMAT_THREADTIME;
313 else if (!strcmp(formatString, "long")) format = FORMAT_LONG;
314 else if (!strcmp(formatString, "color")) format = FORMAT_MODIFIER_COLOR;
315 else if (!strcmp(formatString, "colour")) format = FORMAT_MODIFIER_COLOR;
316 else if (!strcmp(formatString, "usec")) format = FORMAT_MODIFIER_TIME_USEC;
317 else if (!strcmp(formatString, "nsec")) format = FORMAT_MODIFIER_TIME_NSEC;
318 else if (!strcmp(formatString, "printable")) format = FORMAT_MODIFIER_PRINTABLE;
319 else if (!strcmp(formatString, "year")) format = FORMAT_MODIFIER_YEAR;
320 else if (!strcmp(formatString, "zone")) format = FORMAT_MODIFIER_ZONE;
321 else if (!strcmp(formatString, "epoch")) format = FORMAT_MODIFIER_EPOCH;
322 else if (!strcmp(formatString, "monotonic")) format = FORMAT_MODIFIER_MONOTONIC;
323 else if (!strcmp(formatString, "uid")) format = FORMAT_MODIFIER_UID;
324 else if (!strcmp(formatString, "descriptive")) format = FORMAT_MODIFIER_DESCRIPT;
Tom Cherry71ba1642019-01-10 10:37:36 -0800325 /* clang-format on */
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800326
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800327#ifndef __MINGW32__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800328 else {
329 extern char* tzname[2];
330 static const char gmt[] = "GMT";
331 char* cp = getenv(tz);
332 if (cp) {
333 cp = strdup(cp);
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700334 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800335 setenv(tz, formatString, 1);
336 /*
337 * Run tzset here to determine if the timezone is legitimate. If the
338 * zone is GMT, check if that is what was asked for, if not then
339 * did not match any on the system; report an error to caller.
340 */
341 tzset();
342 if (!tzname[0] ||
Tom Cherry71ba1642019-01-10 10:37:36 -0800343 ((!strcmp(tzname[0], utc) || !strcmp(tzname[0], gmt)) /* error? */
344 && strcasecmp(formatString, utc) && strcasecmp(formatString, gmt))) { /* ok */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800345 if (cp) {
346 setenv(tz, cp, 1);
347 } else {
348 unsetenv(tz);
349 }
350 tzset();
351 format = FORMAT_OFF;
352 } else {
353 format = FORMAT_MODIFIER_ZONE;
354 }
355 free(cp);
356 }
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800357#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700358
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800359 return format;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700360}
361
362/**
363 * filterExpression: a single filter expression
364 * eg "AT:d"
365 *
366 * returns 0 on success and -1 on invalid expression
367 *
368 * Assumes single threaded execution
369 */
370
Tom Cherry2d9779e2019-02-08 11:46:19 -0800371int android_log_addFilterRule(AndroidLogFormat* p_format, const char* filterExpression) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800372 size_t tagNameLength;
373 android_LogPriority pri = ANDROID_LOG_DEFAULT;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700374
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800375 tagNameLength = strcspn(filterExpression, ":");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700376
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800377 if (tagNameLength == 0) {
378 goto error;
379 }
380
381 if (filterExpression[tagNameLength] == ':') {
382 pri = filterCharToPri(filterExpression[tagNameLength + 1]);
383
384 if (pri == ANDROID_LOG_UNKNOWN) {
385 goto error;
386 }
387 }
388
389 if (0 == strncmp("*", filterExpression, tagNameLength)) {
390 /*
391 * This filter expression refers to the global filter
392 * The default level for this is DEBUG if the priority
393 * is unspecified
394 */
395 if (pri == ANDROID_LOG_DEFAULT) {
396 pri = ANDROID_LOG_DEBUG;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700397 }
398
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800399 p_format->global_pri = pri;
400 } else {
401 /*
402 * for filter expressions that don't refer to the global
403 * filter, the default is verbose if the priority is unspecified
404 */
405 if (pri == ANDROID_LOG_DEFAULT) {
406 pri = ANDROID_LOG_VERBOSE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700407 }
408
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800409 char* tagName;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700410
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700411/*
412 * Presently HAVE_STRNDUP is never defined, so the second case is always taken
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800413 * Darwin doesn't have strndup, everything else does
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700414 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700415#ifdef HAVE_STRNDUP
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800416 tagName = strndup(filterExpression, tagNameLength);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700417#else
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800418 /* a few extra bytes copied... */
419 tagName = strdup(filterExpression);
420 tagName[tagNameLength] = '\0';
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700421#endif /*HAVE_STRNDUP*/
422
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800423 FilterInfo* p_fi = filterinfo_new(tagName, pri);
424 free(tagName);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700425
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800426 p_fi->p_next = p_format->filters;
427 p_format->filters = p_fi;
428 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700429
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800430 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700431error:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800432 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700433}
434
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800435#ifndef HAVE_STRSEP
436/* KISS replacement helper for below */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800437static char* strsep(char** stringp, const char* delim) {
438 char* token;
439 char* ret = *stringp;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800440
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800441 if (!ret || !*ret) {
442 return NULL;
443 }
444 token = strpbrk(ret, delim);
445 if (token) {
446 *token = '\0';
447 ++token;
448 } else {
449 token = ret + strlen(ret);
450 }
451 *stringp = token;
452 return ret;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800453}
454#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700455
456/**
457 * filterString: a comma/whitespace-separated set of filter expressions
458 *
459 * eg "AT:d *:i"
460 *
461 * returns 0 on success and -1 on invalid expression
462 *
463 * Assumes single threaded execution
464 *
465 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800466int android_log_addFilterString(AndroidLogFormat* p_format, const char* filterString) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800467 char* filterStringCopy = strdup(filterString);
468 char* p_cur = filterStringCopy;
469 char* p_ret;
470 int err;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700471
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800472 /* Yes, I'm using strsep */
473 while (NULL != (p_ret = strsep(&p_cur, " \t,"))) {
474 /* ignore whitespace-only entries */
475 if (p_ret[0] != '\0') {
476 err = android_log_addFilterRule(p_format, p_ret);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700477
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800478 if (err < 0) {
479 goto error;
480 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700481 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800482 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700483
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800484 free(filterStringCopy);
485 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700486error:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800487 free(filterStringCopy);
488 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700489}
490
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700491/**
492 * Splits a wire-format buffer into an AndroidLogEntry
493 * entry allocated by caller. Pointers will point directly into buf
494 *
495 * Returns 0 on success and -1 on invalid wire format (entry will be
496 * in unspecified state)
497 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800498int android_log_processLogBuffer(struct logger_entry* buf, AndroidLogEntry* entry) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800499 entry->message = NULL;
500 entry->messageLen = 0;
Mark Salyzynb1d150b2017-03-02 07:47:21 -0800501
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800502 entry->tv_sec = buf->sec;
503 entry->tv_nsec = buf->nsec;
504 entry->uid = -1;
505 entry->pid = buf->pid;
506 entry->tid = buf->tid;
Kenny Root4bf3c022011-09-30 17:10:14 -0700507
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800508 /*
509 * format: <priority:1><tag:N>\0<message:N>\0
510 *
511 * tag str
512 * starts at buf->msg+1
513 * msg
514 * starts at buf->msg+1+len(tag)+1
515 *
516 * The message may have been truncated by the kernel log driver.
517 * When that happens, we must null-terminate the message ourselves.
518 */
519 if (buf->len < 3) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700520 /*
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800521 * An well-formed entry must consist of at least a priority
522 * and two null characters
Kenny Root4bf3c022011-09-30 17:10:14 -0700523 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800524 fprintf(stderr, "+++ LOG: entry too small\n");
525 return -1;
526 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700527
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800528 int msgStart = -1;
529 int msgEnd = -1;
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700530
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800531 int i;
532 char* msg = buf->msg;
533 struct logger_entry_v2* buf2 = (struct logger_entry_v2*)buf;
534 if (buf2->hdr_size) {
535 if ((buf2->hdr_size < sizeof(((struct log_msg*)NULL)->entry_v1)) ||
536 (buf2->hdr_size > sizeof(((struct log_msg*)NULL)->entry))) {
537 fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
538 return -1;
Mark Salyzyn40b21552013-12-18 12:59:01 -0800539 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800540 msg = ((char*)buf2) + buf2->hdr_size;
541 if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) {
542 entry->uid = ((struct logger_entry_v4*)buf)->uid;
543 }
544 }
545 for (i = 1; i < buf->len; i++) {
546 if (msg[i] == '\0') {
547 if (msgStart == -1) {
548 msgStart = i + 1;
549 } else {
550 msgEnd = i;
551 break;
552 }
553 }
554 }
555
556 if (msgStart == -1) {
557 /* +++ LOG: malformed log message, DYB */
Nick Kraleviche1ede152011-10-18 15:23:33 -0700558 for (i = 1; i < buf->len; i++) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800559 /* odd characters in tag? */
560 if ((msg[i] <= ' ') || (msg[i] == ':') || (msg[i] >= 0x7f)) {
561 msg[i] = '\0';
562 msgStart = i + 1;
563 break;
564 }
Nick Kraleviche1ede152011-10-18 15:23:33 -0700565 }
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700566 if (msgStart == -1) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800567 msgStart = buf->len - 1; /* All tag, no message, print truncates */
Nick Kralevich63f4a842011-10-17 10:45:03 -0700568 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800569 }
570 if (msgEnd == -1) {
571 /* incoming message not null-terminated; force it */
572 msgEnd = buf->len - 1; /* may result in msgEnd < msgStart */
573 msg[msgEnd] = '\0';
574 }
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700575
Tom Cherry71ba1642019-01-10 10:37:36 -0800576 entry->priority = static_cast<android_LogPriority>(msg[0]);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800577 entry->tag = msg + 1;
578 entry->tagLen = msgStart - 1;
579 entry->message = msg + msgStart;
580 entry->messageLen = (msgEnd < msgStart) ? 0 : (msgEnd - msgStart);
Nick Kralevich63f4a842011-10-17 10:45:03 -0700581
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800582 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700583}
584
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700585static bool findChar(const char** cp, size_t* len, int c) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800586 while ((*len) && isspace(*(*cp))) {
587 ++(*cp);
588 --(*len);
589 }
590 if (c == INT_MAX) return *len;
591 if ((*len) && (*(*cp) == c)) {
592 ++(*cp);
593 --(*len);
594 return true;
595 }
596 return false;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700597}
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000598
599/*
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700600 * Recursively convert binary log data to printable form.
601 *
602 * This needs to be recursive because you can have lists of lists.
603 *
604 * If we run out of room, we stop processing immediately. It's important
605 * for us to check for space on every output element to avoid producing
606 * garbled output.
607 *
608 * Returns 0 on success, 1 on buffer full, -1 on failure.
609 */
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700610enum objectType {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800611 TYPE_OBJECTS = '1',
612 TYPE_BYTES = '2',
613 TYPE_MILLISECONDS = '3',
614 TYPE_ALLOCATIONS = '4',
615 TYPE_ID = '5',
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800616 TYPE_PERCENT = '6',
617 TYPE_MONOTONIC = 's'
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700618};
619
Tom Cherry71ba1642019-01-10 10:37:36 -0800620static int android_log_printBinaryEvent(const unsigned char** pEventData, size_t* pEventDataLen,
621 char** pOutBuf, size_t* pOutBufLen, const char** fmtStr,
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800622 size_t* fmtLen) {
623 const unsigned char* eventData = *pEventData;
624 size_t eventDataLen = *pEventDataLen;
625 char* outBuf = *pOutBuf;
626 char* outBufSave = outBuf;
627 size_t outBufLen = *pOutBufLen;
628 size_t outBufLenSave = outBufLen;
629 unsigned char type;
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800630 size_t outCount = 0;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800631 int result = 0;
632 const char* cp;
633 size_t len;
634 int64_t lval;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700635
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800636 if (eventDataLen < 1) return -1;
Mark Salyzyn1a57ae32016-11-11 14:41:30 -0800637
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800638 type = *eventData++;
639 eventDataLen--;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700640
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800641 cp = NULL;
642 len = 0;
643 if (fmtStr && *fmtStr && fmtLen && *fmtLen && **fmtStr) {
644 cp = *fmtStr;
645 len = *fmtLen;
646 }
647 /*
648 * event.logtag format specification:
649 *
650 * Optionally, after the tag names can be put a description for the value(s)
651 * of the tag. Description are in the format
652 * (<name>|data type[|data unit])
653 * Multiple values are separated by commas.
654 *
655 * The data type is a number from the following values:
656 * 1: int
657 * 2: long
658 * 3: string
659 * 4: list
660 * 5: float
661 *
662 * The data unit is a number taken from the following list:
663 * 1: Number of objects
664 * 2: Number of bytes
665 * 3: Number of milliseconds
666 * 4: Number of allocations
667 * 5: Id
668 * 6: Percent
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800669 * s: Number of seconds (monotonic time)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800670 * Default value for data of type int/long is 2 (bytes).
671 */
672 if (!cp || !findChar(&cp, &len, '(')) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700673 len = 0;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800674 } else {
675 char* outBufLastSpace = NULL;
676
677 findChar(&cp, &len, INT_MAX);
678 while (len && *cp && (*cp != '|') && (*cp != ')')) {
679 if (outBufLen <= 0) {
680 /* halt output */
681 goto no_room;
682 }
683 outBufLastSpace = isspace(*cp) ? outBuf : NULL;
684 *outBuf = *cp;
685 ++outBuf;
686 ++cp;
687 --outBufLen;
688 --len;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700689 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800690 if (outBufLastSpace) {
691 outBufLen += outBuf - outBufLastSpace;
692 outBuf = outBufLastSpace;
693 }
694 if (outBufLen <= 0) {
695 /* halt output */
696 goto no_room;
697 }
698 if (outBufSave != outBuf) {
699 *outBuf = '=';
700 ++outBuf;
701 --outBufLen;
702 }
703
704 if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800705 static const unsigned char typeTable[] = {EVENT_TYPE_INT, EVENT_TYPE_LONG, EVENT_TYPE_STRING,
706 EVENT_TYPE_LIST, EVENT_TYPE_FLOAT};
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800707
Tom Cherry71ba1642019-01-10 10:37:36 -0800708 if ((*cp >= '1') && (*cp < (char)('1' + (sizeof(typeTable) / sizeof(typeTable[0])))) &&
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800709 (type != typeTable[(size_t)(*cp - '1')]))
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700710 len = 0;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700711
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800712 if (len) {
713 ++cp;
714 --len;
715 } else {
716 /* reset the format */
717 outBuf = outBufSave;
718 outBufLen = outBufLenSave;
719 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700720 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800721 }
722 outCount = 0;
723 lval = 0;
724 switch (type) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700725 case EVENT_TYPE_INT:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800726 /* 32-bit signed int */
727 {
728 int32_t ival;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700729
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800730 if (eventDataLen < 4) return -1;
Tom Cherry4c63e042019-09-30 14:33:46 -0700731 ival = *reinterpret_cast<const int32_t*>(eventData);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800732 eventData += 4;
733 eventDataLen -= 4;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700734
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800735 lval = ival;
736 }
737 goto pr_lval;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700738 case EVENT_TYPE_LONG:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800739 /* 64-bit signed long */
740 if (eventDataLen < 8) return -1;
Tom Cherry4c63e042019-09-30 14:33:46 -0700741 lval = *reinterpret_cast<const int64_t*>(eventData);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800742 eventData += 8;
743 eventDataLen -= 8;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700744 pr_lval:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800745 outCount = snprintf(outBuf, outBufLen, "%" PRId64, lval);
746 if (outCount < outBufLen) {
747 outBuf += outCount;
748 outBufLen -= outCount;
749 } else {
750 /* halt output */
751 goto no_room;
752 }
753 break;
Jeff Brown44193d92015-04-28 12:47:02 -0700754 case EVENT_TYPE_FLOAT:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800755 /* float */
756 {
757 uint32_t ival;
758 float fval;
Jeff Brown44193d92015-04-28 12:47:02 -0700759
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800760 if (eventDataLen < 4) return -1;
Tom Cherry4c63e042019-09-30 14:33:46 -0700761 ival = *reinterpret_cast<const uint32_t*>(eventData);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800762 fval = *(float*)&ival;
763 eventData += 4;
764 eventDataLen -= 4;
Jeff Brown44193d92015-04-28 12:47:02 -0700765
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800766 outCount = snprintf(outBuf, outBufLen, "%f", fval);
767 if (outCount < outBufLen) {
768 outBuf += outCount;
769 outBufLen -= outCount;
770 } else {
771 /* halt output */
772 goto no_room;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700773 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800774 }
775 break;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700776 case EVENT_TYPE_STRING:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800777 /* UTF-8 chars, not NULL-terminated */
778 {
779 unsigned int strLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700780
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800781 if (eventDataLen < 4) return -1;
Tom Cherry4c63e042019-09-30 14:33:46 -0700782 strLen = *reinterpret_cast<const uint32_t*>(eventData);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800783 eventData += 4;
784 eventDataLen -= 4;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700785
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800786 if (eventDataLen < strLen) {
787 result = -1; /* mark truncated */
788 strLen = eventDataLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700789 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700790
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800791 if (cp && (strLen == 0)) {
792 /* reset the format if no content */
793 outBuf = outBufSave;
794 outBufLen = outBufLenSave;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700795 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800796 if (strLen < outBufLen) {
797 memcpy(outBuf, eventData, strLen);
798 outBuf += strLen;
799 outBufLen -= strLen;
800 } else {
801 if (outBufLen > 0) {
802 /* copy what we can */
803 memcpy(outBuf, eventData, outBufLen);
804 outBuf += outBufLen;
805 outBufLen -= outBufLen;
806 }
807 if (!result) result = 1; /* if not truncated, return no room */
808 }
809 eventData += strLen;
810 eventDataLen -= strLen;
811 if (result != 0) goto bail;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700812 break;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800813 }
814 case EVENT_TYPE_LIST:
815 /* N items, all different types */
816 {
817 unsigned char count;
818 int i;
819
820 if (eventDataLen < 1) return -1;
821
822 count = *eventData++;
823 eventDataLen--;
824
825 if (outBufLen <= 0) goto no_room;
826
827 *outBuf++ = '[';
828 outBufLen--;
829
830 for (i = 0; i < count; i++) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800831 result = android_log_printBinaryEvent(&eventData, &eventDataLen, &outBuf, &outBufLen,
832 fmtStr, fmtLen);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800833 if (result != 0) goto bail;
834
835 if (i < (count - 1)) {
836 if (outBufLen <= 0) goto no_room;
837 *outBuf++ = ',';
838 outBufLen--;
839 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700840 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800841
842 if (outBufLen <= 0) goto no_room;
843
844 *outBuf++ = ']';
845 outBufLen--;
846 }
847 break;
848 default:
849 fprintf(stderr, "Unknown binary event type %d\n", type);
850 return -1;
851 }
852 if (cp && len) {
853 if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) {
854 switch (*cp) {
855 case TYPE_OBJECTS:
856 outCount = 0;
857 /* outCount = snprintf(outBuf, outBufLen, " objects"); */
858 break;
859 case TYPE_BYTES:
860 if ((lval != 0) && ((lval % 1024) == 0)) {
861 /* repaint with multiplier */
Tom Cherry71ba1642019-01-10 10:37:36 -0800862 static const char suffixTable[] = {'K', 'M', 'G', 'T'};
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800863 size_t idx = 0;
864 outBuf -= outCount;
865 outBufLen += outCount;
866 do {
867 lval /= 1024;
868 if ((lval % 1024) != 0) break;
Tom Cherry71ba1642019-01-10 10:37:36 -0800869 } while (++idx < ((sizeof(suffixTable) / sizeof(suffixTable[0])) - 1));
870 outCount = snprintf(outBuf, outBufLen, "%" PRId64 "%cB", lval, suffixTable[idx]);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800871 } else {
872 outCount = snprintf(outBuf, outBufLen, "B");
873 }
874 break;
875 case TYPE_MILLISECONDS:
Tom Cherry71ba1642019-01-10 10:37:36 -0800876 if (((lval <= -1000) || (1000 <= lval)) && (outBufLen || (outBuf[-1] == '0'))) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800877 /* repaint as (fractional) seconds, possibly saving space */
878 if (outBufLen) outBuf[0] = outBuf[-1];
879 outBuf[-1] = outBuf[-2];
880 outBuf[-2] = outBuf[-3];
881 outBuf[-3] = '.';
882 while ((outBufLen == 0) || (*outBuf == '0')) {
883 --outBuf;
884 ++outBufLen;
885 }
886 if (*outBuf != '.') {
887 ++outBuf;
888 --outBufLen;
889 }
890 outCount = snprintf(outBuf, outBufLen, "s");
891 } else {
892 outCount = snprintf(outBuf, outBufLen, "ms");
893 }
894 break;
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800895 case TYPE_MONOTONIC: {
896 static const uint64_t minute = 60;
897 static const uint64_t hour = 60 * minute;
898 static const uint64_t day = 24 * hour;
899
900 /* Repaint as unsigned seconds, minutes, hours ... */
901 outBuf -= outCount;
902 outBufLen += outCount;
903 uint64_t val = lval;
904 if (val >= day) {
905 outCount = snprintf(outBuf, outBufLen, "%" PRIu64 "d ", val / day);
906 if (outCount >= outBufLen) break;
907 outBuf += outCount;
908 outBufLen -= outCount;
909 val = (val % day) + day;
910 }
911 if (val >= minute) {
912 if (val >= hour) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800913 outCount = snprintf(outBuf, outBufLen, "%" PRIu64 ":", (val / hour) % (day / hour));
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800914 if (outCount >= outBufLen) break;
915 outBuf += outCount;
916 outBufLen -= outCount;
917 }
918 outCount =
Tom Cherry71ba1642019-01-10 10:37:36 -0800919 snprintf(outBuf, outBufLen, (val >= hour) ? "%02" PRIu64 ":" : "%" PRIu64 ":",
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800920 (val / minute) % (hour / minute));
921 if (outCount >= outBufLen) break;
922 outBuf += outCount;
923 outBufLen -= outCount;
924 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800925 outCount = snprintf(outBuf, outBufLen, (val >= minute) ? "%02" PRIu64 : "%" PRIu64 "s",
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800926 val % minute);
927 } break;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800928 case TYPE_ALLOCATIONS:
929 outCount = 0;
930 /* outCount = snprintf(outBuf, outBufLen, " allocations"); */
931 break;
932 case TYPE_ID:
933 outCount = 0;
934 break;
935 case TYPE_PERCENT:
936 outCount = snprintf(outBuf, outBufLen, "%%");
937 break;
938 default: /* ? */
939 outCount = 0;
940 break;
941 }
942 ++cp;
943 --len;
944 if (outCount < outBufLen) {
945 outBuf += outCount;
946 outBufLen -= outCount;
947 } else if (outCount) {
948 /* halt output */
949 goto no_room;
950 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700951 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800952 if (!findChar(&cp, &len, ')')) len = 0;
953 if (!findChar(&cp, &len, ',')) len = 0;
954 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700955
956bail:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800957 *pEventData = eventData;
958 *pEventDataLen = eventDataLen;
959 *pOutBuf = outBuf;
960 *pOutBufLen = outBufLen;
961 if (cp) {
962 *fmtStr = cp;
963 *fmtLen = len;
964 }
965 return result;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700966
967no_room:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800968 result = 1;
969 goto bail;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700970}
971
972/**
973 * Convert a binary log entry to ASCII form.
974 *
975 * For convenience we mimic the processLogBuffer API. There is no
976 * pre-defined output length for the binary data, since we're free to format
977 * it however we choose, which means we can't really use a fixed-size buffer
978 * here.
979 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800980int android_log_processBinaryLogBuffer(
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800981 struct logger_entry* buf, AndroidLogEntry* entry,
Tom Cherry71ba1642019-01-10 10:37:36 -0800982 [[maybe_unused]] const EventTagMap* map, /* only on !__ANDROID__ */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800983 char* messageBuf, int messageBufLen) {
984 size_t inCount;
985 uint32_t tagIndex;
986 const unsigned char* eventData;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700987
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800988 entry->message = NULL;
989 entry->messageLen = 0;
Mark Salyzynb1d150b2017-03-02 07:47:21 -0800990
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800991 entry->tv_sec = buf->sec;
992 entry->tv_nsec = buf->nsec;
993 entry->priority = ANDROID_LOG_INFO;
994 entry->uid = -1;
995 entry->pid = buf->pid;
996 entry->tid = buf->tid;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700997
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800998 /*
999 * Pull the tag out, fill in some additional details based on incoming
1000 * buffer version (v3 adds lid, v4 adds uid).
1001 */
1002 eventData = (const unsigned char*)buf->msg;
1003 struct logger_entry_v2* buf2 = (struct logger_entry_v2*)buf;
1004 if (buf2->hdr_size) {
1005 if ((buf2->hdr_size < sizeof(((struct log_msg*)NULL)->entry_v1)) ||
1006 (buf2->hdr_size > sizeof(((struct log_msg*)NULL)->entry))) {
1007 fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
1008 return -1;
1009 }
1010 eventData = ((unsigned char*)buf2) + buf2->hdr_size;
1011 if ((buf2->hdr_size >= sizeof(struct logger_entry_v3)) &&
1012 (((struct logger_entry_v3*)buf)->lid == LOG_ID_SECURITY)) {
1013 entry->priority = ANDROID_LOG_WARN;
1014 }
1015 if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) {
1016 entry->uid = ((struct logger_entry_v4*)buf)->uid;
1017 }
1018 }
1019 inCount = buf->len;
1020 if (inCount < 4) return -1;
Tom Cherry4c63e042019-09-30 14:33:46 -07001021 tagIndex = *reinterpret_cast<const uint32_t*>(eventData);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001022 eventData += 4;
1023 inCount -= 4;
1024
1025 entry->tagLen = 0;
1026 entry->tag = NULL;
1027#ifdef __ANDROID__
1028 if (map != NULL) {
1029 entry->tag = android_lookupEventTag_len(map, &entry->tagLen, tagIndex);
1030 }
1031#endif
1032
1033 /*
1034 * If we don't have a map, or didn't find the tag number in the map,
1035 * stuff a generated tag value into the start of the output buffer and
1036 * shift the buffer pointers down.
1037 */
1038 if (entry->tag == NULL) {
1039 size_t tagLen;
1040
1041 tagLen = snprintf(messageBuf, messageBufLen, "[%" PRIu32 "]", tagIndex);
1042 if (tagLen >= (size_t)messageBufLen) {
1043 tagLen = messageBufLen - 1;
1044 }
1045 entry->tag = messageBuf;
1046 entry->tagLen = tagLen;
1047 messageBuf += tagLen + 1;
1048 messageBufLen -= tagLen + 1;
1049 }
1050
1051 /*
1052 * Format the event log data into the buffer.
1053 */
1054 const char* fmtStr = NULL;
1055 size_t fmtLen = 0;
1056#ifdef __ANDROID__
1057 if (descriptive_output && map) {
1058 fmtStr = android_lookupEventFormat_len(map, &fmtLen, tagIndex);
1059 }
1060#endif
1061
1062 char* outBuf = messageBuf;
1063 size_t outRemaining = messageBufLen - 1; /* leave one for nul byte */
1064 int result = 0;
1065
1066 if ((inCount > 0) || fmtLen) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001067 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, &outRemaining, &fmtStr,
1068 &fmtLen);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001069 }
1070 if ((result == 1) && fmtStr) {
1071 /* We overflowed :-(, let's repaint the line w/o format dressings */
Mark Salyzynb1d150b2017-03-02 07:47:21 -08001072 eventData = (const unsigned char*)buf->msg;
Mark Salyzyn40b21552013-12-18 12:59:01 -08001073 if (buf2->hdr_size) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001074 eventData = ((unsigned char*)buf2) + buf2->hdr_size;
Mark Salyzyn40b21552013-12-18 12:59:01 -08001075 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001076 eventData += 4;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001077 outBuf = messageBuf;
1078 outRemaining = messageBufLen - 1;
Tom Cherry71ba1642019-01-10 10:37:36 -08001079 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, &outRemaining, NULL, NULL);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001080 }
1081 if (result < 0) {
1082 fprintf(stderr, "Binary log entry conversion failed\n");
1083 }
1084 if (result) {
1085 if (!outRemaining) {
1086 /* make space to leave an indicator */
1087 --outBuf;
1088 ++outRemaining;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001089 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001090 *outBuf++ = (result < 0) ? '!' : '^'; /* Error or Truncation? */
1091 outRemaining--;
1092 /* pretend we ate all the data to prevent log stutter */
1093 inCount = 0;
1094 if (result > 0) result = 0;
1095 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001096
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001097 /* eat the silly terminating '\n' */
1098 if (inCount == 1 && *eventData == '\n') {
1099 eventData++;
1100 inCount--;
1101 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001102
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001103 if (inCount != 0) {
1104 fprintf(stderr, "Warning: leftover binary log data (%zu bytes)\n", inCount);
1105 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001106
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001107 /*
1108 * Terminate the buffer. The NUL byte does not count as part of
1109 * entry->messageLen.
1110 */
1111 *outBuf = '\0';
1112 entry->messageLen = outBuf - messageBuf;
1113 assert(entry->messageLen == (messageBufLen - 1) - outRemaining);
Mark Salyzyn1a57ae32016-11-11 14:41:30 -08001114
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001115 entry->message = messageBuf;
Mark Salyzyn1a57ae32016-11-11 14:41:30 -08001116
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001117 return result;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001118}
1119
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001120/*
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001121 * Convert to printable from message to p buffer, return string length. If p is
1122 * NULL, do not copy, but still return the expected string length.
1123 */
Tom Cherry91589842019-04-25 13:10:30 -07001124size_t convertPrintable(char* p, const char* message, size_t messageLen) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001125 char* begin = p;
1126 bool print = p != NULL;
Tom Cherry91589842019-04-25 13:10:30 -07001127 mbstate_t mb_state = {};
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001128
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001129 while (messageLen) {
1130 char buf[6];
1131 ssize_t len = sizeof(buf) - 1;
1132 if ((size_t)len > messageLen) {
1133 len = messageLen;
1134 }
Tom Cherry91589842019-04-25 13:10:30 -07001135 len = mbrtowc(nullptr, message, len, &mb_state);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001136
1137 if (len < 0) {
Tom Cherry91589842019-04-25 13:10:30 -07001138 snprintf(buf, sizeof(buf), "\\x%02X", static_cast<unsigned char>(*message));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001139 len = 1;
1140 } else {
1141 buf[0] = '\0';
1142 if (len == 1) {
1143 if (*message == '\a') {
1144 strcpy(buf, "\\a");
1145 } else if (*message == '\b') {
1146 strcpy(buf, "\\b");
1147 } else if (*message == '\t') {
1148 strcpy(buf, "\t"); /* Do not escape tabs */
1149 } else if (*message == '\v') {
1150 strcpy(buf, "\\v");
1151 } else if (*message == '\f') {
1152 strcpy(buf, "\\f");
1153 } else if (*message == '\r') {
1154 strcpy(buf, "\\r");
1155 } else if (*message == '\\') {
1156 strcpy(buf, "\\\\");
1157 } else if ((*message < ' ') || (*message & 0x80)) {
Tom Cherry91589842019-04-25 13:10:30 -07001158 snprintf(buf, sizeof(buf), "\\x%02X", static_cast<unsigned char>(*message));
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001159 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001160 }
1161 if (!buf[0]) {
1162 strncpy(buf, message, len);
1163 buf[len] = '\0';
1164 }
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001165 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001166 if (print) {
1167 strcpy(p, buf);
1168 }
1169 p += strlen(buf);
1170 message += len;
1171 messageLen -= len;
1172 }
1173 return p - begin;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001174}
1175
Tom Cherry7d045f62019-09-30 12:58:55 -07001176#ifdef __ANDROID__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001177static char* readSeconds(char* e, struct timespec* t) {
1178 unsigned long multiplier;
1179 char* p;
1180 t->tv_sec = strtoul(e, &p, 10);
1181 if (*p != '.') {
1182 return NULL;
1183 }
1184 t->tv_nsec = 0;
1185 multiplier = NS_PER_SEC;
1186 while (isdigit(*++p) && (multiplier /= 10)) {
1187 t->tv_nsec += (*p - '0') * multiplier;
1188 }
1189 return p;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001190}
1191
Tom Cherry71ba1642019-01-10 10:37:36 -08001192static struct timespec* sumTimespec(struct timespec* left, struct timespec* right) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001193 left->tv_nsec += right->tv_nsec;
1194 left->tv_sec += right->tv_sec;
1195 if (left->tv_nsec >= (long)NS_PER_SEC) {
1196 left->tv_nsec -= NS_PER_SEC;
1197 left->tv_sec += 1;
1198 }
1199 return left;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001200}
1201
Tom Cherry71ba1642019-01-10 10:37:36 -08001202static struct timespec* subTimespec(struct timespec* result, struct timespec* left,
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001203 struct timespec* right) {
1204 result->tv_nsec = left->tv_nsec - right->tv_nsec;
1205 result->tv_sec = left->tv_sec - right->tv_sec;
1206 if (result->tv_nsec < 0) {
1207 result->tv_nsec += NS_PER_SEC;
1208 result->tv_sec -= 1;
1209 }
1210 return result;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001211}
1212
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001213static long long nsecTimespec(struct timespec* now) {
1214 return (long long)now->tv_sec * NS_PER_SEC + now->tv_nsec;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001215}
1216
Tom Cherry71ba1642019-01-10 10:37:36 -08001217static void convertMonotonic(struct timespec* result, const AndroidLogEntry* entry) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001218 struct listnode* node;
1219 struct conversionList {
1220 struct listnode node; /* first */
1221 struct timespec time;
1222 struct timespec convert;
1223 } * list, *next;
1224 struct timespec time, convert;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001225
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001226 /* If we do not have a conversion list, build one up */
1227 if (list_empty(&convertHead)) {
1228 bool suspended_pending = false;
Tom Cherry71ba1642019-01-10 10:37:36 -08001229 struct timespec suspended_monotonic = {0, 0};
1230 struct timespec suspended_diff = {0, 0};
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001231
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001232 /*
1233 * Read dmesg for _some_ synchronization markers and insert
1234 * Anything in the Android Logger before the dmesg logging span will
1235 * be highly suspect regarding the monotonic time calculations.
1236 */
1237 FILE* p = popen("/system/bin/dmesg", "re");
1238 if (p) {
1239 char* line = NULL;
1240 size_t len = 0;
1241 while (getline(&line, &len, p) > 0) {
1242 static const char suspend[] = "PM: suspend entry ";
1243 static const char resume[] = "PM: suspend exit ";
1244 static const char healthd[] = "healthd";
1245 static const char battery[] = ": battery ";
1246 static const char suspended[] = "Suspended for ";
1247 struct timespec monotonic;
1248 struct tm tm;
1249 char *cp, *e = line;
1250 bool add_entry = true;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001251
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001252 if (*e == '<') {
1253 while (*e && (*e != '>')) {
1254 ++e;
1255 }
1256 if (*e != '>') {
1257 continue;
1258 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001259 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001260 if (*e != '[') {
1261 continue;
1262 }
1263 while (*++e == ' ') {
1264 ;
1265 }
1266 e = readSeconds(e, &monotonic);
1267 if (!e || (*e != ']')) {
1268 continue;
1269 }
1270
1271 if ((e = strstr(e, suspend))) {
1272 e += sizeof(suspend) - 1;
1273 } else if ((e = strstr(line, resume))) {
1274 e += sizeof(resume) - 1;
1275 } else if (((e = strstr(line, healthd))) &&
1276 ((e = strstr(e + sizeof(healthd) - 1, battery)))) {
1277 /* NB: healthd is roughly 150us late, worth the price to
1278 * deal with ntp-induced or hardware clock drift. */
1279 e += sizeof(battery) - 1;
1280 } else if ((e = strstr(line, suspended))) {
1281 e += sizeof(suspended) - 1;
1282 e = readSeconds(e, &time);
1283 if (!e) {
1284 continue;
1285 }
1286 add_entry = false;
1287 suspended_pending = true;
1288 suspended_monotonic = monotonic;
1289 suspended_diff = time;
1290 } else {
1291 continue;
1292 }
1293 if (add_entry) {
1294 /* look for "????-??-?? ??:??:??.????????? UTC" */
1295 cp = strstr(e, " UTC");
1296 if (!cp || ((cp - e) < 29) || (cp[-10] != '.')) {
1297 continue;
1298 }
1299 e = cp - 29;
1300 cp = readSeconds(cp - 10, &time);
1301 if (!cp) {
1302 continue;
1303 }
1304 cp = strptime(e, "%Y-%m-%d %H:%M:%S.", &tm);
1305 if (!cp) {
1306 continue;
1307 }
1308 cp = getenv(tz);
1309 if (cp) {
1310 cp = strdup(cp);
1311 }
1312 setenv(tz, utc, 1);
1313 time.tv_sec = mktime(&tm);
1314 if (cp) {
1315 setenv(tz, cp, 1);
1316 free(cp);
1317 } else {
1318 unsetenv(tz);
1319 }
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 subTimespec(&list->convert, &time, &monotonic);
1324 list_add_tail(&convertHead, &list->node);
1325 }
1326 if (suspended_pending && !list_empty(&convertHead)) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001327 list = node_to_item(list_tail(&convertHead), struct conversionList, node);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001328 if (subTimespec(&time, subTimespec(&time, &list->time, &list->convert),
1329 &suspended_monotonic)
1330 ->tv_sec > 0) {
1331 /* resume, what is convert factor before? */
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001332 subTimespec(&convert, &list->convert, &suspended_diff);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001333 } else {
1334 /* suspend */
1335 convert = list->convert;
1336 }
1337 time = suspended_monotonic;
1338 sumTimespec(&time, &convert);
1339 /* breakpoint just before sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001340 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001341 list_init(&list->node);
1342 list->time = time;
1343 list->convert = convert;
1344 list_add_tail(&convertHead, &list->node);
1345 /* breakpoint just after sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001346 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001347 list_init(&list->node);
1348 list->time = time;
1349 sumTimespec(&list->time, &suspended_diff);
1350 list->convert = convert;
1351 sumTimespec(&list->convert, &suspended_diff);
1352 list_add_tail(&convertHead, &list->node);
1353 suspended_pending = false;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001354 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001355 }
1356 pclose(p);
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001357 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001358 /* last entry is our current time conversion */
Tom Cherry71ba1642019-01-10 10:37:36 -08001359 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001360 list_init(&list->node);
1361 clock_gettime(CLOCK_REALTIME, &list->time);
1362 clock_gettime(CLOCK_MONOTONIC, &convert);
1363 clock_gettime(CLOCK_MONOTONIC, &time);
1364 /* Correct for instant clock_gettime latency (syscall or ~30ns) */
1365 subTimespec(&time, &convert, subTimespec(&time, &time, &convert));
1366 /* Calculate conversion factor */
1367 subTimespec(&list->convert, &list->time, &time);
1368 list_add_tail(&convertHead, &list->node);
1369 if (suspended_pending) {
1370 /* manufacture a suspend @ point before */
1371 subTimespec(&convert, &list->convert, &suspended_diff);
1372 time = suspended_monotonic;
1373 sumTimespec(&time, &convert);
1374 /* breakpoint just after sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001375 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001376 list_init(&list->node);
1377 list->time = time;
1378 sumTimespec(&list->time, &suspended_diff);
1379 list->convert = convert;
1380 sumTimespec(&list->convert, &suspended_diff);
1381 list_add_head(&convertHead, &list->node);
1382 /* breakpoint just before sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001383 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001384 list_init(&list->node);
1385 list->time = time;
1386 list->convert = convert;
1387 list_add_head(&convertHead, &list->node);
1388 }
1389 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001390
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001391 /* Find the breakpoint in the conversion list */
1392 list = node_to_item(list_head(&convertHead), struct conversionList, node);
1393 next = NULL;
1394 list_for_each(node, &convertHead) {
1395 next = node_to_item(node, struct conversionList, node);
1396 if (entry->tv_sec < next->time.tv_sec) {
1397 break;
1398 } else if (entry->tv_sec == next->time.tv_sec) {
1399 if (entry->tv_nsec < next->time.tv_nsec) {
1400 break;
1401 }
1402 }
1403 list = next;
1404 }
1405
1406 /* blend time from one breakpoint to the next */
1407 convert = list->convert;
1408 if (next) {
1409 unsigned long long total, run;
1410
1411 total = nsecTimespec(subTimespec(&time, &next->time, &list->time));
1412 time.tv_sec = entry->tv_sec;
1413 time.tv_nsec = entry->tv_nsec;
1414 run = nsecTimespec(subTimespec(&time, &time, &list->time));
1415 if (run < total) {
1416 long long crun;
1417
1418 float f = nsecTimespec(subTimespec(&time, &next->convert, &convert));
1419 f *= run;
1420 f /= total;
1421 crun = f;
1422 convert.tv_sec += crun / (long long)NS_PER_SEC;
1423 if (crun < 0) {
1424 convert.tv_nsec -= (-crun) % NS_PER_SEC;
1425 if (convert.tv_nsec < 0) {
1426 convert.tv_nsec += NS_PER_SEC;
1427 convert.tv_sec -= 1;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001428 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001429 } else {
1430 convert.tv_nsec += crun % NS_PER_SEC;
1431 if (convert.tv_nsec >= (long)NS_PER_SEC) {
1432 convert.tv_nsec -= NS_PER_SEC;
1433 convert.tv_sec += 1;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001434 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001435 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001436 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001437 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001438
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001439 /* Apply the correction factor */
1440 result->tv_sec = entry->tv_sec;
1441 result->tv_nsec = entry->tv_nsec;
1442 subTimespec(result, result, &convert);
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001443}
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001444#endif
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001445
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001446/**
1447 * Formats a log message into a buffer
1448 *
1449 * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer
1450 * If return value != defaultBuffer, caller must call free()
1451 * Returns NULL on malloc error
1452 */
1453
Tom Cherry2d9779e2019-02-08 11:46:19 -08001454char* android_log_formatLogLine(AndroidLogFormat* p_format, char* defaultBuffer,
1455 size_t defaultBufferSize, const AndroidLogEntry* entry,
1456 size_t* p_outLength) {
Yabin Cui8a985352014-11-13 10:02:08 -08001457#if !defined(_WIN32)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001458 struct tm tmBuf;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001459#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001460 struct tm* ptm;
1461 /* good margin, 23+nul for msec, 26+nul for usec, 29+nul to nsec */
1462 char timeBuf[64];
1463 char prefixBuf[128], suffixBuf[128];
1464 char priChar;
1465 int prefixSuffixIsHeaderFooter = 0;
1466 char* ret;
1467 time_t now;
1468 unsigned long nsec;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001469
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001470 priChar = filterPriToChar(entry->priority);
1471 size_t prefixLen = 0, suffixLen = 0;
1472 size_t len;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001473
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001474 /*
1475 * Get the current date/time in pretty form
1476 *
1477 * It's often useful when examining a log with "less" to jump to
1478 * a specific point in the file by searching for the date/time stamp.
1479 * For this reason it's very annoying to have regexp meta characters
1480 * in the time stamp. Don't use forward slashes, parenthesis,
1481 * brackets, asterisks, or other special chars here.
1482 *
1483 * The caller may have affected the timezone environment, this is
1484 * expected to be sensitive to that.
1485 */
1486 now = entry->tv_sec;
1487 nsec = entry->tv_nsec;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001488#if __ANDROID__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001489 if (p_format->monotonic_output) {
1490 /* prevent convertMonotonic from being called if logd is monotonic */
1491 if (android_log_clockid() != CLOCK_MONOTONIC) {
1492 struct timespec time;
1493 convertMonotonic(&time, entry);
1494 now = time.tv_sec;
1495 nsec = time.tv_nsec;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001496 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001497 }
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001498#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001499 if (now < 0) {
1500 nsec = NS_PER_SEC - nsec;
1501 }
1502 if (p_format->epoch_output || p_format->monotonic_output) {
1503 ptm = NULL;
Tom Cherry71ba1642019-01-10 10:37:36 -08001504 snprintf(timeBuf, sizeof(timeBuf), p_format->monotonic_output ? "%6lld" : "%19lld",
1505 (long long)now);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001506 } else {
Yabin Cui8a985352014-11-13 10:02:08 -08001507#if !defined(_WIN32)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001508 ptm = localtime_r(&now, &tmBuf);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001509#else
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001510 ptm = localtime(&now);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001511#endif
Tom Cherry71ba1642019-01-10 10:37:36 -08001512 strftime(timeBuf, sizeof(timeBuf), &"%Y-%m-%d %H:%M:%S"[p_format->year_output ? 0 : 3], ptm);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001513 }
1514 len = strlen(timeBuf);
1515 if (p_format->nsec_time_output) {
1516 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%09ld", nsec);
1517 } else if (p_format->usec_time_output) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001518 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%06ld", nsec / US_PER_NSEC);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001519 } else {
Tom Cherry71ba1642019-01-10 10:37:36 -08001520 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%03ld", nsec / MS_PER_NSEC);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001521 }
1522 if (p_format->zone_output && ptm) {
1523 strftime(timeBuf + len, sizeof(timeBuf) - len, " %z", ptm);
1524 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001525
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001526 /*
1527 * Construct a buffer containing the log header and log message.
1528 */
1529 if (p_format->colored_output) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001530 prefixLen =
1531 snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm", colorFromPri(entry->priority));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001532 prefixLen = MIN(prefixLen, sizeof(prefixBuf));
George Burgess IV487de272018-05-15 18:56:36 -07001533
1534 const char suffixContents[] = "\x1B[0m";
1535 strcpy(suffixBuf, suffixContents);
1536 suffixLen = strlen(suffixContents);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001537 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001538
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001539 char uid[16];
1540 uid[0] = '\0';
1541 if (p_format->uid_output) {
1542 if (entry->uid >= 0) {
1543/*
1544 * This code is Android specific, bionic guarantees that
1545 * calls to non-reentrant getpwuid() are thread safe.
1546 */
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001547#if !defined(__MINGW32__)
1548#if (FAKE_LOG_DEVICE == 0)
William Roberts8a5b9ca2016-04-08 12:13:17 -07001549#ifndef __BIONIC__
Tom Cherry71ba1642019-01-10 10:37:36 -08001550#warning "This code assumes that getpwuid is thread safe, only true with Bionic!"
William Roberts8a5b9ca2016-04-08 12:13:17 -07001551#endif
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001552#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001553 struct passwd* pwd = getpwuid(entry->uid);
1554 if (pwd && (strlen(pwd->pw_name) <= 5)) {
1555 snprintf(uid, sizeof(uid), "%5s:", pwd->pw_name);
1556 } else
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001557#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001558 {
1559 /* Not worth parsing package list, names all longer than 5 */
1560 snprintf(uid, sizeof(uid), "%5d:", entry->uid);
1561 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001562 } else {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001563 snprintf(uid, sizeof(uid), " ");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001564 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001565 }
1566
1567 switch (p_format->format) {
1568 case FORMAT_TAG:
Tom Cherry71ba1642019-01-10 10:37:36 -08001569 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c/%-8.*s: ", priChar,
1570 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001571 strcpy(suffixBuf + suffixLen, "\n");
1572 ++suffixLen;
1573 break;
1574 case FORMAT_PROCESS:
Tom Cherry71ba1642019-01-10 10:37:36 -08001575 len = snprintf(suffixBuf + suffixLen, sizeof(suffixBuf) - suffixLen, " (%.*s)\n",
1576 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001577 suffixLen += MIN(len, sizeof(suffixBuf) - suffixLen);
Tom Cherry71ba1642019-01-10 10:37:36 -08001578 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c(%s%5d) ", priChar,
1579 uid, entry->pid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001580 break;
1581 case FORMAT_THREAD:
Tom Cherry71ba1642019-01-10 10:37:36 -08001582 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c(%s%5d:%5d) ",
1583 priChar, uid, entry->pid, entry->tid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001584 strcpy(suffixBuf + suffixLen, "\n");
1585 ++suffixLen;
1586 break;
1587 case FORMAT_RAW:
1588 prefixBuf[prefixLen] = 0;
1589 len = 0;
1590 strcpy(suffixBuf + suffixLen, "\n");
1591 ++suffixLen;
1592 break;
1593 case FORMAT_TIME:
1594 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherry71ba1642019-01-10 10:37:36 -08001595 "%s %c/%-8.*s(%s%5d): ", timeBuf, priChar, (int)entry->tagLen, entry->tag, uid,
1596 entry->pid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001597 strcpy(suffixBuf + suffixLen, "\n");
1598 ++suffixLen;
1599 break;
1600 case FORMAT_THREADTIME:
1601 ret = strchr(uid, ':');
1602 if (ret) {
1603 *ret = ' ';
1604 }
1605 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherry71ba1642019-01-10 10:37:36 -08001606 "%s %s%5d %5d %c %-8.*s: ", timeBuf, uid, entry->pid, entry->tid, priChar,
1607 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001608 strcpy(suffixBuf + suffixLen, "\n");
1609 ++suffixLen;
1610 break;
1611 case FORMAT_LONG:
1612 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherry71ba1642019-01-10 10:37:36 -08001613 "[ %s %s%5d:%5d %c/%-8.*s ]\n", timeBuf, uid, entry->pid, entry->tid, priChar,
1614 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001615 strcpy(suffixBuf + suffixLen, "\n\n");
1616 suffixLen += 2;
1617 prefixSuffixIsHeaderFooter = 1;
1618 break;
1619 case FORMAT_BRIEF:
1620 default:
Tom Cherry71ba1642019-01-10 10:37:36 -08001621 len =
1622 snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
1623 "%c/%-8.*s(%s%5d): ", priChar, (int)entry->tagLen, entry->tag, uid, entry->pid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001624 strcpy(suffixBuf + suffixLen, "\n");
1625 ++suffixLen;
1626 break;
1627 }
1628
1629 /* snprintf has a weird return value. It returns what would have been
1630 * written given a large enough buffer. In the case that the prefix is
1631 * longer then our buffer(128), it messes up the calculations below
1632 * possibly causing heap corruption. To avoid this we double check and
1633 * set the length at the maximum (size minus null byte)
1634 */
1635 prefixLen += len;
1636 if (prefixLen >= sizeof(prefixBuf)) {
1637 prefixLen = sizeof(prefixBuf) - 1;
1638 prefixBuf[sizeof(prefixBuf) - 1] = '\0';
1639 }
1640 if (suffixLen >= sizeof(suffixBuf)) {
1641 suffixLen = sizeof(suffixBuf) - 1;
1642 suffixBuf[sizeof(suffixBuf) - 2] = '\n';
1643 suffixBuf[sizeof(suffixBuf) - 1] = '\0';
1644 }
1645
1646 /* the following code is tragically unreadable */
1647
1648 size_t numLines;
1649 char* p;
1650 size_t bufferSize;
1651 const char* pm;
1652
1653 if (prefixSuffixIsHeaderFooter) {
1654 /* we're just wrapping message with a header/footer */
1655 numLines = 1;
1656 } else {
1657 pm = entry->message;
1658 numLines = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001659
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001660 /*
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001661 * The line-end finding here must match the line-end finding
1662 * in for ( ... numLines...) loop below
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001663 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001664 while (pm < (entry->message + entry->messageLen)) {
1665 if (*pm++ == '\n') numLines++;
1666 }
1667 /* plus one line for anything not newline-terminated at the end */
1668 if (pm > entry->message && *(pm - 1) != '\n') numLines++;
1669 }
1670
1671 /*
1672 * this is an upper bound--newlines in message may be counted
1673 * extraneously
1674 */
1675 bufferSize = (numLines * (prefixLen + suffixLen)) + 1;
1676 if (p_format->printable_output) {
1677 /* Calculate extra length to convert non-printable to printable */
1678 bufferSize += convertPrintable(NULL, entry->message, entry->messageLen);
1679 } else {
1680 bufferSize += entry->messageLen;
1681 }
1682
1683 if (defaultBufferSize >= bufferSize) {
1684 ret = defaultBuffer;
1685 } else {
1686 ret = (char*)malloc(bufferSize);
1687
1688 if (ret == NULL) {
1689 return ret;
1690 }
1691 }
1692
1693 ret[0] = '\0'; /* to start strcat off */
1694
1695 p = ret;
1696 pm = entry->message;
1697
1698 if (prefixSuffixIsHeaderFooter) {
1699 strcat(p, prefixBuf);
1700 p += prefixLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001701 if (p_format->printable_output) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001702 p += convertPrintable(p, entry->message, entry->messageLen);
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001703 } else {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001704 strncat(p, entry->message, entry->messageLen);
1705 p += entry->messageLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001706 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001707 strcat(p, suffixBuf);
1708 p += suffixLen;
1709 } else {
1710 do {
1711 const char* lineStart;
1712 size_t lineLen;
1713 lineStart = pm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001714
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001715 /* Find the next end-of-line in message */
1716 while (pm < (entry->message + entry->messageLen) && *pm != '\n') pm++;
1717 lineLen = pm - lineStart;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001718
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001719 strcat(p, prefixBuf);
1720 p += prefixLen;
1721 if (p_format->printable_output) {
1722 p += convertPrintable(p, lineStart, lineLen);
1723 } else {
1724 strncat(p, lineStart, lineLen);
1725 p += lineLen;
1726 }
1727 strcat(p, suffixBuf);
1728 p += suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001729
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001730 if (*pm == '\n') pm++;
1731 } while (pm < (entry->message + entry->messageLen));
1732 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001733
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001734 if (p_outLength != NULL) {
1735 *p_outLength = p - ret;
1736 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001737
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001738 return ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001739}
1740
1741/**
1742 * Either print or do not print log line, based on filter
1743 *
1744 * Returns count bytes written
1745 */
1746
Tom Cherry2d9779e2019-02-08 11:46:19 -08001747int android_log_printLogLine(AndroidLogFormat* p_format, int fd, const AndroidLogEntry* entry) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001748 int ret;
1749 char defaultBuffer[512];
1750 char* outBuffer = NULL;
1751 size_t totalLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001752
Tom Cherry71ba1642019-01-10 10:37:36 -08001753 outBuffer =
1754 android_log_formatLogLine(p_format, defaultBuffer, sizeof(defaultBuffer), entry, &totalLen);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001755
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001756 if (!outBuffer) return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001757
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001758 do {
1759 ret = write(fd, outBuffer, totalLen);
1760 } while (ret < 0 && errno == EINTR);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001761
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001762 if (ret < 0) {
1763 fprintf(stderr, "+++ LOG: write failed (errno=%d)\n", errno);
1764 ret = 0;
1765 goto done;
1766 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001767
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001768 if (((size_t)ret) < totalLen) {
1769 fprintf(stderr, "+++ LOG: write partial (%d of %d)\n", ret, (int)totalLen);
1770 goto done;
1771 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001772
1773done:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001774 if (outBuffer != defaultBuffer) {
1775 free(outBuffer);
1776 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001777
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001778 return ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001779}