blob: 939c1b3ab7ce810131c3e01f381212ea0dd693f6 [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
585/*
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000586 * Extract a 4-byte value from a byte stream.
587 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800588static inline uint32_t get4LE(const uint8_t* src) {
589 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000590}
591
592/*
593 * Extract an 8-byte value from a byte stream.
594 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800595static inline uint64_t get8LE(const uint8_t* src) {
596 uint32_t low, high;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000597
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800598 low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
599 high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24);
600 return ((uint64_t)high << 32) | (uint64_t)low;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000601}
602
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700603static bool findChar(const char** cp, size_t* len, int c) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800604 while ((*len) && isspace(*(*cp))) {
605 ++(*cp);
606 --(*len);
607 }
608 if (c == INT_MAX) return *len;
609 if ((*len) && (*(*cp) == c)) {
610 ++(*cp);
611 --(*len);
612 return true;
613 }
614 return false;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700615}
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000616
617/*
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700618 * Recursively convert binary log data to printable form.
619 *
620 * This needs to be recursive because you can have lists of lists.
621 *
622 * If we run out of room, we stop processing immediately. It's important
623 * for us to check for space on every output element to avoid producing
624 * garbled output.
625 *
626 * Returns 0 on success, 1 on buffer full, -1 on failure.
627 */
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700628enum objectType {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800629 TYPE_OBJECTS = '1',
630 TYPE_BYTES = '2',
631 TYPE_MILLISECONDS = '3',
632 TYPE_ALLOCATIONS = '4',
633 TYPE_ID = '5',
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800634 TYPE_PERCENT = '6',
635 TYPE_MONOTONIC = 's'
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700636};
637
Tom Cherry71ba1642019-01-10 10:37:36 -0800638static int android_log_printBinaryEvent(const unsigned char** pEventData, size_t* pEventDataLen,
639 char** pOutBuf, size_t* pOutBufLen, const char** fmtStr,
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800640 size_t* fmtLen) {
641 const unsigned char* eventData = *pEventData;
642 size_t eventDataLen = *pEventDataLen;
643 char* outBuf = *pOutBuf;
644 char* outBufSave = outBuf;
645 size_t outBufLen = *pOutBufLen;
646 size_t outBufLenSave = outBufLen;
647 unsigned char type;
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800648 size_t outCount = 0;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800649 int result = 0;
650 const char* cp;
651 size_t len;
652 int64_t lval;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700653
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800654 if (eventDataLen < 1) return -1;
Mark Salyzyn1a57ae32016-11-11 14:41:30 -0800655
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800656 type = *eventData++;
657 eventDataLen--;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700658
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800659 cp = NULL;
660 len = 0;
661 if (fmtStr && *fmtStr && fmtLen && *fmtLen && **fmtStr) {
662 cp = *fmtStr;
663 len = *fmtLen;
664 }
665 /*
666 * event.logtag format specification:
667 *
668 * Optionally, after the tag names can be put a description for the value(s)
669 * of the tag. Description are in the format
670 * (<name>|data type[|data unit])
671 * Multiple values are separated by commas.
672 *
673 * The data type is a number from the following values:
674 * 1: int
675 * 2: long
676 * 3: string
677 * 4: list
678 * 5: float
679 *
680 * The data unit is a number taken from the following list:
681 * 1: Number of objects
682 * 2: Number of bytes
683 * 3: Number of milliseconds
684 * 4: Number of allocations
685 * 5: Id
686 * 6: Percent
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800687 * s: Number of seconds (monotonic time)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800688 * Default value for data of type int/long is 2 (bytes).
689 */
690 if (!cp || !findChar(&cp, &len, '(')) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700691 len = 0;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800692 } else {
693 char* outBufLastSpace = NULL;
694
695 findChar(&cp, &len, INT_MAX);
696 while (len && *cp && (*cp != '|') && (*cp != ')')) {
697 if (outBufLen <= 0) {
698 /* halt output */
699 goto no_room;
700 }
701 outBufLastSpace = isspace(*cp) ? outBuf : NULL;
702 *outBuf = *cp;
703 ++outBuf;
704 ++cp;
705 --outBufLen;
706 --len;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700707 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800708 if (outBufLastSpace) {
709 outBufLen += outBuf - outBufLastSpace;
710 outBuf = outBufLastSpace;
711 }
712 if (outBufLen <= 0) {
713 /* halt output */
714 goto no_room;
715 }
716 if (outBufSave != outBuf) {
717 *outBuf = '=';
718 ++outBuf;
719 --outBufLen;
720 }
721
722 if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800723 static const unsigned char typeTable[] = {EVENT_TYPE_INT, EVENT_TYPE_LONG, EVENT_TYPE_STRING,
724 EVENT_TYPE_LIST, EVENT_TYPE_FLOAT};
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800725
Tom Cherry71ba1642019-01-10 10:37:36 -0800726 if ((*cp >= '1') && (*cp < (char)('1' + (sizeof(typeTable) / sizeof(typeTable[0])))) &&
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800727 (type != typeTable[(size_t)(*cp - '1')]))
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700728 len = 0;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700729
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800730 if (len) {
731 ++cp;
732 --len;
733 } else {
734 /* reset the format */
735 outBuf = outBufSave;
736 outBufLen = outBufLenSave;
737 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700738 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800739 }
740 outCount = 0;
741 lval = 0;
742 switch (type) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700743 case EVENT_TYPE_INT:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800744 /* 32-bit signed int */
745 {
746 int32_t ival;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700747
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800748 if (eventDataLen < 4) return -1;
749 ival = get4LE(eventData);
750 eventData += 4;
751 eventDataLen -= 4;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700752
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800753 lval = ival;
754 }
755 goto pr_lval;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700756 case EVENT_TYPE_LONG:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800757 /* 64-bit signed long */
758 if (eventDataLen < 8) return -1;
759 lval = get8LE(eventData);
760 eventData += 8;
761 eventDataLen -= 8;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700762 pr_lval:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800763 outCount = snprintf(outBuf, outBufLen, "%" PRId64, lval);
764 if (outCount < outBufLen) {
765 outBuf += outCount;
766 outBufLen -= outCount;
767 } else {
768 /* halt output */
769 goto no_room;
770 }
771 break;
Jeff Brown44193d92015-04-28 12:47:02 -0700772 case EVENT_TYPE_FLOAT:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800773 /* float */
774 {
775 uint32_t ival;
776 float fval;
Jeff Brown44193d92015-04-28 12:47:02 -0700777
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800778 if (eventDataLen < 4) return -1;
779 ival = get4LE(eventData);
780 fval = *(float*)&ival;
781 eventData += 4;
782 eventDataLen -= 4;
Jeff Brown44193d92015-04-28 12:47:02 -0700783
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800784 outCount = snprintf(outBuf, outBufLen, "%f", fval);
785 if (outCount < outBufLen) {
786 outBuf += outCount;
787 outBufLen -= outCount;
788 } else {
789 /* halt output */
790 goto no_room;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700791 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800792 }
793 break;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700794 case EVENT_TYPE_STRING:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800795 /* UTF-8 chars, not NULL-terminated */
796 {
797 unsigned int strLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700798
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800799 if (eventDataLen < 4) return -1;
800 strLen = get4LE(eventData);
801 eventData += 4;
802 eventDataLen -= 4;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700803
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800804 if (eventDataLen < strLen) {
805 result = -1; /* mark truncated */
806 strLen = eventDataLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700807 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700808
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800809 if (cp && (strLen == 0)) {
810 /* reset the format if no content */
811 outBuf = outBufSave;
812 outBufLen = outBufLenSave;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700813 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800814 if (strLen < outBufLen) {
815 memcpy(outBuf, eventData, strLen);
816 outBuf += strLen;
817 outBufLen -= strLen;
818 } else {
819 if (outBufLen > 0) {
820 /* copy what we can */
821 memcpy(outBuf, eventData, outBufLen);
822 outBuf += outBufLen;
823 outBufLen -= outBufLen;
824 }
825 if (!result) result = 1; /* if not truncated, return no room */
826 }
827 eventData += strLen;
828 eventDataLen -= strLen;
829 if (result != 0) goto bail;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700830 break;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800831 }
832 case EVENT_TYPE_LIST:
833 /* N items, all different types */
834 {
835 unsigned char count;
836 int i;
837
838 if (eventDataLen < 1) return -1;
839
840 count = *eventData++;
841 eventDataLen--;
842
843 if (outBufLen <= 0) goto no_room;
844
845 *outBuf++ = '[';
846 outBufLen--;
847
848 for (i = 0; i < count; i++) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800849 result = android_log_printBinaryEvent(&eventData, &eventDataLen, &outBuf, &outBufLen,
850 fmtStr, fmtLen);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800851 if (result != 0) goto bail;
852
853 if (i < (count - 1)) {
854 if (outBufLen <= 0) goto no_room;
855 *outBuf++ = ',';
856 outBufLen--;
857 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700858 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800859
860 if (outBufLen <= 0) goto no_room;
861
862 *outBuf++ = ']';
863 outBufLen--;
864 }
865 break;
866 default:
867 fprintf(stderr, "Unknown binary event type %d\n", type);
868 return -1;
869 }
870 if (cp && len) {
871 if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) {
872 switch (*cp) {
873 case TYPE_OBJECTS:
874 outCount = 0;
875 /* outCount = snprintf(outBuf, outBufLen, " objects"); */
876 break;
877 case TYPE_BYTES:
878 if ((lval != 0) && ((lval % 1024) == 0)) {
879 /* repaint with multiplier */
Tom Cherry71ba1642019-01-10 10:37:36 -0800880 static const char suffixTable[] = {'K', 'M', 'G', 'T'};
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800881 size_t idx = 0;
882 outBuf -= outCount;
883 outBufLen += outCount;
884 do {
885 lval /= 1024;
886 if ((lval % 1024) != 0) break;
Tom Cherry71ba1642019-01-10 10:37:36 -0800887 } while (++idx < ((sizeof(suffixTable) / sizeof(suffixTable[0])) - 1));
888 outCount = snprintf(outBuf, outBufLen, "%" PRId64 "%cB", lval, suffixTable[idx]);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800889 } else {
890 outCount = snprintf(outBuf, outBufLen, "B");
891 }
892 break;
893 case TYPE_MILLISECONDS:
Tom Cherry71ba1642019-01-10 10:37:36 -0800894 if (((lval <= -1000) || (1000 <= lval)) && (outBufLen || (outBuf[-1] == '0'))) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800895 /* repaint as (fractional) seconds, possibly saving space */
896 if (outBufLen) outBuf[0] = outBuf[-1];
897 outBuf[-1] = outBuf[-2];
898 outBuf[-2] = outBuf[-3];
899 outBuf[-3] = '.';
900 while ((outBufLen == 0) || (*outBuf == '0')) {
901 --outBuf;
902 ++outBufLen;
903 }
904 if (*outBuf != '.') {
905 ++outBuf;
906 --outBufLen;
907 }
908 outCount = snprintf(outBuf, outBufLen, "s");
909 } else {
910 outCount = snprintf(outBuf, outBufLen, "ms");
911 }
912 break;
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800913 case TYPE_MONOTONIC: {
914 static const uint64_t minute = 60;
915 static const uint64_t hour = 60 * minute;
916 static const uint64_t day = 24 * hour;
917
918 /* Repaint as unsigned seconds, minutes, hours ... */
919 outBuf -= outCount;
920 outBufLen += outCount;
921 uint64_t val = lval;
922 if (val >= day) {
923 outCount = snprintf(outBuf, outBufLen, "%" PRIu64 "d ", val / day);
924 if (outCount >= outBufLen) break;
925 outBuf += outCount;
926 outBufLen -= outCount;
927 val = (val % day) + day;
928 }
929 if (val >= minute) {
930 if (val >= hour) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800931 outCount = snprintf(outBuf, outBufLen, "%" PRIu64 ":", (val / hour) % (day / hour));
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800932 if (outCount >= outBufLen) break;
933 outBuf += outCount;
934 outBufLen -= outCount;
935 }
936 outCount =
Tom Cherry71ba1642019-01-10 10:37:36 -0800937 snprintf(outBuf, outBufLen, (val >= hour) ? "%02" PRIu64 ":" : "%" PRIu64 ":",
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800938 (val / minute) % (hour / minute));
939 if (outCount >= outBufLen) break;
940 outBuf += outCount;
941 outBufLen -= outCount;
942 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800943 outCount = snprintf(outBuf, outBufLen, (val >= minute) ? "%02" PRIu64 : "%" PRIu64 "s",
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800944 val % minute);
945 } break;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800946 case TYPE_ALLOCATIONS:
947 outCount = 0;
948 /* outCount = snprintf(outBuf, outBufLen, " allocations"); */
949 break;
950 case TYPE_ID:
951 outCount = 0;
952 break;
953 case TYPE_PERCENT:
954 outCount = snprintf(outBuf, outBufLen, "%%");
955 break;
956 default: /* ? */
957 outCount = 0;
958 break;
959 }
960 ++cp;
961 --len;
962 if (outCount < outBufLen) {
963 outBuf += outCount;
964 outBufLen -= outCount;
965 } else if (outCount) {
966 /* halt output */
967 goto no_room;
968 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700969 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800970 if (!findChar(&cp, &len, ')')) len = 0;
971 if (!findChar(&cp, &len, ',')) len = 0;
972 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700973
974bail:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800975 *pEventData = eventData;
976 *pEventDataLen = eventDataLen;
977 *pOutBuf = outBuf;
978 *pOutBufLen = outBufLen;
979 if (cp) {
980 *fmtStr = cp;
981 *fmtLen = len;
982 }
983 return result;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700984
985no_room:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800986 result = 1;
987 goto bail;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700988}
989
990/**
991 * Convert a binary log entry to ASCII form.
992 *
993 * For convenience we mimic the processLogBuffer API. There is no
994 * pre-defined output length for the binary data, since we're free to format
995 * it however we choose, which means we can't really use a fixed-size buffer
996 * here.
997 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800998int android_log_processBinaryLogBuffer(
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800999 struct logger_entry* buf, AndroidLogEntry* entry,
Tom Cherry71ba1642019-01-10 10:37:36 -08001000 [[maybe_unused]] const EventTagMap* map, /* only on !__ANDROID__ */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001001 char* messageBuf, int messageBufLen) {
1002 size_t inCount;
1003 uint32_t tagIndex;
1004 const unsigned char* eventData;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001005
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001006 entry->message = NULL;
1007 entry->messageLen = 0;
Mark Salyzynb1d150b2017-03-02 07:47:21 -08001008
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001009 entry->tv_sec = buf->sec;
1010 entry->tv_nsec = buf->nsec;
1011 entry->priority = ANDROID_LOG_INFO;
1012 entry->uid = -1;
1013 entry->pid = buf->pid;
1014 entry->tid = buf->tid;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001015
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001016 /*
1017 * Pull the tag out, fill in some additional details based on incoming
1018 * buffer version (v3 adds lid, v4 adds uid).
1019 */
1020 eventData = (const unsigned char*)buf->msg;
1021 struct logger_entry_v2* buf2 = (struct logger_entry_v2*)buf;
1022 if (buf2->hdr_size) {
1023 if ((buf2->hdr_size < sizeof(((struct log_msg*)NULL)->entry_v1)) ||
1024 (buf2->hdr_size > sizeof(((struct log_msg*)NULL)->entry))) {
1025 fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
1026 return -1;
1027 }
1028 eventData = ((unsigned char*)buf2) + buf2->hdr_size;
1029 if ((buf2->hdr_size >= sizeof(struct logger_entry_v3)) &&
1030 (((struct logger_entry_v3*)buf)->lid == LOG_ID_SECURITY)) {
1031 entry->priority = ANDROID_LOG_WARN;
1032 }
1033 if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) {
1034 entry->uid = ((struct logger_entry_v4*)buf)->uid;
1035 }
1036 }
1037 inCount = buf->len;
1038 if (inCount < 4) return -1;
1039 tagIndex = get4LE(eventData);
1040 eventData += 4;
1041 inCount -= 4;
1042
1043 entry->tagLen = 0;
1044 entry->tag = NULL;
1045#ifdef __ANDROID__
1046 if (map != NULL) {
1047 entry->tag = android_lookupEventTag_len(map, &entry->tagLen, tagIndex);
1048 }
1049#endif
1050
1051 /*
1052 * If we don't have a map, or didn't find the tag number in the map,
1053 * stuff a generated tag value into the start of the output buffer and
1054 * shift the buffer pointers down.
1055 */
1056 if (entry->tag == NULL) {
1057 size_t tagLen;
1058
1059 tagLen = snprintf(messageBuf, messageBufLen, "[%" PRIu32 "]", tagIndex);
1060 if (tagLen >= (size_t)messageBufLen) {
1061 tagLen = messageBufLen - 1;
1062 }
1063 entry->tag = messageBuf;
1064 entry->tagLen = tagLen;
1065 messageBuf += tagLen + 1;
1066 messageBufLen -= tagLen + 1;
1067 }
1068
1069 /*
1070 * Format the event log data into the buffer.
1071 */
1072 const char* fmtStr = NULL;
1073 size_t fmtLen = 0;
1074#ifdef __ANDROID__
1075 if (descriptive_output && map) {
1076 fmtStr = android_lookupEventFormat_len(map, &fmtLen, tagIndex);
1077 }
1078#endif
1079
1080 char* outBuf = messageBuf;
1081 size_t outRemaining = messageBufLen - 1; /* leave one for nul byte */
1082 int result = 0;
1083
1084 if ((inCount > 0) || fmtLen) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001085 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, &outRemaining, &fmtStr,
1086 &fmtLen);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001087 }
1088 if ((result == 1) && fmtStr) {
1089 /* We overflowed :-(, let's repaint the line w/o format dressings */
Mark Salyzynb1d150b2017-03-02 07:47:21 -08001090 eventData = (const unsigned char*)buf->msg;
Mark Salyzyn40b21552013-12-18 12:59:01 -08001091 if (buf2->hdr_size) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001092 eventData = ((unsigned char*)buf2) + buf2->hdr_size;
Mark Salyzyn40b21552013-12-18 12:59:01 -08001093 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001094 eventData += 4;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001095 outBuf = messageBuf;
1096 outRemaining = messageBufLen - 1;
Tom Cherry71ba1642019-01-10 10:37:36 -08001097 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, &outRemaining, NULL, NULL);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001098 }
1099 if (result < 0) {
1100 fprintf(stderr, "Binary log entry conversion failed\n");
1101 }
1102 if (result) {
1103 if (!outRemaining) {
1104 /* make space to leave an indicator */
1105 --outBuf;
1106 ++outRemaining;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001107 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001108 *outBuf++ = (result < 0) ? '!' : '^'; /* Error or Truncation? */
1109 outRemaining--;
1110 /* pretend we ate all the data to prevent log stutter */
1111 inCount = 0;
1112 if (result > 0) result = 0;
1113 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001114
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001115 /* eat the silly terminating '\n' */
1116 if (inCount == 1 && *eventData == '\n') {
1117 eventData++;
1118 inCount--;
1119 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001120
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001121 if (inCount != 0) {
1122 fprintf(stderr, "Warning: leftover binary log data (%zu bytes)\n", inCount);
1123 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001124
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001125 /*
1126 * Terminate the buffer. The NUL byte does not count as part of
1127 * entry->messageLen.
1128 */
1129 *outBuf = '\0';
1130 entry->messageLen = outBuf - messageBuf;
1131 assert(entry->messageLen == (messageBufLen - 1) - outRemaining);
Mark Salyzyn1a57ae32016-11-11 14:41:30 -08001132
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001133 entry->message = messageBuf;
Mark Salyzyn1a57ae32016-11-11 14:41:30 -08001134
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001135 return result;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001136}
1137
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001138/*
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001139 * Convert to printable from message to p buffer, return string length. If p is
1140 * NULL, do not copy, but still return the expected string length.
1141 */
Tom Cherry91589842019-04-25 13:10:30 -07001142size_t convertPrintable(char* p, const char* message, size_t messageLen) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001143 char* begin = p;
1144 bool print = p != NULL;
Tom Cherry91589842019-04-25 13:10:30 -07001145 mbstate_t mb_state = {};
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001146
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001147 while (messageLen) {
1148 char buf[6];
1149 ssize_t len = sizeof(buf) - 1;
1150 if ((size_t)len > messageLen) {
1151 len = messageLen;
1152 }
Tom Cherry91589842019-04-25 13:10:30 -07001153 len = mbrtowc(nullptr, message, len, &mb_state);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001154
1155 if (len < 0) {
Tom Cherry91589842019-04-25 13:10:30 -07001156 snprintf(buf, sizeof(buf), "\\x%02X", static_cast<unsigned char>(*message));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001157 len = 1;
1158 } else {
1159 buf[0] = '\0';
1160 if (len == 1) {
1161 if (*message == '\a') {
1162 strcpy(buf, "\\a");
1163 } else if (*message == '\b') {
1164 strcpy(buf, "\\b");
1165 } else if (*message == '\t') {
1166 strcpy(buf, "\t"); /* Do not escape tabs */
1167 } else if (*message == '\v') {
1168 strcpy(buf, "\\v");
1169 } else if (*message == '\f') {
1170 strcpy(buf, "\\f");
1171 } else if (*message == '\r') {
1172 strcpy(buf, "\\r");
1173 } else if (*message == '\\') {
1174 strcpy(buf, "\\\\");
1175 } else if ((*message < ' ') || (*message & 0x80)) {
Tom Cherry91589842019-04-25 13:10:30 -07001176 snprintf(buf, sizeof(buf), "\\x%02X", static_cast<unsigned char>(*message));
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001177 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001178 }
1179 if (!buf[0]) {
1180 strncpy(buf, message, len);
1181 buf[len] = '\0';
1182 }
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001183 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001184 if (print) {
1185 strcpy(p, buf);
1186 }
1187 p += strlen(buf);
1188 message += len;
1189 messageLen -= len;
1190 }
1191 return p - begin;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001192}
1193
Tom Cherry7d045f62019-09-30 12:58:55 -07001194#ifdef __ANDROID__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001195static char* readSeconds(char* e, struct timespec* t) {
1196 unsigned long multiplier;
1197 char* p;
1198 t->tv_sec = strtoul(e, &p, 10);
1199 if (*p != '.') {
1200 return NULL;
1201 }
1202 t->tv_nsec = 0;
1203 multiplier = NS_PER_SEC;
1204 while (isdigit(*++p) && (multiplier /= 10)) {
1205 t->tv_nsec += (*p - '0') * multiplier;
1206 }
1207 return p;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001208}
1209
Tom Cherry71ba1642019-01-10 10:37:36 -08001210static struct timespec* sumTimespec(struct timespec* left, struct timespec* right) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001211 left->tv_nsec += right->tv_nsec;
1212 left->tv_sec += right->tv_sec;
1213 if (left->tv_nsec >= (long)NS_PER_SEC) {
1214 left->tv_nsec -= NS_PER_SEC;
1215 left->tv_sec += 1;
1216 }
1217 return left;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001218}
1219
Tom Cherry71ba1642019-01-10 10:37:36 -08001220static struct timespec* subTimespec(struct timespec* result, struct timespec* left,
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001221 struct timespec* right) {
1222 result->tv_nsec = left->tv_nsec - right->tv_nsec;
1223 result->tv_sec = left->tv_sec - right->tv_sec;
1224 if (result->tv_nsec < 0) {
1225 result->tv_nsec += NS_PER_SEC;
1226 result->tv_sec -= 1;
1227 }
1228 return result;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001229}
1230
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001231static long long nsecTimespec(struct timespec* now) {
1232 return (long long)now->tv_sec * NS_PER_SEC + now->tv_nsec;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001233}
1234
Tom Cherry71ba1642019-01-10 10:37:36 -08001235static void convertMonotonic(struct timespec* result, const AndroidLogEntry* entry) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001236 struct listnode* node;
1237 struct conversionList {
1238 struct listnode node; /* first */
1239 struct timespec time;
1240 struct timespec convert;
1241 } * list, *next;
1242 struct timespec time, convert;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001243
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001244 /* If we do not have a conversion list, build one up */
1245 if (list_empty(&convertHead)) {
1246 bool suspended_pending = false;
Tom Cherry71ba1642019-01-10 10:37:36 -08001247 struct timespec suspended_monotonic = {0, 0};
1248 struct timespec suspended_diff = {0, 0};
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001249
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001250 /*
1251 * Read dmesg for _some_ synchronization markers and insert
1252 * Anything in the Android Logger before the dmesg logging span will
1253 * be highly suspect regarding the monotonic time calculations.
1254 */
1255 FILE* p = popen("/system/bin/dmesg", "re");
1256 if (p) {
1257 char* line = NULL;
1258 size_t len = 0;
1259 while (getline(&line, &len, p) > 0) {
1260 static const char suspend[] = "PM: suspend entry ";
1261 static const char resume[] = "PM: suspend exit ";
1262 static const char healthd[] = "healthd";
1263 static const char battery[] = ": battery ";
1264 static const char suspended[] = "Suspended for ";
1265 struct timespec monotonic;
1266 struct tm tm;
1267 char *cp, *e = line;
1268 bool add_entry = true;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001269
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001270 if (*e == '<') {
1271 while (*e && (*e != '>')) {
1272 ++e;
1273 }
1274 if (*e != '>') {
1275 continue;
1276 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001277 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001278 if (*e != '[') {
1279 continue;
1280 }
1281 while (*++e == ' ') {
1282 ;
1283 }
1284 e = readSeconds(e, &monotonic);
1285 if (!e || (*e != ']')) {
1286 continue;
1287 }
1288
1289 if ((e = strstr(e, suspend))) {
1290 e += sizeof(suspend) - 1;
1291 } else if ((e = strstr(line, resume))) {
1292 e += sizeof(resume) - 1;
1293 } else if (((e = strstr(line, healthd))) &&
1294 ((e = strstr(e + sizeof(healthd) - 1, battery)))) {
1295 /* NB: healthd is roughly 150us late, worth the price to
1296 * deal with ntp-induced or hardware clock drift. */
1297 e += sizeof(battery) - 1;
1298 } else if ((e = strstr(line, suspended))) {
1299 e += sizeof(suspended) - 1;
1300 e = readSeconds(e, &time);
1301 if (!e) {
1302 continue;
1303 }
1304 add_entry = false;
1305 suspended_pending = true;
1306 suspended_monotonic = monotonic;
1307 suspended_diff = time;
1308 } else {
1309 continue;
1310 }
1311 if (add_entry) {
1312 /* look for "????-??-?? ??:??:??.????????? UTC" */
1313 cp = strstr(e, " UTC");
1314 if (!cp || ((cp - e) < 29) || (cp[-10] != '.')) {
1315 continue;
1316 }
1317 e = cp - 29;
1318 cp = readSeconds(cp - 10, &time);
1319 if (!cp) {
1320 continue;
1321 }
1322 cp = strptime(e, "%Y-%m-%d %H:%M:%S.", &tm);
1323 if (!cp) {
1324 continue;
1325 }
1326 cp = getenv(tz);
1327 if (cp) {
1328 cp = strdup(cp);
1329 }
1330 setenv(tz, utc, 1);
1331 time.tv_sec = mktime(&tm);
1332 if (cp) {
1333 setenv(tz, cp, 1);
1334 free(cp);
1335 } else {
1336 unsetenv(tz);
1337 }
Tom Cherry71ba1642019-01-10 10:37:36 -08001338 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001339 list_init(&list->node);
1340 list->time = time;
1341 subTimespec(&list->convert, &time, &monotonic);
1342 list_add_tail(&convertHead, &list->node);
1343 }
1344 if (suspended_pending && !list_empty(&convertHead)) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001345 list = node_to_item(list_tail(&convertHead), struct conversionList, node);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001346 if (subTimespec(&time, subTimespec(&time, &list->time, &list->convert),
1347 &suspended_monotonic)
1348 ->tv_sec > 0) {
1349 /* resume, what is convert factor before? */
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001350 subTimespec(&convert, &list->convert, &suspended_diff);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001351 } else {
1352 /* suspend */
1353 convert = list->convert;
1354 }
1355 time = suspended_monotonic;
1356 sumTimespec(&time, &convert);
1357 /* breakpoint just before sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001358 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001359 list_init(&list->node);
1360 list->time = time;
1361 list->convert = convert;
1362 list_add_tail(&convertHead, &list->node);
1363 /* breakpoint just after sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001364 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001365 list_init(&list->node);
1366 list->time = time;
1367 sumTimespec(&list->time, &suspended_diff);
1368 list->convert = convert;
1369 sumTimespec(&list->convert, &suspended_diff);
1370 list_add_tail(&convertHead, &list->node);
1371 suspended_pending = false;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001372 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001373 }
1374 pclose(p);
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001375 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001376 /* last entry is our current time conversion */
Tom Cherry71ba1642019-01-10 10:37:36 -08001377 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001378 list_init(&list->node);
1379 clock_gettime(CLOCK_REALTIME, &list->time);
1380 clock_gettime(CLOCK_MONOTONIC, &convert);
1381 clock_gettime(CLOCK_MONOTONIC, &time);
1382 /* Correct for instant clock_gettime latency (syscall or ~30ns) */
1383 subTimespec(&time, &convert, subTimespec(&time, &time, &convert));
1384 /* Calculate conversion factor */
1385 subTimespec(&list->convert, &list->time, &time);
1386 list_add_tail(&convertHead, &list->node);
1387 if (suspended_pending) {
1388 /* manufacture a suspend @ point before */
1389 subTimespec(&convert, &list->convert, &suspended_diff);
1390 time = suspended_monotonic;
1391 sumTimespec(&time, &convert);
1392 /* breakpoint just after sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001393 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001394 list_init(&list->node);
1395 list->time = time;
1396 sumTimespec(&list->time, &suspended_diff);
1397 list->convert = convert;
1398 sumTimespec(&list->convert, &suspended_diff);
1399 list_add_head(&convertHead, &list->node);
1400 /* breakpoint just before sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001401 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001402 list_init(&list->node);
1403 list->time = time;
1404 list->convert = convert;
1405 list_add_head(&convertHead, &list->node);
1406 }
1407 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001408
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001409 /* Find the breakpoint in the conversion list */
1410 list = node_to_item(list_head(&convertHead), struct conversionList, node);
1411 next = NULL;
1412 list_for_each(node, &convertHead) {
1413 next = node_to_item(node, struct conversionList, node);
1414 if (entry->tv_sec < next->time.tv_sec) {
1415 break;
1416 } else if (entry->tv_sec == next->time.tv_sec) {
1417 if (entry->tv_nsec < next->time.tv_nsec) {
1418 break;
1419 }
1420 }
1421 list = next;
1422 }
1423
1424 /* blend time from one breakpoint to the next */
1425 convert = list->convert;
1426 if (next) {
1427 unsigned long long total, run;
1428
1429 total = nsecTimespec(subTimespec(&time, &next->time, &list->time));
1430 time.tv_sec = entry->tv_sec;
1431 time.tv_nsec = entry->tv_nsec;
1432 run = nsecTimespec(subTimespec(&time, &time, &list->time));
1433 if (run < total) {
1434 long long crun;
1435
1436 float f = nsecTimespec(subTimespec(&time, &next->convert, &convert));
1437 f *= run;
1438 f /= total;
1439 crun = f;
1440 convert.tv_sec += crun / (long long)NS_PER_SEC;
1441 if (crun < 0) {
1442 convert.tv_nsec -= (-crun) % NS_PER_SEC;
1443 if (convert.tv_nsec < 0) {
1444 convert.tv_nsec += NS_PER_SEC;
1445 convert.tv_sec -= 1;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001446 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001447 } else {
1448 convert.tv_nsec += crun % NS_PER_SEC;
1449 if (convert.tv_nsec >= (long)NS_PER_SEC) {
1450 convert.tv_nsec -= NS_PER_SEC;
1451 convert.tv_sec += 1;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001452 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001453 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001454 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001455 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001456
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001457 /* Apply the correction factor */
1458 result->tv_sec = entry->tv_sec;
1459 result->tv_nsec = entry->tv_nsec;
1460 subTimespec(result, result, &convert);
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001461}
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001462#endif
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001463
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001464/**
1465 * Formats a log message into a buffer
1466 *
1467 * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer
1468 * If return value != defaultBuffer, caller must call free()
1469 * Returns NULL on malloc error
1470 */
1471
Tom Cherry2d9779e2019-02-08 11:46:19 -08001472char* android_log_formatLogLine(AndroidLogFormat* p_format, char* defaultBuffer,
1473 size_t defaultBufferSize, const AndroidLogEntry* entry,
1474 size_t* p_outLength) {
Yabin Cui8a985352014-11-13 10:02:08 -08001475#if !defined(_WIN32)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001476 struct tm tmBuf;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001477#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001478 struct tm* ptm;
1479 /* good margin, 23+nul for msec, 26+nul for usec, 29+nul to nsec */
1480 char timeBuf[64];
1481 char prefixBuf[128], suffixBuf[128];
1482 char priChar;
1483 int prefixSuffixIsHeaderFooter = 0;
1484 char* ret;
1485 time_t now;
1486 unsigned long nsec;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001487
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001488 priChar = filterPriToChar(entry->priority);
1489 size_t prefixLen = 0, suffixLen = 0;
1490 size_t len;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001491
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001492 /*
1493 * Get the current date/time in pretty form
1494 *
1495 * It's often useful when examining a log with "less" to jump to
1496 * a specific point in the file by searching for the date/time stamp.
1497 * For this reason it's very annoying to have regexp meta characters
1498 * in the time stamp. Don't use forward slashes, parenthesis,
1499 * brackets, asterisks, or other special chars here.
1500 *
1501 * The caller may have affected the timezone environment, this is
1502 * expected to be sensitive to that.
1503 */
1504 now = entry->tv_sec;
1505 nsec = entry->tv_nsec;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001506#if __ANDROID__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001507 if (p_format->monotonic_output) {
1508 /* prevent convertMonotonic from being called if logd is monotonic */
1509 if (android_log_clockid() != CLOCK_MONOTONIC) {
1510 struct timespec time;
1511 convertMonotonic(&time, entry);
1512 now = time.tv_sec;
1513 nsec = time.tv_nsec;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001514 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001515 }
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001516#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001517 if (now < 0) {
1518 nsec = NS_PER_SEC - nsec;
1519 }
1520 if (p_format->epoch_output || p_format->monotonic_output) {
1521 ptm = NULL;
Tom Cherry71ba1642019-01-10 10:37:36 -08001522 snprintf(timeBuf, sizeof(timeBuf), p_format->monotonic_output ? "%6lld" : "%19lld",
1523 (long long)now);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001524 } else {
Yabin Cui8a985352014-11-13 10:02:08 -08001525#if !defined(_WIN32)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001526 ptm = localtime_r(&now, &tmBuf);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001527#else
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001528 ptm = localtime(&now);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001529#endif
Tom Cherry71ba1642019-01-10 10:37:36 -08001530 strftime(timeBuf, sizeof(timeBuf), &"%Y-%m-%d %H:%M:%S"[p_format->year_output ? 0 : 3], ptm);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001531 }
1532 len = strlen(timeBuf);
1533 if (p_format->nsec_time_output) {
1534 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%09ld", nsec);
1535 } else if (p_format->usec_time_output) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001536 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%06ld", nsec / US_PER_NSEC);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001537 } else {
Tom Cherry71ba1642019-01-10 10:37:36 -08001538 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%03ld", nsec / MS_PER_NSEC);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001539 }
1540 if (p_format->zone_output && ptm) {
1541 strftime(timeBuf + len, sizeof(timeBuf) - len, " %z", ptm);
1542 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001543
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001544 /*
1545 * Construct a buffer containing the log header and log message.
1546 */
1547 if (p_format->colored_output) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001548 prefixLen =
1549 snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm", colorFromPri(entry->priority));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001550 prefixLen = MIN(prefixLen, sizeof(prefixBuf));
George Burgess IV487de272018-05-15 18:56:36 -07001551
1552 const char suffixContents[] = "\x1B[0m";
1553 strcpy(suffixBuf, suffixContents);
1554 suffixLen = strlen(suffixContents);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001555 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001556
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001557 char uid[16];
1558 uid[0] = '\0';
1559 if (p_format->uid_output) {
1560 if (entry->uid >= 0) {
1561/*
1562 * This code is Android specific, bionic guarantees that
1563 * calls to non-reentrant getpwuid() are thread safe.
1564 */
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001565#if !defined(__MINGW32__)
1566#if (FAKE_LOG_DEVICE == 0)
William Roberts8a5b9ca2016-04-08 12:13:17 -07001567#ifndef __BIONIC__
Tom Cherry71ba1642019-01-10 10:37:36 -08001568#warning "This code assumes that getpwuid is thread safe, only true with Bionic!"
William Roberts8a5b9ca2016-04-08 12:13:17 -07001569#endif
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001570#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001571 struct passwd* pwd = getpwuid(entry->uid);
1572 if (pwd && (strlen(pwd->pw_name) <= 5)) {
1573 snprintf(uid, sizeof(uid), "%5s:", pwd->pw_name);
1574 } else
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001575#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001576 {
1577 /* Not worth parsing package list, names all longer than 5 */
1578 snprintf(uid, sizeof(uid), "%5d:", entry->uid);
1579 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001580 } else {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001581 snprintf(uid, sizeof(uid), " ");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001582 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001583 }
1584
1585 switch (p_format->format) {
1586 case FORMAT_TAG:
Tom Cherry71ba1642019-01-10 10:37:36 -08001587 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c/%-8.*s: ", priChar,
1588 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001589 strcpy(suffixBuf + suffixLen, "\n");
1590 ++suffixLen;
1591 break;
1592 case FORMAT_PROCESS:
Tom Cherry71ba1642019-01-10 10:37:36 -08001593 len = snprintf(suffixBuf + suffixLen, sizeof(suffixBuf) - suffixLen, " (%.*s)\n",
1594 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001595 suffixLen += MIN(len, sizeof(suffixBuf) - suffixLen);
Tom Cherry71ba1642019-01-10 10:37:36 -08001596 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c(%s%5d) ", priChar,
1597 uid, entry->pid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001598 break;
1599 case FORMAT_THREAD:
Tom Cherry71ba1642019-01-10 10:37:36 -08001600 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c(%s%5d:%5d) ",
1601 priChar, uid, entry->pid, entry->tid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001602 strcpy(suffixBuf + suffixLen, "\n");
1603 ++suffixLen;
1604 break;
1605 case FORMAT_RAW:
1606 prefixBuf[prefixLen] = 0;
1607 len = 0;
1608 strcpy(suffixBuf + suffixLen, "\n");
1609 ++suffixLen;
1610 break;
1611 case FORMAT_TIME:
1612 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherry71ba1642019-01-10 10:37:36 -08001613 "%s %c/%-8.*s(%s%5d): ", timeBuf, priChar, (int)entry->tagLen, entry->tag, uid,
1614 entry->pid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001615 strcpy(suffixBuf + suffixLen, "\n");
1616 ++suffixLen;
1617 break;
1618 case FORMAT_THREADTIME:
1619 ret = strchr(uid, ':');
1620 if (ret) {
1621 *ret = ' ';
1622 }
1623 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherry71ba1642019-01-10 10:37:36 -08001624 "%s %s%5d %5d %c %-8.*s: ", timeBuf, uid, entry->pid, entry->tid, priChar,
1625 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001626 strcpy(suffixBuf + suffixLen, "\n");
1627 ++suffixLen;
1628 break;
1629 case FORMAT_LONG:
1630 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherry71ba1642019-01-10 10:37:36 -08001631 "[ %s %s%5d:%5d %c/%-8.*s ]\n", timeBuf, uid, entry->pid, entry->tid, priChar,
1632 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001633 strcpy(suffixBuf + suffixLen, "\n\n");
1634 suffixLen += 2;
1635 prefixSuffixIsHeaderFooter = 1;
1636 break;
1637 case FORMAT_BRIEF:
1638 default:
Tom Cherry71ba1642019-01-10 10:37:36 -08001639 len =
1640 snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
1641 "%c/%-8.*s(%s%5d): ", priChar, (int)entry->tagLen, entry->tag, uid, entry->pid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001642 strcpy(suffixBuf + suffixLen, "\n");
1643 ++suffixLen;
1644 break;
1645 }
1646
1647 /* snprintf has a weird return value. It returns what would have been
1648 * written given a large enough buffer. In the case that the prefix is
1649 * longer then our buffer(128), it messes up the calculations below
1650 * possibly causing heap corruption. To avoid this we double check and
1651 * set the length at the maximum (size minus null byte)
1652 */
1653 prefixLen += len;
1654 if (prefixLen >= sizeof(prefixBuf)) {
1655 prefixLen = sizeof(prefixBuf) - 1;
1656 prefixBuf[sizeof(prefixBuf) - 1] = '\0';
1657 }
1658 if (suffixLen >= sizeof(suffixBuf)) {
1659 suffixLen = sizeof(suffixBuf) - 1;
1660 suffixBuf[sizeof(suffixBuf) - 2] = '\n';
1661 suffixBuf[sizeof(suffixBuf) - 1] = '\0';
1662 }
1663
1664 /* the following code is tragically unreadable */
1665
1666 size_t numLines;
1667 char* p;
1668 size_t bufferSize;
1669 const char* pm;
1670
1671 if (prefixSuffixIsHeaderFooter) {
1672 /* we're just wrapping message with a header/footer */
1673 numLines = 1;
1674 } else {
1675 pm = entry->message;
1676 numLines = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001677
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001678 /*
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001679 * The line-end finding here must match the line-end finding
1680 * in for ( ... numLines...) loop below
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001681 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001682 while (pm < (entry->message + entry->messageLen)) {
1683 if (*pm++ == '\n') numLines++;
1684 }
1685 /* plus one line for anything not newline-terminated at the end */
1686 if (pm > entry->message && *(pm - 1) != '\n') numLines++;
1687 }
1688
1689 /*
1690 * this is an upper bound--newlines in message may be counted
1691 * extraneously
1692 */
1693 bufferSize = (numLines * (prefixLen + suffixLen)) + 1;
1694 if (p_format->printable_output) {
1695 /* Calculate extra length to convert non-printable to printable */
1696 bufferSize += convertPrintable(NULL, entry->message, entry->messageLen);
1697 } else {
1698 bufferSize += entry->messageLen;
1699 }
1700
1701 if (defaultBufferSize >= bufferSize) {
1702 ret = defaultBuffer;
1703 } else {
1704 ret = (char*)malloc(bufferSize);
1705
1706 if (ret == NULL) {
1707 return ret;
1708 }
1709 }
1710
1711 ret[0] = '\0'; /* to start strcat off */
1712
1713 p = ret;
1714 pm = entry->message;
1715
1716 if (prefixSuffixIsHeaderFooter) {
1717 strcat(p, prefixBuf);
1718 p += prefixLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001719 if (p_format->printable_output) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001720 p += convertPrintable(p, entry->message, entry->messageLen);
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001721 } else {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001722 strncat(p, entry->message, entry->messageLen);
1723 p += entry->messageLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001724 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001725 strcat(p, suffixBuf);
1726 p += suffixLen;
1727 } else {
1728 do {
1729 const char* lineStart;
1730 size_t lineLen;
1731 lineStart = pm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001732
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001733 /* Find the next end-of-line in message */
1734 while (pm < (entry->message + entry->messageLen) && *pm != '\n') pm++;
1735 lineLen = pm - lineStart;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001736
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001737 strcat(p, prefixBuf);
1738 p += prefixLen;
1739 if (p_format->printable_output) {
1740 p += convertPrintable(p, lineStart, lineLen);
1741 } else {
1742 strncat(p, lineStart, lineLen);
1743 p += lineLen;
1744 }
1745 strcat(p, suffixBuf);
1746 p += suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001747
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001748 if (*pm == '\n') pm++;
1749 } while (pm < (entry->message + entry->messageLen));
1750 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001751
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001752 if (p_outLength != NULL) {
1753 *p_outLength = p - ret;
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}
1758
1759/**
1760 * Either print or do not print log line, based on filter
1761 *
1762 * Returns count bytes written
1763 */
1764
Tom Cherry2d9779e2019-02-08 11:46:19 -08001765int android_log_printLogLine(AndroidLogFormat* p_format, int fd, const AndroidLogEntry* entry) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001766 int ret;
1767 char defaultBuffer[512];
1768 char* outBuffer = NULL;
1769 size_t totalLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001770
Tom Cherry71ba1642019-01-10 10:37:36 -08001771 outBuffer =
1772 android_log_formatLogLine(p_format, defaultBuffer, sizeof(defaultBuffer), entry, &totalLen);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001773
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001774 if (!outBuffer) return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001775
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001776 do {
1777 ret = write(fd, outBuffer, totalLen);
1778 } while (ret < 0 && errno == EINTR);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001779
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001780 if (ret < 0) {
1781 fprintf(stderr, "+++ LOG: write failed (errno=%d)\n", errno);
1782 ret = 0;
1783 goto done;
1784 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001785
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001786 if (((size_t)ret) < totalLen) {
1787 fprintf(stderr, "+++ LOG: write partial (%d of %d)\n", ret, (int)totalLen);
1788 goto done;
1789 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001790
1791done:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001792 if (outBuffer != defaultBuffer) {
1793 free(outBuffer);
1794 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001795
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001796 return ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001797}