blob: a33da22116bc80f47a6782b8b745d8065aba9be2 [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
Dan Albertdc15ffd2015-04-29 11:32:23 -070061namespace {
Dan Albertdc15ffd2015-04-29 11:32:23 -070062#if defined(__GLIBC__)
63const char* getprogname() {
64 return program_invocation_short_name;
65}
Josh Gaob08de452016-09-13 14:57:12 -070066#elif defined(_WIN32)
Dan Albertdc15ffd2015-04-29 11:32:23 -070067const char* getprogname() {
68 static bool first = true;
69 static char progname[MAX_PATH] = {};
70
71 if (first) {
Spencer Low55853a92015-08-11 16:00:13 -070072 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 Albertdc15ffd2015-04-29 11:32:23 -070080 first = false;
81 }
82
83 return progname;
84}
Dan Albertdc15ffd2015-04-29 11:32:23 -070085#endif
Mark Salyzyn504d8ed2018-04-06 09:40:26 -070086
87#if defined(__linux__)
88int 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 Albertdc15ffd2015-04-29 11:32:23 -0700103} // namespace
104
Dan Alberte3ea0582015-03-13 23:06:01 -0700105namespace android {
106namespace base {
107
Yabin Cui7ff958a2017-01-23 10:29:23 -0800108static std::mutex& LoggingLock() {
109 static auto& logging_lock = *new std::mutex();
110 return logging_lock;
111}
Dan Alberte3ea0582015-03-13 23:06:01 -0700112
Yabin Cui7ff958a2017-01-23 10:29:23 -0800113static LogFunction& Logger() {
Dan Albert1f65c492015-03-27 11:20:14 -0700114#ifdef __ANDROID__
Yabin Cui7ff958a2017-01-23 10:29:23 -0800115 static auto& logger = *new LogFunction(LogdLogger());
Dan Albert1f65c492015-03-27 11:20:14 -0700116#else
Yabin Cui7ff958a2017-01-23 10:29:23 -0800117 static auto& logger = *new LogFunction(StderrLogger);
Dan Albert1f65c492015-03-27 11:20:14 -0700118#endif
Yabin Cui7ff958a2017-01-23 10:29:23 -0800119 return logger;
120}
Dan Albert1f65c492015-03-27 11:20:14 -0700121
Yabin Cui7ff958a2017-01-23 10:29:23 -0800122static AbortFunction& Aborter() {
123 static auto& aborter = *new AbortFunction(DefaultAborter);
124 return aborter;
125}
126
Andreas Gampefec5b092018-03-05 10:00:19 -0800127static std::recursive_mutex& TagLock() {
128 static auto& tag_lock = *new std::recursive_mutex();
129 return tag_lock;
130}
131static std::string* gDefaultTag;
132std::string GetDefaultTag() {
133 std::lock_guard<std::recursive_mutex> lock(TagLock());
134 if (gDefaultTag == nullptr) {
135 return "";
136 }
137 return *gDefaultTag;
138}
139void 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 Cui7ff958a2017-01-23 10:29:23 -0800148}
Andreas Gampe9008e8d2016-09-08 11:03:58 -0700149
Dan Albert1be4dec2015-04-03 11:28:46 -0700150static bool gInitialized = false;
Dan Alberte3ea0582015-03-13 23:06:01 -0700151static LogSeverity gMinimumLogSeverity = INFO;
Dan Alberte3ea0582015-03-13 23:06:01 -0700152
Elliott Hughes8cf75f02016-08-04 16:09:39 -0700153#if defined(__linux__)
154void KernelLogger(android::base::LogId, android::base::LogSeverity severity,
155 const char* tag, const char*, unsigned int, const char* msg) {
Andreas Gamped2a4f212016-09-07 10:10:50 -0700156 // clang-format off
Elliott Hughes8cf75f02016-08-04 16:09:39 -0700157 static constexpr int kLogSeverityToKernelLogLevel[] = {
Andreas Gamped2a4f212016-09-07 10:10:50 -0700158 [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 Hughes8cf75f02016-08-04 16:09:39 -0700166 };
Andreas Gamped2a4f212016-09-07 10:10:50 -0700167 // clang-format on
Elliott Hughes8cf75f02016-08-04 16:09:39 -0700168 static_assert(arraysize(kLogSeverityToKernelLogLevel) == android::base::FATAL + 1,
169 "Mismatch in size of kLogSeverityToKernelLogLevel and values in LogSeverity");
170
Mark Salyzyn504d8ed2018-04-06 09:40:26 -0700171 static int klog_fd = OpenKmsg();
Elliott Hughes8cf75f02016-08-04 16:09:39 -0700172 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 Wasilczyka419de22017-12-18 06:30:17 -0800193void StderrLogger(LogId, LogSeverity severity, const char* tag, const char* file, unsigned int line,
194 const char* message) {
Elliott Hughes6522eb52016-06-21 14:25:44 -0700195 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 Gamped2a4f212016-09-07 10:10:50 -0700207 static const char log_characters[] = "VDIWEFF";
Spencer Low55853a92015-08-11 16:00:13 -0700208 static_assert(arraysize(log_characters) - 1 == FATAL + 1,
209 "Mismatch in size of log_characters and values in LogSeverity");
Dan Albert1f65c492015-03-27 11:20:14 -0700210 char severity_char = log_characters[severity];
Josh Gaoef102be2018-03-16 14:25:42 -0700211 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 Albert1f65c492015-03-27 11:20:14 -0700213}
214
Andreas Gampe9008e8d2016-09-08 11:03:58 -0700215void 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 Albert1f65c492015-03-27 11:20:14 -0700224
225#ifdef __ANDROID__
226LogdLogger::LogdLogger(LogId default_log_id) : default_log_id_(default_log_id) {
227}
228
Dan Albert1f65c492015-03-27 11:20:14 -0700229void LogdLogger::operator()(LogId id, LogSeverity severity, const char* tag,
230 const char* file, unsigned int line,
231 const char* message) {
Elliott Hughes8cf75f02016-08-04 16:09:39 -0700232 static constexpr android_LogPriority kLogSeverityToAndroidLogPriority[] = {
Andreas Gamped2a4f212016-09-07 10:10:50 -0700233 ANDROID_LOG_VERBOSE, ANDROID_LOG_DEBUG, ANDROID_LOG_INFO,
234 ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL,
235 ANDROID_LOG_FATAL,
Elliott Hughes8cf75f02016-08-04 16:09:39 -0700236 };
237 static_assert(arraysize(kLogSeverityToAndroidLogPriority) == FATAL + 1,
238 "Mismatch in size of kLogSeverityToAndroidLogPriority and values in LogSeverity");
239
Dan Albert1f65c492015-03-27 11:20:14 -0700240 int priority = kLogSeverityToAndroidLogPriority[severity];
241 if (id == DEFAULT) {
242 id = default_log_id_;
243 }
244
Elliott Hughes8cf75f02016-08-04 16:09:39 -0700245 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 Albert1f65c492015-03-27 11:20:14 -0700250 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 Gampe9008e8d2016-09-08 11:03:58 -0700261void InitLogging(char* argv[], LogFunction&& logger, AbortFunction&& aborter) {
Dan Albert1f65c492015-03-27 11:20:14 -0700262 SetLogger(std::forward<LogFunction>(logger));
Andreas Gampe9008e8d2016-09-08 11:03:58 -0700263 SetAborter(std::forward<AbortFunction>(aborter));
Dan Albert1f65c492015-03-27 11:20:14 -0700264
Dan Albert1be4dec2015-04-03 11:28:46 -0700265 if (gInitialized) {
Dan Alberte3ea0582015-03-13 23:06:01 -0700266 return;
267 }
268
Dan Albert1be4dec2015-04-03 11:28:46 -0700269 gInitialized = true;
270
Dan Alberte3ea0582015-03-13 23:06:01 -0700271 // Stash the command line for later use. We can use /proc/self/cmdline on
Spencer Lowbec78622015-11-07 18:51:54 -0800272 // 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 Alberte3ea0582015-03-13 23:06:01 -0700274 if (argv != nullptr) {
Andreas Gampefec5b092018-03-05 10:00:19 -0800275 SetDefaultTag(basename(argv[0]));
Dan Alberte3ea0582015-03-13 23:06:01 -0700276 }
Dan Albert1be4dec2015-04-03 11:28:46 -0700277
Dan Alberte3ea0582015-03-13 23:06:01 -0700278 const char* tags = getenv("ANDROID_LOG_TAGS");
279 if (tags == nullptr) {
280 return;
281 }
282
Dan Albert0d716d02015-03-19 13:24:26 -0700283 std::vector<std::string> specs = Split(tags, " ");
Dan Alberte3ea0582015-03-13 23:06:01 -0700284 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 Gamped2a4f212016-09-07 10:10:50 -0700305 gMinimumLogSeverity = FATAL_WITHOUT_ABORT;
Dan Alberte3ea0582015-03-13 23:06:01 -0700306 continue;
307 // liblog will even suppress FATAL if you say 's' for silent, but that's
308 // crazy!
309 case 's':
Andreas Gamped2a4f212016-09-07 10:10:50 -0700310 gMinimumLogSeverity = FATAL_WITHOUT_ABORT;
Dan Alberte3ea0582015-03-13 23:06:01 -0700311 continue;
312 }
313 }
314 LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags
315 << ")";
316 }
317}
318
Dan Albert1f65c492015-03-27 11:20:14 -0700319void SetLogger(LogFunction&& logger) {
Yabin Cui7ff958a2017-01-23 10:29:23 -0800320 std::lock_guard<std::mutex> lock(LoggingLock());
321 Logger() = std::move(logger);
Dan Albert1f65c492015-03-27 11:20:14 -0700322}
323
Andreas Gampe9008e8d2016-09-08 11:03:58 -0700324void SetAborter(AbortFunction&& aborter) {
Yabin Cui7ff958a2017-01-23 10:29:23 -0800325 std::lock_guard<std::mutex> lock(LoggingLock());
326 Aborter() = std::move(aborter);
Andreas Gampe9008e8d2016-09-08 11:03:58 -0700327}
328
Spencer Low55853a92015-08-11 16:00:13 -0700329static const char* GetFileBasename(const char* file) {
Spencer Lowbec78622015-11-07 18:51:54 -0800330 // We can't use basename(3) even on Unix because the Mac doesn't
331 // have a non-modifying basename.
Spencer Low55853a92015-08-11 16:00:13 -0700332 const char* last_slash = strrchr(file, '/');
Spencer Lowbec78622015-11-07 18:51:54 -0800333 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 Low55853a92015-08-11 16:00:13 -0700343}
344
Dan Alberte3ea0582015-03-13 23:06:01 -0700345// This indirection greatly reduces the stack impact of having lots of
346// checks/logging in a function.
347class LogMessageData {
348 public:
Tomasz Wasilczyka419de22017-12-18 06:30:17 -0800349 LogMessageData(const char* file, unsigned int line, LogId id, LogSeverity severity,
350 const char* tag, int error)
Spencer Low55853a92015-08-11 16:00:13 -0700351 : file_(GetFileBasename(file)),
Dan Albertab5c8822015-03-27 11:20:14 -0700352 line_number_(line),
353 id_(id),
354 severity_(severity),
Tomasz Wasilczyka419de22017-12-18 06:30:17 -0800355 tag_(tag),
356 error_(error) {}
Dan Alberte3ea0582015-03-13 23:06:01 -0700357
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 Wasilczyka419de22017-12-18 06:30:17 -0800370 const char* GetTag() const { return tag_; }
371
Dan Albertab5c8822015-03-27 11:20:14 -0700372 LogId GetId() const {
373 return id_;
374 }
375
Dan Alberte3ea0582015-03-13 23:06:01 -0700376 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 Albertab5c8822015-03-27 11:20:14 -0700392 const LogId id_;
Dan Alberte3ea0582015-03-13 23:06:01 -0700393 const LogSeverity severity_;
Tomasz Wasilczyka419de22017-12-18 06:30:17 -0800394 const char* const tag_;
Dan Alberte3ea0582015-03-13 23:06:01 -0700395 const int error_;
396
397 DISALLOW_COPY_AND_ASSIGN(LogMessageData);
398};
399
Tomasz Wasilczyka419de22017-12-18 06:30:17 -0800400LogMessage::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
404LogMessage::LogMessage(const char* file, unsigned int line, LogId id, LogSeverity severity,
405 int error)
406 : LogMessage(file, line, id, severity, nullptr, error) {}
Dan Alberte3ea0582015-03-13 23:06:01 -0700407
408LogMessage::~LogMessage() {
Andreas Gampec65ea942016-09-23 13:31:52 -0700409 // Check severity again. This is duplicate work wrt/ LOG macros, but not LOG_STREAM.
Andreas Gampecb35a4a2016-09-23 16:37:12 -0700410 if (!WOULD_LOG(data_->GetSeverity())) {
Andreas Gampec65ea942016-09-23 13:31:52 -0700411 return;
412 }
413
Dan Alberte3ea0582015-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
Spencer Lowe0671d62015-09-17 19:36:10 -0700420 {
421 // Do the actual logging with the lock held.
Yabin Cui7ff958a2017-01-23 10:29:23 -0800422 std::lock_guard<std::mutex> lock(LoggingLock());
Spencer Lowe0671d62015-09-17 19:36:10 -0700423 if (msg.find('\n') == std::string::npos) {
Tomasz Wasilczyka419de22017-12-18 06:30:17 -0800424 LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(), data_->GetSeverity(),
425 data_->GetTag(), msg.c_str());
Spencer Lowe0671d62015-09-17 19:36:10 -0700426 } 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 Wasilczyka419de22017-12-18 06:30:17 -0800432 LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(), data_->GetSeverity(),
433 data_->GetTag(), &msg[i]);
Andreas Gampe1ed2be02016-10-04 19:17:07 -0700434 // Undo the zero-termination so we can give the complete message to the aborter.
435 msg[nl] = '\n';
Spencer Lowe0671d62015-09-17 19:36:10 -0700436 i = nl + 1;
437 }
Dan Alberte3ea0582015-03-13 23:06:01 -0700438 }
439 }
440
441 // Abort if necessary.
442 if (data_->GetSeverity() == FATAL) {
Yabin Cui7ff958a2017-01-23 10:29:23 -0800443 Aborter()(msg.c_str());
Dan Alberte3ea0582015-03-13 23:06:01 -0700444 }
445}
446
447std::ostream& LogMessage::stream() {
448 return data_->GetBuffer();
449}
450
Tomasz Wasilczyka419de22017-12-18 06:30:17 -0800451void LogMessage::LogLine(const char* file, unsigned int line, LogId id, LogSeverity severity,
452 const char* tag, const char* message) {
Andreas Gampefec5b092018-03-05 10:00:19 -0800453 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 Alberte3ea0582015-03-13 23:06:01 -0700462}
463
Tomasz Wasilczyka419de22017-12-18 06:30:17 -0800464void 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 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