blob: b46abbfe60c3ccff18bc1adce4bb0e547edbe70d [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.
Andreas Gampeaf05f3b2018-02-15 11:40:30 -080048#include <android/log.h>
Tom Cherry99216302020-01-08 13:41:56 -080049#ifdef __ANDROID__
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
Dan Albertb547c852015-03-27 11:20:14 -0700245LogdLogger::LogdLogger(LogId default_log_id) : default_log_id_(default_log_id) {
246}
247
Dan Albertb547c852015-03-27 11:20:14 -0700248void LogdLogger::operator()(LogId id, LogSeverity severity, const char* tag,
249 const char* file, unsigned int line,
250 const char* message) {
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700251 static constexpr android_LogPriority kLogSeverityToAndroidLogPriority[] = {
Andreas Gampe550829d2016-09-07 10:10:50 -0700252 ANDROID_LOG_VERBOSE, ANDROID_LOG_DEBUG, ANDROID_LOG_INFO,
253 ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL,
254 ANDROID_LOG_FATAL,
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700255 };
256 static_assert(arraysize(kLogSeverityToAndroidLogPriority) == FATAL + 1,
257 "Mismatch in size of kLogSeverityToAndroidLogPriority and values in LogSeverity");
258
Dan Albertb547c852015-03-27 11:20:14 -0700259 int priority = kLogSeverityToAndroidLogPriority[severity];
260 if (id == DEFAULT) {
261 id = default_log_id_;
262 }
263
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700264 static constexpr log_id kLogIdToAndroidLogId[] = {
265 LOG_ID_MAX, LOG_ID_MAIN, LOG_ID_SYSTEM,
266 };
267 static_assert(arraysize(kLogIdToAndroidLogId) == SYSTEM + 1,
268 "Mismatch in size of kLogIdToAndroidLogId and values in LogId");
Dan Albertb547c852015-03-27 11:20:14 -0700269 log_id lg_id = kLogIdToAndroidLogId[id];
270
271 if (priority == ANDROID_LOG_FATAL) {
272 __android_log_buf_print(lg_id, priority, tag, "%s:%u] %s", file, line,
273 message);
274 } else {
275 __android_log_buf_print(lg_id, priority, tag, "%s", message);
276 }
277}
Dan Albertb547c852015-03-27 11:20:14 -0700278
Andreas Gampe2691e332016-09-08 11:03:58 -0700279void InitLogging(char* argv[], LogFunction&& logger, AbortFunction&& aborter) {
Dan Albertb547c852015-03-27 11:20:14 -0700280 SetLogger(std::forward<LogFunction>(logger));
Andreas Gampe2691e332016-09-08 11:03:58 -0700281 SetAborter(std::forward<AbortFunction>(aborter));
Dan Albertb547c852015-03-27 11:20:14 -0700282
Dan Albert7a87d052015-04-03 11:28:46 -0700283 if (gInitialized) {
Dan Albert58310b42015-03-13 23:06:01 -0700284 return;
285 }
286
Dan Albert7a87d052015-04-03 11:28:46 -0700287 gInitialized = true;
288
Dan Albert58310b42015-03-13 23:06:01 -0700289 // Stash the command line for later use. We can use /proc/self/cmdline on
Spencer Low363af562015-11-07 18:51:54 -0800290 // Linux to recover this, but we don't have that luxury on the Mac/Windows,
291 // and there are a couple of argv[0] variants that are commonly used.
Dan Albert58310b42015-03-13 23:06:01 -0700292 if (argv != nullptr) {
Andreas Gampe1923e762018-03-05 10:00:19 -0800293 SetDefaultTag(basename(argv[0]));
Dan Albert58310b42015-03-13 23:06:01 -0700294 }
Dan Albert7a87d052015-04-03 11:28:46 -0700295
Dan Albert58310b42015-03-13 23:06:01 -0700296 const char* tags = getenv("ANDROID_LOG_TAGS");
297 if (tags == nullptr) {
298 return;
299 }
300
Dan Albert47328c92015-03-19 13:24:26 -0700301 std::vector<std::string> specs = Split(tags, " ");
Dan Albert58310b42015-03-13 23:06:01 -0700302 for (size_t i = 0; i < specs.size(); ++i) {
303 // "tag-pattern:[vdiwefs]"
304 std::string spec(specs[i]);
305 if (spec.size() == 3 && StartsWith(spec, "*:")) {
306 switch (spec[2]) {
307 case 'v':
308 gMinimumLogSeverity = VERBOSE;
309 continue;
310 case 'd':
311 gMinimumLogSeverity = DEBUG;
312 continue;
313 case 'i':
314 gMinimumLogSeverity = INFO;
315 continue;
316 case 'w':
317 gMinimumLogSeverity = WARNING;
318 continue;
319 case 'e':
320 gMinimumLogSeverity = ERROR;
321 continue;
322 case 'f':
Andreas Gampe550829d2016-09-07 10:10:50 -0700323 gMinimumLogSeverity = FATAL_WITHOUT_ABORT;
Dan Albert58310b42015-03-13 23:06:01 -0700324 continue;
325 // liblog will even suppress FATAL if you say 's' for silent, but that's
326 // crazy!
327 case 's':
Andreas Gampe550829d2016-09-07 10:10:50 -0700328 gMinimumLogSeverity = FATAL_WITHOUT_ABORT;
Dan Albert58310b42015-03-13 23:06:01 -0700329 continue;
330 }
331 }
332 LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags
333 << ")";
334 }
335}
336
Dan Albertb547c852015-03-27 11:20:14 -0700337void SetLogger(LogFunction&& logger) {
Yabin Cui0c689532017-01-23 10:29:23 -0800338 std::lock_guard<std::mutex> lock(LoggingLock());
339 Logger() = std::move(logger);
Dan Albertb547c852015-03-27 11:20:14 -0700340}
341
Andreas Gampe2691e332016-09-08 11:03:58 -0700342void SetAborter(AbortFunction&& aborter) {
Yabin Cui0c689532017-01-23 10:29:23 -0800343 std::lock_guard<std::mutex> lock(LoggingLock());
344 Aborter() = std::move(aborter);
Andreas Gampe2691e332016-09-08 11:03:58 -0700345}
346
Dan Albert58310b42015-03-13 23:06:01 -0700347// This indirection greatly reduces the stack impact of having lots of
348// checks/logging in a function.
349class LogMessageData {
350 public:
Tom Cherryd044eaa2020-01-10 17:18:55 -0800351 LogMessageData(const char* file, unsigned int line, LogSeverity severity, const char* tag,
352 int error)
Spencer Lowbdab59a2015-08-11 16:00:13 -0700353 : file_(GetFileBasename(file)),
Dan Albert0c055862015-03-27 11:20:14 -0700354 line_number_(line),
Dan Albert0c055862015-03-27 11:20:14 -0700355 severity_(severity),
Tomasz Wasilczykc2516002017-12-18 06:30:17 -0800356 tag_(tag),
357 error_(error) {}
Dan Albert58310b42015-03-13 23:06:01 -0700358
359 const char* GetFile() const {
360 return file_;
361 }
362
363 unsigned int GetLineNumber() const {
364 return line_number_;
365 }
366
367 LogSeverity GetSeverity() const {
368 return severity_;
369 }
370
Tomasz Wasilczykc2516002017-12-18 06:30:17 -0800371 const char* GetTag() const { return tag_; }
372
Dan Albert58310b42015-03-13 23:06:01 -0700373 int GetError() const {
374 return error_;
375 }
376
377 std::ostream& GetBuffer() {
378 return buffer_;
379 }
380
381 std::string ToString() const {
382 return buffer_.str();
383 }
384
385 private:
386 std::ostringstream buffer_;
387 const char* const file_;
388 const unsigned int line_number_;
389 const LogSeverity severity_;
Tomasz Wasilczykc2516002017-12-18 06:30:17 -0800390 const char* const tag_;
Dan Albert58310b42015-03-13 23:06:01 -0700391 const int error_;
392
393 DISALLOW_COPY_AND_ASSIGN(LogMessageData);
394};
395
Tom Cherryd044eaa2020-01-10 17:18:55 -0800396LogMessage::LogMessage(const char* file, unsigned int line, LogId, LogSeverity severity,
Tomasz Wasilczykc2516002017-12-18 06:30:17 -0800397 const char* tag, int error)
Tom Cherryd044eaa2020-01-10 17:18:55 -0800398 : LogMessage(file, line, severity, tag, error) {}
399
400LogMessage::LogMessage(const char* file, unsigned int line, LogSeverity severity, const char* tag,
401 int error)
402 : data_(new LogMessageData(file, line, severity, tag, error)) {}
Tomasz Wasilczykc2516002017-12-18 06:30:17 -0800403
Dan Albert58310b42015-03-13 23:06:01 -0700404LogMessage::~LogMessage() {
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700405 // Check severity again. This is duplicate work wrt/ LOG macros, but not LOG_STREAM.
Andreas Gampe1f5fb432016-09-23 16:37:12 -0700406 if (!WOULD_LOG(data_->GetSeverity())) {
Andreas Gampe19ff8f12016-09-23 13:31:52 -0700407 return;
408 }
409
Dan Albert58310b42015-03-13 23:06:01 -0700410 // Finish constructing the message.
411 if (data_->GetError() != -1) {
412 data_->GetBuffer() << ": " << strerror(data_->GetError());
413 }
414 std::string msg(data_->ToString());
415
Andreas Gampe2819c0b2018-12-05 11:26:14 -0800416 if (data_->GetSeverity() == FATAL) {
417#ifdef __ANDROID__
418 // Set the bionic abort message early to avoid liblog doing it
419 // with the individual lines, so that we get the whole message.
420 android_set_abort_message(msg.c_str());
421#endif
422 }
423
Spencer Low765ae6b2015-09-17 19:36:10 -0700424 {
425 // Do the actual logging with the lock held.
Yabin Cui0c689532017-01-23 10:29:23 -0800426 std::lock_guard<std::mutex> lock(LoggingLock());
Spencer Low765ae6b2015-09-17 19:36:10 -0700427 if (msg.find('\n') == std::string::npos) {
Tom Cherryd044eaa2020-01-10 17:18:55 -0800428 LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), data_->GetTag(),
429 msg.c_str());
Spencer Low765ae6b2015-09-17 19:36:10 -0700430 } else {
431 msg += '\n';
432 size_t i = 0;
433 while (i < msg.size()) {
434 size_t nl = msg.find('\n', i);
435 msg[nl] = '\0';
Tom Cherryd044eaa2020-01-10 17:18:55 -0800436 LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), data_->GetTag(),
437 &msg[i]);
Andreas Gampeb4e32f32016-10-04 19:17:07 -0700438 // Undo the zero-termination so we can give the complete message to the aborter.
439 msg[nl] = '\n';
Spencer Low765ae6b2015-09-17 19:36:10 -0700440 i = nl + 1;
441 }
Dan Albert58310b42015-03-13 23:06:01 -0700442 }
443 }
444
445 // Abort if necessary.
446 if (data_->GetSeverity() == FATAL) {
Yabin Cui0c689532017-01-23 10:29:23 -0800447 Aborter()(msg.c_str());
Dan Albert58310b42015-03-13 23:06:01 -0700448 }
449}
450
451std::ostream& LogMessage::stream() {
452 return data_->GetBuffer();
453}
454
Tom Cherryd044eaa2020-01-10 17:18:55 -0800455void LogMessage::LogLine(const char* file, unsigned int line, LogSeverity severity, const char* tag,
456 const char* message) {
Andreas Gampe1923e762018-03-05 10:00:19 -0800457 if (tag == nullptr) {
458 std::lock_guard<std::recursive_mutex> lock(TagLock());
459 if (gDefaultTag == nullptr) {
460 gDefaultTag = new std::string(getprogname());
461 }
Tom Cherryd044eaa2020-01-10 17:18:55 -0800462 Logger()(DEFAULT, severity, gDefaultTag->c_str(), file, line, message);
Andreas Gampe1923e762018-03-05 10:00:19 -0800463 } else {
Tom Cherryd044eaa2020-01-10 17:18:55 -0800464 Logger()(DEFAULT, severity, tag, file, line, message);
Andreas Gampe1923e762018-03-05 10:00:19 -0800465 }
Dan Albert58310b42015-03-13 23:06:01 -0700466}
467
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700468LogSeverity GetMinimumLogSeverity() {
469 return gMinimumLogSeverity;
470}
471
472LogSeverity SetMinimumLogSeverity(LogSeverity new_severity) {
473 LogSeverity old_severity = gMinimumLogSeverity;
474 gMinimumLogSeverity = new_severity;
475 return old_severity;
476}
477
478ScopedLogSeverity::ScopedLogSeverity(LogSeverity new_severity) {
479 old_ = SetMinimumLogSeverity(new_severity);
Dan Albert58310b42015-03-13 23:06:01 -0700480}
481
482ScopedLogSeverity::~ScopedLogSeverity() {
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700483 SetMinimumLogSeverity(old_);
Dan Albert58310b42015-03-13 23:06:01 -0700484}
485
486} // namespace base
487} // namespace android