Merge "Support logging to other log buffers."
This commit is contained in:
commit
87eca3cb4b
2 changed files with 66 additions and 34 deletions
|
|
@ -34,6 +34,11 @@ enum LogSeverity {
|
||||||
FATAL,
|
FATAL,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum LogId {
|
||||||
|
MAIN,
|
||||||
|
SYSTEM,
|
||||||
|
};
|
||||||
|
|
||||||
// Configure logging based on ANDROID_LOG_TAGS environment variable.
|
// Configure logging based on ANDROID_LOG_TAGS environment variable.
|
||||||
// We need to parse a string that looks like
|
// We need to parse a string that looks like
|
||||||
//
|
//
|
||||||
|
|
@ -60,15 +65,26 @@ extern const char* ProgramInvocationShortName();
|
||||||
// FATAL it also causes an abort. For example:
|
// FATAL it also causes an abort. For example:
|
||||||
//
|
//
|
||||||
// LOG(FATAL) << "We didn't expect to reach here";
|
// LOG(FATAL) << "We didn't expect to reach here";
|
||||||
#define LOG(severity) \
|
#define LOG(severity) \
|
||||||
::android::base::LogMessage(__FILE__, __LINE__, ::android::base::severity, \
|
::android::base::LogMessage(__FILE__, __LINE__, ::android::base::MAIN, \
|
||||||
-1).stream()
|
::android::base::severity, -1).stream()
|
||||||
|
|
||||||
|
// Logs a message to logcat with the specified log ID on Android otherwise to
|
||||||
|
// stderr. If the severity is FATAL it also causes an abort.
|
||||||
|
#define LOG_TO(dest, severity) \
|
||||||
|
::android::base::LogMessage(__FILE__, __LINE__, ::android::base::dest, \
|
||||||
|
::android::base::severity, -1).stream()
|
||||||
|
|
||||||
// A variant of LOG that also logs the current errno value. To be used when
|
// A variant of LOG that also logs the current errno value. To be used when
|
||||||
// library calls fail.
|
// library calls fail.
|
||||||
#define PLOG(severity) \
|
#define PLOG(severity) \
|
||||||
::android::base::LogMessage(__FILE__, __LINE__, ::android::base::severity, \
|
::android::base::LogMessage(__FILE__, __LINE__, ::android::base::MAIN, \
|
||||||
errno).stream()
|
::android::base::severity, errno).stream()
|
||||||
|
|
||||||
|
// Behaves like PLOG, but logs to the specified log ID.
|
||||||
|
#define PLOG_TO(dest, severity) \
|
||||||
|
::android::base::LogMessage(__FILE__, __LINE__, ::android::base::dest, \
|
||||||
|
::android::base::severity, errno).stream()
|
||||||
|
|
||||||
// Marker that code is yet to be implemented.
|
// Marker that code is yet to be implemented.
|
||||||
#define UNIMPLEMENTED(level) \
|
#define UNIMPLEMENTED(level) \
|
||||||
|
|
@ -80,20 +96,20 @@ extern const char* ProgramInvocationShortName();
|
||||||
//
|
//
|
||||||
// CHECK(false == true) results in a log message of
|
// CHECK(false == true) results in a log message of
|
||||||
// "Check failed: false == true".
|
// "Check failed: false == true".
|
||||||
#define CHECK(x) \
|
#define CHECK(x) \
|
||||||
if (UNLIKELY(!(x))) \
|
if (UNLIKELY(!(x))) \
|
||||||
::android::base::LogMessage(__FILE__, __LINE__, ::android::base::FATAL, \
|
::android::base::LogMessage(__FILE__, __LINE__, ::android::base::MAIN, \
|
||||||
-1).stream() \
|
::android::base::FATAL, -1).stream() \
|
||||||
<< "Check failed: " #x << " "
|
<< "Check failed: " #x << " "
|
||||||
|
|
||||||
// Helper for CHECK_xx(x,y) macros.
|
// Helper for CHECK_xx(x,y) macros.
|
||||||
#define CHECK_OP(LHS, RHS, OP) \
|
#define CHECK_OP(LHS, RHS, OP) \
|
||||||
for (auto _values = ::android::base::MakeEagerEvaluator(LHS, RHS); \
|
for (auto _values = ::android::base::MakeEagerEvaluator(LHS, RHS); \
|
||||||
UNLIKELY(!(_values.lhs OP _values.rhs)); \
|
UNLIKELY(!(_values.lhs OP _values.rhs)); \
|
||||||
/* empty */) \
|
/* empty */) \
|
||||||
::android::base::LogMessage(__FILE__, __LINE__, ::android::base::FATAL, -1) \
|
::android::base::LogMessage(__FILE__, __LINE__, ::android::base::MAIN, \
|
||||||
.stream() \
|
::android::base::FATAL, -1).stream() \
|
||||||
<< "Check failed: " << #LHS << " " << #OP << " " << #RHS \
|
<< "Check failed: " << #LHS << " " << #OP << " " << #RHS \
|
||||||
<< " (" #LHS "=" << _values.lhs << ", " #RHS "=" << _values.rhs << ") "
|
<< " (" #LHS "=" << _values.lhs << ", " #RHS "=" << _values.rhs << ") "
|
||||||
|
|
||||||
// Check whether a condition holds between x and y, LOG(FATAL) if not. The value
|
// Check whether a condition holds between x and y, LOG(FATAL) if not. The value
|
||||||
|
|
@ -228,8 +244,8 @@ class LogMessageData;
|
||||||
// of a CHECK. The destructor will abort if the severity is FATAL.
|
// of a CHECK. The destructor will abort if the severity is FATAL.
|
||||||
class LogMessage {
|
class LogMessage {
|
||||||
public:
|
public:
|
||||||
LogMessage(const char* file, unsigned int line, LogSeverity severity,
|
LogMessage(const char* file, unsigned int line, LogId id,
|
||||||
int error);
|
LogSeverity severity, int error);
|
||||||
|
|
||||||
~LogMessage();
|
~LogMessage();
|
||||||
|
|
||||||
|
|
@ -238,8 +254,8 @@ class LogMessage {
|
||||||
std::ostream& stream();
|
std::ostream& stream();
|
||||||
|
|
||||||
// The routine that performs the actual logging.
|
// The routine that performs the actual logging.
|
||||||
static void LogLine(const char* file, unsigned int line, LogSeverity severity,
|
static void LogLine(const char* file, unsigned int line, LogId id,
|
||||||
const char* msg);
|
LogSeverity severity, const char* msg);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const std::unique_ptr<LogMessageData> data_;
|
const std::unique_ptr<LogMessageData> data_;
|
||||||
|
|
|
||||||
|
|
@ -128,9 +128,13 @@ void InitLogging(char* argv[]) {
|
||||||
// checks/logging in a function.
|
// checks/logging in a function.
|
||||||
class LogMessageData {
|
class LogMessageData {
|
||||||
public:
|
public:
|
||||||
LogMessageData(const char* file, unsigned int line, LogSeverity severity,
|
LogMessageData(const char* file, unsigned int line, LogId id,
|
||||||
int error)
|
LogSeverity severity, int error)
|
||||||
: file_(file), line_number_(line), severity_(severity), error_(error) {
|
: file_(file),
|
||||||
|
line_number_(line),
|
||||||
|
id_(id),
|
||||||
|
severity_(severity),
|
||||||
|
error_(error) {
|
||||||
const char* last_slash = strrchr(file, '/');
|
const char* last_slash = strrchr(file, '/');
|
||||||
file = (last_slash == nullptr) ? file : last_slash + 1;
|
file = (last_slash == nullptr) ? file : last_slash + 1;
|
||||||
}
|
}
|
||||||
|
|
@ -147,6 +151,10 @@ class LogMessageData {
|
||||||
return severity_;
|
return severity_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LogId GetId() const {
|
||||||
|
return id_;
|
||||||
|
}
|
||||||
|
|
||||||
int GetError() const {
|
int GetError() const {
|
||||||
return error_;
|
return error_;
|
||||||
}
|
}
|
||||||
|
|
@ -163,15 +171,16 @@ class LogMessageData {
|
||||||
std::ostringstream buffer_;
|
std::ostringstream buffer_;
|
||||||
const char* const file_;
|
const char* const file_;
|
||||||
const unsigned int line_number_;
|
const unsigned int line_number_;
|
||||||
|
const LogId id_;
|
||||||
const LogSeverity severity_;
|
const LogSeverity severity_;
|
||||||
const int error_;
|
const int error_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(LogMessageData);
|
DISALLOW_COPY_AND_ASSIGN(LogMessageData);
|
||||||
};
|
};
|
||||||
|
|
||||||
LogMessage::LogMessage(const char* file, unsigned int line,
|
LogMessage::LogMessage(const char* file, unsigned int line, LogId id,
|
||||||
LogSeverity severity, int error)
|
LogSeverity severity, int error)
|
||||||
: data_(new LogMessageData(file, line, severity, error)) {
|
: data_(new LogMessageData(file, line, id, severity, error)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
LogMessage::~LogMessage() {
|
LogMessage::~LogMessage() {
|
||||||
|
|
@ -189,16 +198,16 @@ LogMessage::~LogMessage() {
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(logging_lock);
|
std::lock_guard<std::mutex> lock(logging_lock);
|
||||||
if (msg.find('\n') == std::string::npos) {
|
if (msg.find('\n') == std::string::npos) {
|
||||||
LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(),
|
LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(),
|
||||||
msg.c_str());
|
data_->GetSeverity(), msg.c_str());
|
||||||
} else {
|
} else {
|
||||||
msg += '\n';
|
msg += '\n';
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
while (i < msg.size()) {
|
while (i < msg.size()) {
|
||||||
size_t nl = msg.find('\n', i);
|
size_t nl = msg.find('\n', i);
|
||||||
msg[nl] = '\0';
|
msg[nl] = '\0';
|
||||||
LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(),
|
LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetId(),
|
||||||
&msg[i]);
|
data_->GetSeverity(), &msg[i]);
|
||||||
i = nl + 1;
|
i = nl + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -224,19 +233,26 @@ static const android_LogPriority kLogSeverityToAndroidLogPriority[] = {
|
||||||
static_assert(arraysize(kLogSeverityToAndroidLogPriority) == FATAL + 1,
|
static_assert(arraysize(kLogSeverityToAndroidLogPriority) == FATAL + 1,
|
||||||
"Mismatch in size of kLogSeverityToAndroidLogPriority and values "
|
"Mismatch in size of kLogSeverityToAndroidLogPriority and values "
|
||||||
"in LogSeverity");
|
"in LogSeverity");
|
||||||
|
|
||||||
|
static const log_id kLogIdToAndroidLogId[] = {LOG_ID_MAIN, LOG_ID_SYSTEM};
|
||||||
|
static_assert(arraysize(kLogIdToAndroidLogId) == SYSTEM + 1,
|
||||||
|
"Mismatch in size of kLogIdToAndroidLogId and values "
|
||||||
|
"in LogSeverity");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void LogMessage::LogLine(const char* file, unsigned int line,
|
void LogMessage::LogLine(const char* file, unsigned int line, LogId id,
|
||||||
LogSeverity log_severity, const char* message) {
|
LogSeverity log_severity, const char* message) {
|
||||||
#ifdef __ANDROID__
|
#ifdef __ANDROID__
|
||||||
const char* tag = ProgramInvocationShortName();
|
const char* tag = ProgramInvocationShortName();
|
||||||
int priority = kLogSeverityToAndroidLogPriority[log_severity];
|
int priority = kLogSeverityToAndroidLogPriority[log_severity];
|
||||||
|
log_id lg_id = kLogIdToAndroidLogId[id];
|
||||||
if (priority == ANDROID_LOG_FATAL) {
|
if (priority == ANDROID_LOG_FATAL) {
|
||||||
LOG_PRI(priority, tag, "%s:%u] %s", file, line, message);
|
__android_log_buf_print(lg_id, priority, tag, "%s:%u] %s", file, line, message);
|
||||||
} else {
|
} else {
|
||||||
LOG_PRI(priority, tag, "%s", message);
|
__android_log_buf_print(lg_id, priority, tag, "%s", message);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
UNUSED(id);
|
||||||
static const char* log_characters = "VDIWEF";
|
static const char* log_characters = "VDIWEF";
|
||||||
CHECK_EQ(strlen(log_characters), FATAL + 1U);
|
CHECK_EQ(strlen(log_characters), FATAL + 1U);
|
||||||
char severity = log_characters[log_severity];
|
char severity = log_characters[log_severity];
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue