| Dan Albert | 58310b4 | 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 | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 17 | #if defined(_WIN32) | 
| Spencer Low | ac3f7d9 | 2015-05-19 22:12:06 -0700 | [diff] [blame] | 18 | #include <windows.h> | 
 | 19 | #endif | 
 | 20 |  | 
| Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 21 | #include "android-base/logging.h" | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 22 |  | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 23 | #include <fcntl.h> | 
| Josh Gao | 5791e21 | 2018-03-16 14:25:42 -0700 | [diff] [blame] | 24 | #include <inttypes.h> | 
| Dan Albert | 7a87d05 | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 25 | #include <libgen.h> | 
| Elliott Hughes | 4e5fd11 | 2016-06-21 14:25:44 -0700 | [diff] [blame] | 26 | #include <time.h> | 
| Dan Albert | 7a87d05 | 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 | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 35 | #if defined(__linux__) | 
 | 36 | #include <sys/uio.h> | 
 | 37 | #endif | 
 | 38 |  | 
| Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 39 | #include <atomic> | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 40 | #include <iostream> | 
 | 41 | #include <limits> | 
| Josh Gao | 63bdcb5 | 2016-09-13 14:57:12 -0700 | [diff] [blame] | 42 | #include <mutex> | 
| Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 43 | #include <optional> | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 44 | #include <sstream> | 
 | 45 | #include <string> | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 46 | #include <utility> | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 47 | #include <vector> | 
 | 48 |  | 
| Andreas Gampe | af05f3b | 2018-02-15 11:40:30 -0800 | [diff] [blame] | 49 | #include <android/log.h> | 
| Tom Cherry | 9921630 | 2020-01-08 13:41:56 -0800 | [diff] [blame] | 50 | #ifdef __ANDROID__ | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 51 | #include <android/set_abort_message.h> | 
| Dan Albert | 58310b4 | 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 | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 57 | #include <android-base/file.h> | 
| Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 58 | #include <android-base/macros.h> | 
| Mark Salyzyn | 2507a04 | 2018-04-06 09:40:26 -0700 | [diff] [blame] | 59 | #include <android-base/parseint.h> | 
| Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 60 | #include <android-base/strings.h> | 
| Josh Gao | 5791e21 | 2018-03-16 14:25:42 -0700 | [diff] [blame] | 61 | #include <android-base/threads.h> | 
| Elliott Hughes | c1fd492 | 2015-11-11 18:02:29 +0000 | [diff] [blame] | 62 |  | 
| Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 63 | #include "liblog_symbols.h" | 
 | 64 |  | 
