Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 17 | #if defined(_WIN32) |
Spencer Low | 4759052 | 2015-05-19 22:12:06 -0700 | [diff] [blame] | 18 | #include <windows.h> |
| 19 | #endif |
| 20 | |
Elliott Hughes | b635162 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 21 | #include "android-base/logging.h" |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 22 | |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 23 | #include <fcntl.h> |
Josh Gao | ef102be | 2018-03-16 14:25:42 -0700 | [diff] [blame] | 24 | #include <inttypes.h> |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 25 | #include <libgen.h> |
Elliott Hughes | 6522eb5 | 2016-06-21 14:25:44 -0700 | [diff] [blame] | 26 | #include <time.h> |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 27 | |
| 28 | // For getprogname(3) or program_invocation_short_name. |
| 29 | #if defined(__ANDROID__) || defined(__APPLE__) |
| 30 | #include <stdlib.h> |
| 31 | #elif defined(__GLIBC__) |
| 32 | #include <errno.h> |
| 33 | #endif |
| 34 | |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 35 | #if defined(__linux__) |
| 36 | #include <sys/uio.h> |
| 37 | #endif |
| 38 | |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 39 | #include <atomic> |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 40 | #include <iostream> |
| 41 | #include <limits> |
Josh Gao | b08de45 | 2016-09-13 14:57:12 -0700 | [diff] [blame] | 42 | #include <mutex> |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 43 | #include <optional> |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 44 | #include <sstream> |
| 45 | #include <string> |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 46 | #include <utility> |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 47 | #include <vector> |
| 48 | |
Andreas Gampe | ed91707 | 2018-02-15 11:40:30 -0800 | [diff] [blame] | 49 | #include <android/log.h> |
Tom Cherry | 17e8580 | 2020-01-08 13:41:56 -0800 | [diff] [blame] | 50 | #ifdef __ANDROID__ |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 51 | #include <android/set_abort_message.h> |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 52 | #else |
| 53 | #include <sys/types.h> |
| 54 | #include <unistd.h> |
| 55 | #endif |
| 56 | |
Elliott Hughes | 397961e | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 57 | #include <android-base/file.h> |
Mark Salyzyn | 5cbb251 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 58 | #include <android-base/macros.h> |
Mark Salyzyn | 504d8ed | 2018-04-06 09:40:26 -0700 | [diff] [blame] | 59 | #include <android-base/parseint.h> |
Mark Salyzyn | 5cbb251 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 60 | #include <android-base/strings.h> |
Josh Gao | ef102be | 2018-03-16 14:25:42 -0700 | [diff] [blame] | 61 | #include <android-base/threads.h> |
Elliott Hughes | 774d7f6 | 2015-11-11 18:02:29 +0000 | [diff] [blame] | 62 | |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 63 | #include "liblog_symbols.h" |
Tom Cherry | a687242 | 2020-04-17 13:05:11 -0700 | [diff] [blame] | 64 | #include "logging_splitters.h" |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 65 | |
Elliott Hughes | 462a45e | 2018-06-06 12:54:41 -0700 | [diff] [blame] | 66 | namespace android { |
| 67 | namespace base { |
| 68 | |
| 69 | // BSD-based systems like Android/macOS have getprogname(). Others need us to provide one. |
| 70 | #if defined(__GLIBC__) || defined(_WIN32) |
| 71 | static const char* getprogname() { |
Dan Albert | dc15ffd | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 72 | #if defined(__GLIBC__) |
Dan Albert | dc15ffd | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 73 | return program_invocation_short_name; |
Josh Gao | b08de45 | 2016-09-13 14:57:12 -0700 | [diff] [blame] | 74 | #elif defined(_WIN32) |
Dan Albert | dc15ffd | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 75 | static bool first = true; |
| 76 | static char progname[MAX_PATH] = {}; |
| 77 | |
| 78 | if (first) { |
Elliott Hughes | 397961e | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 79 | snprintf(progname, sizeof(progname), "%s", |
| 80 | android::base::Basename(android::base::GetExecutablePath()).c_str()); |
Dan Albert | dc15ffd | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 81 | first = false; |
| 82 | } |
| 83 | |
| 84 | return progname; |
Elliott Hughes | 462a45e | 2018-06-06 12:54:41 -0700 | [diff] [blame] | 85 | #endif |
Dan Albert | dc15ffd | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 86 | } |
Dan Albert | dc15ffd | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 87 | #endif |
Mark Salyzyn | 504d8ed | 2018-04-06 09:40:26 -0700 | [diff] [blame] | 88 | |
Elliott Hughes | 462a45e | 2018-06-06 12:54:41 -0700 | [diff] [blame] | 89 | static const char* GetFileBasename(const char* file) { |
| 90 | // We can't use basename(3) even on Unix because the Mac doesn't |
| 91 | // have a non-modifying basename. |
| 92 | const char* last_slash = strrchr(file, '/'); |
| 93 | if (last_slash != nullptr) { |
| 94 | return last_slash + 1; |
| 95 | } |
| 96 | #if defined(_WIN32) |
| 97 | const char* last_backslash = strrchr(file, '\\'); |
| 98 | if (last_backslash != nullptr) { |
| 99 | return last_backslash + 1; |
| 100 | } |
| 101 | #endif |
| 102 | return file; |
| 103 | } |
| 104 | |
Mark Salyzyn | 504d8ed | 2018-04-06 09:40:26 -0700 | [diff] [blame] | 105 | #if defined(__linux__) |
Elliott Hughes | 462a45e | 2018-06-06 12:54:41 -0700 | [diff] [blame] | 106 | static int OpenKmsg() { |
Mark Salyzyn | 504d8ed | 2018-04-06 09:40:26 -0700 | [diff] [blame] | 107 | #if defined(__ANDROID__) |
| 108 | // pick up 'file w /dev/kmsg' environment from daemon's init rc file |
| 109 | const auto val = getenv("ANDROID_FILE__dev_kmsg"); |
| 110 | if (val != nullptr) { |
| 111 | int fd; |
| 112 | if (android::base::ParseInt(val, &fd, 0)) { |
| 113 | auto flags = fcntl(fd, F_GETFL); |
| 114 | if ((flags != -1) && ((flags & O_ACCMODE) == O_WRONLY)) return fd; |
| 115 | } |
| 116 | } |
| 117 | #endif |
| 118 | return TEMP_FAILURE_RETRY(open("/dev/kmsg", O_WRONLY | O_CLOEXEC)); |
| 119 | } |
| 120 | #endif |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 121 | |
Tom Cherry | 784ba9a | 2020-03-12 11:07:07 -0700 | [diff] [blame] | 122 | static LogId log_id_tToLogId(int32_t buffer_id) { |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 123 | switch (buffer_id) { |
| 124 | case LOG_ID_MAIN: |
| 125 | return MAIN; |
| 126 | case LOG_ID_SYSTEM: |
| 127 | return SYSTEM; |
| 128 | case LOG_ID_RADIO: |
| 129 | return RADIO; |
| 130 | case LOG_ID_CRASH: |
| 131 | return CRASH; |
| 132 | case LOG_ID_DEFAULT: |
| 133 | default: |
| 134 | return DEFAULT; |
| 135 | } |
| 136 | } |
| 137 | |
Tom Cherry | 784ba9a | 2020-03-12 11:07:07 -0700 | [diff] [blame] | 138 | static int32_t LogIdTolog_id_t(LogId log_id) { |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 139 | switch (log_id) { |
| 140 | case MAIN: |
| 141 | return LOG_ID_MAIN; |
| 142 | case SYSTEM: |
| 143 | return LOG_ID_SYSTEM; |
| 144 | case RADIO: |
| 145 | return LOG_ID_RADIO; |
| 146 | case CRASH: |
| 147 | return LOG_ID_CRASH; |
| 148 | case DEFAULT: |
| 149 | default: |
| 150 | return LOG_ID_DEFAULT; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | static LogSeverity PriorityToLogSeverity(int priority) { |
| 155 | switch (priority) { |
Tom Cherry | 4916e09 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 156 | case ANDROID_LOG_DEFAULT: |
| 157 | return INFO; |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 158 | case ANDROID_LOG_VERBOSE: |
| 159 | return VERBOSE; |
| 160 | case ANDROID_LOG_DEBUG: |
| 161 | return DEBUG; |
| 162 | case ANDROID_LOG_INFO: |
| 163 | return INFO; |
| 164 | case ANDROID_LOG_WARN: |
| 165 | return WARNING; |
| 166 | case ANDROID_LOG_ERROR: |
| 167 | return ERROR; |
| 168 | case ANDROID_LOG_FATAL: |
| 169 | return FATAL; |
| 170 | default: |
| 171 | return FATAL; |
| 172 | } |
| 173 | } |
| 174 | |
Tom Cherry | 784ba9a | 2020-03-12 11:07:07 -0700 | [diff] [blame] | 175 | static int32_t LogSeverityToPriority(LogSeverity severity) { |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 176 | switch (severity) { |
| 177 | case VERBOSE: |
| 178 | return ANDROID_LOG_VERBOSE; |
| 179 | case DEBUG: |
| 180 | return ANDROID_LOG_DEBUG; |
| 181 | case INFO: |
| 182 | return ANDROID_LOG_INFO; |
| 183 | case WARNING: |
| 184 | return ANDROID_LOG_WARN; |
| 185 | case ERROR: |
| 186 | return ANDROID_LOG_ERROR; |
| 187 | case FATAL_WITHOUT_ABORT: |
| 188 | case FATAL: |
| 189 | default: |
| 190 | return ANDROID_LOG_FATAL; |
| 191 | } |
| 192 | } |
| 193 | |
Yabin Cui | 7ff958a | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 194 | static LogFunction& Logger() { |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 195 | #ifdef __ANDROID__ |
Yabin Cui | 7ff958a | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 196 | static auto& logger = *new LogFunction(LogdLogger()); |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 197 | #else |
Yabin Cui | 7ff958a | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 198 | static auto& logger = *new LogFunction(StderrLogger); |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 199 | #endif |
Yabin Cui | 7ff958a | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 200 | return logger; |
| 201 | } |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 202 | |
Yabin Cui | 7ff958a | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 203 | static AbortFunction& Aborter() { |
| 204 | static auto& aborter = *new AbortFunction(DefaultAborter); |
| 205 | return aborter; |
| 206 | } |
| 207 | |
Tom Cherry | dbc07ce | 2020-01-22 07:48:42 -0800 | [diff] [blame] | 208 | // Only used for Q fallback. |
Andreas Gampe | fec5b09 | 2018-03-05 10:00:19 -0800 | [diff] [blame] | 209 | static std::recursive_mutex& TagLock() { |
| 210 | static auto& tag_lock = *new std::recursive_mutex(); |
| 211 | return tag_lock; |
| 212 | } |
Tom Cherry | dbc07ce | 2020-01-22 07:48:42 -0800 | [diff] [blame] | 213 | // Only used for Q fallback. |
Andreas Gampe | fec5b09 | 2018-03-05 10:00:19 -0800 | [diff] [blame] | 214 | static std::string* gDefaultTag; |
Tom Cherry | dbc07ce | 2020-01-22 07:48:42 -0800 | [diff] [blame] | 215 | |
Andreas Gampe | fec5b09 | 2018-03-05 10:00:19 -0800 | [diff] [blame] | 216 | void SetDefaultTag(const std::string& tag) { |
Tom Cherry | dbc07ce | 2020-01-22 07:48:42 -0800 | [diff] [blame] | 217 | static auto& liblog_functions = GetLibLogFunctions(); |
| 218 | if (liblog_functions) { |
| 219 | liblog_functions->__android_log_set_default_tag(tag.c_str()); |
| 220 | } else { |
| 221 | std::lock_guard<std::recursive_mutex> lock(TagLock()); |
| 222 | if (gDefaultTag != nullptr) { |
| 223 | delete gDefaultTag; |
| 224 | gDefaultTag = nullptr; |
| 225 | } |
| 226 | if (!tag.empty()) { |
| 227 | gDefaultTag = new std::string(tag); |
| 228 | } |
Andreas Gampe | fec5b09 | 2018-03-05 10:00:19 -0800 | [diff] [blame] | 229 | } |
Yabin Cui | 7ff958a | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 230 | } |
Andreas Gampe | 9008e8d | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 231 | |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 232 | static bool gInitialized = false; |
Tom Cherry | 4916e09 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 233 | |
| 234 | // Only used for Q fallback. |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 235 | static LogSeverity gMinimumLogSeverity = INFO; |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 236 | |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 237 | #if defined(__linux__) |
Tom Cherry | a687242 | 2020-04-17 13:05:11 -0700 | [diff] [blame] | 238 | static void KernelLogLine(const char* msg, int length, android::base::LogSeverity severity, |
| 239 | const char* tag) { |
Andreas Gampe | d2a4f21 | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 240 | // clang-format off |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 241 | static constexpr int kLogSeverityToKernelLogLevel[] = { |
Andreas Gampe | d2a4f21 | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 242 | [android::base::VERBOSE] = 7, // KERN_DEBUG (there is no verbose kernel log |
| 243 | // level) |
| 244 | [android::base::DEBUG] = 7, // KERN_DEBUG |
| 245 | [android::base::INFO] = 6, // KERN_INFO |
| 246 | [android::base::WARNING] = 4, // KERN_WARNING |
| 247 | [android::base::ERROR] = 3, // KERN_ERROR |
| 248 | [android::base::FATAL_WITHOUT_ABORT] = 2, // KERN_CRIT |
| 249 | [android::base::FATAL] = 2, // KERN_CRIT |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 250 | }; |
Andreas Gampe | d2a4f21 | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 251 | // clang-format on |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 252 | static_assert(arraysize(kLogSeverityToKernelLogLevel) == android::base::FATAL + 1, |
| 253 | "Mismatch in size of kLogSeverityToKernelLogLevel and values in LogSeverity"); |
| 254 | |
Mark Salyzyn | 504d8ed | 2018-04-06 09:40:26 -0700 | [diff] [blame] | 255 | static int klog_fd = OpenKmsg(); |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 256 | if (klog_fd == -1) return; |
| 257 | |
| 258 | int level = kLogSeverityToKernelLogLevel[severity]; |
| 259 | |
| 260 | // The kernel's printk buffer is only 1024 bytes. |
| 261 | // TODO: should we automatically break up long lines into multiple lines? |
| 262 | // Or we could log but with something like "..." at the end? |
Elliott Hughes | 1888033 | 2020-04-29 14:10:12 -0700 | [diff] [blame] | 263 | char buf[1024] __attribute__((__uninitialized__)); |
Tom Cherry | a687242 | 2020-04-17 13:05:11 -0700 | [diff] [blame] | 264 | size_t size = snprintf(buf, sizeof(buf), "<%d>%s: %.*s\n", level, tag, length, msg); |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 265 | if (size > sizeof(buf)) { |
| 266 | size = snprintf(buf, sizeof(buf), "<%d>%s: %zu-byte message too long for printk\n", |
| 267 | level, tag, size); |
| 268 | } |
| 269 | |
| 270 | iovec iov[1]; |
| 271 | iov[0].iov_base = buf; |
| 272 | iov[0].iov_len = size; |
| 273 | TEMP_FAILURE_RETRY(writev(klog_fd, iov, 1)); |
| 274 | } |
Tom Cherry | a687242 | 2020-04-17 13:05:11 -0700 | [diff] [blame] | 275 | |
| 276 | void KernelLogger(android::base::LogId, android::base::LogSeverity severity, const char* tag, |
| 277 | const char*, unsigned int, const char* full_message) { |
| 278 | SplitByLines(full_message, KernelLogLine, severity, tag); |
| 279 | } |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 280 | #endif |
| 281 | |
Tomasz Wasilczyk | a419de2 | 2017-12-18 06:30:17 -0800 | [diff] [blame] | 282 | void StderrLogger(LogId, LogSeverity severity, const char* tag, const char* file, unsigned int line, |
| 283 | const char* message) { |
Elliott Hughes | 6522eb5 | 2016-06-21 14:25:44 -0700 | [diff] [blame] | 284 | struct tm now; |
| 285 | time_t t = time(nullptr); |
| 286 | |
| 287 | #if defined(_WIN32) |
| 288 | localtime_s(&now, &t); |
| 289 | #else |
| 290 | localtime_r(&t, &now); |
| 291 | #endif |
Tom Cherry | a687242 | 2020-04-17 13:05:11 -0700 | [diff] [blame] | 292 | auto output_string = |
| 293 | StderrOutputGenerator(now, getpid(), GetThreadId(), severity, tag, file, line, message); |
Elliott Hughes | 6522eb5 | 2016-06-21 14:25:44 -0700 | [diff] [blame] | 294 | |
Tom Cherry | a687242 | 2020-04-17 13:05:11 -0700 | [diff] [blame] | 295 | fputs(output_string.c_str(), stderr); |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Elliott Hughes | 1c1409f | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 298 | void StdioLogger(LogId, LogSeverity severity, const char* /*tag*/, const char* /*file*/, |
| 299 | unsigned int /*line*/, const char* message) { |
| 300 | if (severity >= WARNING) { |
| 301 | fflush(stdout); |
Elliott Hughes | 462a45e | 2018-06-06 12:54:41 -0700 | [diff] [blame] | 302 | fprintf(stderr, "%s: %s\n", GetFileBasename(getprogname()), message); |
Elliott Hughes | 1c1409f | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 303 | } else { |
| 304 | fprintf(stdout, "%s\n", message); |
| 305 | } |
| 306 | } |
| 307 | |
Andreas Gampe | 9008e8d | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 308 | void DefaultAborter(const char* abort_message) { |
| 309 | #ifdef __ANDROID__ |
| 310 | android_set_abort_message(abort_message); |
| 311 | #else |
| 312 | UNUSED(abort_message); |
| 313 | #endif |
| 314 | abort(); |
| 315 | } |
| 316 | |
Tom Cherry | a687242 | 2020-04-17 13:05:11 -0700 | [diff] [blame] | 317 | static void LogdLogChunk(LogId id, LogSeverity severity, const char* tag, const char* message) { |
Tom Cherry | 784ba9a | 2020-03-12 11:07:07 -0700 | [diff] [blame] | 318 | int32_t lg_id = LogIdTolog_id_t(id); |
Tom Cherry | a687242 | 2020-04-17 13:05:11 -0700 | [diff] [blame] | 319 | int32_t priority = LogSeverityToPriority(severity); |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 320 | |
| 321 | static auto& liblog_functions = GetLibLogFunctions(); |
| 322 | if (liblog_functions) { |
Tom Cherry | 228eff1 | 2020-03-11 11:07:13 -0700 | [diff] [blame] | 323 | __android_log_message log_message = {sizeof(__android_log_message), lg_id, priority, tag, |
| 324 | static_cast<const char*>(nullptr), 0, message}; |
| 325 | liblog_functions->__android_log_logd_logger(&log_message); |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 326 | } else { |
| 327 | __android_log_buf_print(lg_id, priority, tag, "%s", message); |
| 328 | } |
| 329 | } |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 330 | |
Tom Cherry | a687242 | 2020-04-17 13:05:11 -0700 | [diff] [blame] | 331 | LogdLogger::LogdLogger(LogId default_log_id) : default_log_id_(default_log_id) {} |
| 332 | |
| 333 | void LogdLogger::operator()(LogId id, LogSeverity severity, const char* tag, const char* file, |
| 334 | unsigned int line, const char* message) { |
| 335 | if (id == DEFAULT) { |
| 336 | id = default_log_id_; |
| 337 | } |
| 338 | |
| 339 | SplitByLogdChunks(id, severity, tag, file, line, message, LogdLogChunk); |
| 340 | } |
| 341 | |
Andreas Gampe | 9008e8d | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 342 | void InitLogging(char* argv[], LogFunction&& logger, AbortFunction&& aborter) { |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 343 | SetLogger(std::forward<LogFunction>(logger)); |
Andreas Gampe | 9008e8d | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 344 | SetAborter(std::forward<AbortFunction>(aborter)); |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 345 | |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 346 | if (gInitialized) { |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 347 | return; |
| 348 | } |
| 349 | |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 350 | gInitialized = true; |
| 351 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 352 | // Stash the command line for later use. We can use /proc/self/cmdline on |
Spencer Low | bec7862 | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 353 | // Linux to recover this, but we don't have that luxury on the Mac/Windows, |
| 354 | // and there are a couple of argv[0] variants that are commonly used. |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 355 | if (argv != nullptr) { |
Andreas Gampe | fec5b09 | 2018-03-05 10:00:19 -0800 | [diff] [blame] | 356 | SetDefaultTag(basename(argv[0])); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 357 | } |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 358 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 359 | const char* tags = getenv("ANDROID_LOG_TAGS"); |
| 360 | if (tags == nullptr) { |
| 361 | return; |
| 362 | } |
| 363 | |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 364 | std::vector<std::string> specs = Split(tags, " "); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 365 | for (size_t i = 0; i < specs.size(); ++i) { |
| 366 | // "tag-pattern:[vdiwefs]" |
| 367 | std::string spec(specs[i]); |
| 368 | if (spec.size() == 3 && StartsWith(spec, "*:")) { |
| 369 | switch (spec[2]) { |
| 370 | case 'v': |
Tom Cherry | 4916e09 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 371 | SetMinimumLogSeverity(VERBOSE); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 372 | continue; |
| 373 | case 'd': |
Tom Cherry | 4916e09 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 374 | SetMinimumLogSeverity(DEBUG); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 375 | continue; |
| 376 | case 'i': |
Tom Cherry | 4916e09 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 377 | SetMinimumLogSeverity(INFO); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 378 | continue; |
| 379 | case 'w': |
Tom Cherry | 4916e09 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 380 | SetMinimumLogSeverity(WARNING); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 381 | continue; |
| 382 | case 'e': |
Tom Cherry | 4916e09 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 383 | SetMinimumLogSeverity(ERROR); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 384 | continue; |
| 385 | case 'f': |
Tom Cherry | 4916e09 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 386 | SetMinimumLogSeverity(FATAL_WITHOUT_ABORT); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 387 | continue; |
Luke Huang | 317728b | 2020-07-30 03:04:39 +0800 | [diff] [blame] | 388 | // liblog will even suppress FATAL if you say 's' for silent, but fatal should |
| 389 | // never be suppressed. |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 390 | case 's': |
Tom Cherry | 4916e09 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 391 | SetMinimumLogSeverity(FATAL_WITHOUT_ABORT); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 392 | continue; |
| 393 | } |
| 394 | } |
| 395 | LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags |
| 396 | << ")"; |
| 397 | } |
| 398 | } |
| 399 | |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 400 | void SetLogger(LogFunction&& logger) { |
Tom Cherry | 8a06da8 | 2020-04-22 11:37:26 -0700 | [diff] [blame] | 401 | Logger() = std::move(logger); |
| 402 | |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 403 | static auto& liblog_functions = GetLibLogFunctions(); |
| 404 | if (liblog_functions) { |
Tom Cherry | 228eff1 | 2020-03-11 11:07:13 -0700 | [diff] [blame] | 405 | liblog_functions->__android_log_set_logger([](const struct __android_log_message* log_message) { |
| 406 | auto log_id = log_id_tToLogId(log_message->buffer_id); |
| 407 | auto severity = PriorityToLogSeverity(log_message->priority); |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 408 | |
Tom Cherry | 8a06da8 | 2020-04-22 11:37:26 -0700 | [diff] [blame] | 409 | Logger()(log_id, severity, log_message->tag, log_message->file, log_message->line, |
Tom Cherry | 228eff1 | 2020-03-11 11:07:13 -0700 | [diff] [blame] | 410 | log_message->message); |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 411 | }); |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 412 | } |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Andreas Gampe | 9008e8d | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 415 | void SetAborter(AbortFunction&& aborter) { |
Tom Cherry | 8a06da8 | 2020-04-22 11:37:26 -0700 | [diff] [blame] | 416 | Aborter() = std::move(aborter); |
| 417 | |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 418 | static auto& liblog_functions = GetLibLogFunctions(); |
| 419 | if (liblog_functions) { |
Tom Cherry | 8a06da8 | 2020-04-22 11:37:26 -0700 | [diff] [blame] | 420 | liblog_functions->__android_log_set_aborter( |
| 421 | [](const char* abort_message) { Aborter()(abort_message); }); |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 422 | } |
Andreas Gampe | 9008e8d | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 425 | // This indirection greatly reduces the stack impact of having lots of |
| 426 | // checks/logging in a function. |
| 427 | class LogMessageData { |
| 428 | public: |
Tom Cherry | 46a2e0e | 2020-01-10 17:18:55 -0800 | [diff] [blame] | 429 | LogMessageData(const char* file, unsigned int line, LogSeverity severity, const char* tag, |
| 430 | int error) |
Spencer Low | 55853a9 | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 431 | : file_(GetFileBasename(file)), |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 432 | line_number_(line), |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 433 | severity_(severity), |
Tomasz Wasilczyk | a419de2 | 2017-12-18 06:30:17 -0800 | [diff] [blame] | 434 | tag_(tag), |
| 435 | error_(error) {} |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 436 | |
| 437 | const char* GetFile() const { |
| 438 | return file_; |
| 439 | } |
| 440 | |
| 441 | unsigned int GetLineNumber() const { |
| 442 | return line_number_; |
| 443 | } |
| 444 | |
| 445 | LogSeverity GetSeverity() const { |
| 446 | return severity_; |
| 447 | } |
| 448 | |
Tomasz Wasilczyk | a419de2 | 2017-12-18 06:30:17 -0800 | [diff] [blame] | 449 | const char* GetTag() const { return tag_; } |
| 450 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 451 | int GetError() const { |
| 452 | return error_; |
| 453 | } |
| 454 | |
| 455 | std::ostream& GetBuffer() { |
| 456 | return buffer_; |
| 457 | } |
| 458 | |
| 459 | std::string ToString() const { |
| 460 | return buffer_.str(); |
| 461 | } |
| 462 | |
| 463 | private: |
| 464 | std::ostringstream buffer_; |
| 465 | const char* const file_; |
| 466 | const unsigned int line_number_; |
| 467 | const LogSeverity severity_; |
Tomasz Wasilczyk | a419de2 | 2017-12-18 06:30:17 -0800 | [diff] [blame] | 468 | const char* const tag_; |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 469 | const int error_; |
| 470 | |
| 471 | DISALLOW_COPY_AND_ASSIGN(LogMessageData); |
| 472 | }; |
| 473 | |
Tom Cherry | 46a2e0e | 2020-01-10 17:18:55 -0800 | [diff] [blame] | 474 | LogMessage::LogMessage(const char* file, unsigned int line, LogId, LogSeverity severity, |
Tomasz Wasilczyk | a419de2 | 2017-12-18 06:30:17 -0800 | [diff] [blame] | 475 | const char* tag, int error) |
Tom Cherry | 46a2e0e | 2020-01-10 17:18:55 -0800 | [diff] [blame] | 476 | : LogMessage(file, line, severity, tag, error) {} |
| 477 | |
| 478 | LogMessage::LogMessage(const char* file, unsigned int line, LogSeverity severity, const char* tag, |
| 479 | int error) |
| 480 | : data_(new LogMessageData(file, line, severity, tag, error)) {} |
Tomasz Wasilczyk | a419de2 | 2017-12-18 06:30:17 -0800 | [diff] [blame] | 481 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 482 | LogMessage::~LogMessage() { |
Andreas Gampe | c65ea94 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 483 | // Check severity again. This is duplicate work wrt/ LOG macros, but not LOG_STREAM. |
Andreas Gampe | cb35a4a | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 484 | if (!WOULD_LOG(data_->GetSeverity())) { |
Andreas Gampe | c65ea94 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 485 | return; |
| 486 | } |
| 487 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 488 | // Finish constructing the message. |
| 489 | if (data_->GetError() != -1) { |
| 490 | data_->GetBuffer() << ": " << strerror(data_->GetError()); |
| 491 | } |
| 492 | std::string msg(data_->ToString()); |
| 493 | |
Andreas Gampe | 2f9e49f | 2018-12-05 11:26:14 -0800 | [diff] [blame] | 494 | if (data_->GetSeverity() == FATAL) { |
| 495 | #ifdef __ANDROID__ |
| 496 | // Set the bionic abort message early to avoid liblog doing it |
| 497 | // with the individual lines, so that we get the whole message. |
| 498 | android_set_abort_message(msg.c_str()); |
| 499 | #endif |
| 500 | } |
| 501 | |
Tom Cherry | a687242 | 2020-04-17 13:05:11 -0700 | [diff] [blame] | 502 | LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), data_->GetTag(), |
| 503 | msg.c_str()); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 504 | |
| 505 | // Abort if necessary. |
| 506 | if (data_->GetSeverity() == FATAL) { |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 507 | static auto& liblog_functions = GetLibLogFunctions(); |
| 508 | if (liblog_functions) { |
| 509 | liblog_functions->__android_log_call_aborter(msg.c_str()); |
| 510 | } else { |
| 511 | Aborter()(msg.c_str()); |
| 512 | } |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
| 516 | std::ostream& LogMessage::stream() { |
| 517 | return data_->GetBuffer(); |
| 518 | } |
| 519 | |
Tom Cherry | 46a2e0e | 2020-01-10 17:18:55 -0800 | [diff] [blame] | 520 | void LogMessage::LogLine(const char* file, unsigned int line, LogSeverity severity, const char* tag, |
| 521 | const char* message) { |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 522 | static auto& liblog_functions = GetLibLogFunctions(); |
Tom Cherry | 784ba9a | 2020-03-12 11:07:07 -0700 | [diff] [blame] | 523 | int32_t priority = LogSeverityToPriority(severity); |
Tom Cherry | dbc07ce | 2020-01-22 07:48:42 -0800 | [diff] [blame] | 524 | if (liblog_functions) { |
Tom Cherry | 228eff1 | 2020-03-11 11:07:13 -0700 | [diff] [blame] | 525 | __android_log_message log_message = { |
| 526 | sizeof(__android_log_message), LOG_ID_DEFAULT, priority, tag, file, line, message}; |
| 527 | liblog_functions->__android_log_write_log_message(&log_message); |
Andreas Gampe | fec5b09 | 2018-03-05 10:00:19 -0800 | [diff] [blame] | 528 | } else { |
Tom Cherry | dbc07ce | 2020-01-22 07:48:42 -0800 | [diff] [blame] | 529 | if (tag == nullptr) { |
| 530 | std::lock_guard<std::recursive_mutex> lock(TagLock()); |
| 531 | if (gDefaultTag == nullptr) { |
| 532 | gDefaultTag = new std::string(getprogname()); |
| 533 | } |
| 534 | |
| 535 | Logger()(DEFAULT, severity, gDefaultTag->c_str(), file, line, message); |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 536 | } else { |
| 537 | Logger()(DEFAULT, severity, tag, file, line, message); |
| 538 | } |
Andreas Gampe | fec5b09 | 2018-03-05 10:00:19 -0800 | [diff] [blame] | 539 | } |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 542 | LogSeverity GetMinimumLogSeverity() { |
Tom Cherry | 4916e09 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 543 | static auto& liblog_functions = GetLibLogFunctions(); |
| 544 | if (liblog_functions) { |
| 545 | return PriorityToLogSeverity(liblog_functions->__android_log_get_minimum_priority()); |
| 546 | } else { |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 547 | return gMinimumLogSeverity; |
Tom Cherry | 4916e09 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | |
| 551 | bool ShouldLog(LogSeverity severity, const char* tag) { |
| 552 | static auto& liblog_functions = GetLibLogFunctions(); |
| 553 | // Even though we're not using the R liblog functions in this function, if we're running on Q, |
| 554 | // we need to fall back to using gMinimumLogSeverity, since __android_log_is_loggable() will not |
| 555 | // take into consideration the value from SetMinimumLogSeverity(). |
| 556 | if (liblog_functions) { |
Tom Cherry | 784ba9a | 2020-03-12 11:07:07 -0700 | [diff] [blame] | 557 | int32_t priority = LogSeverityToPriority(severity); |
Tom Cherry | 4916e09 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 558 | return __android_log_is_loggable(priority, tag, ANDROID_LOG_INFO); |
| 559 | } else { |
| 560 | return severity >= gMinimumLogSeverity; |
| 561 | } |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | LogSeverity SetMinimumLogSeverity(LogSeverity new_severity) { |
Tom Cherry | 4916e09 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 565 | static auto& liblog_functions = GetLibLogFunctions(); |
| 566 | if (liblog_functions) { |
Tom Cherry | 784ba9a | 2020-03-12 11:07:07 -0700 | [diff] [blame] | 567 | int32_t priority = LogSeverityToPriority(new_severity); |
Tom Cherry | 4916e09 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 568 | return PriorityToLogSeverity(liblog_functions->__android_log_set_minimum_priority(priority)); |
| 569 | } else { |
| 570 | LogSeverity old_severity = gMinimumLogSeverity; |
| 571 | gMinimumLogSeverity = new_severity; |
| 572 | return old_severity; |
| 573 | } |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | ScopedLogSeverity::ScopedLogSeverity(LogSeverity new_severity) { |
| 577 | old_ = SetMinimumLogSeverity(new_severity); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | ScopedLogSeverity::~ScopedLogSeverity() { |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 581 | SetMinimumLogSeverity(old_); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | } // namespace base |
| 585 | } // namespace android |