Mark Salyzyn | c73e2b1 | 2013-11-22 07:54:30 -0800 | [diff] [blame] | 1 | /* |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2 | ** |
Mark Salyzyn | 64bf668 | 2013-12-18 12:59:01 -0800 | [diff] [blame] | 3 | ** Copyright 2006-2014, The Android Open Source Project |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 4 | ** |
| 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 Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 18 | #ifndef __MINGW32__ |
| 19 | #define HAVE_STRSEP |
| 20 | #endif |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 | |
Mark Salyzyn | 3a5219e | 2014-04-30 08:50:53 -0700 | [diff] [blame] | 22 | #include <assert.h> |
| 23 | #include <ctype.h> |
| 24 | #include <errno.h> |
Mark Salyzyn | 604679a | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 25 | #include <inttypes.h> |
Mark Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 26 | #ifndef __MINGW32__ |
William Roberts | 6877c6c | 2016-04-08 12:13:17 -0700 | [diff] [blame] | 27 | #include <pwd.h> |
Mark Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 28 | #endif |
Pierre Zurek | 507ea56 | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 29 | #include <stdbool.h> |
Mark Salyzyn | 3a5219e | 2014-04-30 08:50:53 -0700 | [diff] [blame] | 30 | #include <stdint.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
Pierre Zurek | 507ea56 | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 34 | #include <sys/param.h> |
William Roberts | 6877c6c | 2016-04-08 12:13:17 -0700 | [diff] [blame] | 35 | #include <sys/types.h> |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 36 | |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 37 | #include <cutils/list.h> |
Mark Salyzyn | 8b8bfd2 | 2016-09-30 13:30:33 -0700 | [diff] [blame] | 38 | #include <log/log.h> |
Colin Cross | e355ded | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 39 | #include <log/logprint.h> |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 40 | |
Mark Salyzyn | dd9094a | 2016-03-01 13:45:42 -0800 | [diff] [blame] | 41 | #include "log_portability.h" |
Mark Salyzyn | a5c2217 | 2016-03-10 08:25:33 -0800 | [diff] [blame] | 42 | |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 43 | #define MS_PER_NSEC 1000000 |
| 44 | #define US_PER_NSEC 1000 |
| 45 | |
Mark Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 46 | #ifndef MIN |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 47 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) |
Mark Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 48 | #endif |
| 49 | |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 50 | typedef struct FilterInfo_t { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 51 | char* mTag; |
| 52 | android_LogPriority mPri; |
| 53 | struct FilterInfo_t* p_next; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 54 | } FilterInfo; |
| 55 | |
| 56 | struct AndroidLogFormat_t { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 57 | 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 Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
Pierre Zurek | 507ea56 | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 72 | /* |
Mark Salyzyn | 604679a | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 73 | * API issues prevent us from exposing "descriptive" in AndroidLogFormat_t |
| 74 | * during android_log_processBinaryLogBuffer(), so we break layering. |
| 75 | */ |
| 76 | static bool descriptive_output = false; |
| 77 | |
| 78 | /* |
Pierre Zurek | 507ea56 | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 79 | * 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 Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 85 | #define ANDROID_COLOR_BLUE 75 |
Pierre Zurek | 507ea56 | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 86 | #define ANDROID_COLOR_DEFAULT 231 |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 87 | #define ANDROID_COLOR_GREEN 40 |
| 88 | #define ANDROID_COLOR_ORANGE 166 |
| 89 | #define ANDROID_COLOR_RED 196 |
| 90 | #define ANDROID_COLOR_YELLOW 226 |
Pierre Zurek | 507ea56 | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 91 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 92 | static FilterInfo* filterinfo_new(const char* tag, android_LogPriority pri) { |
| 93 | FilterInfo* p_ret; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 94 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 95 | p_ret = (FilterInfo*)calloc(1, sizeof(FilterInfo)); |
| 96 | p_ret->mTag = strdup(tag); |
| 97 | p_ret->mPri = pri; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 98 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 99 | return p_ret; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Mark Salyzyn | 3a5219e | 2014-04-30 08:50:53 -0700 | [diff] [blame] | 102 | /* balance to above, filterinfo_free left unimplemented */ |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | * Note: also accepts 0-9 priorities |
| 106 | * returns ANDROID_LOG_UNKNOWN if the character is unrecognized |
| 107 | */ |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 108 | static android_LogPriority filterCharToPri(char c) { |
| 109 | android_LogPriority pri; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 110 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 111 | c = tolower(c); |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 112 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 113 | if (c >= '0' && c <= '9') { |
| 114 | if (c >= ('0' + ANDROID_LOG_SILENT)) { |
| 115 | pri = ANDROID_LOG_VERBOSE; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 116 | } else { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 117 | pri = (android_LogPriority)(c - '0'); |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 118 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 119 | } 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 Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 138 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 139 | return pri; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 142 | static 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 Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 152 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 153 | case ANDROID_LOG_DEFAULT: |
| 154 | case ANDROID_LOG_UNKNOWN: |
| 155 | default: return '?'; |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 156 | /* clang-format on */ |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 157 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 160 | static 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 Zurek | 507ea56 | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 170 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 171 | case ANDROID_LOG_DEFAULT: |
| 172 | case ANDROID_LOG_UNKNOWN: |
| 173 | default: return ANDROID_COLOR_DEFAULT; |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 174 | /* clang-format on */ |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 175 | } |
Pierre Zurek | 507ea56 | 2010-10-17 22:39:37 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 178 | static android_LogPriority filterPriForTag(AndroidLogFormat* p_format, const char* tag) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 179 | FilterInfo* p_curFilter; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 180 | |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 181 | for (p_curFilter = p_format->filters; p_curFilter != NULL; p_curFilter = p_curFilter->p_next) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 182 | 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 Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 188 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 189 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 190 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 191 | return p_format->global_pri; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 192 | } |
| 193 | |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 194 | /** |
| 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 Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 198 | LIBLOG_ABI_PUBLIC int android_log_shouldPrintLine(AndroidLogFormat* p_format, const char* tag, |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 199 | android_LogPriority pri) { |
| 200 | return pri >= filterPriForTag(p_format, tag); |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 203 | LIBLOG_ABI_PUBLIC AndroidLogFormat* android_log_format_new() { |
| 204 | AndroidLogFormat* p_ret; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 205 | |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 206 | p_ret = static_cast<AndroidLogFormat*>(calloc(1, sizeof(AndroidLogFormat))); |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 207 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 208 | 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 Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 217 | #ifdef __ANDROID__ |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 218 | p_ret->monotonic_output = android_log_clockid() == CLOCK_MONOTONIC; |
Mark Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 219 | #else |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 220 | p_ret->monotonic_output = false; |
Mark Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 221 | #endif |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 222 | p_ret->uid_output = false; |
| 223 | p_ret->descriptive_output = false; |
| 224 | descriptive_output = false; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 225 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 226 | return p_ret; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 229 | static list_declare(convertHead); |
| 230 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 231 | LIBLOG_ABI_PUBLIC void android_log_format_free(AndroidLogFormat* p_format) { |
| 232 | FilterInfo *p_info, *p_info_old; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 233 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 234 | p_info = p_format->filters; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 235 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 236 | while (p_info != NULL) { |
| 237 | p_info_old = p_info; |
| 238 | p_info = p_info->p_next; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 239 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 240 | free(p_info_old); |
| 241 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 242 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 243 | free(p_format); |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 244 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 245 | /* 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 Huang | ca6822f | 2017-08-15 17:01:33 -0700 | [diff] [blame] | 249 | LOG_ALWAYS_FATAL_IF(node == list_head(&convertHead), "corrupted list"); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 250 | free(node); |
| 251 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 254 | LIBLOG_ABI_PUBLIC int android_log_setPrintFormat(AndroidLogFormat* p_format, |
| 255 | AndroidLogPrintFormat format) { |
| 256 | switch (format) { |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 257 | case FORMAT_MODIFIER_COLOR: |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 258 | p_format->colored_output = true; |
| 259 | return 0; |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 260 | case FORMAT_MODIFIER_TIME_USEC: |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 261 | p_format->usec_time_output = true; |
| 262 | return 0; |
Mark Salyzyn | f005085 | 2017-01-30 09:16:09 -0800 | [diff] [blame] | 263 | case FORMAT_MODIFIER_TIME_NSEC: |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 264 | p_format->nsec_time_output = true; |
| 265 | return 0; |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 266 | case FORMAT_MODIFIER_PRINTABLE: |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 267 | p_format->printable_output = true; |
| 268 | return 0; |
Mark Salyzyn | eb0456d | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 269 | case FORMAT_MODIFIER_YEAR: |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 270 | p_format->year_output = true; |
| 271 | return 0; |
Mark Salyzyn | eb0456d | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 272 | case FORMAT_MODIFIER_ZONE: |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 273 | p_format->zone_output = !p_format->zone_output; |
| 274 | return 0; |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 275 | case FORMAT_MODIFIER_EPOCH: |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 276 | p_format->epoch_output = true; |
| 277 | return 0; |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 278 | case FORMAT_MODIFIER_MONOTONIC: |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 279 | p_format->monotonic_output = true; |
| 280 | return 0; |
Mark Salyzyn | b71da91 | 2015-12-07 16:52:42 -0800 | [diff] [blame] | 281 | case FORMAT_MODIFIER_UID: |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 282 | p_format->uid_output = true; |
| 283 | return 0; |
Mark Salyzyn | 604679a | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 284 | case FORMAT_MODIFIER_DESCRIPT: |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 285 | p_format->descriptive_output = true; |
| 286 | descriptive_output = true; |
| 287 | return 0; |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 288 | default: |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 289 | break; |
| 290 | } |
| 291 | p_format->format = format; |
| 292 | return 1; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Mark Salyzyn | eb0456d | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 295 | static const char tz[] = "TZ"; |
| 296 | static const char utc[] = "UTC"; |
| 297 | |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 298 | /** |
| 299 | * Returns FORMAT_OFF on invalid string |
| 300 | */ |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 301 | LIBLOG_ABI_PUBLIC AndroidLogPrintFormat android_log_formatFromString(const char* formatString) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 302 | static AndroidLogPrintFormat format; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 303 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 304 | /* clang-format off */ |
| 305 | if (!strcmp(formatString, "brief")) format = FORMAT_BRIEF; |
| 306 | else if (!strcmp(formatString, "process")) format = FORMAT_PROCESS; |
| 307 | else if (!strcmp(formatString, "tag")) format = FORMAT_TAG; |
| 308 | else if (!strcmp(formatString, "thread")) format = FORMAT_THREAD; |
| 309 | else if (!strcmp(formatString, "raw")) format = FORMAT_RAW; |
| 310 | else if (!strcmp(formatString, "time")) format = FORMAT_TIME; |
| 311 | else if (!strcmp(formatString, "threadtime")) format = FORMAT_THREADTIME; |
| 312 | else if (!strcmp(formatString, "long")) format = FORMAT_LONG; |
| 313 | else if (!strcmp(formatString, "color")) format = FORMAT_MODIFIER_COLOR; |
| 314 | else if (!strcmp(formatString, "colour")) format = FORMAT_MODIFIER_COLOR; |
| 315 | else if (!strcmp(formatString, "usec")) format = FORMAT_MODIFIER_TIME_USEC; |
| 316 | else if (!strcmp(formatString, "nsec")) format = FORMAT_MODIFIER_TIME_NSEC; |
| 317 | else if (!strcmp(formatString, "printable")) format = FORMAT_MODIFIER_PRINTABLE; |
| 318 | else if (!strcmp(formatString, "year")) format = FORMAT_MODIFIER_YEAR; |
| 319 | else if (!strcmp(formatString, "zone")) format = FORMAT_MODIFIER_ZONE; |
| 320 | else if (!strcmp(formatString, "epoch")) format = FORMAT_MODIFIER_EPOCH; |
| 321 | else if (!strcmp(formatString, "monotonic")) format = FORMAT_MODIFIER_MONOTONIC; |
| 322 | else if (!strcmp(formatString, "uid")) format = FORMAT_MODIFIER_UID; |
| 323 | else if (!strcmp(formatString, "descriptive")) format = FORMAT_MODIFIER_DESCRIPT; |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 324 | /* clang-format on */ |
Mark Salyzyn | 07ff212 | 2016-11-10 10:24:44 -0800 | [diff] [blame] | 325 | |
Mark Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 326 | #ifndef __MINGW32__ |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 327 | else { |
| 328 | extern char* tzname[2]; |
| 329 | static const char gmt[] = "GMT"; |
| 330 | char* cp = getenv(tz); |
| 331 | if (cp) { |
| 332 | cp = strdup(cp); |
Mark Salyzyn | eb0456d | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 333 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 334 | setenv(tz, formatString, 1); |
| 335 | /* |
| 336 | * Run tzset here to determine if the timezone is legitimate. If the |
| 337 | * zone is GMT, check if that is what was asked for, if not then |
| 338 | * did not match any on the system; report an error to caller. |
| 339 | */ |
| 340 | tzset(); |
| 341 | if (!tzname[0] || |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 342 | ((!strcmp(tzname[0], utc) || !strcmp(tzname[0], gmt)) /* error? */ |
| 343 | && strcasecmp(formatString, utc) && strcasecmp(formatString, gmt))) { /* ok */ |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 344 | if (cp) { |
| 345 | setenv(tz, cp, 1); |
| 346 | } else { |
| 347 | unsetenv(tz); |
| 348 | } |
| 349 | tzset(); |
| 350 | format = FORMAT_OFF; |
| 351 | } else { |
| 352 | format = FORMAT_MODIFIER_ZONE; |
| 353 | } |
| 354 | free(cp); |
| 355 | } |
Mark Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 356 | #endif |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 357 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 358 | return format; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /** |
| 362 | * filterExpression: a single filter expression |
| 363 | * eg "AT:d" |
| 364 | * |
| 365 | * returns 0 on success and -1 on invalid expression |
| 366 | * |
| 367 | * Assumes single threaded execution |
| 368 | */ |
| 369 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 370 | LIBLOG_ABI_PUBLIC int android_log_addFilterRule(AndroidLogFormat* p_format, |
| 371 | const char* filterExpression) { |
| 372 | size_t tagNameLength; |
| 373 | android_LogPriority pri = ANDROID_LOG_DEFAULT; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 374 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 375 | tagNameLength = strcspn(filterExpression, ":"); |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 376 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 377 | 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 Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 399 | 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 Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 409 | char* tagName; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 410 | |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 411 | /* |
| 412 | * Presently HAVE_STRNDUP is never defined, so the second case is always taken |
Mark Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 413 | * Darwin doesn't have strndup, everything else does |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 414 | */ |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 415 | #ifdef HAVE_STRNDUP |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 416 | tagName = strndup(filterExpression, tagNameLength); |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 417 | #else |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 418 | /* a few extra bytes copied... */ |
| 419 | tagName = strdup(filterExpression); |
| 420 | tagName[tagNameLength] = '\0'; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 421 | #endif /*HAVE_STRNDUP*/ |
| 422 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 423 | FilterInfo* p_fi = filterinfo_new(tagName, pri); |
| 424 | free(tagName); |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 425 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 426 | p_fi->p_next = p_format->filters; |
| 427 | p_format->filters = p_fi; |
| 428 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 429 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 430 | return 0; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 431 | error: |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 432 | return -1; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Mark Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 435 | #ifndef HAVE_STRSEP |
| 436 | /* KISS replacement helper for below */ |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 437 | static char* strsep(char** stringp, const char* delim) { |
| 438 | char* token; |
| 439 | char* ret = *stringp; |
Mark Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 440 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 441 | 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 Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 453 | } |
| 454 | #endif |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 455 | |
| 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 | */ |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 466 | LIBLOG_ABI_PUBLIC int android_log_addFilterString(AndroidLogFormat* p_format, |
| 467 | const char* filterString) { |
| 468 | char* filterStringCopy = strdup(filterString); |
| 469 | char* p_cur = filterStringCopy; |
| 470 | char* p_ret; |
| 471 | int err; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 472 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 473 | /* Yes, I'm using strsep */ |
| 474 | while (NULL != (p_ret = strsep(&p_cur, " \t,"))) { |
| 475 | /* ignore whitespace-only entries */ |
| 476 | if (p_ret[0] != '\0') { |
| 477 | err = android_log_addFilterRule(p_format, p_ret); |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 478 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 479 | if (err < 0) { |
| 480 | goto error; |
| 481 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 482 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 483 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 484 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 485 | free(filterStringCopy); |
| 486 | return 0; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 487 | error: |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 488 | free(filterStringCopy); |
| 489 | return -1; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 490 | } |
| 491 | |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 492 | /** |
| 493 | * Splits a wire-format buffer into an AndroidLogEntry |
| 494 | * entry allocated by caller. Pointers will point directly into buf |
| 495 | * |
| 496 | * Returns 0 on success and -1 on invalid wire format (entry will be |
| 497 | * in unspecified state) |
| 498 | */ |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 499 | LIBLOG_ABI_PUBLIC int android_log_processLogBuffer(struct logger_entry* buf, |
| 500 | AndroidLogEntry* entry) { |
| 501 | entry->message = NULL; |
| 502 | entry->messageLen = 0; |
Mark Salyzyn | ed47c7b | 2017-03-02 07:47:21 -0800 | [diff] [blame] | 503 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 504 | entry->tv_sec = buf->sec; |
| 505 | entry->tv_nsec = buf->nsec; |
| 506 | entry->uid = -1; |
| 507 | entry->pid = buf->pid; |
| 508 | entry->tid = buf->tid; |
Kenny Root | fa7c09a | 2011-09-30 17:10:14 -0700 | [diff] [blame] | 509 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 510 | /* |
| 511 | * format: <priority:1><tag:N>\0<message:N>\0 |
| 512 | * |
| 513 | * tag str |
| 514 | * starts at buf->msg+1 |
| 515 | * msg |
| 516 | * starts at buf->msg+1+len(tag)+1 |
| 517 | * |
| 518 | * The message may have been truncated by the kernel log driver. |
| 519 | * When that happens, we must null-terminate the message ourselves. |
| 520 | */ |
| 521 | if (buf->len < 3) { |
Kenny Root | fa7c09a | 2011-09-30 17:10:14 -0700 | [diff] [blame] | 522 | /* |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 523 | * An well-formed entry must consist of at least a priority |
| 524 | * and two null characters |
Kenny Root | fa7c09a | 2011-09-30 17:10:14 -0700 | [diff] [blame] | 525 | */ |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 526 | fprintf(stderr, "+++ LOG: entry too small\n"); |
| 527 | return -1; |
| 528 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 529 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 530 | int msgStart = -1; |
| 531 | int msgEnd = -1; |
Jeff Sharkey | 059ac70 | 2011-10-26 18:40:39 -0700 | [diff] [blame] | 532 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 533 | int i; |
| 534 | char* msg = buf->msg; |
| 535 | struct logger_entry_v2* buf2 = (struct logger_entry_v2*)buf; |
| 536 | if (buf2->hdr_size) { |
| 537 | if ((buf2->hdr_size < sizeof(((struct log_msg*)NULL)->entry_v1)) || |
| 538 | (buf2->hdr_size > sizeof(((struct log_msg*)NULL)->entry))) { |
| 539 | fprintf(stderr, "+++ LOG: entry illegal hdr_size\n"); |
| 540 | return -1; |
Mark Salyzyn | 64bf668 | 2013-12-18 12:59:01 -0800 | [diff] [blame] | 541 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 542 | msg = ((char*)buf2) + buf2->hdr_size; |
| 543 | if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) { |
| 544 | entry->uid = ((struct logger_entry_v4*)buf)->uid; |
| 545 | } |
| 546 | } |
| 547 | for (i = 1; i < buf->len; i++) { |
| 548 | if (msg[i] == '\0') { |
| 549 | if (msgStart == -1) { |
| 550 | msgStart = i + 1; |
| 551 | } else { |
| 552 | msgEnd = i; |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | if (msgStart == -1) { |
| 559 | /* +++ LOG: malformed log message, DYB */ |
Nick Kralevich | 2f2789a | 2011-10-18 15:23:33 -0700 | [diff] [blame] | 560 | for (i = 1; i < buf->len; i++) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 561 | /* odd characters in tag? */ |
| 562 | if ((msg[i] <= ' ') || (msg[i] == ':') || (msg[i] >= 0x7f)) { |
| 563 | msg[i] = '\0'; |
| 564 | msgStart = i + 1; |
| 565 | break; |
| 566 | } |
Nick Kralevich | 2f2789a | 2011-10-18 15:23:33 -0700 | [diff] [blame] | 567 | } |
Jeff Sharkey | 059ac70 | 2011-10-26 18:40:39 -0700 | [diff] [blame] | 568 | if (msgStart == -1) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 569 | msgStart = buf->len - 1; /* All tag, no message, print truncates */ |
Nick Kralevich | 32be08b | 2011-10-17 10:45:03 -0700 | [diff] [blame] | 570 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 571 | } |
| 572 | if (msgEnd == -1) { |
| 573 | /* incoming message not null-terminated; force it */ |
| 574 | msgEnd = buf->len - 1; /* may result in msgEnd < msgStart */ |
| 575 | msg[msgEnd] = '\0'; |
| 576 | } |
Jeff Sharkey | 059ac70 | 2011-10-26 18:40:39 -0700 | [diff] [blame] | 577 | |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 578 | entry->priority = static_cast<android_LogPriority>(msg[0]); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 579 | entry->tag = msg + 1; |
| 580 | entry->tagLen = msgStart - 1; |
| 581 | entry->message = msg + msgStart; |
| 582 | entry->messageLen = (msgEnd < msgStart) ? 0 : (msgEnd - msgStart); |
Nick Kralevich | 32be08b | 2011-10-17 10:45:03 -0700 | [diff] [blame] | 583 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 584 | return 0; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | /* |
Mark Salyzyn | bd8b890 | 2015-03-06 20:42:57 +0000 | [diff] [blame] | 588 | * Extract a 4-byte value from a byte stream. |
| 589 | */ |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 590 | static inline uint32_t get4LE(const uint8_t* src) { |
| 591 | return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24); |
Mark Salyzyn | bd8b890 | 2015-03-06 20:42:57 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /* |
| 595 | * Extract an 8-byte value from a byte stream. |
| 596 | */ |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 597 | static inline uint64_t get8LE(const uint8_t* src) { |
| 598 | uint32_t low, high; |
Mark Salyzyn | bd8b890 | 2015-03-06 20:42:57 +0000 | [diff] [blame] | 599 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 600 | low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24); |
| 601 | high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24); |
| 602 | return ((uint64_t)high << 32) | (uint64_t)low; |
Mark Salyzyn | bd8b890 | 2015-03-06 20:42:57 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Mark Salyzyn | 604679a | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 605 | static bool findChar(const char** cp, size_t* len, int c) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 606 | while ((*len) && isspace(*(*cp))) { |
| 607 | ++(*cp); |
| 608 | --(*len); |
| 609 | } |
| 610 | if (c == INT_MAX) return *len; |
| 611 | if ((*len) && (*(*cp) == c)) { |
| 612 | ++(*cp); |
| 613 | --(*len); |
| 614 | return true; |
| 615 | } |
| 616 | return false; |
Mark Salyzyn | 604679a | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 617 | } |
Mark Salyzyn | bd8b890 | 2015-03-06 20:42:57 +0000 | [diff] [blame] | 618 | |
| 619 | /* |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 620 | * Recursively convert binary log data to printable form. |
| 621 | * |
| 622 | * This needs to be recursive because you can have lists of lists. |
| 623 | * |
| 624 | * If we run out of room, we stop processing immediately. It's important |
| 625 | * for us to check for space on every output element to avoid producing |
| 626 | * garbled output. |
| 627 | * |
| 628 | * Returns 0 on success, 1 on buffer full, -1 on failure. |
| 629 | */ |
Mark Salyzyn | 604679a | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 630 | enum objectType { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 631 | TYPE_OBJECTS = '1', |
| 632 | TYPE_BYTES = '2', |
| 633 | TYPE_MILLISECONDS = '3', |
| 634 | TYPE_ALLOCATIONS = '4', |
| 635 | TYPE_ID = '5', |
Mark Salyzyn | 07ff212 | 2016-11-10 10:24:44 -0800 | [diff] [blame] | 636 | TYPE_PERCENT = '6', |
| 637 | TYPE_MONOTONIC = 's' |
Mark Salyzyn | 604679a | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 638 | }; |
| 639 | |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 640 | static int android_log_printBinaryEvent(const unsigned char** pEventData, size_t* pEventDataLen, |
| 641 | char** pOutBuf, size_t* pOutBufLen, const char** fmtStr, |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 642 | size_t* fmtLen) { |
| 643 | const unsigned char* eventData = *pEventData; |
| 644 | size_t eventDataLen = *pEventDataLen; |
| 645 | char* outBuf = *pOutBuf; |
| 646 | char* outBufSave = outBuf; |
| 647 | size_t outBufLen = *pOutBufLen; |
| 648 | size_t outBufLenSave = outBufLen; |
| 649 | unsigned char type; |
Mark Salyzyn | 07ff212 | 2016-11-10 10:24:44 -0800 | [diff] [blame] | 650 | size_t outCount = 0; |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 651 | int result = 0; |
| 652 | const char* cp; |
| 653 | size_t len; |
| 654 | int64_t lval; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 655 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 656 | if (eventDataLen < 1) return -1; |
Mark Salyzyn | 555064b | 2016-11-11 14:41:30 -0800 | [diff] [blame] | 657 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 658 | type = *eventData++; |
| 659 | eventDataLen--; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 660 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 661 | cp = NULL; |
| 662 | len = 0; |
| 663 | if (fmtStr && *fmtStr && fmtLen && *fmtLen && **fmtStr) { |
| 664 | cp = *fmtStr; |
| 665 | len = *fmtLen; |
| 666 | } |
| 667 | /* |
| 668 | * event.logtag format specification: |
| 669 | * |
| 670 | * Optionally, after the tag names can be put a description for the value(s) |
| 671 | * of the tag. Description are in the format |
| 672 | * (<name>|data type[|data unit]) |
| 673 | * Multiple values are separated by commas. |
| 674 | * |
| 675 | * The data type is a number from the following values: |
| 676 | * 1: int |
| 677 | * 2: long |
| 678 | * 3: string |
| 679 | * 4: list |
| 680 | * 5: float |
| 681 | * |
| 682 | * The data unit is a number taken from the following list: |
| 683 | * 1: Number of objects |
| 684 | * 2: Number of bytes |
| 685 | * 3: Number of milliseconds |
| 686 | * 4: Number of allocations |
| 687 | * 5: Id |
| 688 | * 6: Percent |
Mark Salyzyn | 07ff212 | 2016-11-10 10:24:44 -0800 | [diff] [blame] | 689 | * s: Number of seconds (monotonic time) |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 690 | * Default value for data of type int/long is 2 (bytes). |
| 691 | */ |
| 692 | if (!cp || !findChar(&cp, &len, '(')) { |
Mark Salyzyn | 604679a | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 693 | len = 0; |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 694 | } else { |
| 695 | char* outBufLastSpace = NULL; |
| 696 | |
| 697 | findChar(&cp, &len, INT_MAX); |
| 698 | while (len && *cp && (*cp != '|') && (*cp != ')')) { |
| 699 | if (outBufLen <= 0) { |
| 700 | /* halt output */ |
| 701 | goto no_room; |
| 702 | } |
| 703 | outBufLastSpace = isspace(*cp) ? outBuf : NULL; |
| 704 | *outBuf = *cp; |
| 705 | ++outBuf; |
| 706 | ++cp; |
| 707 | --outBufLen; |
| 708 | --len; |
Mark Salyzyn | 604679a | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 709 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 710 | if (outBufLastSpace) { |
| 711 | outBufLen += outBuf - outBufLastSpace; |
| 712 | outBuf = outBufLastSpace; |
| 713 | } |
| 714 | if (outBufLen <= 0) { |
| 715 | /* halt output */ |
| 716 | goto no_room; |
| 717 | } |
| 718 | if (outBufSave != outBuf) { |
| 719 | *outBuf = '='; |
| 720 | ++outBuf; |
| 721 | --outBufLen; |
| 722 | } |
| 723 | |
| 724 | if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) { |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 725 | static const unsigned char typeTable[] = {EVENT_TYPE_INT, EVENT_TYPE_LONG, EVENT_TYPE_STRING, |
| 726 | EVENT_TYPE_LIST, EVENT_TYPE_FLOAT}; |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 727 | |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 728 | if ((*cp >= '1') && (*cp < (char)('1' + (sizeof(typeTable) / sizeof(typeTable[0])))) && |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 729 | (type != typeTable[(size_t)(*cp - '1')])) |
Mark Salyzyn | 604679a | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 730 | len = 0; |
Mark Salyzyn | 604679a | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 731 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 732 | if (len) { |
| 733 | ++cp; |
| 734 | --len; |
| 735 | } else { |
| 736 | /* reset the format */ |
| 737 | outBuf = outBufSave; |
| 738 | outBufLen = outBufLenSave; |
| 739 | } |
Mark Salyzyn | 604679a | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 740 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 741 | } |
| 742 | outCount = 0; |
| 743 | lval = 0; |
| 744 | switch (type) { |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 745 | case EVENT_TYPE_INT: |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 746 | /* 32-bit signed int */ |
| 747 | { |
| 748 | int32_t ival; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 749 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 750 | if (eventDataLen < 4) return -1; |
| 751 | ival = get4LE(eventData); |
| 752 | eventData += 4; |
| 753 | eventDataLen -= 4; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 754 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 755 | lval = ival; |
| 756 | } |
| 757 | goto pr_lval; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 758 | case EVENT_TYPE_LONG: |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 759 | /* 64-bit signed long */ |
| 760 | if (eventDataLen < 8) return -1; |
| 761 | lval = get8LE(eventData); |
| 762 | eventData += 8; |
| 763 | eventDataLen -= 8; |
Mark Salyzyn | 604679a | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 764 | pr_lval: |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 765 | outCount = snprintf(outBuf, outBufLen, "%" PRId64, lval); |
| 766 | if (outCount < outBufLen) { |
| 767 | outBuf += outCount; |
| 768 | outBufLen -= outCount; |
| 769 | } else { |
| 770 | /* halt output */ |
| 771 | goto no_room; |
| 772 | } |
| 773 | break; |
Jeff Brown | 9cac7fc | 2015-04-28 12:47:02 -0700 | [diff] [blame] | 774 | case EVENT_TYPE_FLOAT: |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 775 | /* float */ |
| 776 | { |
| 777 | uint32_t ival; |
| 778 | float fval; |
Jeff Brown | 9cac7fc | 2015-04-28 12:47:02 -0700 | [diff] [blame] | 779 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 780 | if (eventDataLen < 4) return -1; |
| 781 | ival = get4LE(eventData); |
| 782 | fval = *(float*)&ival; |
| 783 | eventData += 4; |
| 784 | eventDataLen -= 4; |
Jeff Brown | 9cac7fc | 2015-04-28 12:47:02 -0700 | [diff] [blame] | 785 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 786 | outCount = snprintf(outBuf, outBufLen, "%f", fval); |
| 787 | if (outCount < outBufLen) { |
| 788 | outBuf += outCount; |
| 789 | outBufLen -= outCount; |
| 790 | } else { |
| 791 | /* halt output */ |
| 792 | goto no_room; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 793 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 794 | } |
| 795 | break; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 796 | case EVENT_TYPE_STRING: |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 797 | /* UTF-8 chars, not NULL-terminated */ |
| 798 | { |
| 799 | unsigned int strLen; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 800 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 801 | if (eventDataLen < 4) return -1; |
| 802 | strLen = get4LE(eventData); |
| 803 | eventData += 4; |
| 804 | eventDataLen -= 4; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 805 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 806 | if (eventDataLen < strLen) { |
| 807 | result = -1; /* mark truncated */ |
| 808 | strLen = eventDataLen; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 809 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 810 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 811 | if (cp && (strLen == 0)) { |
| 812 | /* reset the format if no content */ |
| 813 | outBuf = outBufSave; |
| 814 | outBufLen = outBufLenSave; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 815 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 816 | if (strLen < outBufLen) { |
| 817 | memcpy(outBuf, eventData, strLen); |
| 818 | outBuf += strLen; |
| 819 | outBufLen -= strLen; |
| 820 | } else { |
| 821 | if (outBufLen > 0) { |
| 822 | /* copy what we can */ |
| 823 | memcpy(outBuf, eventData, outBufLen); |
| 824 | outBuf += outBufLen; |
| 825 | outBufLen -= outBufLen; |
| 826 | } |
| 827 | if (!result) result = 1; /* if not truncated, return no room */ |
| 828 | } |
| 829 | eventData += strLen; |
| 830 | eventDataLen -= strLen; |
| 831 | if (result != 0) goto bail; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 832 | break; |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 833 | } |
| 834 | case EVENT_TYPE_LIST: |
| 835 | /* N items, all different types */ |
| 836 | { |
| 837 | unsigned char count; |
| 838 | int i; |
| 839 | |
| 840 | if (eventDataLen < 1) return -1; |
| 841 | |
| 842 | count = *eventData++; |
| 843 | eventDataLen--; |
| 844 | |
| 845 | if (outBufLen <= 0) goto no_room; |
| 846 | |
| 847 | *outBuf++ = '['; |
| 848 | outBufLen--; |
| 849 | |
| 850 | for (i = 0; i < count; i++) { |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 851 | result = android_log_printBinaryEvent(&eventData, &eventDataLen, &outBuf, &outBufLen, |
| 852 | fmtStr, fmtLen); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 853 | if (result != 0) goto bail; |
| 854 | |
| 855 | if (i < (count - 1)) { |
| 856 | if (outBufLen <= 0) goto no_room; |
| 857 | *outBuf++ = ','; |
| 858 | outBufLen--; |
| 859 | } |
Mark Salyzyn | 604679a | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 860 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 861 | |
| 862 | if (outBufLen <= 0) goto no_room; |
| 863 | |
| 864 | *outBuf++ = ']'; |
| 865 | outBufLen--; |
| 866 | } |
| 867 | break; |
| 868 | default: |
| 869 | fprintf(stderr, "Unknown binary event type %d\n", type); |
| 870 | return -1; |
| 871 | } |
| 872 | if (cp && len) { |
| 873 | if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) { |
| 874 | switch (*cp) { |
| 875 | case TYPE_OBJECTS: |
| 876 | outCount = 0; |
| 877 | /* outCount = snprintf(outBuf, outBufLen, " objects"); */ |
| 878 | break; |
| 879 | case TYPE_BYTES: |
| 880 | if ((lval != 0) && ((lval % 1024) == 0)) { |
| 881 | /* repaint with multiplier */ |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 882 | static const char suffixTable[] = {'K', 'M', 'G', 'T'}; |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 883 | size_t idx = 0; |
| 884 | outBuf -= outCount; |
| 885 | outBufLen += outCount; |
| 886 | do { |
| 887 | lval /= 1024; |
| 888 | if ((lval % 1024) != 0) break; |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 889 | } while (++idx < ((sizeof(suffixTable) / sizeof(suffixTable[0])) - 1)); |
| 890 | outCount = snprintf(outBuf, outBufLen, "%" PRId64 "%cB", lval, suffixTable[idx]); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 891 | } else { |
| 892 | outCount = snprintf(outBuf, outBufLen, "B"); |
| 893 | } |
| 894 | break; |
| 895 | case TYPE_MILLISECONDS: |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 896 | if (((lval <= -1000) || (1000 <= lval)) && (outBufLen || (outBuf[-1] == '0'))) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 897 | /* repaint as (fractional) seconds, possibly saving space */ |
| 898 | if (outBufLen) outBuf[0] = outBuf[-1]; |
| 899 | outBuf[-1] = outBuf[-2]; |
| 900 | outBuf[-2] = outBuf[-3]; |
| 901 | outBuf[-3] = '.'; |
| 902 | while ((outBufLen == 0) || (*outBuf == '0')) { |
| 903 | --outBuf; |
| 904 | ++outBufLen; |
| 905 | } |
| 906 | if (*outBuf != '.') { |
| 907 | ++outBuf; |
| 908 | --outBufLen; |
| 909 | } |
| 910 | outCount = snprintf(outBuf, outBufLen, "s"); |
| 911 | } else { |
| 912 | outCount = snprintf(outBuf, outBufLen, "ms"); |
| 913 | } |
| 914 | break; |
Mark Salyzyn | 07ff212 | 2016-11-10 10:24:44 -0800 | [diff] [blame] | 915 | case TYPE_MONOTONIC: { |
| 916 | static const uint64_t minute = 60; |
| 917 | static const uint64_t hour = 60 * minute; |
| 918 | static const uint64_t day = 24 * hour; |
| 919 | |
| 920 | /* Repaint as unsigned seconds, minutes, hours ... */ |
| 921 | outBuf -= outCount; |
| 922 | outBufLen += outCount; |
| 923 | uint64_t val = lval; |
| 924 | if (val >= day) { |
| 925 | outCount = snprintf(outBuf, outBufLen, "%" PRIu64 "d ", val / day); |
| 926 | if (outCount >= outBufLen) break; |
| 927 | outBuf += outCount; |
| 928 | outBufLen -= outCount; |
| 929 | val = (val % day) + day; |
| 930 | } |
| 931 | if (val >= minute) { |
| 932 | if (val >= hour) { |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 933 | outCount = snprintf(outBuf, outBufLen, "%" PRIu64 ":", (val / hour) % (day / hour)); |
Mark Salyzyn | 07ff212 | 2016-11-10 10:24:44 -0800 | [diff] [blame] | 934 | if (outCount >= outBufLen) break; |
| 935 | outBuf += outCount; |
| 936 | outBufLen -= outCount; |
| 937 | } |
| 938 | outCount = |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 939 | snprintf(outBuf, outBufLen, (val >= hour) ? "%02" PRIu64 ":" : "%" PRIu64 ":", |
Mark Salyzyn | 07ff212 | 2016-11-10 10:24:44 -0800 | [diff] [blame] | 940 | (val / minute) % (hour / minute)); |
| 941 | if (outCount >= outBufLen) break; |
| 942 | outBuf += outCount; |
| 943 | outBufLen -= outCount; |
| 944 | } |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 945 | outCount = snprintf(outBuf, outBufLen, (val >= minute) ? "%02" PRIu64 : "%" PRIu64 "s", |
Mark Salyzyn | 07ff212 | 2016-11-10 10:24:44 -0800 | [diff] [blame] | 946 | val % minute); |
| 947 | } break; |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 948 | case TYPE_ALLOCATIONS: |
| 949 | outCount = 0; |
| 950 | /* outCount = snprintf(outBuf, outBufLen, " allocations"); */ |
| 951 | break; |
| 952 | case TYPE_ID: |
| 953 | outCount = 0; |
| 954 | break; |
| 955 | case TYPE_PERCENT: |
| 956 | outCount = snprintf(outBuf, outBufLen, "%%"); |
| 957 | break; |
| 958 | default: /* ? */ |
| 959 | outCount = 0; |
| 960 | break; |
| 961 | } |
| 962 | ++cp; |
| 963 | --len; |
| 964 | if (outCount < outBufLen) { |
| 965 | outBuf += outCount; |
| 966 | outBufLen -= outCount; |
| 967 | } else if (outCount) { |
| 968 | /* halt output */ |
| 969 | goto no_room; |
| 970 | } |
Mark Salyzyn | 604679a | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 971 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 972 | if (!findChar(&cp, &len, ')')) len = 0; |
| 973 | if (!findChar(&cp, &len, ',')) len = 0; |
| 974 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 975 | |
| 976 | bail: |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 977 | *pEventData = eventData; |
| 978 | *pEventDataLen = eventDataLen; |
| 979 | *pOutBuf = outBuf; |
| 980 | *pOutBufLen = outBufLen; |
| 981 | if (cp) { |
| 982 | *fmtStr = cp; |
| 983 | *fmtLen = len; |
| 984 | } |
| 985 | return result; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 986 | |
| 987 | no_room: |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 988 | result = 1; |
| 989 | goto bail; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | /** |
| 993 | * Convert a binary log entry to ASCII form. |
| 994 | * |
| 995 | * For convenience we mimic the processLogBuffer API. There is no |
| 996 | * pre-defined output length for the binary data, since we're free to format |
| 997 | * it however we choose, which means we can't really use a fixed-size buffer |
| 998 | * here. |
| 999 | */ |
Mark Salyzyn | a5c2217 | 2016-03-10 08:25:33 -0800 | [diff] [blame] | 1000 | LIBLOG_ABI_PUBLIC int android_log_processBinaryLogBuffer( |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1001 | struct logger_entry* buf, AndroidLogEntry* entry, |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1002 | [[maybe_unused]] const EventTagMap* map, /* only on !__ANDROID__ */ |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1003 | char* messageBuf, int messageBufLen) { |
| 1004 | size_t inCount; |
| 1005 | uint32_t tagIndex; |
| 1006 | const unsigned char* eventData; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1007 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1008 | entry->message = NULL; |
| 1009 | entry->messageLen = 0; |
Mark Salyzyn | ed47c7b | 2017-03-02 07:47:21 -0800 | [diff] [blame] | 1010 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1011 | entry->tv_sec = buf->sec; |
| 1012 | entry->tv_nsec = buf->nsec; |
| 1013 | entry->priority = ANDROID_LOG_INFO; |
| 1014 | entry->uid = -1; |
| 1015 | entry->pid = buf->pid; |
| 1016 | entry->tid = buf->tid; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1017 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1018 | /* |
| 1019 | * Pull the tag out, fill in some additional details based on incoming |
| 1020 | * buffer version (v3 adds lid, v4 adds uid). |
| 1021 | */ |
| 1022 | eventData = (const unsigned char*)buf->msg; |
| 1023 | struct logger_entry_v2* buf2 = (struct logger_entry_v2*)buf; |
| 1024 | if (buf2->hdr_size) { |
| 1025 | if ((buf2->hdr_size < sizeof(((struct log_msg*)NULL)->entry_v1)) || |
| 1026 | (buf2->hdr_size > sizeof(((struct log_msg*)NULL)->entry))) { |
| 1027 | fprintf(stderr, "+++ LOG: entry illegal hdr_size\n"); |
| 1028 | return -1; |
| 1029 | } |
| 1030 | eventData = ((unsigned char*)buf2) + buf2->hdr_size; |
| 1031 | if ((buf2->hdr_size >= sizeof(struct logger_entry_v3)) && |
| 1032 | (((struct logger_entry_v3*)buf)->lid == LOG_ID_SECURITY)) { |
| 1033 | entry->priority = ANDROID_LOG_WARN; |
| 1034 | } |
| 1035 | if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) { |
| 1036 | entry->uid = ((struct logger_entry_v4*)buf)->uid; |
| 1037 | } |
| 1038 | } |
| 1039 | inCount = buf->len; |
| 1040 | if (inCount < 4) return -1; |
| 1041 | tagIndex = get4LE(eventData); |
| 1042 | eventData += 4; |
| 1043 | inCount -= 4; |
| 1044 | |
| 1045 | entry->tagLen = 0; |
| 1046 | entry->tag = NULL; |
| 1047 | #ifdef __ANDROID__ |
| 1048 | if (map != NULL) { |
| 1049 | entry->tag = android_lookupEventTag_len(map, &entry->tagLen, tagIndex); |
| 1050 | } |
| 1051 | #endif |
| 1052 | |
| 1053 | /* |
| 1054 | * If we don't have a map, or didn't find the tag number in the map, |
| 1055 | * stuff a generated tag value into the start of the output buffer and |
| 1056 | * shift the buffer pointers down. |
| 1057 | */ |
| 1058 | if (entry->tag == NULL) { |
| 1059 | size_t tagLen; |
| 1060 | |
| 1061 | tagLen = snprintf(messageBuf, messageBufLen, "[%" PRIu32 "]", tagIndex); |
| 1062 | if (tagLen >= (size_t)messageBufLen) { |
| 1063 | tagLen = messageBufLen - 1; |
| 1064 | } |
| 1065 | entry->tag = messageBuf; |
| 1066 | entry->tagLen = tagLen; |
| 1067 | messageBuf += tagLen + 1; |
| 1068 | messageBufLen -= tagLen + 1; |
| 1069 | } |
| 1070 | |
| 1071 | /* |
| 1072 | * Format the event log data into the buffer. |
| 1073 | */ |
| 1074 | const char* fmtStr = NULL; |
| 1075 | size_t fmtLen = 0; |
| 1076 | #ifdef __ANDROID__ |
| 1077 | if (descriptive_output && map) { |
| 1078 | fmtStr = android_lookupEventFormat_len(map, &fmtLen, tagIndex); |
| 1079 | } |
| 1080 | #endif |
| 1081 | |
| 1082 | char* outBuf = messageBuf; |
| 1083 | size_t outRemaining = messageBufLen - 1; /* leave one for nul byte */ |
| 1084 | int result = 0; |
| 1085 | |
| 1086 | if ((inCount > 0) || fmtLen) { |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1087 | result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, &outRemaining, &fmtStr, |
| 1088 | &fmtLen); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1089 | } |
| 1090 | if ((result == 1) && fmtStr) { |
| 1091 | /* We overflowed :-(, let's repaint the line w/o format dressings */ |
Mark Salyzyn | ed47c7b | 2017-03-02 07:47:21 -0800 | [diff] [blame] | 1092 | eventData = (const unsigned char*)buf->msg; |
Mark Salyzyn | 64bf668 | 2013-12-18 12:59:01 -0800 | [diff] [blame] | 1093 | if (buf2->hdr_size) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1094 | eventData = ((unsigned char*)buf2) + buf2->hdr_size; |
Mark Salyzyn | 64bf668 | 2013-12-18 12:59:01 -0800 | [diff] [blame] | 1095 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1096 | eventData += 4; |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1097 | outBuf = messageBuf; |
| 1098 | outRemaining = messageBufLen - 1; |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1099 | result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, &outRemaining, NULL, NULL); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1100 | } |
| 1101 | if (result < 0) { |
| 1102 | fprintf(stderr, "Binary log entry conversion failed\n"); |
| 1103 | } |
| 1104 | if (result) { |
| 1105 | if (!outRemaining) { |
| 1106 | /* make space to leave an indicator */ |
| 1107 | --outBuf; |
| 1108 | ++outRemaining; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1109 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1110 | *outBuf++ = (result < 0) ? '!' : '^'; /* Error or Truncation? */ |
| 1111 | outRemaining--; |
| 1112 | /* pretend we ate all the data to prevent log stutter */ |
| 1113 | inCount = 0; |
| 1114 | if (result > 0) result = 0; |
| 1115 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1116 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1117 | /* eat the silly terminating '\n' */ |
| 1118 | if (inCount == 1 && *eventData == '\n') { |
| 1119 | eventData++; |
| 1120 | inCount--; |
| 1121 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1122 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1123 | if (inCount != 0) { |
| 1124 | fprintf(stderr, "Warning: leftover binary log data (%zu bytes)\n", inCount); |
| 1125 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1126 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1127 | /* |
| 1128 | * Terminate the buffer. The NUL byte does not count as part of |
| 1129 | * entry->messageLen. |
| 1130 | */ |
| 1131 | *outBuf = '\0'; |
| 1132 | entry->messageLen = outBuf - messageBuf; |
| 1133 | assert(entry->messageLen == (messageBufLen - 1) - outRemaining); |
Mark Salyzyn | 555064b | 2016-11-11 14:41:30 -0800 | [diff] [blame] | 1134 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1135 | entry->message = messageBuf; |
Mark Salyzyn | 555064b | 2016-11-11 14:41:30 -0800 | [diff] [blame] | 1136 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1137 | return result; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1140 | /* |
| 1141 | * One utf8 character at a time |
| 1142 | * |
| 1143 | * Returns the length of the utf8 character in the buffer, |
| 1144 | * or -1 if illegal or truncated |
| 1145 | * |
| 1146 | * Open coded from libutils/Unicode.cpp, borrowed from utf8_length(), |
| 1147 | * can not remove from here because of library circular dependencies. |
| 1148 | * Expect one-day utf8_character_length with the same signature could |
| 1149 | * _also_ be part of libutils/Unicode.cpp if its usefullness needs to |
| 1150 | * propagate globally. |
| 1151 | */ |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1152 | LIBLOG_WEAK ssize_t utf8_character_length(const char* src, size_t len) { |
| 1153 | const char* cur = src; |
| 1154 | const char first_char = *cur++; |
| 1155 | static const uint32_t kUnicodeMaxCodepoint = 0x0010FFFF; |
| 1156 | int32_t mask, to_ignore_mask; |
| 1157 | size_t num_to_read; |
| 1158 | uint32_t utf32; |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1159 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1160 | if ((first_char & 0x80) == 0) { /* ASCII */ |
| 1161 | return first_char ? 1 : -1; |
| 1162 | } |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1163 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1164 | /* |
| 1165 | * (UTF-8's character must not be like 10xxxxxx, |
| 1166 | * but 110xxxxx, 1110xxxx, ... or 1111110x) |
| 1167 | */ |
| 1168 | if ((first_char & 0x40) == 0) { |
| 1169 | return -1; |
| 1170 | } |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1171 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1172 | for (utf32 = 1, num_to_read = 1, mask = 0x40, to_ignore_mask = 0x80; |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1173 | num_to_read < 5 && (first_char & mask); num_to_read++, to_ignore_mask |= mask, mask >>= 1) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1174 | if (num_to_read > len) { |
| 1175 | return -1; |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1176 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1177 | if ((*cur & 0xC0) != 0x80) { /* can not be 10xxxxxx? */ |
| 1178 | return -1; |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1179 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1180 | utf32 = (utf32 << 6) + (*cur++ & 0b00111111); |
| 1181 | } |
| 1182 | /* "first_char" must be (110xxxxx - 11110xxx) */ |
| 1183 | if (num_to_read >= 5) { |
| 1184 | return -1; |
| 1185 | } |
| 1186 | to_ignore_mask |= mask; |
| 1187 | utf32 |= ((~to_ignore_mask) & first_char) << (6 * (num_to_read - 1)); |
| 1188 | if (utf32 > kUnicodeMaxCodepoint) { |
| 1189 | return -1; |
| 1190 | } |
| 1191 | return num_to_read; |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | /* |
| 1195 | * Convert to printable from message to p buffer, return string length. If p is |
| 1196 | * NULL, do not copy, but still return the expected string length. |
| 1197 | */ |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1198 | static size_t convertPrintable(char* p, const char* message, size_t messageLen) { |
| 1199 | char* begin = p; |
| 1200 | bool print = p != NULL; |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1201 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1202 | while (messageLen) { |
| 1203 | char buf[6]; |
| 1204 | ssize_t len = sizeof(buf) - 1; |
| 1205 | if ((size_t)len > messageLen) { |
| 1206 | len = messageLen; |
| 1207 | } |
| 1208 | len = utf8_character_length(message, len); |
| 1209 | |
| 1210 | if (len < 0) { |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1211 | snprintf(buf, sizeof(buf), ((messageLen > 1) && isdigit(message[1])) ? "\\%03o" : "\\%o", |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1212 | *message & 0377); |
| 1213 | len = 1; |
| 1214 | } else { |
| 1215 | buf[0] = '\0'; |
| 1216 | if (len == 1) { |
| 1217 | if (*message == '\a') { |
| 1218 | strcpy(buf, "\\a"); |
| 1219 | } else if (*message == '\b') { |
| 1220 | strcpy(buf, "\\b"); |
| 1221 | } else if (*message == '\t') { |
| 1222 | strcpy(buf, "\t"); /* Do not escape tabs */ |
| 1223 | } else if (*message == '\v') { |
| 1224 | strcpy(buf, "\\v"); |
| 1225 | } else if (*message == '\f') { |
| 1226 | strcpy(buf, "\\f"); |
| 1227 | } else if (*message == '\r') { |
| 1228 | strcpy(buf, "\\r"); |
| 1229 | } else if (*message == '\\') { |
| 1230 | strcpy(buf, "\\\\"); |
| 1231 | } else if ((*message < ' ') || (*message & 0x80)) { |
| 1232 | snprintf(buf, sizeof(buf), "\\%o", *message & 0377); |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1233 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1234 | } |
| 1235 | if (!buf[0]) { |
| 1236 | strncpy(buf, message, len); |
| 1237 | buf[len] = '\0'; |
| 1238 | } |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1239 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1240 | if (print) { |
| 1241 | strcpy(p, buf); |
| 1242 | } |
| 1243 | p += strlen(buf); |
| 1244 | message += len; |
| 1245 | messageLen -= len; |
| 1246 | } |
| 1247 | return p - begin; |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1250 | static char* readSeconds(char* e, struct timespec* t) { |
| 1251 | unsigned long multiplier; |
| 1252 | char* p; |
| 1253 | t->tv_sec = strtoul(e, &p, 10); |
| 1254 | if (*p != '.') { |
| 1255 | return NULL; |
| 1256 | } |
| 1257 | t->tv_nsec = 0; |
| 1258 | multiplier = NS_PER_SEC; |
| 1259 | while (isdigit(*++p) && (multiplier /= 10)) { |
| 1260 | t->tv_nsec += (*p - '0') * multiplier; |
| 1261 | } |
| 1262 | return p; |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1265 | static struct timespec* sumTimespec(struct timespec* left, struct timespec* right) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1266 | left->tv_nsec += right->tv_nsec; |
| 1267 | left->tv_sec += right->tv_sec; |
| 1268 | if (left->tv_nsec >= (long)NS_PER_SEC) { |
| 1269 | left->tv_nsec -= NS_PER_SEC; |
| 1270 | left->tv_sec += 1; |
| 1271 | } |
| 1272 | return left; |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1275 | static struct timespec* subTimespec(struct timespec* result, struct timespec* left, |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1276 | struct timespec* right) { |
| 1277 | result->tv_nsec = left->tv_nsec - right->tv_nsec; |
| 1278 | result->tv_sec = left->tv_sec - right->tv_sec; |
| 1279 | if (result->tv_nsec < 0) { |
| 1280 | result->tv_nsec += NS_PER_SEC; |
| 1281 | result->tv_sec -= 1; |
| 1282 | } |
| 1283 | return result; |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1286 | static long long nsecTimespec(struct timespec* now) { |
| 1287 | return (long long)now->tv_sec * NS_PER_SEC + now->tv_nsec; |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
Mark Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 1290 | #ifdef __ANDROID__ |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1291 | static void convertMonotonic(struct timespec* result, const AndroidLogEntry* entry) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1292 | struct listnode* node; |
| 1293 | struct conversionList { |
| 1294 | struct listnode node; /* first */ |
| 1295 | struct timespec time; |
| 1296 | struct timespec convert; |
| 1297 | } * list, *next; |
| 1298 | struct timespec time, convert; |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1299 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1300 | /* If we do not have a conversion list, build one up */ |
| 1301 | if (list_empty(&convertHead)) { |
| 1302 | bool suspended_pending = false; |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1303 | struct timespec suspended_monotonic = {0, 0}; |
| 1304 | struct timespec suspended_diff = {0, 0}; |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1305 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1306 | /* |
| 1307 | * Read dmesg for _some_ synchronization markers and insert |
| 1308 | * Anything in the Android Logger before the dmesg logging span will |
| 1309 | * be highly suspect regarding the monotonic time calculations. |
| 1310 | */ |
| 1311 | FILE* p = popen("/system/bin/dmesg", "re"); |
| 1312 | if (p) { |
| 1313 | char* line = NULL; |
| 1314 | size_t len = 0; |
| 1315 | while (getline(&line, &len, p) > 0) { |
| 1316 | static const char suspend[] = "PM: suspend entry "; |
| 1317 | static const char resume[] = "PM: suspend exit "; |
| 1318 | static const char healthd[] = "healthd"; |
| 1319 | static const char battery[] = ": battery "; |
| 1320 | static const char suspended[] = "Suspended for "; |
| 1321 | struct timespec monotonic; |
| 1322 | struct tm tm; |
| 1323 | char *cp, *e = line; |
| 1324 | bool add_entry = true; |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1325 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1326 | if (*e == '<') { |
| 1327 | while (*e && (*e != '>')) { |
| 1328 | ++e; |
| 1329 | } |
| 1330 | if (*e != '>') { |
| 1331 | continue; |
| 1332 | } |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1333 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1334 | if (*e != '[') { |
| 1335 | continue; |
| 1336 | } |
| 1337 | while (*++e == ' ') { |
| 1338 | ; |
| 1339 | } |
| 1340 | e = readSeconds(e, &monotonic); |
| 1341 | if (!e || (*e != ']')) { |
| 1342 | continue; |
| 1343 | } |
| 1344 | |
| 1345 | if ((e = strstr(e, suspend))) { |
| 1346 | e += sizeof(suspend) - 1; |
| 1347 | } else if ((e = strstr(line, resume))) { |
| 1348 | e += sizeof(resume) - 1; |
| 1349 | } else if (((e = strstr(line, healthd))) && |
| 1350 | ((e = strstr(e + sizeof(healthd) - 1, battery)))) { |
| 1351 | /* NB: healthd is roughly 150us late, worth the price to |
| 1352 | * deal with ntp-induced or hardware clock drift. */ |
| 1353 | e += sizeof(battery) - 1; |
| 1354 | } else if ((e = strstr(line, suspended))) { |
| 1355 | e += sizeof(suspended) - 1; |
| 1356 | e = readSeconds(e, &time); |
| 1357 | if (!e) { |
| 1358 | continue; |
| 1359 | } |
| 1360 | add_entry = false; |
| 1361 | suspended_pending = true; |
| 1362 | suspended_monotonic = monotonic; |
| 1363 | suspended_diff = time; |
| 1364 | } else { |
| 1365 | continue; |
| 1366 | } |
| 1367 | if (add_entry) { |
| 1368 | /* look for "????-??-?? ??:??:??.????????? UTC" */ |
| 1369 | cp = strstr(e, " UTC"); |
| 1370 | if (!cp || ((cp - e) < 29) || (cp[-10] != '.')) { |
| 1371 | continue; |
| 1372 | } |
| 1373 | e = cp - 29; |
| 1374 | cp = readSeconds(cp - 10, &time); |
| 1375 | if (!cp) { |
| 1376 | continue; |
| 1377 | } |
| 1378 | cp = strptime(e, "%Y-%m-%d %H:%M:%S.", &tm); |
| 1379 | if (!cp) { |
| 1380 | continue; |
| 1381 | } |
| 1382 | cp = getenv(tz); |
| 1383 | if (cp) { |
| 1384 | cp = strdup(cp); |
| 1385 | } |
| 1386 | setenv(tz, utc, 1); |
| 1387 | time.tv_sec = mktime(&tm); |
| 1388 | if (cp) { |
| 1389 | setenv(tz, cp, 1); |
| 1390 | free(cp); |
| 1391 | } else { |
| 1392 | unsetenv(tz); |
| 1393 | } |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1394 | list = static_cast<conversionList*>(calloc(1, sizeof(conversionList))); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1395 | list_init(&list->node); |
| 1396 | list->time = time; |
| 1397 | subTimespec(&list->convert, &time, &monotonic); |
| 1398 | list_add_tail(&convertHead, &list->node); |
| 1399 | } |
| 1400 | if (suspended_pending && !list_empty(&convertHead)) { |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1401 | list = node_to_item(list_tail(&convertHead), struct conversionList, node); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1402 | if (subTimespec(&time, subTimespec(&time, &list->time, &list->convert), |
| 1403 | &suspended_monotonic) |
| 1404 | ->tv_sec > 0) { |
| 1405 | /* resume, what is convert factor before? */ |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1406 | subTimespec(&convert, &list->convert, &suspended_diff); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1407 | } else { |
| 1408 | /* suspend */ |
| 1409 | convert = list->convert; |
| 1410 | } |
| 1411 | time = suspended_monotonic; |
| 1412 | sumTimespec(&time, &convert); |
| 1413 | /* breakpoint just before sleep */ |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1414 | list = static_cast<conversionList*>(calloc(1, sizeof(conversionList))); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1415 | list_init(&list->node); |
| 1416 | list->time = time; |
| 1417 | list->convert = convert; |
| 1418 | list_add_tail(&convertHead, &list->node); |
| 1419 | /* breakpoint just after sleep */ |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1420 | list = static_cast<conversionList*>(calloc(1, sizeof(conversionList))); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1421 | list_init(&list->node); |
| 1422 | list->time = time; |
| 1423 | sumTimespec(&list->time, &suspended_diff); |
| 1424 | list->convert = convert; |
| 1425 | sumTimespec(&list->convert, &suspended_diff); |
| 1426 | list_add_tail(&convertHead, &list->node); |
| 1427 | suspended_pending = false; |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1428 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1429 | } |
| 1430 | pclose(p); |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1431 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1432 | /* last entry is our current time conversion */ |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1433 | list = static_cast<conversionList*>(calloc(1, sizeof(conversionList))); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1434 | list_init(&list->node); |
| 1435 | clock_gettime(CLOCK_REALTIME, &list->time); |
| 1436 | clock_gettime(CLOCK_MONOTONIC, &convert); |
| 1437 | clock_gettime(CLOCK_MONOTONIC, &time); |
| 1438 | /* Correct for instant clock_gettime latency (syscall or ~30ns) */ |
| 1439 | subTimespec(&time, &convert, subTimespec(&time, &time, &convert)); |
| 1440 | /* Calculate conversion factor */ |
| 1441 | subTimespec(&list->convert, &list->time, &time); |
| 1442 | list_add_tail(&convertHead, &list->node); |
| 1443 | if (suspended_pending) { |
| 1444 | /* manufacture a suspend @ point before */ |
| 1445 | subTimespec(&convert, &list->convert, &suspended_diff); |
| 1446 | time = suspended_monotonic; |
| 1447 | sumTimespec(&time, &convert); |
| 1448 | /* breakpoint just after sleep */ |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1449 | list = static_cast<conversionList*>(calloc(1, sizeof(conversionList))); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1450 | list_init(&list->node); |
| 1451 | list->time = time; |
| 1452 | sumTimespec(&list->time, &suspended_diff); |
| 1453 | list->convert = convert; |
| 1454 | sumTimespec(&list->convert, &suspended_diff); |
| 1455 | list_add_head(&convertHead, &list->node); |
| 1456 | /* breakpoint just before sleep */ |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1457 | list = static_cast<conversionList*>(calloc(1, sizeof(conversionList))); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1458 | list_init(&list->node); |
| 1459 | list->time = time; |
| 1460 | list->convert = convert; |
| 1461 | list_add_head(&convertHead, &list->node); |
| 1462 | } |
| 1463 | } |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1464 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1465 | /* Find the breakpoint in the conversion list */ |
| 1466 | list = node_to_item(list_head(&convertHead), struct conversionList, node); |
| 1467 | next = NULL; |
| 1468 | list_for_each(node, &convertHead) { |
| 1469 | next = node_to_item(node, struct conversionList, node); |
| 1470 | if (entry->tv_sec < next->time.tv_sec) { |
| 1471 | break; |
| 1472 | } else if (entry->tv_sec == next->time.tv_sec) { |
| 1473 | if (entry->tv_nsec < next->time.tv_nsec) { |
| 1474 | break; |
| 1475 | } |
| 1476 | } |
| 1477 | list = next; |
| 1478 | } |
| 1479 | |
| 1480 | /* blend time from one breakpoint to the next */ |
| 1481 | convert = list->convert; |
| 1482 | if (next) { |
| 1483 | unsigned long long total, run; |
| 1484 | |
| 1485 | total = nsecTimespec(subTimespec(&time, &next->time, &list->time)); |
| 1486 | time.tv_sec = entry->tv_sec; |
| 1487 | time.tv_nsec = entry->tv_nsec; |
| 1488 | run = nsecTimespec(subTimespec(&time, &time, &list->time)); |
| 1489 | if (run < total) { |
| 1490 | long long crun; |
| 1491 | |
| 1492 | float f = nsecTimespec(subTimespec(&time, &next->convert, &convert)); |
| 1493 | f *= run; |
| 1494 | f /= total; |
| 1495 | crun = f; |
| 1496 | convert.tv_sec += crun / (long long)NS_PER_SEC; |
| 1497 | if (crun < 0) { |
| 1498 | convert.tv_nsec -= (-crun) % NS_PER_SEC; |
| 1499 | if (convert.tv_nsec < 0) { |
| 1500 | convert.tv_nsec += NS_PER_SEC; |
| 1501 | convert.tv_sec -= 1; |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1502 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1503 | } else { |
| 1504 | convert.tv_nsec += crun % NS_PER_SEC; |
| 1505 | if (convert.tv_nsec >= (long)NS_PER_SEC) { |
| 1506 | convert.tv_nsec -= NS_PER_SEC; |
| 1507 | convert.tv_sec += 1; |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1508 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1509 | } |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1510 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1511 | } |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1512 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1513 | /* Apply the correction factor */ |
| 1514 | result->tv_sec = entry->tv_sec; |
| 1515 | result->tv_nsec = entry->tv_nsec; |
| 1516 | subTimespec(result, result, &convert); |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1517 | } |
Mark Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 1518 | #endif |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1519 | |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1520 | /** |
| 1521 | * Formats a log message into a buffer |
| 1522 | * |
| 1523 | * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer |
| 1524 | * If return value != defaultBuffer, caller must call free() |
| 1525 | * Returns NULL on malloc error |
| 1526 | */ |
| 1527 | |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1528 | LIBLOG_ABI_PUBLIC char* android_log_formatLogLine(AndroidLogFormat* p_format, char* defaultBuffer, |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1529 | size_t defaultBufferSize, |
| 1530 | const AndroidLogEntry* entry, |
| 1531 | size_t* p_outLength) { |
Yabin Cui | 945b83f | 2014-11-13 10:02:08 -0800 | [diff] [blame] | 1532 | #if !defined(_WIN32) |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1533 | struct tm tmBuf; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1534 | #endif |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1535 | struct tm* ptm; |
| 1536 | /* good margin, 23+nul for msec, 26+nul for usec, 29+nul to nsec */ |
| 1537 | char timeBuf[64]; |
| 1538 | char prefixBuf[128], suffixBuf[128]; |
| 1539 | char priChar; |
| 1540 | int prefixSuffixIsHeaderFooter = 0; |
| 1541 | char* ret; |
| 1542 | time_t now; |
| 1543 | unsigned long nsec; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1544 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1545 | priChar = filterPriToChar(entry->priority); |
| 1546 | size_t prefixLen = 0, suffixLen = 0; |
| 1547 | size_t len; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1548 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1549 | /* |
| 1550 | * Get the current date/time in pretty form |
| 1551 | * |
| 1552 | * It's often useful when examining a log with "less" to jump to |
| 1553 | * a specific point in the file by searching for the date/time stamp. |
| 1554 | * For this reason it's very annoying to have regexp meta characters |
| 1555 | * in the time stamp. Don't use forward slashes, parenthesis, |
| 1556 | * brackets, asterisks, or other special chars here. |
| 1557 | * |
| 1558 | * The caller may have affected the timezone environment, this is |
| 1559 | * expected to be sensitive to that. |
| 1560 | */ |
| 1561 | now = entry->tv_sec; |
| 1562 | nsec = entry->tv_nsec; |
Mark Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 1563 | #if __ANDROID__ |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1564 | if (p_format->monotonic_output) { |
| 1565 | /* prevent convertMonotonic from being called if logd is monotonic */ |
| 1566 | if (android_log_clockid() != CLOCK_MONOTONIC) { |
| 1567 | struct timespec time; |
| 1568 | convertMonotonic(&time, entry); |
| 1569 | now = time.tv_sec; |
| 1570 | nsec = time.tv_nsec; |
Mark Salyzyn | 4d0473f | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 1571 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1572 | } |
Mark Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 1573 | #endif |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1574 | if (now < 0) { |
| 1575 | nsec = NS_PER_SEC - nsec; |
| 1576 | } |
| 1577 | if (p_format->epoch_output || p_format->monotonic_output) { |
| 1578 | ptm = NULL; |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1579 | snprintf(timeBuf, sizeof(timeBuf), p_format->monotonic_output ? "%6lld" : "%19lld", |
| 1580 | (long long)now); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1581 | } else { |
Yabin Cui | 945b83f | 2014-11-13 10:02:08 -0800 | [diff] [blame] | 1582 | #if !defined(_WIN32) |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1583 | ptm = localtime_r(&now, &tmBuf); |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1584 | #else |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1585 | ptm = localtime(&now); |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1586 | #endif |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1587 | strftime(timeBuf, sizeof(timeBuf), &"%Y-%m-%d %H:%M:%S"[p_format->year_output ? 0 : 3], ptm); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1588 | } |
| 1589 | len = strlen(timeBuf); |
| 1590 | if (p_format->nsec_time_output) { |
| 1591 | len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%09ld", nsec); |
| 1592 | } else if (p_format->usec_time_output) { |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1593 | len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%06ld", nsec / US_PER_NSEC); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1594 | } else { |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1595 | len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%03ld", nsec / MS_PER_NSEC); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1596 | } |
| 1597 | if (p_format->zone_output && ptm) { |
| 1598 | strftime(timeBuf + len, sizeof(timeBuf) - len, " %z", ptm); |
| 1599 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1600 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1601 | /* |
| 1602 | * Construct a buffer containing the log header and log message. |
| 1603 | */ |
| 1604 | if (p_format->colored_output) { |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1605 | prefixLen = |
| 1606 | snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm", colorFromPri(entry->priority)); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1607 | prefixLen = MIN(prefixLen, sizeof(prefixBuf)); |
George Burgess IV | 3d8ca6b | 2018-05-15 18:56:36 -0700 | [diff] [blame] | 1608 | |
| 1609 | const char suffixContents[] = "\x1B[0m"; |
| 1610 | strcpy(suffixBuf, suffixContents); |
| 1611 | suffixLen = strlen(suffixContents); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1612 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1613 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1614 | char uid[16]; |
| 1615 | uid[0] = '\0'; |
| 1616 | if (p_format->uid_output) { |
| 1617 | if (entry->uid >= 0) { |
| 1618 | /* |
| 1619 | * This code is Android specific, bionic guarantees that |
| 1620 | * calls to non-reentrant getpwuid() are thread safe. |
| 1621 | */ |
Mark Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 1622 | #if !defined(__MINGW32__) |
| 1623 | #if (FAKE_LOG_DEVICE == 0) |
William Roberts | 6877c6c | 2016-04-08 12:13:17 -0700 | [diff] [blame] | 1624 | #ifndef __BIONIC__ |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1625 | #warning "This code assumes that getpwuid is thread safe, only true with Bionic!" |
William Roberts | 6877c6c | 2016-04-08 12:13:17 -0700 | [diff] [blame] | 1626 | #endif |
Mark Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 1627 | #endif |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1628 | struct passwd* pwd = getpwuid(entry->uid); |
| 1629 | if (pwd && (strlen(pwd->pw_name) <= 5)) { |
| 1630 | snprintf(uid, sizeof(uid), "%5s:", pwd->pw_name); |
| 1631 | } else |
Mark Salyzyn | 7ad9192 | 2016-03-08 16:18:26 -0800 | [diff] [blame] | 1632 | #endif |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1633 | { |
| 1634 | /* Not worth parsing package list, names all longer than 5 */ |
| 1635 | snprintf(uid, sizeof(uid), "%5d:", entry->uid); |
| 1636 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1637 | } else { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1638 | snprintf(uid, sizeof(uid), " "); |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1639 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | switch (p_format->format) { |
| 1643 | case FORMAT_TAG: |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1644 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c/%-8.*s: ", priChar, |
| 1645 | (int)entry->tagLen, entry->tag); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1646 | strcpy(suffixBuf + suffixLen, "\n"); |
| 1647 | ++suffixLen; |
| 1648 | break; |
| 1649 | case FORMAT_PROCESS: |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1650 | len = snprintf(suffixBuf + suffixLen, sizeof(suffixBuf) - suffixLen, " (%.*s)\n", |
| 1651 | (int)entry->tagLen, entry->tag); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1652 | suffixLen += MIN(len, sizeof(suffixBuf) - suffixLen); |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1653 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c(%s%5d) ", priChar, |
| 1654 | uid, entry->pid); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1655 | break; |
| 1656 | case FORMAT_THREAD: |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1657 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c(%s%5d:%5d) ", |
| 1658 | priChar, uid, entry->pid, entry->tid); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1659 | strcpy(suffixBuf + suffixLen, "\n"); |
| 1660 | ++suffixLen; |
| 1661 | break; |
| 1662 | case FORMAT_RAW: |
| 1663 | prefixBuf[prefixLen] = 0; |
| 1664 | len = 0; |
| 1665 | strcpy(suffixBuf + suffixLen, "\n"); |
| 1666 | ++suffixLen; |
| 1667 | break; |
| 1668 | case FORMAT_TIME: |
| 1669 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1670 | "%s %c/%-8.*s(%s%5d): ", timeBuf, priChar, (int)entry->tagLen, entry->tag, uid, |
| 1671 | entry->pid); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1672 | strcpy(suffixBuf + suffixLen, "\n"); |
| 1673 | ++suffixLen; |
| 1674 | break; |
| 1675 | case FORMAT_THREADTIME: |
| 1676 | ret = strchr(uid, ':'); |
| 1677 | if (ret) { |
| 1678 | *ret = ' '; |
| 1679 | } |
| 1680 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1681 | "%s %s%5d %5d %c %-8.*s: ", timeBuf, uid, entry->pid, entry->tid, priChar, |
| 1682 | (int)entry->tagLen, entry->tag); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1683 | strcpy(suffixBuf + suffixLen, "\n"); |
| 1684 | ++suffixLen; |
| 1685 | break; |
| 1686 | case FORMAT_LONG: |
| 1687 | len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1688 | "[ %s %s%5d:%5d %c/%-8.*s ]\n", timeBuf, uid, entry->pid, entry->tid, priChar, |
| 1689 | (int)entry->tagLen, entry->tag); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1690 | strcpy(suffixBuf + suffixLen, "\n\n"); |
| 1691 | suffixLen += 2; |
| 1692 | prefixSuffixIsHeaderFooter = 1; |
| 1693 | break; |
| 1694 | case FORMAT_BRIEF: |
| 1695 | default: |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1696 | len = |
| 1697 | snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, |
| 1698 | "%c/%-8.*s(%s%5d): ", priChar, (int)entry->tagLen, entry->tag, uid, entry->pid); |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1699 | strcpy(suffixBuf + suffixLen, "\n"); |
| 1700 | ++suffixLen; |
| 1701 | break; |
| 1702 | } |
| 1703 | |
| 1704 | /* snprintf has a weird return value. It returns what would have been |
| 1705 | * written given a large enough buffer. In the case that the prefix is |
| 1706 | * longer then our buffer(128), it messes up the calculations below |
| 1707 | * possibly causing heap corruption. To avoid this we double check and |
| 1708 | * set the length at the maximum (size minus null byte) |
| 1709 | */ |
| 1710 | prefixLen += len; |
| 1711 | if (prefixLen >= sizeof(prefixBuf)) { |
| 1712 | prefixLen = sizeof(prefixBuf) - 1; |
| 1713 | prefixBuf[sizeof(prefixBuf) - 1] = '\0'; |
| 1714 | } |
| 1715 | if (suffixLen >= sizeof(suffixBuf)) { |
| 1716 | suffixLen = sizeof(suffixBuf) - 1; |
| 1717 | suffixBuf[sizeof(suffixBuf) - 2] = '\n'; |
| 1718 | suffixBuf[sizeof(suffixBuf) - 1] = '\0'; |
| 1719 | } |
| 1720 | |
| 1721 | /* the following code is tragically unreadable */ |
| 1722 | |
| 1723 | size_t numLines; |
| 1724 | char* p; |
| 1725 | size_t bufferSize; |
| 1726 | const char* pm; |
| 1727 | |
| 1728 | if (prefixSuffixIsHeaderFooter) { |
| 1729 | /* we're just wrapping message with a header/footer */ |
| 1730 | numLines = 1; |
| 1731 | } else { |
| 1732 | pm = entry->message; |
| 1733 | numLines = 0; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1734 | |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1735 | /* |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1736 | * The line-end finding here must match the line-end finding |
| 1737 | * in for ( ... numLines...) loop below |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1738 | */ |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1739 | while (pm < (entry->message + entry->messageLen)) { |
| 1740 | if (*pm++ == '\n') numLines++; |
| 1741 | } |
| 1742 | /* plus one line for anything not newline-terminated at the end */ |
| 1743 | if (pm > entry->message && *(pm - 1) != '\n') numLines++; |
| 1744 | } |
| 1745 | |
| 1746 | /* |
| 1747 | * this is an upper bound--newlines in message may be counted |
| 1748 | * extraneously |
| 1749 | */ |
| 1750 | bufferSize = (numLines * (prefixLen + suffixLen)) + 1; |
| 1751 | if (p_format->printable_output) { |
| 1752 | /* Calculate extra length to convert non-printable to printable */ |
| 1753 | bufferSize += convertPrintable(NULL, entry->message, entry->messageLen); |
| 1754 | } else { |
| 1755 | bufferSize += entry->messageLen; |
| 1756 | } |
| 1757 | |
| 1758 | if (defaultBufferSize >= bufferSize) { |
| 1759 | ret = defaultBuffer; |
| 1760 | } else { |
| 1761 | ret = (char*)malloc(bufferSize); |
| 1762 | |
| 1763 | if (ret == NULL) { |
| 1764 | return ret; |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | ret[0] = '\0'; /* to start strcat off */ |
| 1769 | |
| 1770 | p = ret; |
| 1771 | pm = entry->message; |
| 1772 | |
| 1773 | if (prefixSuffixIsHeaderFooter) { |
| 1774 | strcat(p, prefixBuf); |
| 1775 | p += prefixLen; |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1776 | if (p_format->printable_output) { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1777 | p += convertPrintable(p, entry->message, entry->messageLen); |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1778 | } else { |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1779 | strncat(p, entry->message, entry->messageLen); |
| 1780 | p += entry->messageLen; |
Mark Salyzyn | 1ebdb35 | 2015-05-15 09:01:58 -0700 | [diff] [blame] | 1781 | } |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1782 | strcat(p, suffixBuf); |
| 1783 | p += suffixLen; |
| 1784 | } else { |
| 1785 | do { |
| 1786 | const char* lineStart; |
| 1787 | size_t lineLen; |
| 1788 | lineStart = pm; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1789 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1790 | /* Find the next end-of-line in message */ |
| 1791 | while (pm < (entry->message + entry->messageLen) && *pm != '\n') pm++; |
| 1792 | lineLen = pm - lineStart; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1793 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1794 | strcat(p, prefixBuf); |
| 1795 | p += prefixLen; |
| 1796 | if (p_format->printable_output) { |
| 1797 | p += convertPrintable(p, lineStart, lineLen); |
| 1798 | } else { |
| 1799 | strncat(p, lineStart, lineLen); |
| 1800 | p += lineLen; |
| 1801 | } |
| 1802 | strcat(p, suffixBuf); |
| 1803 | p += suffixLen; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1804 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1805 | if (*pm == '\n') pm++; |
| 1806 | } while (pm < (entry->message + entry->messageLen)); |
| 1807 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1808 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1809 | if (p_outLength != NULL) { |
| 1810 | *p_outLength = p - ret; |
| 1811 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1812 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1813 | return ret; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | /** |
| 1817 | * Either print or do not print log line, based on filter |
| 1818 | * |
| 1819 | * Returns count bytes written |
| 1820 | */ |
| 1821 | |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1822 | LIBLOG_ABI_PUBLIC int android_log_printLogLine(AndroidLogFormat* p_format, int fd, |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1823 | const AndroidLogEntry* entry) { |
| 1824 | int ret; |
| 1825 | char defaultBuffer[512]; |
| 1826 | char* outBuffer = NULL; |
| 1827 | size_t totalLen; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1828 | |
Tom Cherry | f623f02 | 2019-01-10 10:37:36 -0800 | [diff] [blame^] | 1829 | outBuffer = |
| 1830 | android_log_formatLogLine(p_format, defaultBuffer, sizeof(defaultBuffer), entry, &totalLen); |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1831 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1832 | if (!outBuffer) return -1; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1833 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1834 | do { |
| 1835 | ret = write(fd, outBuffer, totalLen); |
| 1836 | } while (ret < 0 && errno == EINTR); |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1837 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1838 | if (ret < 0) { |
| 1839 | fprintf(stderr, "+++ LOG: write failed (errno=%d)\n", errno); |
| 1840 | ret = 0; |
| 1841 | goto done; |
| 1842 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1843 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1844 | if (((size_t)ret) < totalLen) { |
| 1845 | fprintf(stderr, "+++ LOG: write partial (%d of %d)\n", ret, (int)totalLen); |
| 1846 | goto done; |
| 1847 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1848 | |
| 1849 | done: |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1850 | if (outBuffer != defaultBuffer) { |
| 1851 | free(outBuffer); |
| 1852 | } |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1853 | |
Mark Salyzyn | 6e31568 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 1854 | return ret; |
The Android Open Source Project | 32315d4 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1855 | } |