| Elliott Hughes | 11a64eb | 2018-06-06 12:54:41 -0700 | [diff] [blame] | 65 | namespace android { | 
 | 66 | namespace base { | 
 | 67 |  | 
 | 68 | // BSD-based systems like Android/macOS have getprogname(). Others need us to provide one. | 
 | 69 | #if defined(__GLIBC__) || defined(_WIN32) | 
 | 70 | static const char* getprogname() { | 
| Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 71 | #if defined(__GLIBC__) | 
| Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 72 |   return program_invocation_short_name; | 
| Josh Gao | 63bdcb5 | 2016-09-13 14:57:12 -0700 | [diff] [blame] | 73 | #elif defined(_WIN32) | 
| Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 74 |   static bool first = true; | 
 | 75 |   static char progname[MAX_PATH] = {}; | 
 | 76 |  | 
 | 77 |   if (first) { | 
| Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 78 |     snprintf(progname, sizeof(progname), "%s", | 
 | 79 |              android::base::Basename(android::base::GetExecutablePath()).c_str()); | 
| Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 80 |     first = false; | 
 | 81 |   } | 
 | 82 |  | 
 | 83 |   return progname; | 
| Elliott Hughes | 11a64eb | 2018-06-06 12:54:41 -0700 | [diff] [blame] | 84 | #endif | 
| Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 85 | } | 
| Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 86 | #endif | 
| Mark Salyzyn | 2507a04 | 2018-04-06 09:40:26 -0700 | [diff] [blame] | 87 |  | 
| Elliott Hughes | 11a64eb | 2018-06-06 12:54:41 -0700 | [diff] [blame] | 88 | static const char* GetFileBasename(const char* file) { | 
 | 89 |   // We can't use basename(3) even on Unix because the Mac doesn't | 
 | 90 |   // have a non-modifying basename. | 
 | 91 |   const char* last_slash = strrchr(file, '/'); | 
 | 92 |   if (last_slash != nullptr) { | 
 | 93 |     return last_slash + 1; | 
 | 94 |   } | 
 | 95 | #if defined(_WIN32) | 
 | 96 |   const char* last_backslash = strrchr(file, '\\'); | 
 | 97 |   if (last_backslash != nullptr) { | 
 | 98 |     return last_backslash + 1; | 
 | 99 |   } | 
 | 100 | #endif | 
 | 101 |   return file; | 
 | 102 | } | 
 | 103 |  | 
| Mark Salyzyn | 2507a04 | 2018-04-06 09:40:26 -0700 | [diff] [blame] | 104 | #if defined(__linux__) | 
| Elliott Hughes | 11a64eb | 2018-06-06 12:54:41 -0700 | [diff] [blame] | 105 | static int OpenKmsg() { | 
| Mark Salyzyn | 2507a04 | 2018-04-06 09:40:26 -0700 | [diff] [blame] | 106 | #if defined(__ANDROID__) | 
 | 107 |   // pick up 'file w /dev/kmsg' environment from daemon's init rc file | 
 | 108 |   const auto val = getenv("ANDROID_FILE__dev_kmsg"); | 
 | 109 |   if (val != nullptr) { | 
 | 110 |     int fd; | 
 | 111 |     if (android::base::ParseInt(val, &fd, 0)) { | 
 | 112 |       auto flags = fcntl(fd, F_GETFL); | 
 | 113 |       if ((flags != -1) && ((flags & O_ACCMODE) == O_WRONLY)) return fd; | 
 | 114 |     } | 
 | 115 |   } | 
 | 116 | #endif | 
 | 117 |   return TEMP_FAILURE_RETRY(open("/dev/kmsg", O_WRONLY | O_CLOEXEC)); | 
 | 118 | } | 
 | 119 | #endif | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 120 |  | 
| Tom Cherry | f1a975b | 2020-03-12 11:07:07 -0700 | [diff] [blame] | 121 | static LogId log_id_tToLogId(int32_t buffer_id) { | 
| Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 122 |   switch (buffer_id) { | 
 | 123 |     case LOG_ID_MAIN: | 
 | 124 |       return MAIN; | 
 | 125 |     case LOG_ID_SYSTEM: | 
 | 126 |       return SYSTEM; | 
 | 127 |     case LOG_ID_RADIO: | 
 | 128 |       return RADIO; | 
 | 129 |     case LOG_ID_CRASH: | 
 | 130 |       return CRASH; | 
 | 131 |     case LOG_ID_DEFAULT: | 
 | 132 |     default: | 
 | 133 |       return DEFAULT; | 
 | 134 |   } | 
 | 135 | } | 
 | 136 |  | 
| Tom Cherry | f1a975b | 2020-03-12 11:07:07 -0700 | [diff] [blame] | 137 | static int32_t LogIdTolog_id_t(LogId log_id) { | 
| Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 138 |   switch (log_id) { | 
 | 139 |     case MAIN: | 
 | 140 |       return LOG_ID_MAIN; | 
 | 141 |     case SYSTEM: | 
 | 142 |       return LOG_ID_SYSTEM; | 
 | 143 |     case RADIO: | 
 | 144 |       return LOG_ID_RADIO; | 
 | 145 |     case CRASH: | 
 | 146 |       return LOG_ID_CRASH; | 
 | 147 |     case DEFAULT: | 
 | 148 |     default: | 
 | 149 |       return LOG_ID_DEFAULT; | 
 | 150 |   } | 
 | 151 | } | 
 | 152 |  | 
 | 153 | static LogSeverity PriorityToLogSeverity(int priority) { | 
 | 154 |   switch (priority) { | 
| Tom Cherry | 0391a87 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 155 |     case ANDROID_LOG_DEFAULT: | 
 | 156 |       return INFO; | 
| Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 157 |     case ANDROID_LOG_VERBOSE: | 
 | 158 |       return VERBOSE; | 
 | 159 |     case ANDROID_LOG_DEBUG: | 
 | 160 |       return DEBUG; | 
 | 161 |     case ANDROID_LOG_INFO: | 
 | 162 |       return INFO; | 
 | 163 |     case ANDROID_LOG_WARN: | 
 | 164 |       return WARNING; | 
 | 165 |     case ANDROID_LOG_ERROR: | 
 | 166 |       return ERROR; | 
 | 167 |     case ANDROID_LOG_FATAL: | 
 | 168 |       return FATAL; | 
 | 169 |     default: | 
 | 170 |       return FATAL; | 
 | 171 |   } | 
 | 172 | } | 
 | 173 |  | 
| Tom Cherry | f1a975b | 2020-03-12 11:07:07 -0700 | [diff] [blame] | 174 | static int32_t LogSeverityToPriority(LogSeverity severity) { | 
| Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 175 |   switch (severity) { | 
 | 176 |     case VERBOSE: | 
 | 177 |       return ANDROID_LOG_VERBOSE; | 
 | 178 |     case DEBUG: | 
 | 179 |       return ANDROID_LOG_DEBUG; | 
 | 180 |     case INFO: | 
 | 181 |       return ANDROID_LOG_INFO; | 
 | 182 |     case WARNING: | 
 | 183 |       return ANDROID_LOG_WARN; | 
 | 184 |     case ERROR: | 
 | 185 |       return ANDROID_LOG_ERROR; | 
 | 186 |     case FATAL_WITHOUT_ABORT: | 
 | 187 |     case FATAL: | 
 | 188 |     default: | 
 | 189 |       return ANDROID_LOG_FATAL; | 
 | 190 |   } | 
 | 191 | } | 
 | 192 |  | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 193 | static std::mutex& LoggingLock() { | 
 | 194 |   static auto& logging_lock = *new std::mutex(); | 
 | 195 |   return logging_lock; | 
 | 196 | } | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 197 |  | 
| Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 198 | // Only used for Q fallback. | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 199 | static LogFunction& Logger() { | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 200 | #ifdef __ANDROID__ | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 201 |   static auto& logger = *new LogFunction(LogdLogger()); | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 202 | #else | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 203 |   static auto& logger = *new LogFunction(StderrLogger); | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 204 | #endif | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 205 |   return logger; | 
 | 206 | } | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 207 |  | 
| Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 208 | // Only used for Q fallback. | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 209 | static AbortFunction& Aborter() { | 
 | 210 |   static auto& aborter = *new AbortFunction(DefaultAborter); | 
 | 211 |   return aborter; | 
 | 212 | } | 
 | 213 |  | 
