blob: d60d91d5dfa26c0a00f90c84e6e8f9ab4f8e2ed7 [file] [log] [blame]
Dan Alberte3ea0582015-03-13 23:06:01 -07001/*
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 Hughes8cf75f02016-08-04 16:09:39 -070017#if defined(_WIN32)
Spencer Low47590522015-05-19 22:12:06 -070018#include <windows.h>
19#endif
20
Elliott Hughesb6351622015-12-04 22:00:26 -080021#include "android-base/logging.h"
Dan Alberte3ea0582015-03-13 23:06:01 -070022
Elliott Hughes8cf75f02016-08-04 16:09:39 -070023#include <fcntl.h>
Josh Gaoef102be2018-03-16 14:25:42 -070024#include <inttypes.h>
Dan Albert1be4dec2015-04-03 11:28:46 -070025#include <libgen.h>
Elliott Hughes6522eb52016-06-21 14:25:44 -070026#include <time.h>
Dan Albert1be4dec2015-04-03 11:28:46 -070027
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 Hughes8cf75f02016-08-04 16:09:39 -070035#if defined(__linux__)
36#include <sys/uio.h>
37#endif
38
Dan Alberte3ea0582015-03-13 23:06:01 -070039#include <iostream>
40#include <limits>
Josh Gaob08de452016-09-13 14:57:12 -070041#include <mutex>
Dan Alberte3ea0582015-03-13 23:06:01 -070042#include <sstream>
43#include <string>
Dan Albert1f65c492015-03-27 11:20:14 -070044#include <utility>
Dan Alberte3ea0582015-03-13 23:06:01 -070045#include <vector>
46
Dan Alberte3ea0582015-03-13 23:06:01 -070047// Headers for LogMessage::LogLine.
48#ifdef __ANDROID__
Andreas Gampeed917072018-02-15 11:40:30 -080049#include <android/log.h>
Dan Alberte3ea0582015-03-13 23:06:01 -070050#include <android/set_abort_message.h>
Dan Alberte3ea0582015-03-13 23:06:01 -070051#else
52#include <sys/types.h>
53#include <unistd.h>
54#endif
55
Mark Salyzyn5cbb2512016-09-28 15:54:45 -070056#include <android-base/macros.h>
Mark Salyzyn504d8ed2018-04-06 09:40:26 -070057#include <android-base/parseint.h>
Mark Salyzyn5cbb2512016-09-28 15:54:45 -070058#include <android-base/strings.h>
Josh Gaoef102be2018-03-16 14:25:42 -070059#include <android-base/threads.h>
Elliott Hughes774d7f62015-11-11 18:02:29 +000060
Elliott Hughes462a45e2018-06-06 12:54:41 -070061namespace android {
62namespace base {
63
64// BSD-based systems like Android/macOS have getprogname(). Others need us to provide one.
65#if defined(__GLIBC__) || defined(_WIN32)
66static const char* getprogname() {
Dan Albertdc15ffd2015-04-29 11:32:23 -070067#if defined(__GLIBC__)
Dan Albertdc15ffd2015-04-29 11:32:23 -070068 return program_invocation_short_name;
Josh Gaob08de452016-09-13 14:57:12 -070069#elif defined(_WIN32)
Dan Albertdc15ffd2015-04-29 11:32:23 -070070 static bool first = true;
71 static char progname[MAX_PATH] = {};
72
73 if (first) {
Spencer Low55853a92015-08-11 16:00:13 -070074 CHAR longname[MAX_PATH];
75 DWORD nchars = GetModuleFileNameA(nullptr, longname, arraysize(longname));
76 if ((nchars >= arraysize(longname)) || (nchars == 0)) {
77 // String truncation or some other error.
78 strcpy(progname, "<unknown>");
79 } else {
80 strcpy(progname, basename(longname));
81 }
Dan Albertdc15ffd2015-04-29 11:32:23 -070082 first = false;
83 }
84
85 return progname;
Elliott Hughes462a45e2018-06-06 12:54:41 -070086#endif
Dan Albertdc15ffd2015-04-29 11:32:23 -070087}
Dan Albertdc15ffd2015-04-29 11:32:23 -070088#endif
Mark Salyzyn504d8ed2018-04-06 09:40:26 -070089
Elliott Hughes462a45e2018-06-06 12:54:41 -070090static const char* GetFileBasename(const char* file) {
91 // We can't use basename(3) even on Unix because the Mac doesn't
92 // have a non-modifying basename.
93 const char* last_slash = strrchr(file, '/');
94 if (last_slash != nullptr) {
95 return last_slash + 1;
96 }
97#if defined(_WIN32)
98 const char* last_backslash = strrchr(file, '\\');
99 if (last_backslash != nullptr) {
100 return last_backslash + 1;
101 }
102#endif
103 return file;
104}
105
Mark Salyzyn504d8ed2018-04-06 09:40:26 -0700106#if defined(__linux__)
Elliott Hughes462a45e2018-06-06 12:54:41 -0700107static int OpenKmsg() {
Mark Salyzyn504d8ed2018-04-06 09:40:26 -0700108#if defined(__ANDROID__)
109 // pick up 'file w /dev/kmsg' environment from daemon's init rc file
110 const auto val = getenv("ANDROID_FILE__dev_kmsg");
111 if (val != nullptr) {
112 int fd;
113 if (android::base::ParseInt(val, &fd, 0)) {
114 auto flags = fcntl(fd, F_GETFL);
115 if ((flags != -1) && ((flags & O_ACCMODE) == O_WRONLY)) return fd;
116 }
117 }
118#endif
119 return TEMP_FAILURE_RETRY(open("/dev/kmsg", O_WRONLY | O_CLOEXEC));
120}
121#endif
Dan Alberte3ea0582015-03-13 23:06:01 -0700122
Yabin Cui7ff958a2017-01-23 10:29:23 -0800123static std::mutex& LoggingLock() {
124 static auto& logging_lock = *new std::mutex();
125 return logging_lock;
126}
Dan Alberte3ea0582015-03-13 23:06:01 -0700127
Yabin Cui7ff958a2017-01-23 10:29:23 -0800128static LogFunction& Logger() {
Dan Albert1f65c492015-03-27 11:20:14 -0700129#ifdef __ANDROID__
Yabin Cui7ff958a2017-01-23 10:29:23 -0800130 static auto& logger = *new LogFunction(LogdLogger());
Dan Albert1f65c492015-03-27 11:20:14 -0700131#else
Yabin Cui7ff958a2017-01-23 10:29:23 -0800132 static auto& logger = *new LogFunction(StderrLogger);
Dan Albert1f65c492015-03-27 11:20:14 -0700133#endif
Yabin Cui7ff958a2017-01-23 10:29:23 -0800134 return logger;
135}
Dan Albert1f65c492015-03-27 11:20:14 -0700136
Yabin Cui7ff958a2017-01-23 10:29:23 -0800137static AbortFunction& Aborter() {
138 static auto& aborter = *new AbortFunction(DefaultAborter);
139 return aborter;
140}
141
Andreas Gampefec5b092018-03-05 10:00:19 -0800142static std::recursive_mutex& TagLock() {
143 static auto& tag_lock = *new std::recursive_mutex();
144 return tag_lock;
145}
146static std::string* gDefaultTag;
147std::string GetDefaultTag() {
148 std::lock_guard<std::recursive_mutex> lock(TagLock());
149 if (gDefaultTag == nullptr) {
150 return "";
151 }
152 return *gDefaultTag;
153}
154void SetDefaultTag(const std::string& tag) {
155 std::lock_guard<std::recursive_mutex> lock(TagLock());
156 if (gDefaultTag != nullptr) {
157 delete gDefaultTag;
158 gDefaultTag = nullptr;
159 }
160 if (!tag.empty()) {
161 gDefaultTag = new std::string(tag);
162 }
Yabin Cui7ff958a2017-01-23 10:29:23 -0800163}
Andreas Gampe9008e8d2016-09-08 11:03:58 -0700164
Dan Albert1be4dec2015-04-03 11:28:46 -0700165static bool gInitialized = false;
Dan Alberte3ea0582015-03-13 23:06:01 -0700166static LogSeverity gMinimumLogSeverity = INFO;
Dan Alberte3ea0582015-03-13 23:06:01 -0700167
Elliott Hughes8cf75f02016-08-04 16:09:39 -0700168#if defined(__linux__)
169void KernelLogger(android::base::LogId, android::base::LogSeverity severity,
170 const char* tag, const char*, unsigned int, const char* msg) {
Andreas Gamped2a4f212016-09-07 10:10:50 -0700171 // clang-format off
Elliott Hughes8cf75f02016-08-04 16:09:39 -0700172 static constexpr int kLogSeverityToKernelLogLevel[] = {
Andreas Gamped2a4f212016-09-07 10:10:50 -0700173 [android::base::VERBOSE] = 7, // KERN_DEBUG (there is no verbose kernel log
174 // level)
175 [android::base::DEBUG] = 7, // KERN_DEBUG
176 [android::base::INFO] = 6, // KERN_INFO
177 [android::base::WARNING] = 4, // KERN_WARNING
178 [android::base::ERROR] = 3, // KERN_ERROR
179 [android::base::FATAL_WITHOUT_ABORT] = 2, // KERN_CRIT
180 [android::base::FATAL] = 2, // KERN_CRIT
Elliott Hughes8cf75f02016-08-04 16:09:39 -0700181 };
Andreas Gamped2a4f212016-09-07 10:10:50 -0700182 // clang-format on
Elliott Hughes8cf75f02016-08-04 16:09:39 -0700183 static_assert(arraysize(kLogSeverityToKernelLogLevel) == android::base::FATAL + 1,
184 "Mismatch in size of kLogSeverityToKernelLogLevel and values in LogSeverity");
185
Mark Salyzyn504d8ed2018-04-06 09:40:26 -0700186 static int klog_fd = OpenKmsg();
Elliott Hughes8cf75f02016-08-04 16:09:39 -0700187 if (klog_fd == -1) return;
188
189 int level = kLogSeverityToKernelLogLevel[severity];
190
191 // The kernel's printk buffer is only 1024 bytes.
192 // TODO: should we automatically break up long lines into multiple lines?
193 // Or we could log but with something like "..." at the end?
194 char buf[1024];
195 size_t size = snprintf(buf, sizeof(buf), "<%d>%s: %s\n", level, tag, msg);
196 if (size > sizeof(buf)) {
197 size = snprintf(buf, sizeof(buf), "<%d>%s: %zu-byte message too long for printk\n",
198 level, tag, size);
199 }
200
201 iovec iov[1];
202 iov[0].iov_base = buf;
203 iov[0].iov_len = size;
204 TEMP_FAILURE_RETRY(writev(klog_fd, iov, 1));
205}
206#endif
207
Tomasz Wasilczyka419de22017-12-18 06:30:17 -0800208void StderrLogger(LogId, LogSeverity severity, const char* tag, const char* file, unsigned int line,
209 const char* message) {
Elliott Hughes6522eb52016-06-21 14:25:44 -0700210 struct tm now;
211 time_t t = time(nullptr);
212
213#if defined(_WIN32)
214 localtime_s(&now, &t);
215#else
216 localtime_r(&t, &now);
217#endif
218
219 char timestamp[32];
220 strftime(timestamp, sizeof(timestamp), "%m-%d %H:%M:%S", &now);
221
Andreas Gamped2a4f212016-09-07 10:10:50 -0700222 static const char log_characters[] = "VDIWEFF";
Spencer Low55853a92015-08-11 16:00:13 -0700223 static_assert(arraysize(log_characters) - 1 == FATAL + 1,
224 "Mismatch in size of log_characters and values in LogSeverity");
Dan Albert1f65c492015-03-27 11:20:14 -0700225 char severity_char = log_characters[severity];
Josh Gaoef102be2018-03-16 14:25:42 -0700226 fprintf(stderr, "%s %c %s %5d %5" PRIu64 " %s:%u] %s\n", tag ? tag : "nullptr", severity_char,
227 timestamp, getpid(), GetThreadId(), file, line, message);
Dan Albert1f65c492015-03-27 11:20:14 -0700228}
229
Elliott Hughes1c1409f2018-05-23 09:16:46 -0700230void StdioLogger(LogId, LogSeverity severity, const char* /*tag*/, const char* /*file*/,
231 unsigned int /*line*/, const char* message) {
232 if (severity >= WARNING) {
233 fflush(stdout);
Elliott Hughes462a45e2018-06-06 12:54:41 -0700234 fprintf(stderr, "%s: %s\n", GetFileBasename(getprogname()), message);
Elliott Hughes1c1409f2018-05-23 09:16:46 -0700235 } else {
236 fprintf(stdout, "%s\n", message);
237 }
238}
239
Andreas Gampe9008e8d2016-09-08 11:03:58 -0700240void DefaultAborter(const char* abort_message) {
241#ifdef __ANDROID__
242 android_set_abort_message(abort_message);
243#else
244 UNUSED(abort_message);
245#endif
246 abort();
247}
248
Dan Albert1f65c492015-03-27 11:20:14 -0700249
250#ifdef __ANDROID__
251LogdLogger::LogdLogger(LogId default_log_id) : default_log_id_(default_log_id) {
252}
253
Dan Albert1f65c492015-03-27 11:20:14 -0700254void LogdLogger::operator()(LogId id, LogSeverity severity, const char* tag,
255 const char* file, unsigned int line,
256 const char* message) {
Elliott Hughes8cf75f02016-08-04 16:09:39 -0700257 static constexpr android_LogPriority kLogSeverityToAndroidLogPriority[] = {
Andreas Gamped2a4f212016-09-07 10:10:50 -0700258 ANDROID_LOG_VERBOSE, ANDROID_LOG_DEBUG, ANDROID_LOG_INFO,
259 ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL,
260 ANDROID_LOG_FATAL,
Elliott Hughes8cf75f02016-08-04 16:09:39 -0700261 };
262 static_assert(arraysize(kLogSeverityToAndroidLogPriority) == FATAL + 1,
263 "Mismatch in size of kLogSeverityToAndroidLogPriority and values in LogSeverity");
264
Dan Albert1f65c492015-03-27 11:20:14 -0700265 int priority = kLogSeverityToAndroidLogPriority[severity];
266 if (id == DEFAULT) {
267 id = default_log_id_;
268 }
269
Elliott Hughes8cf75f02016-08-04 16:09:39 -0700270 static constexpr log_id kLogIdToAndroidLogId[] = {
271 LOG_ID_MAX, LOG_ID_MAIN, LOG_ID_SYSTEM,
272 };
273 static_assert(arraysize(kLogIdToAndroidLogId) == SYSTEM + 1,
274 "Mismatch in size of kLogIdToAndroidLogId and values in LogId");
Dan Albert1f65c492015-03-27 11:20:14 -0700275 log_id lg_id = kLogIdToAndroidLogId[id];
276
277 if (priority == ANDROID_LOG_FATAL) {
278 __android_log_buf_print(lg_id, priority, tag, "%s:%u] %s", file, line,
279 message);
280 } else {
281 __android_log_buf_print(lg_id, priority, tag, "%s", message);
282 }
283}
284#endif
285
Andreas Gampe9008e8d2016-09-08 11:03:58 -0700286void InitLogging(char* argv[], LogFunction&& logger, AbortFunction&& aborter) {
Dan Albert1f65c492015-03-27 11:20:14 -0700287 SetLogger(std::forward<LogFunction>(logger));
Andreas Gampe9008e8d2016-09-08 11:03:58 -0700288 SetAborter(std::forward<AbortFunction>(aborter));
Dan Albert1f65c492015-03-27 11:20:14 -0700289
Dan Albert1be4dec2015-04-03 11:28:46 -0700290 if (gInitialized) {
Dan Alberte3ea0582015-03-13 23:06:01 -0700291 return;
292 }
293
Dan Albert1be4dec2015-04-03 11:28:46 -0700294 gInitialized = true;
295
Dan Alberte3ea0582015-03-13 23:06:01 -0700296 // Stash the command line for later use. We can use /proc/self/cmdline on
Spencer Lowbec78622015-11-07 18:51:54 -0800297 // Linux to recover this, but we don't have that luxury on the Mac/Windows,
298 // and there are a couple of argv[0] variants that are commonly used.
Dan Alberte3ea0582015-03-13 23:06:01 -0700299 if (argv != nullptr) {
Andreas Gampefec5b092018-03-05 10:00:19 -0800300 SetDefaultTag(basename(argv[0]));
Dan Alberte3ea0582015-03-13 23:06:01 -0700301 }
Dan Albert1be4dec2015-04-03 11:28:46 -0700302
Dan Alberte3ea0582015-03-13 23:06:01 -0700303 const char* tags = getenv("ANDROID_LOG_TAGS");
304 if (tags == nullptr) {
305 return;
306 }
307
Dan Albert0d716d02015-03-19 13:24:26 -0700308 std::vector<std::string> specs = Split(tags, " ");
Dan Alberte3ea0582015-03-13 23:06:01 -0700309 for (size_t i = 0; i < specs.size(); ++i) {
310 // "tag-pattern:[vdiwefs]"
311 std::string spec(specs[i]);
312 if (spec.size() == 3 && StartsWith(spec, "*:")) {
313 switch (spec[2]) {
314 case 'v':
315 gMinimumLogSeverity = VERBOSE;
316 continue;
317 case 'd':
318 gMinimumLogSeverity = DEBUG;
319 continue;
320 case 'i':
321 gMinimumLogSeverity = INFO;
322 continue;
323 case 'w':
324 gMinimumLogSeverity = WARNING;
325 continue;
326 case 'e':
327 gMinimumLogSeverity = ERROR;
328 continue;
329 case 'f':
Andreas Gamped2a4f212016-09-07 10:10:50 -0700330 gMinimumLogSeverity = FATAL_WITHOUT_ABORT;
Dan Alberte3ea0582015-03-13 23:06:01 -0700331 continue;
332 // liblog will even suppress FATAL if you say 's' for silent, but that's
333 // crazy!
334 case 's':
Andreas Gamped2a4f212016-09-07 10:10:50 -0700335 gMinimumLogSeverity = FATAL_WITHOUT_ABORT;
Dan Alberte3ea0582015-03-13 23:06:01 -0700336 continue;
337 }
338 }
339 LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags
340 << ")";
341 }
342}
343
Dan Albert1f65c492015-03-27 11:20:14 -0700344void SetLogger(LogFunction&& logger) {
Yabin Cui7ff958a2017-01-23 10:29:23 -0800345 std::lock_guard<std::mutex> lock(LoggingLock());
346 Logger() = std::move(logger);
Dan Albert1f65c492015-03-27 11:20:14 -0700347}
348
Andreas Gampe9008e8d2016-09-08 11:03:58 -0700349void SetAborter(AbortFunction&& aborter) {
Yabin Cui7ff958a2017-01-23 10:29:23 -0800350 std::lock_guard<std::mutex> lock(LoggingLock());
351 Aborter() = std::move(aborter);
Andreas Gampe9008e8d2016-09-08 11:03:58 -0700352}
353
Dan Alberte3ea0582015-03-13 23:06:01 -0700354// This indirection greatly reduces the stack impact of having lots of
355// checks/logging in a function.
356class LogMessageData {
357 public:
Tomasz Wasilczyka419de22017-12-18 06:30:17 -0800358 LogMessageData(const char* file, unsigned int line, LogId id, LogSeverity severity,
359 const char* tag, int error)
Spencer Low55853a92015-08-11 16:00:13 -0700360 : file_(GetFileBasename(file)),
Dan Albertab5c8822015-03-27 11:20:14 -0700361 line_number_(line),
362 id_(id),
363 severity_(severity),
Tomasz Wasilczyka419de22017-12-18 06:30:17 -0800364 tag_(tag),
365 error_(error) {}
Dan Alberte3ea0582015-03-13 23:06:01 -0700366
367 const char* GetFile() const {
368 return file_;
369 }
370
371 unsigned int GetLineNumber() const {
372 return line_number_;
373 }
374
375 LogSeverity GetSeverity() const {
376 return severity_;
377 }
378
Tomasz Wasilczyka419de22017-12-18 06:30:17 -0800379 const char* GetTag() const { return tag_; }
380
Dan Albertab5c8822015-03-27 11:20:14 -0700381 LogId GetId() const {
382 return id_;
383 }
384
Dan Alberte3ea0582015-03-13 23:06:01 -0700385 int GetError() const {
386 return error_;
387 }
388
389 std::ostream& GetBuffer() {
390 return buffer_;
391 }
392
393 std::string ToString() const {
394 return buffer_.str();
395 }
396
397 private:
398 std::ostringstream buffer_;
399 const char* const file_;
400 const unsigned int line_number_;
Dan Albertab5c8822015-03-27 11:20:14 -0700401 const LogId id_;
Dan Alberte3ea0582015-03-13 23:06:01 -0700402 const LogSeverity severity_;
Tomasz Wasilczyka419de22017-12-18 06:30:17 -0800403 const char* const tag_;
Dan Alberte3ea0582015-03-13 23:06:01 -0700404 const int error_;
405
406 DISALLOW_COPY_AND_ASSIGN(LogMessageData);
407};
408
Tomasz Wasilczyka419de22017-12-18 06:30:17 -0800409LogMessage::LogMessage(const char* file, unsigned int line, LogId id, LogSeverity severity,
410 const char* tag, int error)
411 : data_(new LogMessageData(file, line, id, severity, tag, error)) {}
412
Dan Alberte3ea0582015-03-13 23:06:01 -0700413LogMessage::~LogMessage() {
Andreas Gampec65ea942016-09-23 13:31:52 -0700414 // Check severity again. This is duplicate work wrt/ LOG macros, but not LOG_STREAM.
Andreas Gampecb35a4a2016-09-23 16:37:12 -0700415 if (!WOULD_LOG(data_->GetSeverity())) {
Andreas Gampec65ea942016-09-23 13:31:52 -0700416 return;
417 }
418
Dan Alberte3ea0582015-03-13 23:06:01 -0700419 // Finish constructing the message.
420 if (data_->GetError() != -1) {
421 data_->GetBuffer() << ": " << strerror(data_->GetError());
422 }
423 std::string msg(data_->ToString());
424
Spencer Lowe0671d62015-09-17 19:36:10 -0700425 {
426 // Do the actual logging with the lock held.
Yabin Cui7ff958a2017-01-23 10:29:23 -0800427 std::lock_guard<std::mutex> lock(LoggingLock());
Spencer Lowe0671d62015-09-17 19:36:10 -0700428 if (msg.find('\n') == std::string::npos) {
Tomasz Wasilczyka419de22017-12-18 06:30:17 -0800429 LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(), data_->GetSeverity(),
430 data_->GetTag(), msg.c_str());
Spencer Lowe0671d62015-09-17 19:36:10 -0700431 } else {
432 msg += '\n';
433 size_t i = 0;
434 while (i < msg.size()) {
435 size_t nl = msg.find('\n', i);
436 msg[nl] = '\0';
Tomasz Wasilczyka419de22017-12-18 06:30:17 -0800437 LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(), data_->GetSeverity(),
438 data_->GetTag(), &msg[i]);
Andreas Gampe1ed2be02016-10-04 19:17:07 -0700439 // Undo the zero-termination so we can give the complete message to the aborter.
440 msg[nl] = '\n';
Spencer Lowe0671d62015-09-17 19:36:10 -0700441 i = nl + 1;
442 }
Dan Alberte3ea0582015-03-13 23:06:01 -0700443 }
444 }
445
446 // Abort if necessary.
447 if (data_->GetSeverity() == FATAL) {
Yabin Cui7ff958a2017-01-23 10:29:23 -0800448 Aborter()(msg.c_str());
Dan Alberte3ea0582015-03-13 23:06:01 -0700449 }
450}
451
452std::ostream& LogMessage::stream() {
453 return data_->GetBuffer();
454}
455
Tomasz Wasilczyka419de22017-12-18 06:30:17 -0800456void LogMessage::LogLine(const char* file, unsigned int line, LogId id, LogSeverity severity,
457 const char* tag, const char* message) {
Andreas Gampefec5b092018-03-05 10:00:19 -0800458 if (tag == nullptr) {
459 std::lock_guard<std::recursive_mutex> lock(TagLock());
460 if (gDefaultTag == nullptr) {
461 gDefaultTag = new std::string(getprogname());
462 }
463 Logger()(id, severity, gDefaultTag->c_str(), file, line, message);
464 } else {
465 Logger()(id, severity, tag, file, line, message);
466 }
Dan Alberte3ea0582015-03-13 23:06:01 -0700467}
468
Elliott Hughes8cf75f02016-08-04 16:09:39 -0700469LogSeverity GetMinimumLogSeverity() {
470 return gMinimumLogSeverity;
471}
472
473LogSeverity SetMinimumLogSeverity(LogSeverity new_severity) {
474 LogSeverity old_severity = gMinimumLogSeverity;
475 gMinimumLogSeverity = new_severity;
476 return old_severity;
477}
478
479ScopedLogSeverity::ScopedLogSeverity(LogSeverity new_severity) {
480 old_ = SetMinimumLogSeverity(new_severity);
Dan Alberte3ea0582015-03-13 23:06:01 -0700481}
482
483ScopedLogSeverity::~ScopedLogSeverity() {
Elliott Hughes8cf75f02016-08-04 16:09:39 -0700484 SetMinimumLogSeverity(old_);
Dan Alberte3ea0582015-03-13 23:06:01 -0700485}
486
487} // namespace base
488} // namespace android