2015-03-14 07:06:01 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-08-05 01:09:39 +02:00
|
|
|
#if defined(_WIN32)
|
2015-05-20 07:12:06 +02:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2015-12-05 07:00:26 +01:00
|
|
|
#include "android-base/logging.h"
|
2015-03-14 07:06:01 +01:00
|
|
|
|
2016-08-05 01:09:39 +02:00
|
|
|
#include <fcntl.h>
|
2018-03-16 22:25:42 +01:00
|
|
|
#include <inttypes.h>
|
2015-04-03 20:28:46 +02:00
|
|
|
#include <libgen.h>
|
2016-06-21 23:25:44 +02:00
|
|
|
#include <time.h>
|
2015-04-03 20:28:46 +02:00
|
|
|
|
|
|
|
// For getprogname(3) or program_invocation_short_name.
|
|
|
|
#if defined(__ANDROID__) || defined(__APPLE__)
|
|
|
|
#include <stdlib.h>
|
|
|
|
#elif defined(__GLIBC__)
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
|
2016-08-05 01:09:39 +02:00
|
|
|
#if defined(__linux__)
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#endif
|
|
|
|
|
2020-01-08 23:47:42 +01:00
|
|
|
#include <atomic>
|
2015-03-14 07:06:01 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <limits>
|
2016-09-13 23:57:12 +02:00
|
|
|
#include <mutex>
|
2020-01-08 23:47:42 +01:00
|
|
|
#include <optional>
|
2015-03-14 07:06:01 +01:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2015-03-27 19:20:14 +01:00
|
|
|
#include <utility>
|
2015-03-14 07:06:01 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2018-02-15 20:40:30 +01:00
|
|
|
#include <android/log.h>
|
2020-01-08 22:41:56 +01:00
|
|
|
#ifdef __ANDROID__
|
2015-03-14 07:06:01 +01:00
|
|
|
#include <android/set_abort_message.h>
|
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2018-10-19 22:59:44 +02:00
|
|
|
#include <android-base/file.h>
|
2016-09-29 00:54:45 +02:00
|
|
|
#include <android-base/macros.h>
|
2018-04-06 18:40:26 +02:00
|
|
|
#include <android-base/parseint.h>
|
2016-09-29 00:54:45 +02:00
|
|
|
#include <android-base/strings.h>
|
2018-03-16 22:25:42 +01:00
|
|
|
#include <android-base/threads.h>
|
2015-11-11 19:02:29 +01:00
|
|
|
|
2020-01-08 23:47:42 +01:00
|
|
|
#include "liblog_symbols.h"
|
|
|
|
|
2018-06-06 21:54:41 +02:00
|
|
|
namespace android {
|
|
|
|
namespace base {
|
|
|
|
|
|
|
|
// BSD-based systems like Android/macOS have getprogname(). Others need us to provide one.
|
|
|
|
#if defined(__GLIBC__) || defined(_WIN32)
|
|
|
|
static const char* getprogname() {
|
2015-04-29 20:32:23 +02:00
|
|
|
#if defined(__GLIBC__)
|
|
|
|
return program_invocation_short_name;
|
2016-09-13 23:57:12 +02:00
|
|
|
#elif defined(_WIN32)
|
2015-04-29 20:32:23 +02:00
|
|
|
static bool first = true;
|
|
|
|
static char progname[MAX_PATH] = {};
|
|
|
|
|
|
|
|
if (first) {
|
2018-10-19 22:59:44 +02:00
|
|
|
snprintf(progname, sizeof(progname), "%s",
|
|
|
|
android::base::Basename(android::base::GetExecutablePath()).c_str());
|
2015-04-29 20:32:23 +02:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return progname;
|
2018-06-06 21:54:41 +02:00
|
|
|
#endif
|
2015-04-29 20:32:23 +02:00
|
|
|
}
|
|
|
|
#endif
|
2018-04-06 18:40:26 +02:00
|
|
|
|
2018-06-06 21:54:41 +02:00
|
|
|
static const char* GetFileBasename(const char* file) {
|
|
|
|
// We can't use basename(3) even on Unix because the Mac doesn't
|
|
|
|
// have a non-modifying basename.
|
|
|
|
const char* last_slash = strrchr(file, '/');
|
|
|
|
if (last_slash != nullptr) {
|
|
|
|
return last_slash + 1;
|
|
|
|
}
|
|
|
|
#if defined(_WIN32)
|
|
|
|
const char* last_backslash = strrchr(file, '\\');
|
|
|
|
if (last_backslash != nullptr) {
|
|
|
|
return last_backslash + 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2018-04-06 18:40:26 +02:00
|
|
|
#if defined(__linux__)
|
2018-06-06 21:54:41 +02:00
|
|
|
static int OpenKmsg() {
|
2018-04-06 18:40:26 +02:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
// pick up 'file w /dev/kmsg' environment from daemon's init rc file
|
|
|
|
const auto val = getenv("ANDROID_FILE__dev_kmsg");
|
|
|
|
if (val != nullptr) {
|
|
|
|
int fd;
|
|
|
|
if (android::base::ParseInt(val, &fd, 0)) {
|
|
|
|
auto flags = fcntl(fd, F_GETFL);
|
|
|
|
if ((flags != -1) && ((flags & O_ACCMODE) == O_WRONLY)) return fd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return TEMP_FAILURE_RETRY(open("/dev/kmsg", O_WRONLY | O_CLOEXEC));
|
|
|
|
}
|
|
|
|
#endif
|
2015-03-14 07:06:01 +01:00
|
|
|
|
2020-01-08 23:47:42 +01:00
|
|
|
static LogId log_id_tToLogId(int buffer_id) {
|
|
|
|
switch (buffer_id) {
|
|
|
|
case LOG_ID_MAIN:
|
|
|
|
return MAIN;
|
|
|
|
case LOG_ID_SYSTEM:
|
|
|
|
return SYSTEM;
|
|
|
|
case LOG_ID_RADIO:
|
|
|
|
return RADIO;
|
|
|
|
case LOG_ID_CRASH:
|
|
|
|
return CRASH;
|
|
|
|
case LOG_ID_DEFAULT:
|
|
|
|
default:
|
|
|
|
return DEFAULT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int LogIdTolog_id_t(LogId log_id) {
|
|
|
|
switch (log_id) {
|
|
|
|
case MAIN:
|
|
|
|
return LOG_ID_MAIN;
|
|
|
|
case SYSTEM:
|
|
|
|
return LOG_ID_SYSTEM;
|
|
|
|
case RADIO:
|
|
|
|
return LOG_ID_RADIO;
|
|
|
|
case CRASH:
|
|
|
|
return LOG_ID_CRASH;
|
|
|
|
case DEFAULT:
|
|
|
|
default:
|
|
|
|
return LOG_ID_DEFAULT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static LogSeverity PriorityToLogSeverity(int priority) {
|
|
|
|
switch (priority) {
|
2020-01-17 00:58:02 +01:00
|
|
|
case ANDROID_LOG_DEFAULT:
|
|
|
|
return INFO;
|
2020-01-08 23:47:42 +01:00
|
|
|
case ANDROID_LOG_VERBOSE:
|
|
|
|
return VERBOSE;
|
|
|
|
case ANDROID_LOG_DEBUG:
|
|
|
|
return DEBUG;
|
|
|
|
case ANDROID_LOG_INFO:
|
|
|
|
return INFO;
|
|
|
|
case ANDROID_LOG_WARN:
|
|
|
|
return WARNING;
|
|
|
|
case ANDROID_LOG_ERROR:
|
|
|
|
return ERROR;
|
|
|
|
case ANDROID_LOG_FATAL:
|
|
|
|
return FATAL;
|
|
|
|
default:
|
|
|
|
return FATAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static android_LogPriority LogSeverityToPriority(LogSeverity severity) {
|
|
|
|
switch (severity) {
|
|
|
|
case VERBOSE:
|
|
|
|
return ANDROID_LOG_VERBOSE;
|
|
|
|
case DEBUG:
|
|
|
|
return ANDROID_LOG_DEBUG;
|
|
|
|
case INFO:
|
|
|
|
return ANDROID_LOG_INFO;
|
|
|
|
case WARNING:
|
|
|
|
return ANDROID_LOG_WARN;
|
|
|
|
case ERROR:
|
|
|
|
return ANDROID_LOG_ERROR;
|
|
|
|
case FATAL_WITHOUT_ABORT:
|
|
|
|
case FATAL:
|
|
|
|
default:
|
|
|
|
return ANDROID_LOG_FATAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-23 19:29:23 +01:00
|
|
|
static std::mutex& LoggingLock() {
|
|
|
|
static auto& logging_lock = *new std::mutex();
|
|
|
|
return logging_lock;
|
|
|
|
}
|
2015-03-14 07:06:01 +01:00
|
|
|
|
2020-01-08 23:47:42 +01:00
|
|
|
// Only used for Q fallback.
|
2017-01-23 19:29:23 +01:00
|
|
|
static LogFunction& Logger() {
|
2015-03-27 19:20:14 +01:00
|
|
|
#ifdef __ANDROID__
|
2017-01-23 19:29:23 +01:00
|
|
|
static auto& logger = *new LogFunction(LogdLogger());
|
2015-03-27 19:20:14 +01:00
|
|
|
#else
|
2017-01-23 19:29:23 +01:00
|
|
|
static auto& logger = *new LogFunction(StderrLogger);
|
2015-03-27 19:20:14 +01:00
|
|
|
#endif
|
2017-01-23 19:29:23 +01:00
|
|
|
return logger;
|
|
|
|
}
|
2015-03-27 19:20:14 +01:00
|
|
|
|
2020-01-08 23:47:42 +01:00
|
|
|
// Only used for Q fallback.
|
2017-01-23 19:29:23 +01:00
|
|
|
static AbortFunction& Aborter() {
|
|
|
|
static auto& aborter = *new AbortFunction(DefaultAborter);
|
|
|
|
return aborter;
|
|
|
|
}
|
|
|
|
|
2020-01-22 16:48:42 +01:00
|
|
|
// Only used for Q fallback.
|
2018-03-05 19:00:19 +01:00
|
|
|
static std::recursive_mutex& TagLock() {
|
|
|
|
static auto& tag_lock = *new std::recursive_mutex();
|
|
|
|
return tag_lock;
|
|
|
|
}
|
2020-01-22 16:48:42 +01:00
|
|
|
// Only used for Q fallback.
|
2018-03-05 19:00:19 +01:00
|
|
|
static std::string* gDefaultTag;
|
2020-01-22 16:48:42 +01:00
|
|
|
|
2018-03-05 19:00:19 +01:00
|
|
|
void SetDefaultTag(const std::string& tag) {
|
2020-01-22 16:48:42 +01:00
|
|
|
static auto& liblog_functions = GetLibLogFunctions();
|
|
|
|
if (liblog_functions) {
|
|
|
|
liblog_functions->__android_log_set_default_tag(tag.c_str());
|
|
|
|
} else {
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(TagLock());
|
|
|
|
if (gDefaultTag != nullptr) {
|
|
|
|
delete gDefaultTag;
|
|
|
|
gDefaultTag = nullptr;
|
|
|
|
}
|
|
|
|
if (!tag.empty()) {
|
|
|
|
gDefaultTag = new std::string(tag);
|
|
|
|
}
|
2018-03-05 19:00:19 +01:00
|
|
|
}
|
2017-01-23 19:29:23 +01:00
|
|
|
}
|
2016-09-08 20:03:58 +02:00
|
|
|
|
2015-04-03 20:28:46 +02:00
|
|
|
static bool gInitialized = false;
|
2020-01-17 00:58:02 +01:00
|
|
|
|
|
|
|
// Only used for Q fallback.
|
2015-03-14 07:06:01 +01:00
|
|
|
static LogSeverity gMinimumLogSeverity = INFO;
|
|
|
|
|
2016-08-05 01:09:39 +02:00
|
|
|
#if defined(__linux__)
|
|
|
|
void KernelLogger(android::base::LogId, android::base::LogSeverity severity,
|
|
|
|
const char* tag, const char*, unsigned int, const char* msg) {
|
2016-09-07 19:10:50 +02:00
|
|
|
// clang-format off
|
2016-08-05 01:09:39 +02:00
|
|
|
static constexpr int kLogSeverityToKernelLogLevel[] = {
|
2016-09-07 19:10:50 +02:00
|
|
|
[android::base::VERBOSE] = 7, // KERN_DEBUG (there is no verbose kernel log
|
|
|
|
// level)
|
|
|
|
[android::base::DEBUG] = 7, // KERN_DEBUG
|
|
|
|
[android::base::INFO] = 6, // KERN_INFO
|
|
|
|
[android::base::WARNING] = 4, // KERN_WARNING
|
|
|
|
[android::base::ERROR] = 3, // KERN_ERROR
|
|
|
|
[android::base::FATAL_WITHOUT_ABORT] = 2, // KERN_CRIT
|
|
|
|
[android::base::FATAL] = 2, // KERN_CRIT
|
2016-08-05 01:09:39 +02:00
|
|
|
};
|
2016-09-07 19:10:50 +02:00
|
|
|
// clang-format on
|
2016-08-05 01:09:39 +02:00
|
|
|
static_assert(arraysize(kLogSeverityToKernelLogLevel) == android::base::FATAL + 1,
|
|
|
|
"Mismatch in size of kLogSeverityToKernelLogLevel and values in LogSeverity");
|
|
|
|
|
2018-04-06 18:40:26 +02:00
|
|
|
static int klog_fd = OpenKmsg();
|
2016-08-05 01:09:39 +02:00
|
|
|
if (klog_fd == -1) return;
|
|
|
|
|
|
|
|
int level = kLogSeverityToKernelLogLevel[severity];
|
|
|
|
|
|
|
|
// The kernel's printk buffer is only 1024 bytes.
|
|
|
|
// TODO: should we automatically break up long lines into multiple lines?
|
|
|
|
// Or we could log but with something like "..." at the end?
|
|
|
|
char buf[1024];
|
|
|
|
size_t size = snprintf(buf, sizeof(buf), "<%d>%s: %s\n", level, tag, msg);
|
|
|
|
if (size > sizeof(buf)) {
|
|
|
|
size = snprintf(buf, sizeof(buf), "<%d>%s: %zu-byte message too long for printk\n",
|
|
|
|
level, tag, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
iovec iov[1];
|
|
|
|
iov[0].iov_base = buf;
|
|
|
|
iov[0].iov_len = size;
|
|
|
|
TEMP_FAILURE_RETRY(writev(klog_fd, iov, 1));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-12-18 15:30:17 +01:00
|
|
|
void StderrLogger(LogId, LogSeverity severity, const char* tag, const char* file, unsigned int line,
|
|
|
|
const char* message) {
|
2016-06-21 23:25:44 +02:00
|
|
|
struct tm now;
|
|
|
|
time_t t = time(nullptr);
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
localtime_s(&now, &t);
|
|
|
|
#else
|
|
|
|
localtime_r(&t, &now);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char timestamp[32];
|
|
|
|
strftime(timestamp, sizeof(timestamp), "%m-%d %H:%M:%S", &now);
|
|
|
|
|
2016-09-07 19:10:50 +02:00
|
|
|
static const char log_characters[] = "VDIWEFF";
|
2015-08-12 01:00:13 +02:00
|
|
|
static_assert(arraysize(log_characters) - 1 == FATAL + 1,
|
|
|
|
"Mismatch in size of log_characters and values in LogSeverity");
|
2015-03-27 19:20:14 +01:00
|
|
|
char severity_char = log_characters[severity];
|
2020-01-08 23:47:42 +01:00
|
|
|
if (file != nullptr) {
|
|
|
|
fprintf(stderr, "%s %c %s %5d %5" PRIu64 " %s:%u] %s\n", tag ? tag : "nullptr", severity_char,
|
|
|
|
timestamp, getpid(), GetThreadId(), file, line, message);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s %c %s %5d %5" PRIu64 " %s\n", tag ? tag : "nullptr", severity_char,
|
|
|
|
timestamp, getpid(), GetThreadId(), message);
|
|
|
|
}
|
2015-03-27 19:20:14 +01:00
|
|
|
}
|
|
|
|
|
2018-05-23 18:16:46 +02:00
|
|
|
void StdioLogger(LogId, LogSeverity severity, const char* /*tag*/, const char* /*file*/,
|
|
|
|
unsigned int /*line*/, const char* message) {
|
|
|
|
if (severity >= WARNING) {
|
|
|
|
fflush(stdout);
|
2018-06-06 21:54:41 +02:00
|
|
|
fprintf(stderr, "%s: %s\n", GetFileBasename(getprogname()), message);
|
2018-05-23 18:16:46 +02:00
|
|
|
} else {
|
|
|
|
fprintf(stdout, "%s\n", message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 20:03:58 +02:00
|
|
|
void DefaultAborter(const char* abort_message) {
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
android_set_abort_message(abort_message);
|
|
|
|
#else
|
|
|
|
UNUSED(abort_message);
|
|
|
|
#endif
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2015-03-27 19:20:14 +01:00
|
|
|
|
|
|
|
LogdLogger::LogdLogger(LogId default_log_id) : default_log_id_(default_log_id) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogdLogger::operator()(LogId id, LogSeverity severity, const char* tag,
|
|
|
|
const char* file, unsigned int line,
|
|
|
|
const char* message) {
|
2020-01-08 23:47:42 +01:00
|
|
|
android_LogPriority priority = LogSeverityToPriority(severity);
|
2015-03-27 19:20:14 +01:00
|
|
|
if (id == DEFAULT) {
|
|
|
|
id = default_log_id_;
|
|
|
|
}
|
|
|
|
|
2020-01-08 23:47:42 +01:00
|
|
|
int lg_id = LogIdTolog_id_t(id);
|
2015-03-27 19:20:14 +01:00
|
|
|
|
2020-01-31 18:03:05 +01:00
|
|
|
char log_message_with_file[4068]; // LOGGER_ENTRY_MAX_PAYLOAD, not available in the NDK.
|
2020-01-08 23:47:42 +01:00
|
|
|
if (priority == ANDROID_LOG_FATAL && file != nullptr) {
|
2020-01-31 18:03:05 +01:00
|
|
|
snprintf(log_message_with_file, sizeof(log_message_with_file), "%s:%u] %s", file, line,
|
|
|
|
message);
|
|
|
|
message = log_message_with_file;
|
2020-01-08 23:47:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static auto& liblog_functions = GetLibLogFunctions();
|
|
|
|
if (liblog_functions) {
|
|
|
|
__android_logger_data logger_data = {sizeof(__android_logger_data), lg_id, priority, tag,
|
|
|
|
static_cast<const char*>(nullptr), 0};
|
2020-01-31 18:03:05 +01:00
|
|
|
liblog_functions->__android_log_logd_logger(&logger_data, message);
|
2015-03-27 19:20:14 +01:00
|
|
|
} else {
|
|
|
|
__android_log_buf_print(lg_id, priority, tag, "%s", message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 20:03:58 +02:00
|
|
|
void InitLogging(char* argv[], LogFunction&& logger, AbortFunction&& aborter) {
|
2015-03-27 19:20:14 +01:00
|
|
|
SetLogger(std::forward<LogFunction>(logger));
|
2016-09-08 20:03:58 +02:00
|
|
|
SetAborter(std::forward<AbortFunction>(aborter));
|
2015-03-27 19:20:14 +01:00
|
|
|
|
2015-04-03 20:28:46 +02:00
|
|
|
if (gInitialized) {
|
2015-03-14 07:06:01 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-03 20:28:46 +02:00
|
|
|
gInitialized = true;
|
|
|
|
|
2015-03-14 07:06:01 +01:00
|
|
|
// Stash the command line for later use. We can use /proc/self/cmdline on
|
2015-11-08 03:51:54 +01:00
|
|
|
// Linux to recover this, but we don't have that luxury on the Mac/Windows,
|
|
|
|
// and there are a couple of argv[0] variants that are commonly used.
|
2015-03-14 07:06:01 +01:00
|
|
|
if (argv != nullptr) {
|
2018-03-05 19:00:19 +01:00
|
|
|
SetDefaultTag(basename(argv[0]));
|
2015-03-14 07:06:01 +01:00
|
|
|
}
|
2015-04-03 20:28:46 +02:00
|
|
|
|
2015-03-14 07:06:01 +01:00
|
|
|
const char* tags = getenv("ANDROID_LOG_TAGS");
|
|
|
|
if (tags == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-19 21:24:26 +01:00
|
|
|
std::vector<std::string> specs = Split(tags, " ");
|
2015-03-14 07:06:01 +01:00
|
|
|
for (size_t i = 0; i < specs.size(); ++i) {
|
|
|
|
// "tag-pattern:[vdiwefs]"
|
|
|
|
std::string spec(specs[i]);
|
|
|
|
if (spec.size() == 3 && StartsWith(spec, "*:")) {
|
|
|
|
switch (spec[2]) {
|
|
|
|
case 'v':
|
2020-01-17 00:58:02 +01:00
|
|
|
SetMinimumLogSeverity(VERBOSE);
|
2015-03-14 07:06:01 +01:00
|
|
|
continue;
|
|
|
|
case 'd':
|
2020-01-17 00:58:02 +01:00
|
|
|
SetMinimumLogSeverity(DEBUG);
|
2015-03-14 07:06:01 +01:00
|
|
|
continue;
|
|
|
|
case 'i':
|
2020-01-17 00:58:02 +01:00
|
|
|
SetMinimumLogSeverity(INFO);
|
2015-03-14 07:06:01 +01:00
|
|
|
continue;
|
|
|
|
case 'w':
|
2020-01-17 00:58:02 +01:00
|
|
|
SetMinimumLogSeverity(WARNING);
|
2015-03-14 07:06:01 +01:00
|
|
|
continue;
|
|
|
|
case 'e':
|
2020-01-17 00:58:02 +01:00
|
|
|
SetMinimumLogSeverity(ERROR);
|
2015-03-14 07:06:01 +01:00
|
|
|
continue;
|
|
|
|
case 'f':
|
2020-01-17 00:58:02 +01:00
|
|
|
SetMinimumLogSeverity(FATAL_WITHOUT_ABORT);
|
2015-03-14 07:06:01 +01:00
|
|
|
continue;
|
|
|
|
// liblog will even suppress FATAL if you say 's' for silent, but that's
|
|
|
|
// crazy!
|
|
|
|
case 's':
|
2020-01-17 00:58:02 +01:00
|
|
|
SetMinimumLogSeverity(FATAL_WITHOUT_ABORT);
|
2015-03-14 07:06:01 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags
|
|
|
|
<< ")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-27 19:20:14 +01:00
|
|
|
void SetLogger(LogFunction&& logger) {
|
2020-01-08 23:47:42 +01:00
|
|
|
static auto& liblog_functions = GetLibLogFunctions();
|
|
|
|
if (liblog_functions) {
|
|
|
|
// We need to atomically swap the old and new pointers since other threads may be logging.
|
|
|
|
// We know all threads will be using the new logger after __android_log_set_logger() returns,
|
|
|
|
// so we can delete it then.
|
|
|
|
// This leaks one std::function<> per instance of libbase if multiple copies of libbase within a
|
|
|
|
// single process call SetLogger(). That is the same cost as having a static
|
|
|
|
// std::function<>, which is the not-thread-safe alternative.
|
|
|
|
static std::atomic<LogFunction*> logger_function(nullptr);
|
|
|
|
auto* old_logger_function = logger_function.exchange(new LogFunction(logger));
|
|
|
|
liblog_functions->__android_log_set_logger([](const struct __android_logger_data* logger_data,
|
|
|
|
const char* message) {
|
|
|
|
auto log_id = log_id_tToLogId(logger_data->buffer_id);
|
|
|
|
auto severity = PriorityToLogSeverity(logger_data->priority);
|
|
|
|
|
|
|
|
auto& function = *logger_function.load(std::memory_order_acquire);
|
|
|
|
function(log_id, severity, logger_data->tag, logger_data->file, logger_data->line, message);
|
|
|
|
});
|
|
|
|
delete old_logger_function;
|
|
|
|
} else {
|
|
|
|
std::lock_guard<std::mutex> lock(LoggingLock());
|
|
|
|
Logger() = std::move(logger);
|
|
|
|
}
|
2015-03-27 19:20:14 +01:00
|
|
|
}
|
|
|
|
|
2016-09-08 20:03:58 +02:00
|
|
|
void SetAborter(AbortFunction&& aborter) {
|
2020-01-08 23:47:42 +01:00
|
|
|
static auto& liblog_functions = GetLibLogFunctions();
|
|
|
|
if (liblog_functions) {
|
|
|
|
// See the comment in SetLogger().
|
|
|
|
static std::atomic<AbortFunction*> abort_function(nullptr);
|
|
|
|
auto* old_abort_function = abort_function.exchange(new AbortFunction(aborter));
|
|
|
|
__android_log_set_aborter([](const char* abort_message) {
|
|
|
|
auto& function = *abort_function.load(std::memory_order_acquire);
|
|
|
|
function(abort_message);
|
|
|
|
});
|
|
|
|
delete old_abort_function;
|
|
|
|
} else {
|
|
|
|
std::lock_guard<std::mutex> lock(LoggingLock());
|
|
|
|
Aborter() = std::move(aborter);
|
|
|
|
}
|
2016-09-08 20:03:58 +02:00
|
|
|
}
|
|
|
|
|
2015-03-14 07:06:01 +01:00
|
|
|
// This indirection greatly reduces the stack impact of having lots of
|
|
|
|
// checks/logging in a function.
|
|
|
|
class LogMessageData {
|
|
|
|
public:
|
2020-01-11 02:18:55 +01:00
|
|
|
LogMessageData(const char* file, unsigned int line, LogSeverity severity, const char* tag,
|
|
|
|
int error)
|
2015-08-12 01:00:13 +02:00
|
|
|
: file_(GetFileBasename(file)),
|
2015-03-27 19:20:14 +01:00
|
|
|
line_number_(line),
|
|
|
|
severity_(severity),
|
2017-12-18 15:30:17 +01:00
|
|
|
tag_(tag),
|
|
|
|
error_(error) {}
|
2015-03-14 07:06:01 +01:00
|
|
|
|
|
|
|
const char* GetFile() const {
|
|
|
|
return file_;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int GetLineNumber() const {
|
|
|
|
return line_number_;
|
|
|
|
}
|
|
|
|
|
|
|
|
LogSeverity GetSeverity() const {
|
|
|
|
return severity_;
|
|
|
|
}
|
|
|
|
|
2017-12-18 15:30:17 +01:00
|
|
|
const char* GetTag() const { return tag_; }
|
|
|
|
|
2015-03-14 07:06:01 +01:00
|
|
|
int GetError() const {
|
|
|
|
return error_;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& GetBuffer() {
|
|
|
|
return buffer_;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ToString() const {
|
|
|
|
return buffer_.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::ostringstream buffer_;
|
|
|
|
const char* const file_;
|
|
|
|
const unsigned int line_number_;
|
|
|
|
const LogSeverity severity_;
|
2017-12-18 15:30:17 +01:00
|
|
|
const char* const tag_;
|
2015-03-14 07:06:01 +01:00
|
|
|
const int error_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(LogMessageData);
|
|
|
|
};
|
|
|
|
|
2020-01-11 02:18:55 +01:00
|
|
|
LogMessage::LogMessage(const char* file, unsigned int line, LogId, LogSeverity severity,
|
2017-12-18 15:30:17 +01:00
|
|
|
const char* tag, int error)
|
2020-01-11 02:18:55 +01:00
|
|
|
: LogMessage(file, line, severity, tag, error) {}
|
|
|
|
|
|
|
|
LogMessage::LogMessage(const char* file, unsigned int line, LogSeverity severity, const char* tag,
|
|
|
|
int error)
|
|
|
|
: data_(new LogMessageData(file, line, severity, tag, error)) {}
|
2017-12-18 15:30:17 +01:00
|
|
|
|
2015-03-14 07:06:01 +01:00
|
|
|
LogMessage::~LogMessage() {
|
2016-09-23 22:31:52 +02:00
|
|
|
// Check severity again. This is duplicate work wrt/ LOG macros, but not LOG_STREAM.
|
2016-09-24 01:37:12 +02:00
|
|
|
if (!WOULD_LOG(data_->GetSeverity())) {
|
2016-09-23 22:31:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-14 07:06:01 +01:00
|
|
|
// Finish constructing the message.
|
|
|
|
if (data_->GetError() != -1) {
|
|
|
|
data_->GetBuffer() << ": " << strerror(data_->GetError());
|
|
|
|
}
|
|
|
|
std::string msg(data_->ToString());
|
|
|
|
|
2018-12-05 20:26:14 +01:00
|
|
|
if (data_->GetSeverity() == FATAL) {
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
// Set the bionic abort message early to avoid liblog doing it
|
|
|
|
// with the individual lines, so that we get the whole message.
|
|
|
|
android_set_abort_message(msg.c_str());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-09-18 04:36:10 +02:00
|
|
|
{
|
|
|
|
// Do the actual logging with the lock held.
|
2017-01-23 19:29:23 +01:00
|
|
|
std::lock_guard<std::mutex> lock(LoggingLock());
|
2015-09-18 04:36:10 +02:00
|
|
|
if (msg.find('\n') == std::string::npos) {
|
2020-01-11 02:18:55 +01:00
|
|
|
LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), data_->GetTag(),
|
|
|
|
msg.c_str());
|
2015-09-18 04:36:10 +02:00
|
|
|
} else {
|
|
|
|
msg += '\n';
|
|
|
|
size_t i = 0;
|
|
|
|
while (i < msg.size()) {
|
|
|
|
size_t nl = msg.find('\n', i);
|
|
|
|
msg[nl] = '\0';
|
2020-01-11 02:18:55 +01:00
|
|
|
LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), data_->GetTag(),
|
|
|
|
&msg[i]);
|
2016-10-05 04:17:07 +02:00
|
|
|
// Undo the zero-termination so we can give the complete message to the aborter.
|
|
|
|
msg[nl] = '\n';
|
2015-09-18 04:36:10 +02:00
|
|
|
i = nl + 1;
|
|
|
|
}
|
2015-03-14 07:06:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Abort if necessary.
|
|
|
|
if (data_->GetSeverity() == FATAL) {
|
2020-01-08 23:47:42 +01:00
|
|
|
static auto& liblog_functions = GetLibLogFunctions();
|
|
|
|
if (liblog_functions) {
|
|
|
|
liblog_functions->__android_log_call_aborter(msg.c_str());
|
|
|
|
} else {
|
|
|
|
Aborter()(msg.c_str());
|
|
|
|
}
|
2015-03-14 07:06:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& LogMessage::stream() {
|
|
|
|
return data_->GetBuffer();
|
|
|
|
}
|
|
|
|
|
2020-01-11 02:18:55 +01:00
|
|
|
void LogMessage::LogLine(const char* file, unsigned int line, LogSeverity severity, const char* tag,
|
|
|
|
const char* message) {
|
2020-01-08 23:47:42 +01:00
|
|
|
static auto& liblog_functions = GetLibLogFunctions();
|
|
|
|
auto priority = LogSeverityToPriority(severity);
|
2020-01-22 16:48:42 +01:00
|
|
|
if (liblog_functions) {
|
|
|
|
__android_logger_data logger_data = {
|
|
|
|
sizeof(__android_logger_data), LOG_ID_DEFAULT, priority, tag, file, line};
|
|
|
|
__android_log_write_logger_data(&logger_data, message);
|
|
|
|
} else {
|
|
|
|
if (tag == nullptr) {
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(TagLock());
|
|
|
|
if (gDefaultTag == nullptr) {
|
|
|
|
gDefaultTag = new std::string(getprogname());
|
|
|
|
}
|
2020-01-08 23:47:42 +01:00
|
|
|
|
|
|
|
Logger()(DEFAULT, severity, gDefaultTag->c_str(), file, line, message);
|
|
|
|
} else {
|
|
|
|
Logger()(DEFAULT, severity, tag, file, line, message);
|
|
|
|
}
|
2018-03-05 19:00:19 +01:00
|
|
|
}
|
2015-03-14 07:06:01 +01:00
|
|
|
}
|
|
|
|
|
2016-08-05 01:09:39 +02:00
|
|
|
LogSeverity GetMinimumLogSeverity() {
|
2020-01-17 00:58:02 +01:00
|
|
|
static auto& liblog_functions = GetLibLogFunctions();
|
|
|
|
if (liblog_functions) {
|
|
|
|
return PriorityToLogSeverity(liblog_functions->__android_log_get_minimum_priority());
|
|
|
|
} else {
|
2016-08-05 01:09:39 +02:00
|
|
|
return gMinimumLogSeverity;
|
2020-01-17 00:58:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShouldLog(LogSeverity severity, const char* tag) {
|
|
|
|
static auto& liblog_functions = GetLibLogFunctions();
|
|
|
|
// Even though we're not using the R liblog functions in this function, if we're running on Q,
|
|
|
|
// we need to fall back to using gMinimumLogSeverity, since __android_log_is_loggable() will not
|
|
|
|
// take into consideration the value from SetMinimumLogSeverity().
|
|
|
|
if (liblog_functions) {
|
|
|
|
int priority = LogSeverityToPriority(severity);
|
|
|
|
return __android_log_is_loggable(priority, tag, ANDROID_LOG_INFO);
|
|
|
|
} else {
|
|
|
|
return severity >= gMinimumLogSeverity;
|
|
|
|
}
|
2016-08-05 01:09:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LogSeverity SetMinimumLogSeverity(LogSeverity new_severity) {
|
2020-01-17 00:58:02 +01:00
|
|
|
static auto& liblog_functions = GetLibLogFunctions();
|
|
|
|
if (liblog_functions) {
|
|
|
|
auto priority = LogSeverityToPriority(new_severity);
|
|
|
|
return PriorityToLogSeverity(liblog_functions->__android_log_set_minimum_priority(priority));
|
|
|
|
} else {
|
|
|
|
LogSeverity old_severity = gMinimumLogSeverity;
|
|
|
|
gMinimumLogSeverity = new_severity;
|
|
|
|
return old_severity;
|
|
|
|
}
|
2016-08-05 01:09:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ScopedLogSeverity::ScopedLogSeverity(LogSeverity new_severity) {
|
|
|
|
old_ = SetMinimumLogSeverity(new_severity);
|
2015-03-14 07:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ScopedLogSeverity::~ScopedLogSeverity() {
|
2016-08-05 01:09:39 +02:00
|
|
|
SetMinimumLogSeverity(old_);
|
2015-03-14 07:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace base
|
|
|
|
} // namespace android
|