| Tom Cherry | 69ee5dd | 2020-01-22 07:48:42 -0800 | [diff] [blame] | 214 | // Only used for Q fallback. | 
| Andreas Gampe | 1923e76 | 2018-03-05 10:00:19 -0800 | [diff] [blame] | 215 | static std::recursive_mutex& TagLock() { | 
 | 216 |   static auto& tag_lock = *new std::recursive_mutex(); | 
 | 217 |   return tag_lock; | 
 | 218 | } | 
| Tom Cherry | 69ee5dd | 2020-01-22 07:48:42 -0800 | [diff] [blame] | 219 | // Only used for Q fallback. | 
| Andreas Gampe | 1923e76 | 2018-03-05 10:00:19 -0800 | [diff] [blame] | 220 | static std::string* gDefaultTag; | 
| Tom Cherry | 69ee5dd | 2020-01-22 07:48:42 -0800 | [diff] [blame] | 221 |  | 
| Andreas Gampe | 1923e76 | 2018-03-05 10:00:19 -0800 | [diff] [blame] | 222 | void SetDefaultTag(const std::string& tag) { | 
| Tom Cherry | 69ee5dd | 2020-01-22 07:48:42 -0800 | [diff] [blame] | 223 |   static auto& liblog_functions = GetLibLogFunctions(); | 
 | 224 |   if (liblog_functions) { | 
 | 225 |     liblog_functions->__android_log_set_default_tag(tag.c_str()); | 
 | 226 |   } else { | 
 | 227 |     std::lock_guard<std::recursive_mutex> lock(TagLock()); | 
 | 228 |     if (gDefaultTag != nullptr) { | 
 | 229 |       delete gDefaultTag; | 
 | 230 |       gDefaultTag = nullptr; | 
 | 231 |     } | 
 | 232 |     if (!tag.empty()) { | 
 | 233 |       gDefaultTag = new std::string(tag); | 
 | 234 |     } | 
| Andreas Gampe | 1923e76 | 2018-03-05 10:00:19 -0800 | [diff] [blame] | 235 |   } | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 236 | } | 
| Andreas Gampe | 2691e33 | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 237 |  | 
| Dan Albert | 7a87d05 | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 238 | static bool gInitialized = false; | 
| Tom Cherry | 0391a87 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 239 |  | 
 | 240 | // Only used for Q fallback. | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 241 | static LogSeverity gMinimumLogSeverity = INFO; | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 242 |  | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 243 | #if defined(__linux__) | 
 | 244 | void KernelLogger(android::base::LogId, android::base::LogSeverity severity, | 
 | 245 |                   const char* tag, const char*, unsigned int, const char* msg) { | 
| Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 246 |   // clang-format off | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 247 |   static constexpr int kLogSeverityToKernelLogLevel[] = { | 
| Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 248 |       [android::base::VERBOSE] = 7,              // KERN_DEBUG (there is no verbose kernel log | 
 | 249 |                                                  //             level) | 
 | 250 |       [android::base::DEBUG] = 7,                // KERN_DEBUG | 
 | 251 |       [android::base::INFO] = 6,                 // KERN_INFO | 
 | 252 |       [android::base::WARNING] = 4,              // KERN_WARNING | 
 | 253 |       [android::base::ERROR] = 3,                // KERN_ERROR | 
 | 254 |       [android::base::FATAL_WITHOUT_ABORT] = 2,  // KERN_CRIT | 
 | 255 |       [android::base::FATAL] = 2,                // KERN_CRIT | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 256 |   }; | 
| Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 257 |   // clang-format on | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 258 |   static_assert(arraysize(kLogSeverityToKernelLogLevel) == android::base::FATAL + 1, | 
 | 259 |                 "Mismatch in size of kLogSeverityToKernelLogLevel and values in LogSeverity"); | 
 | 260 |  | 
