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