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