| Mark Salyzyn | 2507a04 | 2018-04-06 09:40:26 -0700 | [diff] [blame] | 261 |   static int klog_fd = OpenKmsg(); | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 262 |   if (klog_fd == -1) return; | 
 | 263 |  | 
 | 264 |   int level = kLogSeverityToKernelLogLevel[severity]; | 
 | 265 |  | 
 | 266 |   // The kernel's printk buffer is only 1024 bytes. | 
 | 267 |   // TODO: should we automatically break up long lines into multiple lines? | 
 | 268 |   // Or we could log but with something like "..." at the end? | 
 | 269 |   char buf[1024]; | 
 | 270 |   size_t size = snprintf(buf, sizeof(buf), "<%d>%s: %s\n", level, tag, msg); | 
 | 271 |   if (size > sizeof(buf)) { | 
 | 272 |     size = snprintf(buf, sizeof(buf), "<%d>%s: %zu-byte message too long for printk\n", | 
 | 273 |                     level, tag, size); | 
 | 274 |   } | 
 | 275 |  | 
 | 276 |   iovec iov[1]; | 
 | 277 |   iov[0].iov_base = buf; | 
 | 278 |   iov[0].iov_len = size; | 
 | 279 |   TEMP_FAILURE_RETRY(writev(klog_fd, iov, 1)); | 
 | 280 | } | 
 | 281 | #endif | 
 | 282 |  | 
| Tomasz Wasilczyk | c251600 | 2017-12-18 06:30:17 -0800 | [diff] [blame] | 283 | void StderrLogger(LogId, LogSeverity severity, const char* tag, const char* file, unsigned int line, | 
 | 284 |                   const char* message) { | 
| Elliott Hughes | 4e5fd11 | 2016-06-21 14:25:44 -0700 | [diff] [blame] | 285 |   struct tm now; | 
 | 286 |   time_t t = time(nullptr); | 
 | 287 |  | 
 | 288 | #if defined(_WIN32) | 
 | 289 |   localtime_s(&now, &t); | 
 | 290 | #else | 
 | 291 |   localtime_r(&t, &now); | 
 | 292 | #endif | 
 | 293 |  | 
 | 294 |   char timestamp[32]; | 
 | 295 |   strftime(timestamp, sizeof(timestamp), "%m-%d %H:%M:%S", &now); | 
 | 296 |  | 
| Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 297 |   static const char log_characters[] = "VDIWEFF"; | 
| Spencer Low | bdab59a | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 298 |   static_assert(arraysize(log_characters) - 1 == FATAL + 1, | 
 | 299 |                 "Mismatch in size of log_characters and values in LogSeverity"); | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 300 |   char severity_char = log_characters[severity]; | 
| Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 301 |   if (file != nullptr) { | 
 | 302 |     fprintf(stderr, "%s %c %s %5d %5" PRIu64 " %s:%u] %s\n", tag ? tag : "nullptr", severity_char, | 
 | 303 |             timestamp, getpid(), GetThreadId(), file, line, message); | 
 | 304 |   } else { | 
 | 305 |     fprintf(stderr, "%s %c %s %5d %5" PRIu64 " %s\n", tag ? tag : "nullptr", severity_char, | 
 | 306 |             timestamp, getpid(), GetThreadId(), message); | 
 | 307 |   } | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 308 | } | 
 | 309 |  | 
| Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 310 | void StdioLogger(LogId, LogSeverity severity, const char* /*tag*/, const char* /*file*/, | 
 | 311 |                  unsigned int /*line*/, const char* message) { | 
 | 312 |   if (severity >= WARNING) { | 
 | 313 |     fflush(stdout); | 
| Elliott Hughes | 11a64eb | 2018-06-06 12:54:41 -0700 | [diff] [blame] | 314 |     fprintf(stderr, "%s: %s\n", GetFileBasename(getprogname()), message); | 
| Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 315 |   } else { | 
 | 316 |     fprintf(stdout, "%s\n", message); | 
 | 317 |   } | 
 | 318 | } | 
 | 319 |  | 
