Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 17 | #if defined(_WIN32) |
Spencer Low | 4759052 | 2015-05-19 22:12:06 -0700 | [diff] [blame] | 18 | #include <windows.h> |
| 19 | #endif |
| 20 | |
Elliott Hughes | b635162 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 21 | #include "android-base/logging.h" |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 22 | |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 23 | #include <fcntl.h> |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 24 | #include <libgen.h> |
Elliott Hughes | 6522eb5 | 2016-06-21 14:25:44 -0700 | [diff] [blame] | 25 | #include <time.h> |
Dan Albert | 1be4dec | 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 | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 34 | #if defined(__linux__) |
| 35 | #include <sys/uio.h> |
| 36 | #endif |
| 37 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 38 | #include <iostream> |
| 39 | #include <limits> |
Josh Gao | b08de45 | 2016-09-13 14:57:12 -0700 | [diff] [blame] | 40 | #include <mutex> |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 41 | #include <sstream> |
| 42 | #include <string> |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 43 | #include <utility> |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 44 | #include <vector> |
| 45 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 46 | // Headers for LogMessage::LogLine. |
| 47 | #ifdef __ANDROID__ |
Mark Salyzyn | 5cbb251 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 48 | #include <android/log.h> |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 49 | #include <android/set_abort_message.h> |
Dan Albert | e3ea058 | 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 | 5cbb251 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 55 | #include <android-base/macros.h> |
| 56 | #include <android-base/strings.h> |
| 57 | |
Elliott Hughes | 774d7f6 | 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 | c4e5425 | 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 | 774d7f6 | 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 | dc15ffd | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 91 | namespace { |
Dan Albert | dc15ffd | 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 | b08de45 | 2016-09-13 14:57:12 -0700 | [diff] [blame] | 96 | #elif defined(_WIN32) |
Dan Albert | dc15ffd | 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 | 55853a9 | 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 | dc15ffd | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 110 | first = false; |
| 111 | } |
| 112 | |
| 113 | return progname; |
| 114 | } |
Dan Albert | dc15ffd | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 115 | #endif |
| 116 | } // namespace |
| 117 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 118 | namespace android { |
| 119 | namespace base { |
| 120 | |
Josh Gao | b08de45 | 2016-09-13 14:57:12 -0700 | [diff] [blame] | 121 | static auto& logging_lock = *new std::mutex(); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 122 | |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 123 | #ifdef __ANDROID__ |
Josh Gao | 7f43862 | 2015-11-12 11:54:47 -0800 | [diff] [blame] | 124 | static auto& gLogger = *new LogFunction(LogdLogger()); |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 125 | #else |
Josh Gao | 7f43862 | 2015-11-12 11:54:47 -0800 | [diff] [blame] | 126 | static auto& gLogger = *new LogFunction(StderrLogger); |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 127 | #endif |
| 128 | |
Andreas Gampe | 9008e8d | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 129 | static auto& gAborter = *new AbortFunction(DefaultAborter); |
| 130 | |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 131 | static bool gInitialized = false; |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 132 | static LogSeverity gMinimumLogSeverity = INFO; |
Josh Gao | 7f43862 | 2015-11-12 11:54:47 -0800 | [diff] [blame] | 133 | static auto& gProgramInvocationName = *new std::unique_ptr<std::string>(); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 134 | |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 135 | static const char* ProgramInvocationName() { |
| 136 | if (gProgramInvocationName == nullptr) { |
| 137 | gProgramInvocationName.reset(new std::string(getprogname())); |
| 138 | } |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 139 | |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 140 | return gProgramInvocationName->c_str(); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 143 | #if defined(__linux__) |
| 144 | void KernelLogger(android::base::LogId, android::base::LogSeverity severity, |
| 145 | const char* tag, const char*, unsigned int, const char* msg) { |
Andreas Gampe | d2a4f21 | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 146 | // clang-format off |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 147 | static constexpr int kLogSeverityToKernelLogLevel[] = { |
Andreas Gampe | d2a4f21 | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 148 | [android::base::VERBOSE] = 7, // KERN_DEBUG (there is no verbose kernel log |
| 149 | // level) |
| 150 | [android::base::DEBUG] = 7, // KERN_DEBUG |
| 151 | [android::base::INFO] = 6, // KERN_INFO |
| 152 | [android::base::WARNING] = 4, // KERN_WARNING |
| 153 | [android::base::ERROR] = 3, // KERN_ERROR |
| 154 | [android::base::FATAL_WITHOUT_ABORT] = 2, // KERN_CRIT |
| 155 | [android::base::FATAL] = 2, // KERN_CRIT |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 156 | }; |
Andreas Gampe | d2a4f21 | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 157 | // clang-format on |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 158 | static_assert(arraysize(kLogSeverityToKernelLogLevel) == android::base::FATAL + 1, |
| 159 | "Mismatch in size of kLogSeverityToKernelLogLevel and values in LogSeverity"); |
| 160 | |
| 161 | static int klog_fd = TEMP_FAILURE_RETRY(open("/dev/kmsg", O_WRONLY | O_CLOEXEC)); |
| 162 | if (klog_fd == -1) return; |
| 163 | |
| 164 | int level = kLogSeverityToKernelLogLevel[severity]; |
| 165 | |
| 166 | // The kernel's printk buffer is only 1024 bytes. |
| 167 | // TODO: should we automatically break up long lines into multiple lines? |
| 168 | // Or we could log but with something like "..." at the end? |
| 169 | char buf[1024]; |
| 170 | size_t size = snprintf(buf, sizeof(buf), "<%d>%s: %s\n", level, tag, msg); |
| 171 | if (size > sizeof(buf)) { |
| 172 | size = snprintf(buf, sizeof(buf), "<%d>%s: %zu-byte message too long for printk\n", |
| 173 | level, tag, size); |
| 174 | } |
| 175 | |
| 176 | iovec iov[1]; |
| 177 | iov[0].iov_base = buf; |
| 178 | iov[0].iov_len = size; |
| 179 | TEMP_FAILURE_RETRY(writev(klog_fd, iov, 1)); |
| 180 | } |
| 181 | #endif |
| 182 | |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 183 | void StderrLogger(LogId, LogSeverity severity, const char*, const char* file, |
| 184 | unsigned int line, const char* message) { |
Elliott Hughes | 6522eb5 | 2016-06-21 14:25:44 -0700 | [diff] [blame] | 185 | struct tm now; |
| 186 | time_t t = time(nullptr); |
| 187 | |
| 188 | #if defined(_WIN32) |
| 189 | localtime_s(&now, &t); |
| 190 | #else |
| 191 | localtime_r(&t, &now); |
| 192 | #endif |
| 193 | |
| 194 | char timestamp[32]; |
| 195 | strftime(timestamp, sizeof(timestamp), "%m-%d %H:%M:%S", &now); |
| 196 | |
Andreas Gampe | d2a4f21 | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 197 | static const char log_characters[] = "VDIWEFF"; |
Spencer Low | 55853a9 | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 198 | static_assert(arraysize(log_characters) - 1 == FATAL + 1, |
| 199 | "Mismatch in size of log_characters and values in LogSeverity"); |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 200 | char severity_char = log_characters[severity]; |
Elliott Hughes | 6522eb5 | 2016-06-21 14:25:44 -0700 | [diff] [blame] | 201 | fprintf(stderr, "%s %c %s %5d %5d %s:%u] %s\n", ProgramInvocationName(), |
| 202 | severity_char, timestamp, getpid(), GetThreadId(), file, line, message); |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Andreas Gampe | 9008e8d | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 205 | void DefaultAborter(const char* abort_message) { |
| 206 | #ifdef __ANDROID__ |
| 207 | android_set_abort_message(abort_message); |
| 208 | #else |
| 209 | UNUSED(abort_message); |
| 210 | #endif |
| 211 | abort(); |
| 212 | } |
| 213 | |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 214 | |
| 215 | #ifdef __ANDROID__ |
| 216 | LogdLogger::LogdLogger(LogId default_log_id) : default_log_id_(default_log_id) { |
| 217 | } |
| 218 | |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 219 | void LogdLogger::operator()(LogId id, LogSeverity severity, const char* tag, |
| 220 | const char* file, unsigned int line, |
| 221 | const char* message) { |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 222 | static constexpr android_LogPriority kLogSeverityToAndroidLogPriority[] = { |
Andreas Gampe | d2a4f21 | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 223 | ANDROID_LOG_VERBOSE, ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, |
| 224 | ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL, |
| 225 | ANDROID_LOG_FATAL, |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 226 | }; |
| 227 | static_assert(arraysize(kLogSeverityToAndroidLogPriority) == FATAL + 1, |
| 228 | "Mismatch in size of kLogSeverityToAndroidLogPriority and values in LogSeverity"); |
| 229 | |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 230 | int priority = kLogSeverityToAndroidLogPriority[severity]; |
| 231 | if (id == DEFAULT) { |
| 232 | id = default_log_id_; |
| 233 | } |
| 234 | |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 235 | static constexpr log_id kLogIdToAndroidLogId[] = { |
| 236 | LOG_ID_MAX, LOG_ID_MAIN, LOG_ID_SYSTEM, |
| 237 | }; |
| 238 | static_assert(arraysize(kLogIdToAndroidLogId) == SYSTEM + 1, |
| 239 | "Mismatch in size of kLogIdToAndroidLogId and values in LogId"); |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 240 | log_id lg_id = kLogIdToAndroidLogId[id]; |
| 241 | |
| 242 | if (priority == ANDROID_LOG_FATAL) { |
| 243 | __android_log_buf_print(lg_id, priority, tag, "%s:%u] %s", file, line, |
| 244 | message); |
| 245 | } else { |
| 246 | __android_log_buf_print(lg_id, priority, tag, "%s", message); |
| 247 | } |
| 248 | } |
| 249 | #endif |
| 250 | |
Andreas Gampe | 9008e8d | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 251 | void InitLogging(char* argv[], LogFunction&& logger, AbortFunction&& aborter) { |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 252 | SetLogger(std::forward<LogFunction>(logger)); |
Andreas Gampe | 9008e8d | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 253 | SetAborter(std::forward<AbortFunction>(aborter)); |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 254 | |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 255 | if (gInitialized) { |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 256 | return; |
| 257 | } |
| 258 | |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 259 | gInitialized = true; |
| 260 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 261 | // Stash the command line for later use. We can use /proc/self/cmdline on |
Spencer Low | bec7862 | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 262 | // Linux to recover this, but we don't have that luxury on the Mac/Windows, |
| 263 | // and there are a couple of argv[0] variants that are commonly used. |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 264 | if (argv != nullptr) { |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 265 | gProgramInvocationName.reset(new std::string(basename(argv[0]))); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 266 | } |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 267 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 268 | const char* tags = getenv("ANDROID_LOG_TAGS"); |
| 269 | if (tags == nullptr) { |
| 270 | return; |
| 271 | } |
| 272 | |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 273 | std::vector<std::string> specs = Split(tags, " "); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 274 | for (size_t i = 0; i < specs.size(); ++i) { |
| 275 | // "tag-pattern:[vdiwefs]" |
| 276 | std::string spec(specs[i]); |
| 277 | if (spec.size() == 3 && StartsWith(spec, "*:")) { |
| 278 | switch (spec[2]) { |
| 279 | case 'v': |
| 280 | gMinimumLogSeverity = VERBOSE; |
| 281 | continue; |
| 282 | case 'd': |
| 283 | gMinimumLogSeverity = DEBUG; |
| 284 | continue; |
| 285 | case 'i': |
| 286 | gMinimumLogSeverity = INFO; |
| 287 | continue; |
| 288 | case 'w': |
| 289 | gMinimumLogSeverity = WARNING; |
| 290 | continue; |
| 291 | case 'e': |
| 292 | gMinimumLogSeverity = ERROR; |
| 293 | continue; |
| 294 | case 'f': |
Andreas Gampe | d2a4f21 | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 295 | gMinimumLogSeverity = FATAL_WITHOUT_ABORT; |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 296 | continue; |
| 297 | // liblog will even suppress FATAL if you say 's' for silent, but that's |
| 298 | // crazy! |
| 299 | case 's': |
Andreas Gampe | d2a4f21 | 2016-09-07 10:10:50 -0700 | [diff] [blame] | 300 | gMinimumLogSeverity = FATAL_WITHOUT_ABORT; |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 301 | continue; |
| 302 | } |
| 303 | } |
| 304 | LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags |
| 305 | << ")"; |
| 306 | } |
| 307 | } |
| 308 | |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 309 | void SetLogger(LogFunction&& logger) { |
Josh Gao | b08de45 | 2016-09-13 14:57:12 -0700 | [diff] [blame] | 310 | std::lock_guard<std::mutex> lock(logging_lock); |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 311 | gLogger = std::move(logger); |
| 312 | } |
| 313 | |
Andreas Gampe | 9008e8d | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 314 | void SetAborter(AbortFunction&& aborter) { |
Josh Gao | b08de45 | 2016-09-13 14:57:12 -0700 | [diff] [blame] | 315 | std::lock_guard<std::mutex> lock(logging_lock); |
Andreas Gampe | 9008e8d | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 316 | gAborter = std::move(aborter); |
| 317 | } |
| 318 | |
Spencer Low | 55853a9 | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 319 | static const char* GetFileBasename(const char* file) { |
Spencer Low | bec7862 | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 320 | // We can't use basename(3) even on Unix because the Mac doesn't |
| 321 | // have a non-modifying basename. |
Spencer Low | 55853a9 | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 322 | const char* last_slash = strrchr(file, '/'); |
Spencer Low | bec7862 | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 323 | if (last_slash != nullptr) { |
| 324 | return last_slash + 1; |
| 325 | } |
| 326 | #if defined(_WIN32) |
| 327 | const char* last_backslash = strrchr(file, '\\'); |
| 328 | if (last_backslash != nullptr) { |
| 329 | return last_backslash + 1; |
| 330 | } |
| 331 | #endif |
| 332 | return file; |
Spencer Low | 55853a9 | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 335 | // This indirection greatly reduces the stack impact of having lots of |
| 336 | // checks/logging in a function. |
| 337 | class LogMessageData { |
| 338 | public: |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 339 | LogMessageData(const char* file, unsigned int line, LogId id, |
Spencer Low | e0671d6 | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 340 | LogSeverity severity, int error) |
Spencer Low | 55853a9 | 2015-08-11 16:00:13 -0700 | [diff] [blame] | 341 | : file_(GetFileBasename(file)), |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 342 | line_number_(line), |
| 343 | id_(id), |
| 344 | severity_(severity), |
Spencer Low | e0671d6 | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 345 | error_(error) { |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | const char* GetFile() const { |
| 349 | return file_; |
| 350 | } |
| 351 | |
| 352 | unsigned int GetLineNumber() const { |
| 353 | return line_number_; |
| 354 | } |
| 355 | |
| 356 | LogSeverity GetSeverity() const { |
| 357 | return severity_; |
| 358 | } |
| 359 | |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 360 | LogId GetId() const { |
| 361 | return id_; |
| 362 | } |
| 363 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 364 | int GetError() const { |
| 365 | return error_; |
| 366 | } |
| 367 | |
| 368 | std::ostream& GetBuffer() { |
| 369 | return buffer_; |
| 370 | } |
| 371 | |
| 372 | std::string ToString() const { |
| 373 | return buffer_.str(); |
| 374 | } |
| 375 | |
| 376 | private: |
| 377 | std::ostringstream buffer_; |
| 378 | const char* const file_; |
| 379 | const unsigned int line_number_; |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 380 | const LogId id_; |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 381 | const LogSeverity severity_; |
| 382 | const int error_; |
| 383 | |
| 384 | DISALLOW_COPY_AND_ASSIGN(LogMessageData); |
| 385 | }; |
| 386 | |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 387 | LogMessage::LogMessage(const char* file, unsigned int line, LogId id, |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 388 | LogSeverity severity, int error) |
Spencer Low | e0671d6 | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 389 | : data_(new LogMessageData(file, line, id, severity, error)) { |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | LogMessage::~LogMessage() { |
Andreas Gampe | c65ea94 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 393 | // Check severity again. This is duplicate work wrt/ LOG macros, but not LOG_STREAM. |
Andreas Gampe | cb35a4a | 2016-09-23 16:37:12 -0700 | [diff] [blame] | 394 | if (!WOULD_LOG(data_->GetSeverity())) { |
Andreas Gampe | c65ea94 | 2016-09-23 13:31:52 -0700 | [diff] [blame] | 395 | return; |
| 396 | } |
| 397 | |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 398 | // Finish constructing the message. |
| 399 | if (data_->GetError() != -1) { |
| 400 | data_->GetBuffer() << ": " << strerror(data_->GetError()); |
| 401 | } |
| 402 | std::string msg(data_->ToString()); |
| 403 | |
Spencer Low | e0671d6 | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 404 | { |
| 405 | // Do the actual logging with the lock held. |
Josh Gao | b08de45 | 2016-09-13 14:57:12 -0700 | [diff] [blame] | 406 | std::lock_guard<std::mutex> lock(logging_lock); |
Spencer Low | e0671d6 | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 407 | if (msg.find('\n') == std::string::npos) { |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 408 | LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(), |
Spencer Low | e0671d6 | 2015-09-17 19:36:10 -0700 | [diff] [blame] | 409 | data_->GetSeverity(), msg.c_str()); |
| 410 | } else { |
| 411 | msg += '\n'; |
| 412 | size_t i = 0; |
| 413 | while (i < msg.size()) { |
| 414 | size_t nl = msg.find('\n', i); |
| 415 | msg[nl] = '\0'; |
| 416 | LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(), |
| 417 | data_->GetSeverity(), &msg[i]); |
| 418 | i = nl + 1; |
| 419 | } |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
| 423 | // Abort if necessary. |
| 424 | if (data_->GetSeverity() == FATAL) { |
Andreas Gampe | 9008e8d | 2016-09-08 11:03:58 -0700 | [diff] [blame] | 425 | gAborter(msg.c_str()); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
| 429 | std::ostream& LogMessage::stream() { |
| 430 | return data_->GetBuffer(); |
| 431 | } |
| 432 | |
Dan Albert | ab5c882 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 433 | void LogMessage::LogLine(const char* file, unsigned int line, LogId id, |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 434 | LogSeverity severity, const char* message) { |
Dan Albert | 1be4dec | 2015-04-03 11:28:46 -0700 | [diff] [blame] | 435 | const char* tag = ProgramInvocationName(); |
Dan Albert | 1f65c49 | 2015-03-27 11:20:14 -0700 | [diff] [blame] | 436 | gLogger(id, severity, tag, file, line, message); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 439 | LogSeverity GetMinimumLogSeverity() { |
| 440 | return gMinimumLogSeverity; |
| 441 | } |
| 442 | |
| 443 | LogSeverity SetMinimumLogSeverity(LogSeverity new_severity) { |
| 444 | LogSeverity old_severity = gMinimumLogSeverity; |
| 445 | gMinimumLogSeverity = new_severity; |
| 446 | return old_severity; |
| 447 | } |
| 448 | |
| 449 | ScopedLogSeverity::ScopedLogSeverity(LogSeverity new_severity) { |
| 450 | old_ = SetMinimumLogSeverity(new_severity); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | ScopedLogSeverity::~ScopedLogSeverity() { |
Elliott Hughes | 8cf75f0 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 454 | SetMinimumLogSeverity(old_); |
Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | } // namespace base |
| 458 | } // namespace android |