blob: f89168c0fc4dff5a1808ea23d82d476452ba22d5 [file] [log] [blame]
Dan Albert58310b42015-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 Hughes7bc87a52016-08-04 16:09:39 -070017#if defined(_WIN32)
Spencer Lowac3f7d92015-05-19 22:12:06 -070018#include <windows.h>
19#endif
20
Elliott Hughes4f713192015-12-04 22:00:26 -080021#include "android-base/logging.h"
Dan Albert58310b42015-03-13 23:06:01 -070022
Elliott Hughes7bc87a52016-08-04 16:09:39 -070023#include <fcntl.h>
Josh Gao5791e212018-03-16 14:25:42 -070024#include <inttypes.h>
Dan Albert7a87d052015-04-03 11:28:46 -070025#include <libgen.h>
Elliott Hughes4e5fd112016-06-21 14:25:44 -070026#include <time.h>
Dan Albert7a87d052015-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 Hughes7bc87a52016-08-04 16:09:39 -070035#if defined(__linux__)
36#include <sys/uio.h>
37#endif
38
Dan Albert58310b42015-03-13 23:06:01 -070039#include <iostream>
40#include <limits>
Josh Gao63bdcb52016-09-13 14:57:12 -070041#include <mutex>
Dan Albert58310b42015-03-13 23:06:01 -070042#include <sstream>
43#include <string>
Dan Albertb547c852015-03-27 11:20:14 -070044#include <utility>
Dan Albert58310b42015-03-13 23:06:01 -070045#include <vector>
46
Dan Albert58310b42015-03-13 23:06:01 -070047// Headers for LogMessage::LogLine.
48#ifdef __ANDROID__
Andreas Gampeaf05f3b2018-02-15 11:40:30 -080049#include <android/log.h>
Dan Albert58310b42015-03-13 23:06:01 -070050#include <android/set_abort_message.h>
Dan Albert58310b42015-03-13 23:06:01 -070051#else
52#include <sys/types.h>
53#include <unistd.h>
54#endif
55
Elliott Hughes4679a392018-10-19 13:59:44 -070056#include <android-base/file.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070057#include <android-base/macros.h>
Mark Salyzyn2507a042018-04-06 09:40:26 -070058#include <android-base/parseint.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070059#include <android-base/strings.h>
Josh Gao5791e212018-03-16 14:25:42 -070060#include <android-base/threads.h>
Elliott Hughesc1fd4922015-11-11 18:02:29 +000061
Elliott Hughes11a64eb2018-06-06 12:54:41 -070062namespace android {
63namespace base {
64
65// BSD-based systems like Android/macOS have getprogname(). Others need us to provide one.
66#if defined(__GLIBC__) || defined(_WIN32)
67static const char* getprogname() {
Dan Albert5c190402015-04-29 11:32:23 -070068#if defined(__GLIBC__)
Dan Albert5c190402015-04-29 11:32:23 -070069 return program_invocation_short_name;
Josh Gao63bdcb52016-09-13 14:57:12 -070070#elif defined(_WIN32)
Dan Albert5c190402015-04-29 11:32:23 -070071 static bool first = true;
72 static char progname[MAX_PATH] = {};
73
74 if (first) {
Elliott Hughes4679a392018-10-19 13:59:44 -070075 snprintf(progname, sizeof(progname), "%s",
76 android::base::Basename(android::base::GetExecutablePath()).c_str());
Dan Albert5c190402015-04-29 11:32:23 -070077 first = false;
78 }
79
80 return progname;
Elliott Hughes11a64eb2018-06-06 12:54:41 -070081#endif
Dan Albert5c190402015-04-29 11:32:23 -070082}
Dan Albert5c190402015-04-29 11:32:23 -070083#endif
Mark Salyzyn2507a042018-04-06 09:40:26 -070084
Elliott Hughes11a64eb2018-06-06 12:54:41 -070085static const char* GetFileBasename(const char* file) {
86 // We can't use basename(3) even on Unix because the Mac doesn't
87 // have a non-modifying basename.
88 const char* last_slash = strrchr(file, '/');
89 if (last_slash != nullptr) {
90 return last_slash + 1;
91 }
92#if defined(_WIN32)
93 const char* last_backslash = strrchr(file, '\\');
94 if (last_backslash != nullptr) {
95 return last_backslash + 1;
96 }
97#endif
98 return file;
99}
100
Mark Salyzyn2507a042018-04-06 09:40:26 -0700101#if defined(__linux__)
Elliott Hughes11a64eb2018-06-06 12:54:41 -0700102static int OpenKmsg() {
Mark Salyzyn2507a042018-04-06 09:40:26 -0700103#if defined(__ANDROID__)
104 // pick up 'file w /dev/kmsg' environment from daemon's init rc file
105 const auto val = getenv("ANDROID_FILE__dev_kmsg");
106 if (val != nullptr) {
107 int fd;
108 if (android::base::ParseInt(val, &fd, 0)) {
109 auto flags = fcntl(fd, F_GETFL);
110 if ((flags != -1) && ((flags & O_ACCMODE) == O_WRONLY)) return fd;
111 }
112 }
113#endif
114 return TEMP_FAILURE_RETRY(open("/dev/kmsg", O_WRONLY | O_CLOEXEC));
115}
116#endif
Dan Albert58310b42015-03-13 23:06:01 -0700117
Yabin Cui0c689532017-01-23 10:29:23 -0800118static std::mutex& LoggingLock() {
119 static auto& logging_lock = *new std::mutex();
120 return logging_lock;
121}
Dan Albert58310b42015-03-13 23:06:01 -0700122
Yabin Cui0c689532017-01-23 10:29:23 -0800123static LogFunction& Logger() {
Dan Albertb547c852015-03-27 11:20:14 -0700124#ifdef __ANDROID__
Yabin Cui0c689532017-01-23 10:29:23 -0800125 static auto& logger = *new LogFunction(LogdLogger());
Dan Albertb547c852015-03-27 11:20:14 -0700126#else
Yabin Cui0c689532017-01-23 10:29:23 -0800127 static auto& logger = *new LogFunction(StderrLogger);
Dan Albertb547c852015-03-27 11:20:14 -0700128#endif
Yabin Cui0c689532017-01-23 10:29:23 -0800129 return logger;
130}
Dan Albertb547c852015-03-27 11:20:14 -0700131
Yabin Cui0c689532017-01-23 10:29:23 -0800132static AbortFunction& Aborter() {
133 static auto& aborter = *new AbortFunction(DefaultAborter);
134 return aborter;
135}
136
Andreas Gampe1923e762018-03-05 10:00:19 -0800137static std::recursive_mutex& TagLock() {
138 static auto& tag_lock = *new std::recursive_mutex();
139 return tag_lock;
140}
141static std::string* gDefaultTag;
142std::string GetDefaultTag() {
143 std::lock_guard<std::recursive_mutex> lock(TagLock());
144 if (gDefaultTag == nullptr) {
145 return "";
146 }
147 return *gDefaultTag;
148}
149void SetDefaultTag(const std::string& tag) {
150 std::lock_guard<std::recursive_mutex> lock(TagLock());
151 if (gDefaultTag != nullptr) {
152 delete gDefaultTag;
153 gDefaultTag = nullptr;
154 }
155 if (!tag.empty()) {
156 gDefaultTag = new std::string(tag);
157 }
Yabin Cui0c689532017-01-23 10:29:23 -0800158}
Andreas Gampe2691e332016-09-08 11:03:58 -0700159
Dan Albert7a87d052015-04-03 11:28:46 -0700160static bool gInitialized = false;
Dan Albert58310b42015-03-13 23:06:01 -0700161static LogSeverity gMinimumLogSeverity = INFO;
Dan Albert58310b42015-03-13 23:06:01 -0700162
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700163#if defined(__linux__)
164void KernelLogger(android::base::LogId, android::base::LogSeverity severity,
165 const char* tag, const char*, unsigned int, const char* msg) {
Andreas Gampe550829d2016-09-07 10:10:50 -0700166 // clang-format off
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700167 static constexpr int kLogSeverityToKernelLogLevel[] = {
Andreas Gampe550829d2016-09-07 10:10:50 -0700168 [android::base::VERBOSE] = 7, // KERN_DEBUG (there is no verbose kernel log
169 // level)
170 [android::base::DEBUG] = 7, // KERN_DEBUG
171 [android::base::INFO] = 6, // KERN_INFO
172 [android::base::WARNING] = 4, // KERN_WARNING
173 [android::base::ERROR] = 3, // KERN_ERROR
174 [android::base::FATAL_WITHOUT_ABORT] = 2, // KERN_CRIT
175 [android::base::FATAL] = 2, // KERN_CRIT
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700176 };
Andreas Gampe550829d2016-09-07 10:10:50 -0700177 // clang-format on
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700178 static_assert(arraysize(kLogSeverityToKernelLogLevel) == android::base::FATAL + 1,
179 "Mismatch in size of kLogSeverityToKernelLogLevel and values in LogSeverity");
180
Mark Salyzyn2507a042018-04-06 09:40:26 -0700181 static int klog_fd = OpenKmsg();
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700182 if (klog_fd == -1) return;
183
184 int level = kLogSeverityToKernelLogLevel[severity];
185
186 // The kernel's printk buffer is only 1024 bytes.
187 // TODO: should we automatically break up long lines into multiple lines?
188 // Or we could log but with something like "..." at the end?
189 char buf[1024];
190 size_t size = snprintf(buf, sizeof(buf), "<%d>%s: %s\n", level, tag, msg);
191 if (size > sizeof(buf)) {
192 size = snprintf(buf, sizeof(buf), "<%d>%s: %zu-byte message too long for printk\n",
193 level, tag, size);
194 }
195
196 iovec iov[1];
197 iov[0].iov_base = buf;
198 iov[0].iov_len = size;
199 TEMP_FAILURE_RETRY(writev(klog_fd, iov, 1));
200}
201#endif
202
Tomasz Wasilczykc2516002017-12-18 06:30:17 -0800203void StderrLogger(LogId, LogSeverity severity, const char* tag, const char* file, unsigned int line,
204 const char* message) {
Elliott Hughes4e5fd112016-06-21 14:25:44 -0700205 struct tm now;
206 time_t t = time(nullptr);
207
208#if defined(_WIN32)
209 localtime_s(&now, &t);
210#else
211 localtime_r(&t, &now);
212#endif
213
214 char timestamp[32];
215 strftime(timestamp, sizeof(timestamp), "%m-%d %H:%M:%S", &now);
216
Andreas Gampe550829d2016-09-07 10:10:50 -0700217 static const char log_characters[] = "VDIWEFF";
Spencer Lowbdab59a2015-08-11 16:00:13 -0700218 static_assert(arraysize(log_characters) - 1 == FATAL + 1,
219 "Mismatch in size of log_characters and values in LogSeverity");
Dan Albertb547c852015-03-27 11:20:14 -0700220 char severity_char = log_characters[severity];
Josh Gao5791e212018-03-16 14:25:42 -0700221 fprintf(stderr, "%s %c %s %5d %5" PRIu64 " %s:%u] %s\n", tag ? tag : "nullptr", severity_char,
222 timestamp, getpid(), GetThreadId(), file, line, message);
Dan Albertb547c852015-03-27 11:20:14 -0700223}
224
Elliott Hughes1be0d142018-05-23 09:16:46 -0700225void StdioLogger(LogId, LogSeverity severity, const char* /*tag*/, const char* /*file*/,
226 unsigned int /*line*/, const char* message) {
227 if (severity >= WARNING) {
228 fflush(stdout);
Elliott Hughes11a64eb2018-06-06 12:54:41 -0700229 fprintf(stderr, "%s: %s\n", GetFileBasename(getprogname()), message);
Elliott Hughes1be0d142018-05-23 09:16:46 -0700230 } else {
231 fprintf(stdout, "%s\n", message);
232 }
233}
234
Andreas Gampe2691e332016-09-08 11:03:58 -0700235void DefaultAborter(const char* abort_message) {
236#ifdef __ANDROID__
237 android_set_abort_message(abort_message);
238#else
239 UNUSED(abort_message);
240#endif
241 abort();
242}
243
Dan Albertb547c852015-03-27 11:20:14 -0700244
245#ifdef __ANDROID__
246LogdLogger::LogdLogger(LogId default_log_id) : default_log_id_(default_log_id) {
247}
248
Dan Albertb547c852015-03-27 11:20:14 -0700249void LogdLogger::operator()(LogId id, LogSeverity severity, const char* tag,
250 const char* file, unsigned int line,
251 const char* message) {
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700252 static constexpr android_LogPriority kLogSeverityToAndroidLogPriority[] = {
Andreas Gampe550829d2016-09-07 10:10:50 -0700253 ANDROID_LOG_VERBOSE, ANDROID_LOG_DEBUG, ANDROID_LOG_INFO,
254 ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL,
255 ANDROID_LOG_FATAL,
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700256 };
257 static_assert(arraysize(kLogSeverityToAndroidLogPriority) == FATAL + 1,
258 "Mismatch in size of kLogSeverityToAndroidLogPriority and values in LogSeverity");
259
Dan Albertb547c852015-03-27 11:20:14 -0700260 int priority = kLogSeverityToAndroidLogPriority[severity];
261 if (id == DEFAULT) {
262 id = default_log_id_;
263 }
264
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700265 static constexpr log_id kLogIdToAndroidLogId[] = {
266 LOG_ID_MAX, LOG_ID_MAIN, LOG_ID_SYSTEM,
267 };
268 static_assert(arraysize(kLogIdToAndroidLogId) == SYSTEM + 1,
269 "Mismatch in size of kLogIdToAndroidLogId and values in LogId");
Dan Albertb547c852015-03-27 11:20:14 -0700270 log_id lg_id = kLogIdToAndroidLogId[id];
271
272 if (priority == ANDROID_LOG_FATAL) {
273 __android_log_buf_print(lg_id, priority, tag, "%s:%u] %s", file, line,
274 message);
275 } else {
276 __android_log_buf_print(lg_id, priority, tag, "%s", message);
277 }
278}
279#endif
280
Andreas Gampe2691e332016-09-08 11:03:58 -0700281void InitLogging(char* argv[], LogFunction&& logger, AbortFunction&& aborter) {
Dan Albertb547c852015-03-27 11:20:14 -0700282 SetLogger(std::forward<LogFunction>(logger));
Andreas Gampe2691e332016-09-08 11:03:58 -0700283 SetAborter(std::forward<AbortFunction>(aborter));
Dan Albertb547c852015-03-27 11:20:14 -0700284
Dan Albert7a87d052015-04-03 11:28:46 -0700285 if (gInitialized) {
Dan Albert58310b42015-03-13 23:06:01 -0700286 return;
287 }
288
Dan Albert7a87d052015-04-03 11:28:46 -0700289 gInitialized = true;
290
Dan Albert58310b42015-03-13 23:06:01 -0700291 // Stash the command line for later use. We can use /proc/self/cmdline on
Spencer Low363af562015-11-07 18:51:54 -0800292 // Linux to recover this, but we don't have that luxury on the Mac/Windows,
293 // and there are a couple of argv[0] variants that are commonly used.
Dan Albert58310b42015-03-13 23:06:01 -0700294 if (argv != nullptr) {
Andreas Gampe1923e762018-03-05 10:00:19 -0800295 SetDefaultTag(basename(argv[0]));
Dan Albert58310b42015-03-13 23:06:01 -0700296 }
Dan Albert7a87d052015-04-03 11:28:46 -0700297
Dan Albert58310b42015-03-13 23:06:01 -0700298 const char* tags = getenv("ANDROID_LOG_TAGS");
299 if (tags == nullptr) {
300 return;
301 }
302
Dan Albert47328c92015-03-19 13:24:26 -0700303 std::vector<std::string> specs = Split(tags, " ");
Dan Albert58310b42015-03-13 23:06:01 -0700304 for (size_t i = 0; i < specs.size(); ++i) {
305 // "tag-pattern:[vdiwefs]"
306 std::string spec(specs[i]);
307 if (spec.size() == 3 && StartsWith(spec, "*:")) {
308 switch (spec[2]) {
309 case 'v':
310 gMinimumLogSeverity = VERBOSE;
311 continue;
312 case 'd':
313 gMinimumLogSeverity = DEBUG;
314 continue;
315 case 'i':
316 gMinimumLogSeverity = INFO;
317 continue;
318 case 'w':
319 gMinimumLogSeverity = WARNING;
320 continue;
321 case 'e':
322 gMinimumLogSeverity = ERROR;
323 continue;
324 case 'f':
Andreas Gampe550829d2016-09-07 10:10:50 -0700325 gMinimumLogSeverity = FATAL_WITHOUT_ABORT;
Dan Albert58310b42015-03-13 23:06:01 -0700326 continue;
327 // liblog will even suppress FATAL if you say 's' for silent, but that's
328 // crazy!
329 case 's':
Andreas Gampe550829d2016-09-07 10:10:50 -0700330 gMinimumLogSeverity = FATAL_WITHOUT_ABORT;
Dan Albert58310b42015-03-13 23:06:01 -0700331 continue;
332 }
333 }
334 LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags
335 << ")";
336 }
337}
338
Dan Albertb547c852015-03-27 11:20:14 -0700339void SetLogger(LogFunction&& logger) {
Yabin Cui0c689532017-01-23 10:29:23 -0800340 std::lock_guard<std::mutex> lock(LoggingLock());
341 Logger() = std::move(logger);
Dan Albertb547c852015-03-27 11:20:14 -0700342}
343
Andreas Gampe2691e332016-09-08 11:03:58 -0700344void SetAborter(AbortFunction&& aborter) {
Yabin Cui0c689532017-01-23 10:29:23 -0800345 std::lock_guard<std::mutex> lock(LoggingLock());
346 Aborter() = std::move(aborter);
Andreas Gampe2691e332016-09-08 11:03:58 -0700347}
348
Dan Albert58310b42015-03-13 23:06:01 -0700349// This indirection greatly reduces the stack impact of having lots of
350// checks/logging in a function.
351class LogMessageData {
352 public:
Tomasz Wasilczykc2516002017-12-18 06:30:17 -0800353 LogMessageData(const char* file, unsigned int line, LogId id, LogSeverity severity,
354 const char* tag, int error)
Spencer Lowbdab59a2015-08-11 16:00:13 -0700355 : file_(GetFileBasename(file)),
Dan Albert0c055862015-03-27 11:20:14 -0700356 line_number_(line),
357 id_(id),
358 severity_(severity),
Tomasz Wasilczykc2516002017-12-18 06:30:17 -0800359 tag_(tag),
360 error_(error) {}
Dan Albert58310b42015-03-13 23:06:01 -0700361
362 const char* GetFile() const {
363 return file_;
364 }
365
366 unsigned int GetLineNumber() const {
367 return line_number_;
368 }
369
370 LogSeverity GetSeverity() const {
371 return severity_;
372 }
373
Tomasz Wasilczykc2516002017-12-18 06:30:17 -0800374 const char* GetTag() const { return tag_; }
375
Dan Albert0c055862015-03-27 11:20:14 -0700376 LogId GetId() const {
377 return id_;
378 }
379
Dan Albert58310b42015-03-13 23:06:01 -0700380 int GetError() const {
381 return error_;
382 }
383
384 std::ostream& GetBuffer() {
385 return buffer_;
386 }
387
388 std::string ToString() const {
389 return buffer_.str();
390 }
391
392 private:
393 std::ostringstream buffer_;
394 const char* const file_;
395 const unsigned int line_number_;
Dan Albert0c055862015-03-27 11:20:14 -0700396 const LogId id_;
Dan Albert58310b42015-03-13 23:06:01 -0700397 const LogSeverity severity_;
Tomasz Wasilczykc2516002017-12-18 06:30:17 -0800398 const char* const tag_;
Dan Albert58310b42015-03-13 23:06:01 -0700399 const int error_;
400
401 DISALLOW_COPY_AND_ASSIGN(LogMessageData);
402};
403
Tomasz Wasilczykc2516002017-12-18 06:30:17 -0800404LogMessage::LogMessage(const char* file, unsigned int line, LogId id, LogSeverity severity,
405 const char* tag, int error)
406 : data_(new LogMessageData(file, line, id, severity, tag, error)) {}
407
Dan Albert58310b42015-03-13 23:06:01 -0700408LogMessage::~LogMessage() {
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700409 // Check severity again. This is duplicate work wrt/ LOG macros, but not LOG_STREAM.
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700410 if (!WOULD_LOG(data_->GetSeverity())) {
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700411 return;
412 }
413
Dan Albert58310b42015-03-13 23:06:01 -0700414 // Finish constructing the message.
415 if (data_->GetError() != -1) {
416 data_->GetBuffer() << ": " << strerror(data_->GetError());
417 }
418 std::string msg(data_->ToString());
419
Andreas Gampe2819c0b2018-12-05 11:26:14 -0800420 if (data_->GetSeverity() == FATAL) {
421#ifdef __ANDROID__
422 // Set the bionic abort message early to avoid liblog doing it
423 // with the individual lines, so that we get the whole message.
424 android_set_abort_message(msg.c_str());
425#endif
426 }
427
Spencer Low765ae6b2015-09-17 19:36:10 -0700428 {
429 // Do the actual logging with the lock held.
Yabin Cui0c689532017-01-23 10:29:23 -0800430 std::lock_guard<std::mutex> lock(LoggingLock());
Spencer Low765ae6b2015-09-17 19:36:10 -0700431 if (msg.find('\n') == std::string::npos) {
Tomasz Wasilczykc2516002017-12-18 06:30:17 -0800432 LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(), data_->GetSeverity(),
433 data_->GetTag(), msg.c_str());
Spencer Low765ae6b2015-09-17 19:36:10 -0700434 } else {
435 msg += '\n';
436 size_t i = 0;
437 while (i < msg.size()) {
438 size_t nl = msg.find('\n', i);
439 msg[nl] = '\0';
Tomasz Wasilczykc2516002017-12-18 06:30:17 -0800440 LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(), data_->GetSeverity(),
441 data_->GetTag(), &msg[i]);
Andreas Gampeb4e32f32016-10-04 19:17:07 -0700442 // Undo the zero-termination so we can give the complete message to the aborter.
443 msg[nl] = '\n';
Spencer Low765ae6b2015-09-17 19:36:10 -0700444 i = nl + 1;
445 }
Dan Albert58310b42015-03-13 23:06:01 -0700446 }
447 }
448
449 // Abort if necessary.
450 if (data_->GetSeverity() == FATAL) {
Yabin Cui0c689532017-01-23 10:29:23 -0800451 Aborter()(msg.c_str());
Dan Albert58310b42015-03-13 23:06:01 -0700452 }
453}
454
455std::ostream& LogMessage::stream() {
456 return data_->GetBuffer();
457}
458
Tomasz Wasilczykc2516002017-12-18 06:30:17 -0800459void LogMessage::LogLine(const char* file, unsigned int line, LogId id, LogSeverity severity,
460 const char* tag, const char* message) {
Andreas Gampe1923e762018-03-05 10:00:19 -0800461 if (tag == nullptr) {
462 std::lock_guard<std::recursive_mutex> lock(TagLock());
463 if (gDefaultTag == nullptr) {
464 gDefaultTag = new std::string(getprogname());
465 }
466 Logger()(id, severity, gDefaultTag->c_str(), file, line, message);
467 } else {
468 Logger()(id, severity, tag, file, line, message);
469 }
Dan Albert58310b42015-03-13 23:06:01 -0700470}
471
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700472LogSeverity GetMinimumLogSeverity() {
473 return gMinimumLogSeverity;
474}
475
476LogSeverity SetMinimumLogSeverity(LogSeverity new_severity) {
477 LogSeverity old_severity = gMinimumLogSeverity;
478 gMinimumLogSeverity = new_severity;
479 return old_severity;
480}
481
482ScopedLogSeverity::ScopedLogSeverity(LogSeverity new_severity) {
483 old_ = SetMinimumLogSeverity(new_severity);
Dan Albert58310b42015-03-13 23:06:01 -0700484}
485
486ScopedLogSeverity::~ScopedLogSeverity() {
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700487 SetMinimumLogSeverity(old_);
Dan Albert58310b42015-03-13 23:06:01 -0700488}
489
490} // namespace base
491} // namespace android