| Andreas Gampe | 2691e33 | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 320 | void DefaultAborter(const char* abort_message) { | 
 | 321 | #ifdef __ANDROID__ | 
 | 322 |   android_set_abort_message(abort_message); | 
 | 323 | #else | 
 | 324 |   UNUSED(abort_message); | 
 | 325 | #endif | 
 | 326 |   abort(); | 
 | 327 | } | 
 | 328 |  | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 329 |  | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 330 | LogdLogger::LogdLogger(LogId default_log_id) : default_log_id_(default_log_id) { | 
 | 331 | } | 
 | 332 |  | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 333 | void LogdLogger::operator()(LogId id, LogSeverity severity, const char* tag, | 
 | 334 |                             const char* file, unsigned int line, | 
 | 335 |                             const char* message) { | 
| Tom Cherry | f1a975b | 2020-03-12 11:07:07 -0700 | [diff] [blame] | 336 |   int32_t priority = LogSeverityToPriority(severity); | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 337 |   if (id == DEFAULT) { | 
 | 338 |     id = default_log_id_; | 
 | 339 |   } | 
 | 340 |  | 
| Tom Cherry | f1a975b | 2020-03-12 11:07:07 -0700 | [diff] [blame] | 341 |   int32_t lg_id = LogIdTolog_id_t(id); | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 342 |  | 
| Tom Cherry | 42ee2e4 | 2020-01-31 09:03:05 -0800 | [diff] [blame] | 343 |   char log_message_with_file[4068];  // LOGGER_ENTRY_MAX_PAYLOAD, not available in the NDK. | 
| Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 344 |   if (priority == ANDROID_LOG_FATAL && file != nullptr) { | 
| Tom Cherry | 42ee2e4 | 2020-01-31 09:03:05 -0800 | [diff] [blame] | 345 |     snprintf(log_message_with_file, sizeof(log_message_with_file), "%s:%u] %s", file, line, | 
 | 346 |              message); | 
 | 347 |     message = log_message_with_file; | 
| Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 348 |   } | 
 | 349 |  | 
 | 350 |   static auto& liblog_functions = GetLibLogFunctions(); | 
 | 351 |   if (liblog_functions) { | 
| Tom Cherry | ebf43ad | 2020-03-11 11:07:13 -0700 | [diff] [blame] | 352 |     __android_log_message log_message = {sizeof(__android_log_message),     lg_id, priority, tag, | 
 | 353 |                                          static_cast<const char*>(nullptr), 0,     message}; | 
 | 354 |     liblog_functions->__android_log_logd_logger(&log_message); | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 355 |   } else { | 
 | 356 |     __android_log_buf_print(lg_id, priority, tag, "%s", message); | 
 | 357 |   } | 
 | 358 | } | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 359 |  | 
| Andreas Gampe | 2691e33 | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 360 | void InitLogging(char* argv[], LogFunction&& logger, AbortFunction&& aborter) { | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 361 |   SetLogger(std::forward<LogFunction>(logger)); | 
| Andreas Gampe | 2691e33 | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 362 |   SetAborter(std::forward<AbortFunction>(aborter)); | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 363 |  | 
| Dan Albert | 7a87d05 | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 364 |   if (gInitialized) { | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 365 |     return; | 
 | 366 |   } | 
 | 367 |  | 
| Dan Albert | 7a87d05 | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 368 |   gInitialized = true; | 
 | 369 |  | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 370 |   // Stash the command line for later use. We can use /proc/self/cmdline on | 
| Spencer Low | 363af56 | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 371 |   // Linux to recover this, but we don't have that luxury on the Mac/Windows, | 
 | 372 |   // and there are a couple of argv[0] variants that are commonly used. | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 373 |   if (argv != nullptr) { | 
| Andreas Gampe | 1923e76 | 2018-03-05 10:00:19 -0800 | [diff] [blame] | 374 |     SetDefaultTag(basename(argv[0])); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 375 |   } | 
| Dan Albert | 7a87d05 | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 376 |  | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 377 |   const char* tags = getenv("ANDROID_LOG_TAGS"); | 
 | 378 |   if (tags == nullptr) { | 
 | 379 |     return; | 
 | 380 |   } | 
 | 381 |  | 
| Dan Albert | 47328c9 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 382 |   std::vector<std::string> specs = Split(tags, " "); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 383 |   for (size_t i = 0; i < specs.size(); ++i) { | 
 | 384 |     // "tag-pattern:[vdiwefs]" | 
 | 385 |     std::string spec(specs[i]); | 
 | 386 |     if (spec.size() == 3 && StartsWith(spec, "*:")) { | 
 | 387 |       switch (spec[2]) { | 
 | 388 |         case 'v': | 
| Tom Cherry | 0391a87 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 389 |           SetMinimumLogSeverity(VERBOSE); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 390 |           continue; | 
 | 391 |         case 'd': | 
| Tom Cherry | 0391a87 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 392 |           SetMinimumLogSeverity(DEBUG); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 393 |           continue; | 
 | 394 |         case 'i': | 
| Tom Cherry | 0391a87 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 395 |           SetMinimumLogSeverity(INFO); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 396 |           continue; | 
 | 397 |         case 'w': | 
| Tom Cherry | 0391a87 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 398 |           SetMinimumLogSeverity(WARNING); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 399 |           continue; | 
 | 400 |         case 'e': | 
| Tom Cherry | 0391a87 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 401 |           SetMinimumLogSeverity(ERROR); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 402 |           continue; | 
 | 403 |         case 'f': | 
| Tom Cherry | 0391a87 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 404 |           SetMinimumLogSeverity(FATAL_WITHOUT_ABORT); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 405 |           continue; | 
 | 406 |         // liblog will even suppress FATAL if you say 's' for silent, but that's | 
 | 407 |         // crazy! | 
 | 408 |         case 's': | 
| Tom Cherry | 0391a87 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 409 |           SetMinimumLogSeverity(FATAL_WITHOUT_ABORT); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 410 |           continue; | 
 | 411 |       } | 
 | 412 |     } | 
 | 413 |     LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags | 
 | 414 |                << ")"; | 
 | 415 |   } | 
 | 416 | } | 
 | 417 |  | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 418 | void SetLogger(LogFunction&& logger) { | 
| Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 419 |   static auto& liblog_functions = GetLibLogFunctions(); | 
 | 420 |   if (liblog_functions) { | 
 | 421 |     // We need to atomically swap the old and new pointers since other threads may be logging. | 
 | 422 |     // We know all threads will be using the new logger after __android_log_set_logger() returns, | 
 | 423 |     // so we can delete it then. | 
 | 424 |     // This leaks one std::function<> per instance of libbase if multiple copies of libbase within a | 
 | 425 |     // single process call SetLogger().  That is the same cost as having a static | 
 | 426 |     // std::function<>, which is the not-thread-safe alternative. | 
 | 427 |     static std::atomic<LogFunction*> logger_function(nullptr); | 
 | 428 |     auto* old_logger_function = logger_function.exchange(new LogFunction(logger)); | 
| Tom Cherry | ebf43ad | 2020-03-11 11:07:13 -0700 | [diff] [blame] | 429 |     liblog_functions->__android_log_set_logger([](const struct __android_log_message* log_message) { | 
 | 430 |       auto log_id = log_id_tToLogId(log_message->buffer_id); | 
 | 431 |       auto severity = PriorityToLogSeverity(log_message->priority); | 
| Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 432 |  | 
 | 433 |       auto& function = *logger_function.load(std::memory_order_acquire); | 
| Tom Cherry | ebf43ad | 2020-03-11 11:07:13 -0700 | [diff] [blame] | 434 |       function(log_id, severity, log_message->tag, log_message->file, log_message->line, | 
 | 435 |                log_message->message); | 
| Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 436 |     }); | 
 | 437 |     delete old_logger_function; | 
 | 438 |   } else { | 
 | 439 |     std::lock_guard<std::mutex> lock(LoggingLock()); | 
 | 440 |     Logger() = std::move(logger); | 
 | 441 |   } | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 442 | } | 
 | 443 |  | 
