| 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> | 
| Dan Albert | 7a87d05 | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 24 | #include <libgen.h> | 
| Elliott Hughes | 4e5fd11 | 2016-06-21 14:25:44 -0700 | [diff] [blame] | 25 | #include <time.h> | 
| Dan Albert | 7a87d05 | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 26 |  | 
|  | 27 | // For getprogname(3) or program_invocation_short_name. | 
|  | 28 | #if defined(__ANDROID__) || defined(__APPLE__) | 
|  | 29 | #include <stdlib.h> | 
|  | 30 | #elif defined(__GLIBC__) | 
|  | 31 | #include <errno.h> | 
|  | 32 | #endif | 
|  | 33 |  | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 34 | #if defined(__linux__) | 
|  | 35 | #include <sys/uio.h> | 
|  | 36 | #endif | 
|  | 37 |  | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 38 | #include <iostream> | 
|  | 39 | #include <limits> | 
| Josh Gao | 63bdcb5 | 2016-09-13 14:57:12 -0700 | [diff] [blame] | 40 | #include <mutex> | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 41 | #include <sstream> | 
|  | 42 | #include <string> | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 43 | #include <utility> | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 44 | #include <vector> | 
|  | 45 |  | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 46 | // Headers for LogMessage::LogLine. | 
|  | 47 | #ifdef __ANDROID__ | 
| Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 48 | #include <log/log.h> | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 49 | #include <android/set_abort_message.h> | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 50 | #else | 
|  | 51 | #include <sys/types.h> | 
|  | 52 | #include <unistd.h> | 
|  | 53 | #endif | 
|  | 54 |  | 
| Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 55 | #include <android-base/macros.h> | 
|  | 56 | #include <android-base/strings.h> | 
|  | 57 |  | 
| Elliott Hughes | c1fd492 | 2015-11-11 18:02:29 +0000 | [diff] [blame] | 58 | // For gettid. | 
|  | 59 | #if defined(__APPLE__) | 
|  | 60 | #include "AvailabilityMacros.h"  // For MAC_OS_X_VERSION_MAX_ALLOWED | 
|  | 61 | #include <stdint.h> | 
|  | 62 | #include <stdlib.h> | 
|  | 63 | #include <sys/syscall.h> | 
|  | 64 | #include <sys/time.h> | 
|  | 65 | #include <unistd.h> | 
|  | 66 | #elif defined(__linux__) && !defined(__ANDROID__) | 
|  | 67 | #include <syscall.h> | 
|  | 68 | #include <unistd.h> | 
|  | 69 | #elif defined(_WIN32) | 
|  | 70 | #include <windows.h> | 
|  | 71 | #endif | 
|  | 72 |  | 
| Dan Willemsen | 86cf941 | 2016-02-03 23:29:32 -0800 | [diff] [blame] | 73 | #if defined(_WIN32) | 
|  | 74 | typedef uint32_t thread_id; | 
|  | 75 | #else | 
|  | 76 | typedef pid_t thread_id; | 
|  | 77 | #endif | 
|  | 78 |  | 
|  | 79 | static thread_id GetThreadId() { | 
| Elliott Hughes | c1fd492 | 2015-11-11 18:02:29 +0000 | [diff] [blame] | 80 | #if defined(__BIONIC__) | 
|  | 81 | return gettid(); | 
|  | 82 | #elif defined(__APPLE__) | 
|  | 83 | return syscall(SYS_thread_selfid); | 
|  | 84 | #elif defined(__linux__) | 
|  | 85 | return syscall(__NR_gettid); | 
|  | 86 | #elif defined(_WIN32) | 
|  | 87 | return GetCurrentThreadId(); | 
|  | 88 | #endif | 
|  | 89 | } | 
|  | 90 |  | 
| Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 91 | namespace { | 
| Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 92 | #if defined(__GLIBC__) | 
|  | 93 | const char* getprogname() { | 
|  | 94 | return program_invocation_short_name; | 
|  | 95 | } | 
| Josh Gao | 63bdcb5 | 2016-09-13 14:57:12 -0700 | [diff] [blame] | 96 | #elif defined(_WIN32) | 
| Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 97 | const char* getprogname() { | 
|  | 98 | static bool first = true; | 
|  | 99 | static char progname[MAX_PATH] = {}; | 
|  | 100 |  | 
|  | 101 | if (first) { | 
| Spencer Low | bdab59a | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 102 | CHAR longname[MAX_PATH]; | 
|  | 103 | DWORD nchars = GetModuleFileNameA(nullptr, longname, arraysize(longname)); | 
|  | 104 | if ((nchars >= arraysize(longname)) || (nchars == 0)) { | 
|  | 105 | // String truncation or some other error. | 
|  | 106 | strcpy(progname, "<unknown>"); | 
|  | 107 | } else { | 
|  | 108 | strcpy(progname, basename(longname)); | 
|  | 109 | } | 
| Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 110 | first = false; | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | return progname; | 
|  | 114 | } | 
| Dan Albert | 5c19040 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 115 | #endif | 
|  | 116 | } // namespace | 
|  | 117 |  | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 118 | namespace android { | 
|  | 119 | namespace base { | 
|  | 120 |  | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 121 | static std::mutex& LoggingLock() { | 
|  | 122 | static auto& logging_lock = *new std::mutex(); | 
|  | 123 | return logging_lock; | 
|  | 124 | } | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 125 |  | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 126 | static LogFunction& Logger() { | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 127 | #ifdef __ANDROID__ | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 128 | static auto& logger = *new LogFunction(LogdLogger()); | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 129 | #else | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 130 | static auto& logger = *new LogFunction(StderrLogger); | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 131 | #endif | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 132 | return logger; | 
|  | 133 | } | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 134 |  | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 135 | static AbortFunction& Aborter() { | 
|  | 136 | static auto& aborter = *new AbortFunction(DefaultAborter); | 
|  | 137 | return aborter; | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | static std::string& ProgramInvocationName() { | 
|  | 141 | static auto& programInvocationName = *new std::string(getprogname()); | 
|  | 142 | return programInvocationName; | 
|  | 143 | } | 
| Andreas Gampe | 2691e33 | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 144 |  | 
| Dan Albert | 7a87d05 | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 145 | static bool gInitialized = false; | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 146 | static LogSeverity gMinimumLogSeverity = INFO; | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 147 |  | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 148 | #if defined(__linux__) | 
|  | 149 | void KernelLogger(android::base::LogId, android::base::LogSeverity severity, | 
|  | 150 | const char* tag, const char*, unsigned int, const char* msg) { | 
| Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 151 | // clang-format off | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 152 | static constexpr int kLogSeverityToKernelLogLevel[] = { | 
| Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 153 | [android::base::VERBOSE] = 7,              // KERN_DEBUG (there is no verbose kernel log | 
|  | 154 | //             level) | 
|  | 155 | [android::base::DEBUG] = 7,                // KERN_DEBUG | 
|  | 156 | [android::base::INFO] = 6,                 // KERN_INFO | 
|  | 157 | [android::base::WARNING] = 4,              // KERN_WARNING | 
|  | 158 | [android::base::ERROR] = 3,                // KERN_ERROR | 
|  | 159 | [android::base::FATAL_WITHOUT_ABORT] = 2,  // KERN_CRIT | 
|  | 160 | [android::base::FATAL] = 2,                // KERN_CRIT | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 161 | }; | 
| Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 162 | // clang-format on | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 163 | static_assert(arraysize(kLogSeverityToKernelLogLevel) == android::base::FATAL + 1, | 
|  | 164 | "Mismatch in size of kLogSeverityToKernelLogLevel and values in LogSeverity"); | 
|  | 165 |  | 
|  | 166 | static int klog_fd = TEMP_FAILURE_RETRY(open("/dev/kmsg", O_WRONLY | O_CLOEXEC)); | 
|  | 167 | if (klog_fd == -1) return; | 
|  | 168 |  | 
|  | 169 | int level = kLogSeverityToKernelLogLevel[severity]; | 
|  | 170 |  | 
|  | 171 | // The kernel's printk buffer is only 1024 bytes. | 
|  | 172 | // TODO: should we automatically break up long lines into multiple lines? | 
|  | 173 | // Or we could log but with something like "..." at the end? | 
|  | 174 | char buf[1024]; | 
|  | 175 | size_t size = snprintf(buf, sizeof(buf), "<%d>%s: %s\n", level, tag, msg); | 
|  | 176 | if (size > sizeof(buf)) { | 
|  | 177 | size = snprintf(buf, sizeof(buf), "<%d>%s: %zu-byte message too long for printk\n", | 
|  | 178 | level, tag, size); | 
|  | 179 | } | 
|  | 180 |  | 
|  | 181 | iovec iov[1]; | 
|  | 182 | iov[0].iov_base = buf; | 
|  | 183 | iov[0].iov_len = size; | 
|  | 184 | TEMP_FAILURE_RETRY(writev(klog_fd, iov, 1)); | 
|  | 185 | } | 
|  | 186 | #endif | 
|  | 187 |  | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 188 | void StderrLogger(LogId, LogSeverity severity, const char*, const char* file, | 
|  | 189 | unsigned int line, const char* message) { | 
| Elliott Hughes | 4e5fd11 | 2016-06-21 14:25:44 -0700 | [diff] [blame] | 190 | struct tm now; | 
|  | 191 | time_t t = time(nullptr); | 
|  | 192 |  | 
|  | 193 | #if defined(_WIN32) | 
|  | 194 | localtime_s(&now, &t); | 
|  | 195 | #else | 
|  | 196 | localtime_r(&t, &now); | 
|  | 197 | #endif | 
|  | 198 |  | 
|  | 199 | char timestamp[32]; | 
|  | 200 | strftime(timestamp, sizeof(timestamp), "%m-%d %H:%M:%S", &now); | 
|  | 201 |  | 
| Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 202 | static const char log_characters[] = "VDIWEFF"; | 
| Spencer Low | bdab59a | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 203 | static_assert(arraysize(log_characters) - 1 == FATAL + 1, | 
|  | 204 | "Mismatch in size of log_characters and values in LogSeverity"); | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 205 | char severity_char = log_characters[severity]; | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 206 | fprintf(stderr, "%s %c %s %5d %5d %s:%u] %s\n", ProgramInvocationName().c_str(), | 
| Elliott Hughes | 4e5fd11 | 2016-06-21 14:25:44 -0700 | [diff] [blame] | 207 | severity_char, timestamp, getpid(), GetThreadId(), file, line, message); | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 208 | } | 
|  | 209 |  | 
| Andreas Gampe | 2691e33 | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 210 | void DefaultAborter(const char* abort_message) { | 
|  | 211 | #ifdef __ANDROID__ | 
|  | 212 | android_set_abort_message(abort_message); | 
|  | 213 | #else | 
|  | 214 | UNUSED(abort_message); | 
|  | 215 | #endif | 
|  | 216 | abort(); | 
|  | 217 | } | 
|  | 218 |  | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 219 |  | 
|  | 220 | #ifdef __ANDROID__ | 
|  | 221 | LogdLogger::LogdLogger(LogId default_log_id) : default_log_id_(default_log_id) { | 
|  | 222 | } | 
|  | 223 |  | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 224 | void LogdLogger::operator()(LogId id, LogSeverity severity, const char* tag, | 
|  | 225 | const char* file, unsigned int line, | 
|  | 226 | const char* message) { | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 227 | static constexpr android_LogPriority kLogSeverityToAndroidLogPriority[] = { | 
| Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 228 | ANDROID_LOG_VERBOSE, ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, | 
|  | 229 | ANDROID_LOG_WARN,    ANDROID_LOG_ERROR, ANDROID_LOG_FATAL, | 
|  | 230 | ANDROID_LOG_FATAL, | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 231 | }; | 
|  | 232 | static_assert(arraysize(kLogSeverityToAndroidLogPriority) == FATAL + 1, | 
|  | 233 | "Mismatch in size of kLogSeverityToAndroidLogPriority and values in LogSeverity"); | 
|  | 234 |  | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 235 | int priority = kLogSeverityToAndroidLogPriority[severity]; | 
|  | 236 | if (id == DEFAULT) { | 
|  | 237 | id = default_log_id_; | 
|  | 238 | } | 
|  | 239 |  | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 240 | static constexpr log_id kLogIdToAndroidLogId[] = { | 
|  | 241 | LOG_ID_MAX, LOG_ID_MAIN, LOG_ID_SYSTEM, | 
|  | 242 | }; | 
|  | 243 | static_assert(arraysize(kLogIdToAndroidLogId) == SYSTEM + 1, | 
|  | 244 | "Mismatch in size of kLogIdToAndroidLogId and values in LogId"); | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 245 | log_id lg_id = kLogIdToAndroidLogId[id]; | 
|  | 246 |  | 
|  | 247 | if (priority == ANDROID_LOG_FATAL) { | 
|  | 248 | __android_log_buf_print(lg_id, priority, tag, "%s:%u] %s", file, line, | 
|  | 249 | message); | 
|  | 250 | } else { | 
|  | 251 | __android_log_buf_print(lg_id, priority, tag, "%s", message); | 
|  | 252 | } | 
|  | 253 | } | 
|  | 254 | #endif | 
|  | 255 |  | 
| Andreas Gampe | 2691e33 | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 256 | void InitLogging(char* argv[], LogFunction&& logger, AbortFunction&& aborter) { | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 257 | SetLogger(std::forward<LogFunction>(logger)); | 
| Andreas Gampe | 2691e33 | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 258 | SetAborter(std::forward<AbortFunction>(aborter)); | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 259 |  | 
| Dan Albert | 7a87d05 | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 260 | if (gInitialized) { | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 261 | return; | 
|  | 262 | } | 
|  | 263 |  | 
| Dan Albert | 7a87d05 | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 264 | gInitialized = true; | 
|  | 265 |  | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 266 | // 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] | 267 | // Linux to recover this, but we don't have that luxury on the Mac/Windows, | 
|  | 268 | // 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] | 269 | if (argv != nullptr) { | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 270 | std::lock_guard<std::mutex> lock(LoggingLock()); | 
|  | 271 | ProgramInvocationName() = basename(argv[0]); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 272 | } | 
| Dan Albert | 7a87d05 | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 273 |  | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 274 | const char* tags = getenv("ANDROID_LOG_TAGS"); | 
|  | 275 | if (tags == nullptr) { | 
|  | 276 | return; | 
|  | 277 | } | 
|  | 278 |  | 
| Dan Albert | 47328c9 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 279 | std::vector<std::string> specs = Split(tags, " "); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 280 | for (size_t i = 0; i < specs.size(); ++i) { | 
|  | 281 | // "tag-pattern:[vdiwefs]" | 
|  | 282 | std::string spec(specs[i]); | 
|  | 283 | if (spec.size() == 3 && StartsWith(spec, "*:")) { | 
|  | 284 | switch (spec[2]) { | 
|  | 285 | case 'v': | 
|  | 286 | gMinimumLogSeverity = VERBOSE; | 
|  | 287 | continue; | 
|  | 288 | case 'd': | 
|  | 289 | gMinimumLogSeverity = DEBUG; | 
|  | 290 | continue; | 
|  | 291 | case 'i': | 
|  | 292 | gMinimumLogSeverity = INFO; | 
|  | 293 | continue; | 
|  | 294 | case 'w': | 
|  | 295 | gMinimumLogSeverity = WARNING; | 
|  | 296 | continue; | 
|  | 297 | case 'e': | 
|  | 298 | gMinimumLogSeverity = ERROR; | 
|  | 299 | continue; | 
|  | 300 | case 'f': | 
| Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 301 | gMinimumLogSeverity = FATAL_WITHOUT_ABORT; | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 302 | continue; | 
|  | 303 | // liblog will even suppress FATAL if you say 's' for silent, but that's | 
|  | 304 | // crazy! | 
|  | 305 | case 's': | 
| Andreas Gampe | 550829d | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 306 | gMinimumLogSeverity = FATAL_WITHOUT_ABORT; | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 307 | continue; | 
|  | 308 | } | 
|  | 309 | } | 
|  | 310 | LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags | 
|  | 311 | << ")"; | 
|  | 312 | } | 
|  | 313 | } | 
|  | 314 |  | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 315 | void SetLogger(LogFunction&& logger) { | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 316 | std::lock_guard<std::mutex> lock(LoggingLock()); | 
|  | 317 | Logger() = std::move(logger); | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 318 | } | 
|  | 319 |  | 
| Andreas Gampe | 2691e33 | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 320 | void SetAborter(AbortFunction&& aborter) { | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 321 | std::lock_guard<std::mutex> lock(LoggingLock()); | 
|  | 322 | Aborter() = std::move(aborter); | 
| Andreas Gampe | 2691e33 | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 323 | } | 
|  | 324 |  | 
| Spencer Low | bdab59a | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 325 | static const char* GetFileBasename(const char* file) { | 
| Spencer Low | 363af56 | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 326 | // We can't use basename(3) even on Unix because the Mac doesn't | 
|  | 327 | // have a non-modifying basename. | 
| Spencer Low | bdab59a | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 328 | const char* last_slash = strrchr(file, '/'); | 
| Spencer Low | 363af56 | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 329 | if (last_slash != nullptr) { | 
|  | 330 | return last_slash + 1; | 
|  | 331 | } | 
|  | 332 | #if defined(_WIN32) | 
|  | 333 | const char* last_backslash = strrchr(file, '\\'); | 
|  | 334 | if (last_backslash != nullptr) { | 
|  | 335 | return last_backslash + 1; | 
|  | 336 | } | 
|  | 337 | #endif | 
|  | 338 | return file; | 
| Spencer Low | bdab59a | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 339 | } | 
|  | 340 |  | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 341 | // This indirection greatly reduces the stack impact of having lots of | 
|  | 342 | // checks/logging in a function. | 
|  | 343 | class LogMessageData { | 
|  | 344 | public: | 
| Dan Albert | 0c05586 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 345 | LogMessageData(const char* file, unsigned int line, LogId id, | 
| Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 346 | LogSeverity severity, int error) | 
| Spencer Low | bdab59a | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 347 | : file_(GetFileBasename(file)), | 
| Dan Albert | 0c05586 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 348 | line_number_(line), | 
|  | 349 | id_(id), | 
|  | 350 | severity_(severity), | 
| Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 351 | error_(error) { | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 352 | } | 
|  | 353 |  | 
|  | 354 | const char* GetFile() const { | 
|  | 355 | return file_; | 
|  | 356 | } | 
|  | 357 |  | 
|  | 358 | unsigned int GetLineNumber() const { | 
|  | 359 | return line_number_; | 
|  | 360 | } | 
|  | 361 |  | 
|  | 362 | LogSeverity GetSeverity() const { | 
|  | 363 | return severity_; | 
|  | 364 | } | 
|  | 365 |  | 
| Dan Albert | 0c05586 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 366 | LogId GetId() const { | 
|  | 367 | return id_; | 
|  | 368 | } | 
|  | 369 |  | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 370 | int GetError() const { | 
|  | 371 | return error_; | 
|  | 372 | } | 
|  | 373 |  | 
|  | 374 | std::ostream& GetBuffer() { | 
|  | 375 | return buffer_; | 
|  | 376 | } | 
|  | 377 |  | 
|  | 378 | std::string ToString() const { | 
|  | 379 | return buffer_.str(); | 
|  | 380 | } | 
|  | 381 |  | 
|  | 382 | private: | 
|  | 383 | std::ostringstream buffer_; | 
|  | 384 | const char* const file_; | 
|  | 385 | const unsigned int line_number_; | 
| Dan Albert | 0c05586 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 386 | const LogId id_; | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 387 | const LogSeverity severity_; | 
|  | 388 | const int error_; | 
|  | 389 |  | 
|  | 390 | DISALLOW_COPY_AND_ASSIGN(LogMessageData); | 
|  | 391 | }; | 
|  | 392 |  | 
| Dan Albert | 0c05586 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 393 | LogMessage::LogMessage(const char* file, unsigned int line, LogId id, | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 394 | LogSeverity severity, int error) | 
| Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 395 | : data_(new LogMessageData(file, line, id, severity, error)) { | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 396 | } | 
|  | 397 |  | 
|  | 398 | LogMessage::~LogMessage() { | 
| Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 399 | // 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] | 400 | if (!WOULD_LOG(data_->GetSeverity())) { | 
| Andreas Gampe | 19ff8f1 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 401 | return; | 
|  | 402 | } | 
|  | 403 |  | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 404 | // Finish constructing the message. | 
|  | 405 | if (data_->GetError() != -1) { | 
|  | 406 | data_->GetBuffer() << ": " << strerror(data_->GetError()); | 
|  | 407 | } | 
|  | 408 | std::string msg(data_->ToString()); | 
|  | 409 |  | 
| Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 410 | { | 
|  | 411 | // Do the actual logging with the lock held. | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 412 | std::lock_guard<std::mutex> lock(LoggingLock()); | 
| Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 413 | if (msg.find('\n') == std::string::npos) { | 
| Dan Albert | 0c05586 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 414 | LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(), | 
| Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 415 | data_->GetSeverity(), msg.c_str()); | 
|  | 416 | } else { | 
|  | 417 | msg += '\n'; | 
|  | 418 | size_t i = 0; | 
|  | 419 | while (i < msg.size()) { | 
|  | 420 | size_t nl = msg.find('\n', i); | 
|  | 421 | msg[nl] = '\0'; | 
|  | 422 | LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(), | 
|  | 423 | data_->GetSeverity(), &msg[i]); | 
| Andreas Gampe | b4e32f3 | 2016-10-04 19:17:07 -0700 | [diff] [blame] | 424 | // Undo the zero-termination so we can give the complete message to the aborter. | 
|  | 425 | msg[nl] = '\n'; | 
| Spencer Low | 765ae6b | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 426 | i = nl + 1; | 
|  | 427 | } | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 428 | } | 
|  | 429 | } | 
|  | 430 |  | 
|  | 431 | // Abort if necessary. | 
|  | 432 | if (data_->GetSeverity() == FATAL) { | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 433 | Aborter()(msg.c_str()); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 434 | } | 
|  | 435 | } | 
|  | 436 |  | 
|  | 437 | std::ostream& LogMessage::stream() { | 
|  | 438 | return data_->GetBuffer(); | 
|  | 439 | } | 
|  | 440 |  | 
| Dan Albert | 0c05586 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 441 | void LogMessage::LogLine(const char* file, unsigned int line, LogId id, | 
| Dan Albert | b547c85 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 442 | LogSeverity severity, const char* message) { | 
| Yabin Cui | 0c68953 | 2017-01-23 10:29:23 -0800 | [diff] [blame] | 443 | const char* tag = ProgramInvocationName().c_str(); | 
|  | 444 | Logger()(id, severity, tag, file, line, message); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 445 | } | 
|  | 446 |  | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 447 | LogSeverity GetMinimumLogSeverity() { | 
|  | 448 | return gMinimumLogSeverity; | 
|  | 449 | } | 
|  | 450 |  | 
|  | 451 | LogSeverity SetMinimumLogSeverity(LogSeverity new_severity) { | 
|  | 452 | LogSeverity old_severity = gMinimumLogSeverity; | 
|  | 453 | gMinimumLogSeverity = new_severity; | 
|  | 454 | return old_severity; | 
|  | 455 | } | 
|  | 456 |  | 
|  | 457 | ScopedLogSeverity::ScopedLogSeverity(LogSeverity new_severity) { | 
|  | 458 | old_ = SetMinimumLogSeverity(new_severity); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 459 | } | 
|  | 460 |  | 
|  | 461 | ScopedLogSeverity::~ScopedLogSeverity() { | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 462 | SetMinimumLogSeverity(old_); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 463 | } | 
|  | 464 |  | 
|  | 465 | }  // namespace base | 
|  | 466 | }  // namespace android |