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