| Andreas Gampe | 2691e33 | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 444 | void SetAborter(AbortFunction&& aborter) { | 
| Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 445 |   static auto& liblog_functions = GetLibLogFunctions(); | 
 | 446 |   if (liblog_functions) { | 
 | 447 |     // See the comment in SetLogger(). | 
 | 448 |     static std::atomic<AbortFunction*> abort_function(nullptr); | 
 | 449 |     auto* old_abort_function = abort_function.exchange(new AbortFunction(aborter)); | 
| Tom Cherry | d620ec7 | 2020-03-05 10:00:52 -0800 | [diff] [blame] | 450 |     liblog_functions->__android_log_set_aborter([](const char* abort_message) { | 
| Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 451 |       auto& function = *abort_function.load(std::memory_order_acquire); | 
 | 452 |       function(abort_message); | 
 | 453 |     }); | 
 | 454 |     delete old_abort_function; | 
 | 455 |   } else { | 
 | 456 |     std::lock_guard<std::mutex> lock(LoggingLock()); | 
 | 457 |     Aborter() = std::move(aborter); | 
 | 458 |   } | 
| Andreas Gampe | 2691e33 | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 459 | } | 
 | 460 |  | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 461 | // This indirection greatly reduces the stack impact of having lots of | 
 | 462 | // checks/logging in a function. | 
 | 463 | class LogMessageData { | 
 | 464 |  public: | 
| Tom Cherry | d044eaa | 2020-01-10 17:18:55 -0800 | [diff] [blame] | 465 |   LogMessageData(const char* file, unsigned int line, LogSeverity severity, const char* tag, | 
 | 466 |                  int error) | 
| Spencer Low | bdab59a | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 467 |       : file_(GetFileBasename(file)), | 
| Dan Albert | 0c05586 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 468 |         line_number_(line), | 
| Dan Albert | 0c05586 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 469 |         severity_(severity), | 
| Tomasz Wasilczyk | c251600 | 2017-12-18 06:30:17 -0800 | [diff] [blame] | 470 |         tag_(tag), | 
 | 471 |         error_(error) {} | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 472 |  | 
 | 473 |   const char* GetFile() const { | 
 | 474 |     return file_; | 
 | 475 |   } | 
 | 476 |  | 
 | 477 |   unsigned int GetLineNumber() const { | 
 | 478 |     return line_number_; | 
 | 479 |   } | 
 | 480 |  | 
 | 481 |   LogSeverity GetSeverity() const { | 
 | 482 |     return severity_; | 
 | 483 |   } | 
 | 484 |  | 
| Tomasz Wasilczyk | c251600 | 2017-12-18 06:30:17 -0800 | [diff] [blame] | 485 |   const char* GetTag() const { return tag_; } | 
 | 486 |  | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 487 |   int GetError() const { | 
 | 488 |     return error_; | 
 | 489 |   } | 
 | 490 |  | 
 | 491 |   std::ostream& GetBuffer() { | 
 | 492 |     return buffer_; | 
 | 493 |   } | 
 | 494 |  | 
 | 495 |   std::string ToString() const { | 
 | 496 |     return buffer_.str(); | 
 | 497 |   } | 
 | 498 |  | 
 | 499 |  private: | 
 | 500 |   std::ostringstream buffer_; | 
 | 501 |   const char* const file_; | 
 | 502 |   const unsigned int line_number_; | 
 | 503 |   const LogSeverity severity_; | 
| Tomasz Wasilczyk | c251600 | 2017-12-18 06:30:17 -0800 | [diff] [blame] | 504 |   const char* const tag_; | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 505 |   const int error_; | 
 | 506 |  | 
 | 507 |   DISALLOW_COPY_AND_ASSIGN(LogMessageData); | 
 | 508 | }; | 
 | 509 |  | 
| Tom Cherry | d044eaa | 2020-01-10 17:18:55 -0800 | [diff] [blame] | 510 | LogMessage::LogMessage(const char* file, unsigned int line, LogId, LogSeverity severity, | 
| Tomasz Wasilczyk | c251600 | 2017-12-18 06:30:17 -0800 | [diff] [blame] | 511 |                        const char* tag, int error) | 
| Tom Cherry | d044eaa | 2020-01-10 17:18:55 -0800 | [diff] [blame] | 512 |     : LogMessage(file, line, severity, tag, error) {} | 
 | 513 |  | 
 | 514 | LogMessage::LogMessage(const char* file, unsigned int line, LogSeverity severity, const char* tag, | 
 | 515 |                        int error) | 
 | 516 |     : data_(new LogMessageData(file, line, severity, tag, error)) {} | 
| Tomasz Wasilczyk | c251600 | 2017-12-18 06:30:17 -0800 | [diff] [blame] | 517 |  | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 518 | LogMessage::~LogMessage() { | 
| Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 519 |   // Check severity again. This is duplicate work wrt/ LOG macros, but not LOG_STREAM. | 
| Andreas Gampe | 1f5fb43 | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 520 |   if (!WOULD_LOG(data_->GetSeverity())) { | 
| Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 521 |     return; | 
 | 522 |   } | 
 | 523 |  | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 524 |   // Finish constructing the message. | 
 | 525 |   if (data_->GetError() != -1) { | 
 | 526 |     data_->GetBuffer() << ": " << strerror(data_->GetError()); | 
 | 527 |   } | 
 | 528 |   std::string msg(data_->ToString()); | 
 | 529 |  | 
| Andreas Gampe | 2819c0b | 2018-12-05 11:26:14 -0800 | [diff] [blame] | 530 |   if (data_->GetSeverity() == FATAL) { | 
 | 531 | #ifdef __ANDROID__ | 
 | 532 |     // Set the bionic abort message early to avoid liblog doing it | 
 | 533 |     // with the individual lines, so that we get the whole message. | 
 | 534 |     android_set_abort_message(msg.c_str()); | 
 | 535 | #endif | 
 | 536 |   } | 
 | 537 |  | 
| Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 538 |   { | 
 | 539 |     // Do the actual logging with the lock held. | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 540 |     std::lock_guard<std::mutex> lock(LoggingLock()); | 
| Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 541 |     if (msg.find('\n') == std::string::npos) { | 
| Tom Cherry | d044eaa | 2020-01-10 17:18:55 -0800 | [diff] [blame] | 542 |       LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), data_->GetTag(), | 
 | 543 |               msg.c_str()); | 
| Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 544 |     } else { | 
 | 545 |       msg += '\n'; | 
 | 546 |       size_t i = 0; | 
 | 547 |       while (i < msg.size()) { | 
 | 548 |         size_t nl = msg.find('\n', i); | 
 | 549 |         msg[nl] = '\0'; | 
| Tom Cherry | d044eaa | 2020-01-10 17:18:55 -0800 | [diff] [blame] | 550 |         LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), data_->GetTag(), | 
 | 551 |                 &msg[i]); | 
| Andreas Gampe | b4e32f3 | 2016-10-04 19:17:07 -0700 | [diff] [blame] | 552 |         // Undo the zero-termination so we can give the complete message to the aborter. | 
 | 553 |         msg[nl] = '\n'; | 
| Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 554 |         i = nl + 1; | 
 | 555 |       } | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 556 |     } | 
 | 557 |   } | 
 | 558 |  | 
 | 559 |   // Abort if necessary. | 
 | 560 |   if (data_->GetSeverity() == FATAL) { | 
| Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 561 |     static auto& liblog_functions = GetLibLogFunctions(); | 
 | 562 |     if (liblog_functions) { | 
 | 563 |       liblog_functions->__android_log_call_aborter(msg.c_str()); | 
 | 564 |     } else { | 
 | 565 |       Aborter()(msg.c_str()); | 
 | 566 |     } | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 567 |   } | 
 | 568 | } | 
 | 569 |  | 
 | 570 | std::ostream& LogMessage::stream() { | 
 | 571 |   return data_->GetBuffer(); | 
 | 572 | } | 
 | 573 |  | 
