2015-03-14 14:06:01 +08: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "base/logging.h"
|
|
|
|
|
2015-04-04 02:28:46 +08:00
|
|
|
#include <libgen.h>
|
|
|
|
|
|
|
|
// For getprogname(3) or program_invocation_short_name.
|
|
|
|
#if defined(__ANDROID__) || defined(__APPLE__)
|
|
|
|
#include <stdlib.h>
|
|
|
|
#elif defined(__GLIBC__)
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
|
2015-03-14 14:06:01 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <limits>
|
2015-03-27 14:23:32 +08:00
|
|
|
#include <mutex>
|
2015-03-14 14:06:01 +08:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2015-03-28 02:20:14 +08:00
|
|
|
#include <utility>
|
2015-03-14 14:06:01 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "base/strings.h"
|
2015-03-21 04:46:28 +08:00
|
|
|
#include "cutils/threads.h"
|
2015-03-14 14:06:01 +08:00
|
|
|
|
|
|
|
// Headers for LogMessage::LogLine.
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
#include <android/set_abort_message.h>
|
|
|
|
#include "cutils/log.h"
|
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace base {
|
|
|
|
|
|
|
|
static std::mutex logging_lock;
|
|
|
|
|
2015-03-28 02:20:14 +08:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
static LogFunction gLogger = LogdLogger();
|
|
|
|
#else
|
|
|
|
static LogFunction gLogger = StderrLogger;
|
|
|
|
#endif
|
|
|
|
|
2015-04-04 02:28:46 +08:00
|
|
|
static bool gInitialized = false;
|
2015-03-14 14:06:01 +08:00
|
|
|
static LogSeverity gMinimumLogSeverity = INFO;
|
|
|
|
static std::unique_ptr<std::string> gProgramInvocationName;
|
|
|
|
|
2015-04-04 02:28:46 +08:00
|
|
|
#if defined(__GLIBC__)
|
|
|
|
static const char* getprogname() {
|
|
|
|
return program_invocation_short_name;
|
2015-03-14 14:06:01 +08:00
|
|
|
}
|
2015-04-04 02:28:46 +08:00
|
|
|
#endif
|
2015-03-14 14:06:01 +08:00
|
|
|
|
2015-04-04 02:28:46 +08:00
|
|
|
static const char* ProgramInvocationName() {
|
|
|
|
if (gProgramInvocationName == nullptr) {
|
|
|
|
gProgramInvocationName.reset(new std::string(getprogname()));
|
|
|
|
}
|
2015-03-14 14:06:01 +08:00
|
|
|
|
2015-04-04 02:28:46 +08:00
|
|
|
return gProgramInvocationName->c_str();
|
2015-03-14 14:06:01 +08:00
|
|
|
}
|
|
|
|
|
2015-03-28 02:20:14 +08:00
|
|
|
void StderrLogger(LogId, LogSeverity severity, const char*, const char* file,
|
|
|
|
unsigned int line, const char* message) {
|
|
|
|
static const char* log_characters = "VDIWEF";
|
|
|
|
CHECK_EQ(strlen(log_characters), FATAL + 1U);
|
|
|
|
char severity_char = log_characters[severity];
|
2015-04-04 02:28:46 +08:00
|
|
|
fprintf(stderr, "%s %c %5d %5d %s:%u] %s\n", ProgramInvocationName(),
|
2015-03-28 02:20:14 +08:00
|
|
|
severity_char, getpid(), gettid(), file, line, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
LogdLogger::LogdLogger(LogId default_log_id) : default_log_id_(default_log_id) {
|
|
|
|
}
|
|
|
|
|
|
|
|
static const android_LogPriority kLogSeverityToAndroidLogPriority[] = {
|
|
|
|
ANDROID_LOG_VERBOSE, ANDROID_LOG_DEBUG, ANDROID_LOG_INFO,
|
|
|
|
ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL,
|
|
|
|
};
|
|
|
|
static_assert(arraysize(kLogSeverityToAndroidLogPriority) == FATAL + 1,
|
|
|
|
"Mismatch in size of kLogSeverityToAndroidLogPriority and values "
|
|
|
|
"in LogSeverity");
|
|
|
|
|
|
|
|
static const log_id kLogIdToAndroidLogId[] = {
|
|
|
|
LOG_ID_MAX, LOG_ID_MAIN, LOG_ID_SYSTEM,
|
|
|
|
};
|
|
|
|
static_assert(arraysize(kLogIdToAndroidLogId) == SYSTEM + 1,
|
|
|
|
"Mismatch in size of kLogIdToAndroidLogId and values in LogId");
|
|
|
|
|
|
|
|
void LogdLogger::operator()(LogId id, LogSeverity severity, const char* tag,
|
|
|
|
const char* file, unsigned int line,
|
|
|
|
const char* message) {
|
|
|
|
int priority = kLogSeverityToAndroidLogPriority[severity];
|
|
|
|
if (id == DEFAULT) {
|
|
|
|
id = default_log_id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_id lg_id = kLogIdToAndroidLogId[id];
|
|
|
|
|
|
|
|
if (priority == ANDROID_LOG_FATAL) {
|
|
|
|
__android_log_buf_print(lg_id, priority, tag, "%s:%u] %s", file, line,
|
|
|
|
message);
|
|
|
|
} else {
|
|
|
|
__android_log_buf_print(lg_id, priority, tag, "%s", message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void InitLogging(char* argv[], LogFunction&& logger) {
|
|
|
|
SetLogger(std::forward<LogFunction>(logger));
|
|
|
|
InitLogging(argv);
|
|
|
|
}
|
|
|
|
|
2015-03-14 14:06:01 +08:00
|
|
|
void InitLogging(char* argv[]) {
|
2015-04-04 02:28:46 +08:00
|
|
|
if (gInitialized) {
|
2015-03-14 14:06:01 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-04 02:28:46 +08:00
|
|
|
gInitialized = true;
|
|
|
|
|
2015-03-14 14:06:01 +08:00
|
|
|
// Stash the command line for later use. We can use /proc/self/cmdline on
|
|
|
|
// Linux to recover this, but we don't have that luxury on the Mac, and there
|
|
|
|
// are a couple of argv[0] variants that are commonly used.
|
|
|
|
if (argv != nullptr) {
|
2015-04-04 02:28:46 +08:00
|
|
|
gProgramInvocationName.reset(new std::string(basename(argv[0])));
|
2015-03-14 14:06:01 +08:00
|
|
|
}
|
2015-04-04 02:28:46 +08:00
|
|
|
|
2015-03-14 14:06:01 +08:00
|
|
|
const char* tags = getenv("ANDROID_LOG_TAGS");
|
|
|
|
if (tags == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-20 04:24:26 +08:00
|
|
|
std::vector<std::string> specs = Split(tags, " ");
|
2015-03-14 14:06:01 +08: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':
|
|
|
|
gMinimumLogSeverity = VERBOSE;
|
|
|
|
continue;
|
|
|
|
case 'd':
|
|
|
|
gMinimumLogSeverity = DEBUG;
|
|
|
|
continue;
|
|
|
|
case 'i':
|
|
|
|
gMinimumLogSeverity = INFO;
|
|
|
|
continue;
|
|
|
|
case 'w':
|
|
|
|
gMinimumLogSeverity = WARNING;
|
|
|
|
continue;
|
|
|
|
case 'e':
|
|
|
|
gMinimumLogSeverity = ERROR;
|
|
|
|
continue;
|
|
|
|
case 'f':
|
|
|
|
gMinimumLogSeverity = FATAL;
|
|
|
|
continue;
|
|
|
|
// liblog will even suppress FATAL if you say 's' for silent, but that's
|
|
|
|
// crazy!
|
|
|
|
case 's':
|
|
|
|
gMinimumLogSeverity = FATAL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags
|
|
|
|
<< ")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-28 02:20:14 +08:00
|
|
|
void SetLogger(LogFunction&& logger) {
|
|
|
|
std::lock_guard<std::mutex> lock(logging_lock);
|
|
|
|
gLogger = std::move(logger);
|
|
|
|
}
|
|
|
|
|
2015-03-14 14:06:01 +08:00
|
|
|
// This indirection greatly reduces the stack impact of having lots of
|
|
|
|
// checks/logging in a function.
|
|
|
|
class LogMessageData {
|
|
|
|
public:
|
2015-03-28 02:20:14 +08:00
|
|
|
LogMessageData(const char* file, unsigned int line, LogId id,
|
|
|
|
LogSeverity severity, int error)
|
|
|
|
: file_(file),
|
|
|
|
line_number_(line),
|
|
|
|
id_(id),
|
|
|
|
severity_(severity),
|
|
|
|
error_(error) {
|
2015-03-14 14:06:01 +08:00
|
|
|
const char* last_slash = strrchr(file, '/');
|
|
|
|
file = (last_slash == nullptr) ? file : last_slash + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* GetFile() const {
|
|
|
|
return file_;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int GetLineNumber() const {
|
|
|
|
return line_number_;
|
|
|
|
}
|
|
|
|
|
|
|
|
LogSeverity GetSeverity() const {
|
|
|
|
return severity_;
|
|
|
|
}
|
|
|
|
|
2015-03-28 02:20:14 +08:00
|
|
|
LogId GetId() const {
|
|
|
|
return id_;
|
|
|
|
}
|
|
|
|
|
2015-03-14 14:06:01 +08: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_;
|
2015-03-28 02:20:14 +08:00
|
|
|
const LogId id_;
|
2015-03-14 14:06:01 +08:00
|
|
|
const LogSeverity severity_;
|
|
|
|
const int error_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(LogMessageData);
|
|
|
|
};
|
|
|
|
|
2015-03-28 02:20:14 +08:00
|
|
|
LogMessage::LogMessage(const char* file, unsigned int line, LogId id,
|
2015-03-14 14:06:01 +08:00
|
|
|
LogSeverity severity, int error)
|
2015-03-28 02:20:14 +08:00
|
|
|
: data_(new LogMessageData(file, line, id, severity, error)) {
|
2015-03-14 14:06:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LogMessage::~LogMessage() {
|
|
|
|
if (data_->GetSeverity() < gMinimumLogSeverity) {
|
|
|
|
return; // No need to format something we're not going to output.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finish constructing the message.
|
|
|
|
if (data_->GetError() != -1) {
|
|
|
|
data_->GetBuffer() << ": " << strerror(data_->GetError());
|
|
|
|
}
|
|
|
|
std::string msg(data_->ToString());
|
|
|
|
|
2015-03-28 02:20:14 +08:00
|
|
|
if (msg.find('\n') == std::string::npos) {
|
|
|
|
LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(),
|
|
|
|
data_->GetSeverity(), msg.c_str());
|
|
|
|
} else {
|
|
|
|
msg += '\n';
|
|
|
|
size_t i = 0;
|
|
|
|
while (i < msg.size()) {
|
|
|
|
size_t nl = msg.find('\n', i);
|
|
|
|
msg[nl] = '\0';
|
2015-03-28 02:20:14 +08:00
|
|
|
LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(),
|
2015-03-28 02:20:14 +08:00
|
|
|
data_->GetSeverity(), &msg[i]);
|
|
|
|
i = nl + 1;
|
2015-03-14 14:06:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Abort if necessary.
|
|
|
|
if (data_->GetSeverity() == FATAL) {
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
android_set_abort_message(msg.c_str());
|
|
|
|
#endif
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& LogMessage::stream() {
|
|
|
|
return data_->GetBuffer();
|
|
|
|
}
|
|
|
|
|
2015-03-28 02:20:14 +08:00
|
|
|
void LogMessage::LogLine(const char* file, unsigned int line, LogId id,
|
2015-03-28 02:20:14 +08:00
|
|
|
LogSeverity severity, const char* message) {
|
2015-04-04 02:28:46 +08:00
|
|
|
const char* tag = ProgramInvocationName();
|
2015-03-28 02:20:14 +08:00
|
|
|
std::lock_guard<std::mutex> lock(logging_lock);
|
|
|
|
gLogger(id, severity, tag, file, line, message);
|
2015-03-14 14:06:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ScopedLogSeverity::ScopedLogSeverity(LogSeverity level) {
|
|
|
|
old_ = gMinimumLogSeverity;
|
|
|
|
gMinimumLogSeverity = level;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedLogSeverity::~ScopedLogSeverity() {
|
|
|
|
gMinimumLogSeverity = old_;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace base
|
|
|
|
} // namespace android
|