Base: Add INTERNAL_FATAL to logging severity

Add the INTERNAL_FATAL level. It will print 'F' like FATAL, but
does not abort after logging.

Add a test to logging_test.

Bug: 31338270
Test: m
Test: mmma system/core/base && adb sync && adb shell /data/nativetest/libbase_test/libbase_test32
Change-Id: Idf74c08e8516881efccaefc58fa3f41d57e56396
This commit is contained in:
Andreas Gampe 2016-09-07 10:10:50 -07:00
parent af6a185fb4
commit 550829d84b
3 changed files with 40 additions and 12 deletions

View file

@ -41,6 +41,7 @@ enum LogSeverity {
INFO,
WARNING,
ERROR,
FATAL_WITHOUT_ABORT,
FATAL,
};

View file

@ -188,14 +188,18 @@ static const char* ProgramInvocationName() {
#if defined(__linux__)
void KernelLogger(android::base::LogId, android::base::LogSeverity severity,
const char* tag, const char*, unsigned int, const char* msg) {
// clang-format off
static constexpr int kLogSeverityToKernelLogLevel[] = {
[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] = 2, // KERN_CRIT
[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
};
// clang-format on
static_assert(arraysize(kLogSeverityToKernelLogLevel) == android::base::FATAL + 1,
"Mismatch in size of kLogSeverityToKernelLogLevel and values in LogSeverity");
@ -235,7 +239,7 @@ void StderrLogger(LogId, LogSeverity severity, const char*, const char* file,
char timestamp[32];
strftime(timestamp, sizeof(timestamp), "%m-%d %H:%M:%S", &now);
static const char log_characters[] = "VDIWEF";
static const char log_characters[] = "VDIWEFF";
static_assert(arraysize(log_characters) - 1 == FATAL + 1,
"Mismatch in size of log_characters and values in LogSeverity");
char severity_char = log_characters[severity];
@ -252,8 +256,9 @@ void LogdLogger::operator()(LogId id, LogSeverity severity, const char* tag,
const char* file, unsigned int line,
const char* message) {
static constexpr android_LogPriority kLogSeverityToAndroidLogPriority[] = {
ANDROID_LOG_VERBOSE, ANDROID_LOG_DEBUG, ANDROID_LOG_INFO,
ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL,
ANDROID_LOG_VERBOSE, ANDROID_LOG_DEBUG, ANDROID_LOG_INFO,
ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL,
ANDROID_LOG_FATAL,
};
static_assert(arraysize(kLogSeverityToAndroidLogPriority) == FATAL + 1,
"Mismatch in size of kLogSeverityToAndroidLogPriority and values in LogSeverity");
@ -325,12 +330,12 @@ void InitLogging(char* argv[]) {
gMinimumLogSeverity = ERROR;
continue;
case 'f':
gMinimumLogSeverity = FATAL;
gMinimumLogSeverity = FATAL_WITHOUT_ABORT;
continue;
// liblog will even suppress FATAL if you say 's' for silent, but that's
// crazy!
case 's':
gMinimumLogSeverity = FATAL;
gMinimumLogSeverity = FATAL_WITHOUT_ABORT;
continue;
}
}

View file

@ -122,7 +122,9 @@ TEST(logging, CHECK) {
static std::string make_log_pattern(android::base::LogSeverity severity,
const char* message) {
static const char* log_characters = "VDIWEF";
static const char log_characters[] = "VDIWEFF";
static_assert(arraysize(log_characters) - 1 == android::base::FATAL + 1,
"Mismatch in size of log_characters and values in LogSeverity");
char log_char = log_characters[severity];
std::string holder(__FILE__);
return android::base::StringPrintf(
@ -164,6 +166,14 @@ TEST(logging, LOG_FATAL) {
ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar");
}
TEST(logging, LOG_FATAL_WITHOUT_ABORT_disabled) {
CHECK_LOG_DISABLED(FATAL_WITHOUT_ABORT);
}
TEST(logging, LOG_FATAL_WITHOUT_ABORT_enabled) {
CHECK_LOG_ENABLED(FATAL_WITHOUT_ABORT);
}
TEST(logging, LOG_ERROR_disabled) {
CHECK_LOG_DISABLED(ERROR);
}
@ -260,6 +270,18 @@ TEST(logging, LOG_does_not_have_dangling_if) {
PLOG(severity) << "foobar"; \
CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \
TEST(logging, PLOG_FATAL) {
ASSERT_DEATH({SuppressAbortUI(); PLOG(FATAL) << "foobar";}, "foobar");
}
TEST(logging, PLOG_FATAL_WITHOUT_ABORT_disabled) {
CHECK_PLOG_DISABLED(FATAL_WITHOUT_ABORT);
}
TEST(logging, PLOG_FATAL_WITHOUT_ABORT_enabled) {
CHECK_PLOG_ENABLED(FATAL_WITHOUT_ABORT);
}
TEST(logging, PLOG_ERROR_disabled) {
CHECK_PLOG_DISABLED(ERROR);
}