| Tom Cherry | d044eaa | 2020-01-10 17:18:55 -0800 | [diff] [blame] | 574 | void LogMessage::LogLine(const char* file, unsigned int line, LogSeverity severity, const char* tag, | 
 | 575 |                          const char* message) { | 
| Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 576 |   static auto& liblog_functions = GetLibLogFunctions(); | 
| Tom Cherry | f1a975b | 2020-03-12 11:07:07 -0700 | [diff] [blame] | 577 |   int32_t priority = LogSeverityToPriority(severity); | 
| Tom Cherry | 69ee5dd | 2020-01-22 07:48:42 -0800 | [diff] [blame] | 578 |   if (liblog_functions) { | 
| Tom Cherry | ebf43ad | 2020-03-11 11:07:13 -0700 | [diff] [blame] | 579 |     __android_log_message log_message = { | 
 | 580 |         sizeof(__android_log_message), LOG_ID_DEFAULT, priority, tag, file, line, message}; | 
 | 581 |     liblog_functions->__android_log_write_log_message(&log_message); | 
| Andreas Gampe | 1923e76 | 2018-03-05 10:00:19 -0800 | [diff] [blame] | 582 |   } else { | 
| Tom Cherry | 69ee5dd | 2020-01-22 07:48:42 -0800 | [diff] [blame] | 583 |     if (tag == nullptr) { | 
 | 584 |       std::lock_guard<std::recursive_mutex> lock(TagLock()); | 
 | 585 |       if (gDefaultTag == nullptr) { | 
 | 586 |         gDefaultTag = new std::string(getprogname()); | 
 | 587 |       } | 
 | 588 |  | 
 | 589 |       Logger()(DEFAULT, severity, gDefaultTag->c_str(), file, line, message); | 
| Tom Cherry | 349b0c4 | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 590 |     } else { | 
 | 591 |       Logger()(DEFAULT, severity, tag, file, line, message); | 
 | 592 |     } | 
| Andreas Gampe | 1923e76 | 2018-03-05 10:00:19 -0800 | [diff] [blame] | 593 |   } | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 594 | } | 
 | 595 |  | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 596 | LogSeverity GetMinimumLogSeverity() { | 
| Tom Cherry | 0391a87 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 597 |   static auto& liblog_functions = GetLibLogFunctions(); | 
 | 598 |   if (liblog_functions) { | 
 | 599 |     return PriorityToLogSeverity(liblog_functions->__android_log_get_minimum_priority()); | 
 | 600 |   } else { | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 601 |     return gMinimumLogSeverity; | 
| Tom Cherry | 0391a87 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 602 |   } | 
 | 603 | } | 
 | 604 |  | 
 | 605 | bool ShouldLog(LogSeverity severity, const char* tag) { | 
 | 606 |   static auto& liblog_functions = GetLibLogFunctions(); | 
 | 607 |   // Even though we're not using the R liblog functions in this function, if we're running on Q, | 
 | 608 |   // we need to fall back to using gMinimumLogSeverity, since __android_log_is_loggable() will not | 
 | 609 |   // take into consideration the value from SetMinimumLogSeverity(). | 
 | 610 |   if (liblog_functions) { | 
| Tom Cherry | f1a975b | 2020-03-12 11:07:07 -0700 | [diff] [blame] | 611 |     int32_t priority = LogSeverityToPriority(severity); | 
| Tom Cherry | 0391a87 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 612 |     return __android_log_is_loggable(priority, tag, ANDROID_LOG_INFO); | 
 | 613 |   } else { | 
 | 614 |     return severity >= gMinimumLogSeverity; | 
 | 615 |   } | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 616 | } | 
 | 617 |  | 
 | 618 | LogSeverity SetMinimumLogSeverity(LogSeverity new_severity) { | 
| Tom Cherry | 0391a87 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 619 |   static auto& liblog_functions = GetLibLogFunctions(); | 
 | 620 |   if (liblog_functions) { | 
| Tom Cherry | f1a975b | 2020-03-12 11:07:07 -0700 | [diff] [blame] | 621 |     int32_t priority = LogSeverityToPriority(new_severity); | 
| Tom Cherry | 0391a87 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 622 |     return PriorityToLogSeverity(liblog_functions->__android_log_set_minimum_priority(priority)); | 
 | 623 |   } else { | 
 | 624 |     LogSeverity old_severity = gMinimumLogSeverity; | 
 | 625 |     gMinimumLogSeverity = new_severity; | 
 | 626 |     return old_severity; | 
 | 627 |   } | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 628 | } | 
 | 629 |  | 
 | 630 | ScopedLogSeverity::ScopedLogSeverity(LogSeverity new_severity) { | 
 | 631 |   old_ = SetMinimumLogSeverity(new_severity); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 632 | } | 
 | 633 |  | 
 | 634 | ScopedLogSeverity::~ScopedLogSeverity() { | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 635 |   SetMinimumLogSeverity(old_); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 636 | } | 
 | 637 |  | 
 | 638 | }  // namespace base | 
 | 639 | }  // namespace android |