Base: Add more log macros
Add WOULD_LOG to determine whether a given severity would be logged.
Add LOG_STREAM to have direct access to a logging stream.
Add LOG_S variants that take a fully qualified severity. This allows
complex expressions as parameters, e.g., ternaries for conditional
severity levels.
Add tests.
Bug: 31338270
Test: m
Test: mmma system/core/base && $ANDROID_HOST_OUT/nativetest64/libbase_test/libbase_test64
Change-Id: I242b960594e68caff6db9cd8aaa4ce8aaf90474c
diff --git a/base/include/android-base/logging.h b/base/include/android-base/logging.h
index e8b445f..1285f53 100644
--- a/base/include/android-base/logging.h
+++ b/base/include/android-base/logging.h
@@ -120,11 +120,35 @@
DISALLOW_COPY_AND_ASSIGN(ErrnoRestorer);
};
+// Defines whether the given severity will be logged or silently swallowed.
+#define WOULD_LOG(severity) WOULD_LOG_SEVERITY(::android::base::severity)
+#define WOULD_LOG_SEVERITY(severity) \
+ UNLIKELY((severity) >= ::android::base::GetMinimumLogSeverity())
+
+// Get an ostream that can be used for logging at the given severity and to the default
+// destination.
+//
+// Notes:
+// 1) This will not check whether the severity is high enough. One should use WOULD_LOG to filter
+// usage manually.
+// 2) This does not save and restore errno.
+#define LOG_STREAM(severity) LOG_STREAM_S(::android::base::severity)
+#define LOG_STREAM_S(severity) LOG_STREAM_TO_S(DEFAULT, severity)
+
+// Get an ostream that can be used for logging at the given severity and to the
+// given destination. The same notes as for LOG_STREAM apply.
+#define LOG_STREAM_TO(dest, severity) LOG_STREAM_TO_S(dest, ::android::base::severity)
+#define LOG_STREAM_TO_S(dest, severity) \
+ ::android::base::LogMessage(__FILE__, __LINE__, \
+ ::android::base::dest, \
+ severity, -1).stream()
+
// Logs a message to logcat on Android otherwise to stderr. If the severity is
// FATAL it also causes an abort. For example:
//
// LOG(FATAL) << "We didn't expect to reach here";
-#define LOG(severity) LOG_TO(DEFAULT, severity)
+#define LOG(severity) LOG_S(::android::base::severity)
+#define LOG_S(severity) LOG_TO_S(DEFAULT, severity)
// 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.
@@ -132,24 +156,25 @@
// else statement after LOG() macro, it won't bind to the if statement in the macro.
// do-while(0) statement doesn't work here. Because we need to support << operator
// following the macro, like "LOG(DEBUG) << xxx;".
-#define LOG_TO(dest, severity) \
- UNLIKELY(::android::base::severity >= ::android::base::GetMinimumLogSeverity()) && \
- ::android::base::ErrnoRestorer() && \
- ::android::base::LogMessage(__FILE__, __LINE__, \
- ::android::base::dest, \
- ::android::base::severity, -1).stream()
+#define LOG_TO(dest, severity) LOG_TO_S(dest, ::android::base::severity)
+#define LOG_TO_S(dest, severity) \
+ WOULD_LOG_SEVERITY(severity) && \
+ ::android::base::ErrnoRestorer() && \
+ LOG_STREAM_TO_S(dest, severity) \
// A variant of LOG that also logs the current errno value. To be used when
// library calls fail.
-#define PLOG(severity) PLOG_TO(DEFAULT, severity)
+#define PLOG(severity) PLOG_S(::android::base::severity)
+#define PLOG_S(severity) PLOG_TO_S(DEFAULT, severity)
// Behaves like PLOG, but logs to the specified log ID.
-#define PLOG_TO(dest, severity) \
- UNLIKELY(::android::base::severity >= ::android::base::GetMinimumLogSeverity()) && \
- ::android::base::ErrnoRestorer() && \
- ::android::base::LogMessage(__FILE__, __LINE__, \
- ::android::base::dest, \
- ::android::base::severity, errno).stream()
+#define PLOG_TO(dest, severity) PLOG_TO_S(dest, ::android::base::severity)
+#define PLOG_TO_S(dest, severity) \
+ WOULD_LOG_SEVERITY(severity) && \
+ ::android::base::ErrnoRestorer() && \
+ ::android::base::LogMessage(__FILE__, __LINE__, \
+ ::android::base::dest, \
+ severity, errno).stream()
// Marker that code is yet to be implemented.
#define UNIMPLEMENTED(level) \
diff --git a/base/logging_test.cpp b/base/logging_test.cpp
index 8813525..a1ed05b 100644
--- a/base/logging_test.cpp
+++ b/base/logging_test.cpp
@@ -142,6 +142,151 @@
// we don't get more bit-rot).
}
+#define CHECK_WOULD_LOG_DISABLED(severity) \
+ static_assert(android::base::severity < android::base::FATAL, "Bad input"); \
+ for (size_t i = static_cast<size_t>(android::base::severity) + 1; \
+ i <= static_cast<size_t>(android::base::FATAL); \
+ ++i) { \
+ android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \
+ EXPECT_FALSE(WOULD_LOG(severity)) << i; \
+ }
+
+#define CHECK_WOULD_LOG_ENABLED(severity) \
+ for (size_t i = static_cast<size_t>(android::base::VERBOSE); \
+ i <= static_cast<size_t>(android::base::severity); \
+ ++i) { \
+ android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \
+ EXPECT_TRUE(WOULD_LOG(severity)) << i; \
+ }
+
+TEST(logging, WOULD_LOG_FATAL) {
+ CHECK_WOULD_LOG_ENABLED(FATAL);
+}
+
+TEST(logging, WOULD_LOG_FATAL_WITHOUT_ABORT_disabled) {
+ CHECK_WOULD_LOG_DISABLED(FATAL_WITHOUT_ABORT);
+}
+
+TEST(logging, WOULD_LOG_FATAL_WITHOUT_ABORT_enabled) {
+ CHECK_WOULD_LOG_ENABLED(FATAL_WITHOUT_ABORT);
+}
+
+TEST(logging, WOULD_LOG_ERROR_disabled) {
+ CHECK_WOULD_LOG_DISABLED(ERROR);
+}
+
+TEST(logging, WOULD_LOG_ERROR_enabled) {
+ CHECK_WOULD_LOG_ENABLED(ERROR);
+}
+
+TEST(logging, WOULD_LOG_WARNING_disabled) {
+ CHECK_WOULD_LOG_DISABLED(WARNING);
+}
+
+TEST(logging, WOULD_LOG_WARNING_enabled) {
+ CHECK_WOULD_LOG_ENABLED(WARNING);
+}
+
+TEST(logging, WOULD_LOG_INFO_disabled) {
+ CHECK_WOULD_LOG_DISABLED(INFO);
+}
+
+TEST(logging, WOULD_LOG_INFO_enabled) {
+ CHECK_WOULD_LOG_ENABLED(INFO);
+}
+
+TEST(logging, WOULD_LOG_DEBUG_disabled) {
+ CHECK_WOULD_LOG_DISABLED(DEBUG);
+}
+
+TEST(logging, WOULD_LOG_DEBUG_enabled) {
+ CHECK_WOULD_LOG_ENABLED(DEBUG);
+}
+
+TEST(logging, WOULD_LOG_VERBOSE_disabled) {
+ CHECK_WOULD_LOG_DISABLED(VERBOSE);
+}
+
+TEST(logging, WOULD_LOG_VERBOSE_enabled) {
+ CHECK_WOULD_LOG_ENABLED(VERBOSE);
+}
+
+#undef CHECK_WOULD_LOG_DISABLED
+#undef CHECK_WOULD_LOG_ENABLED
+
+
+#define CHECK_WOULD_LOG_SEVERITY_DISABLED(severity) \
+ static_assert(android::base::severity < android::base::FATAL, "Bad input"); \
+ for (size_t i = static_cast<size_t>(android::base::severity) + 1; \
+ i <= static_cast<size_t>(android::base::FATAL); \
+ ++i) { \
+ android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \
+ EXPECT_FALSE(WOULD_LOG_SEVERITY(::android::base::severity)) << i; \
+ }
+
+#define CHECK_WOULD_LOG_SEVERITY_ENABLED(severity) \
+ for (size_t i = static_cast<size_t>(android::base::VERBOSE); \
+ i <= static_cast<size_t>(android::base::severity); \
+ ++i) { \
+ android::base::ScopedLogSeverity sls2(static_cast<android::base::LogSeverity>(i)); \
+ EXPECT_TRUE(WOULD_LOG_SEVERITY(::android::base::severity)) << i; \
+ }
+
+TEST(logging,WOULD_LOG_SEVERITY_FATAL) {
+ CHECK_WOULD_LOG_SEVERITY_ENABLED(FATAL);
+}
+
+TEST(logging,WOULD_LOG_SEVERITY_FATAL_WITHOUT_ABORT_disabled) {
+ CHECK_WOULD_LOG_SEVERITY_DISABLED(FATAL_WITHOUT_ABORT);
+}
+
+TEST(logging, WOULD_LOG_SEVERITY_FATAL_WITHOUT_ABORT_enabled) {
+ CHECK_WOULD_LOG_SEVERITY_ENABLED(FATAL_WITHOUT_ABORT);
+}
+
+TEST(logging, WOULD_LOG_SEVERITY_ERROR_disabled) {
+ CHECK_WOULD_LOG_SEVERITY_DISABLED(ERROR);
+}
+
+TEST(logging, WOULD_LOG_SEVERITY_ERROR_enabled) {
+ CHECK_WOULD_LOG_SEVERITY_ENABLED(ERROR);
+}
+
+TEST(logging, WOULD_LOG_SEVERITY_WARNING_disabled) {
+ CHECK_WOULD_LOG_SEVERITY_DISABLED(WARNING);
+}
+
+TEST(logging, WOULD_LOG_SEVERITY_WARNING_enabled) {
+ CHECK_WOULD_LOG_SEVERITY_ENABLED(WARNING);
+}
+
+TEST(logging, WOULD_LOG_SEVERITY_INFO_disabled) {
+ CHECK_WOULD_LOG_SEVERITY_DISABLED(INFO);
+}
+
+TEST(logging, WOULD_LOG_SEVERITY_INFO_enabled) {
+ CHECK_WOULD_LOG_SEVERITY_ENABLED(INFO);
+}
+
+TEST(logging, WOULD_LOG_SEVERITY_DEBUG_disabled) {
+ CHECK_WOULD_LOG_SEVERITY_DISABLED(DEBUG);
+}
+
+TEST(logging, WOULD_LOG_SEVERITY_DEBUG_enabled) {
+ CHECK_WOULD_LOG_SEVERITY_ENABLED(DEBUG);
+}
+
+TEST(logging, WOULD_LOG_SEVERITYVERBOSE_disabled) {
+ CHECK_WOULD_LOG_SEVERITY_DISABLED(VERBOSE);
+}
+
+TEST(logging, WOULD_LOG_SEVERITY_VERBOSE_enabled) {
+ CHECK_WOULD_LOG_SEVERITY_ENABLED(VERBOSE);
+}
+
+#undef CHECK_WOULD_LOG_SEVERITY_DISABLED
+#undef CHECK_WOULD_LOG_SEVERITY_ENABLED
+
static std::string make_log_pattern(android::base::LogSeverity severity,
const char* message) {
static const char log_characters[] = "VDIWEFF";
@@ -154,18 +299,6 @@
log_char, basename(&holder[0]), message);
}
-#define CHECK_LOG_DISABLED(severity) \
- android::base::ScopedLogSeverity sls1(android::base::FATAL); \
- CapturedStderr cap1; \
- LOG(severity) << "foo bar"; \
- ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
-
-#define CHECK_LOG_ENABLED(severity) \
- android::base::ScopedLogSeverity sls2(android::base::severity); \
- CapturedStderr cap2; \
- LOG(severity) << "foobar"; \
- CheckMessage(cap2, android::base::severity, "foobar"); \
-
static void CheckMessage(const CapturedStderr& cap,
android::base::LogSeverity severity, const char* expected) {
std::string output;
@@ -184,6 +317,83 @@
#endif
}
+#define CHECK_LOG_STREAM(severity) \
+ android::base::ScopedLogSeverity sls2(android::base::severity); \
+ CapturedStderr cap2; \
+ LOG_STREAM(severity) << "foobar"; \
+ CheckMessage(cap2, android::base::severity, "foobar"); \
+
+TEST(logging, LOG_STREAM_FATAL_WITHOUT_ABORT) {
+ CHECK_LOG_STREAM(FATAL_WITHOUT_ABORT);
+}
+
+TEST(logging, LOG_STREAM_ERROR) {
+ CHECK_LOG_STREAM(ERROR);
+}
+
+TEST(logging, LOG_STREAM_WARNING) {
+ CHECK_LOG_STREAM(WARNING);
+}
+
+TEST(logging, LOG_STREAM_INFO) {
+ CHECK_LOG_STREAM(INFO);
+}
+
+TEST(logging, LOG_STREAM_DEBUG) {
+ CHECK_LOG_STREAM(DEBUG);
+}
+
+TEST(logging, LOG_STREAM_VERBOSE) {
+ CHECK_LOG_STREAM(VERBOSE);
+}
+
+#undef CHECK_LOG_STREAM
+
+#define CHECK_LOG_STREAM_S(severity) \
+ android::base::ScopedLogSeverity sls2(android::base::severity); \
+ CapturedStderr cap2; \
+ LOG_STREAM_S(android::base::severity) << "foobar"; \
+ CheckMessage(cap2, android::base::severity, "foobar"); \
+
+TEST(logging, LOG_STREAM_S_FATAL_WITHOUT_ABORT) {
+ CHECK_LOG_STREAM_S(FATAL_WITHOUT_ABORT);
+}
+
+TEST(logging, LOG_STREAM_S_ERROR) {
+ CHECK_LOG_STREAM_S(ERROR);
+}
+
+TEST(logging, LOG_STREAM_S_WARNING) {
+ CHECK_LOG_STREAM_S(WARNING);
+}
+
+TEST(logging, LOG_STREAM_S_INFO) {
+ CHECK_LOG_STREAM_S(INFO);
+}
+
+TEST(logging, LOG_STREAM_S_DEBUG) {
+ CHECK_LOG_STREAM_S(DEBUG);
+}
+
+TEST(logging, LOG_STREAM_S_VERBOSE) {
+ CHECK_LOG_STREAM_S(VERBOSE);
+}
+
+#undef CHECK_LOG_STREAM_S
+
+
+#define CHECK_LOG_DISABLED(severity) \
+ android::base::ScopedLogSeverity sls1(android::base::FATAL); \
+ CapturedStderr cap1; \
+ LOG(severity) << "foo bar"; \
+ ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
+
+#define CHECK_LOG_ENABLED(severity) \
+ android::base::ScopedLogSeverity sls2(android::base::severity); \
+ CapturedStderr cap2; \
+ LOG(severity) << "foobar"; \
+ CheckMessage(cap2, android::base::severity, "foobar"); \
+
TEST(logging, LOG_FATAL) {
ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar");
}
@@ -236,6 +446,102 @@
CHECK_LOG_ENABLED(VERBOSE);
}
+#undef CHECK_LOG_DISABLED
+#undef CHECK_LOG_ENABLED
+
+#define CHECK_LOG_S_DISABLED(severity) \
+ android::base::ScopedLogSeverity sls1(android::base::FATAL); \
+ CapturedStderr cap1; \
+ LOG_S(::android::base::severity) << "foo bar"; \
+ ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
+
+#define CHECK_LOG_S_ENABLED(severity) \
+ android::base::ScopedLogSeverity sls2(android::base::severity); \
+ CapturedStderr cap2; \
+ LOG_S(::android::base::severity) << "foobar"; \
+ CheckMessage(cap2, android::base::severity, "foobar"); \
+
+TEST(logging, LOG_S_FATAL) {
+ ASSERT_DEATH({SuppressAbortUI(); LOG_S(::android::base::FATAL) << "foobar";}, "foobar");
+}
+
+TEST(logging, LOG_S_FATAL_WITHOUT_ABORT_disabled) {
+ CHECK_LOG_S_DISABLED(FATAL_WITHOUT_ABORT);
+}
+
+TEST(logging, LOG_S_FATAL_WITHOUT_ABORT_enabled) {
+ CHECK_LOG_S_ENABLED(FATAL_WITHOUT_ABORT);
+}
+
+TEST(logging, LOG_S_ERROR_disabled) {
+ CHECK_LOG_S_DISABLED(ERROR);
+}
+
+TEST(logging, LOG_S_ERROR_enabled) {
+ CHECK_LOG_S_ENABLED(ERROR);
+}
+
+TEST(logging, LOG_S_WARNING_disabled) {
+ CHECK_LOG_S_DISABLED(WARNING);
+}
+
+TEST(logging, LOG_S_WARNING_enabled) {
+ CHECK_LOG_S_ENABLED(WARNING);
+}
+
+TEST(logging, LOG_S_INFO_disabled) {
+ CHECK_LOG_S_DISABLED(INFO);
+}
+
+TEST(logging, LOG_S_INFO_enabled) {
+ CHECK_LOG_S_ENABLED(INFO);
+}
+
+TEST(logging, LOG_S_DEBUG_disabled) {
+ CHECK_LOG_S_DISABLED(DEBUG);
+}
+
+TEST(logging, LOG_S_DEBUG_enabled) {
+ CHECK_LOG_S_ENABLED(DEBUG);
+}
+
+TEST(logging, LOG_S_VERBOSE_disabled) {
+ CHECK_LOG_S_DISABLED(VERBOSE);
+}
+
+TEST(logging, LOG_S_VERBOSE_enabled) {
+ CHECK_LOG_S_ENABLED(VERBOSE);
+}
+
+#undef CHECK_LOG_S_DISABLED
+#undef CHECK_LOG_S_ENABLED
+
+TEST(logging, LOG_S_complex_param) {
+#define CHECK_LOG_S_COMBINATION(use_scoped_log_severity_info, use_logging_severity_info) \
+ { \
+ android::base::ScopedLogSeverity sls( \
+ (use_scoped_log_severity_info) ? ::android::base::INFO : ::android::base::WARNING); \
+ CapturedStderr cap; \
+ LOG_S((use_logging_severity_info) ? ::android::base::INFO : ::android::base::WARNING) \
+ << "foobar"; \
+ if ((use_scoped_log_severity_info) || !(use_logging_severity_info)) { \
+ CheckMessage(cap, \
+ (use_logging_severity_info) ? ::android::base::INFO : ::android::base::WARNING, \
+ "foobar"); \
+ } else { \
+ ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_CUR)); \
+ } \
+ }
+
+ CHECK_LOG_S_COMBINATION(false,false);
+ CHECK_LOG_S_COMBINATION(false,true);
+ CHECK_LOG_S_COMBINATION(true,false);
+ CHECK_LOG_S_COMBINATION(true,true);
+
+#undef CHECK_LOG_S_COMBINATION
+}
+
+
TEST(logging, LOG_does_not_clobber_errno) {
CapturedStderr cap;
errno = 12345;
@@ -245,6 +551,15 @@
CheckMessage(cap, android::base::INFO, "67890");
}
+TEST(logging, LOG_S_does_not_clobber_errno) {
+ CapturedStderr cap;
+ errno = 12345;
+ LOG_S(::android::base::INFO) << (errno = 67890);
+ EXPECT_EQ(12345, errno) << "errno was not restored";
+
+ CheckMessage(cap, android::base::INFO, "67890");
+}
+
TEST(logging, PLOG_does_not_clobber_errno) {
CapturedStderr cap;
errno = 12345;
@@ -254,6 +569,16 @@
CheckMessage(cap, android::base::INFO, "67890");
}
+TEST(logging, PLOG_S_does_not_clobber_errno) {
+ CapturedStderr cap;
+ errno = 12345;
+ PLOG_S(::android::base::INFO) << (errno = 67890);
+ EXPECT_EQ(12345, errno) << "errno was not restored";
+
+ CheckMessage(cap, android::base::INFO, "67890");
+}
+
+
TEST(logging, LOG_does_not_have_dangling_if) {
CapturedStderr cap; // So the logging below has no side-effects.
@@ -275,9 +600,23 @@
flag = true;
EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
-}
-#define CHECK_PLOG(severity) \
+ flag = false;
+ if (true)
+ LOG_S(::android::base::INFO) << "foobar";
+ else
+ flag = true;
+
+ EXPECT_FALSE(flag) << "LOG_S macro probably has a dangling if with no else";
+
+ flag = false;
+ if (true)
+ LOG_S(::android::base::VERBOSE) << "foobar";
+ else
+ flag = true;
+
+ EXPECT_FALSE(flag) << "LOG_S macro probably has a dangling if with no else";
+}
#define CHECK_PLOG_DISABLED(severity) \
android::base::ScopedLogSeverity sls1(android::base::FATAL); \
@@ -344,6 +683,79 @@
CHECK_PLOG_ENABLED(VERBOSE);
}
+#undef CHECK_PLOG_DISABLED
+#undef CHECK_PLOG_ENABLED
+
+
+#define CHECK_PLOG_S_DISABLED(severity) \
+ android::base::ScopedLogSeverity sls1(android::base::FATAL); \
+ CapturedStderr cap1; \
+ PLOG_S(android::base::severity) << "foo bar"; \
+ ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); \
+
+#define CHECK_PLOG_S_ENABLED(severity) \
+ android::base::ScopedLogSeverity sls2(android::base::severity); \
+ CapturedStderr cap2; \
+ errno = ENOENT; \
+ PLOG_S(android::base::severity) << "foobar"; \
+ CheckMessage(cap2, android::base::severity, "foobar: No such file or directory"); \
+
+TEST(logging, PLOG_S_FATAL) {
+ ASSERT_DEATH({SuppressAbortUI(); PLOG_S(::android::base::FATAL) << "foobar";}, "foobar");
+}
+
+TEST(logging, PLOG_S_FATAL_WITHOUT_ABORT_disabled) {
+ CHECK_PLOG_S_DISABLED(FATAL_WITHOUT_ABORT);
+}
+
+TEST(logging, PLOG_S_FATAL_WITHOUT_ABORT_enabled) {
+ CHECK_PLOG_S_ENABLED(FATAL_WITHOUT_ABORT);
+}
+
+TEST(logging, PLOG_S_ERROR_disabled) {
+ CHECK_PLOG_S_DISABLED(ERROR);
+}
+
+TEST(logging, PLOG_S_ERROR_enabled) {
+ CHECK_PLOG_S_ENABLED(ERROR);
+}
+
+TEST(logging, PLOG_S_WARNING_disabled) {
+ CHECK_PLOG_S_DISABLED(WARNING);
+}
+
+TEST(logging, PLOG_S_WARNING_enabled) {
+ CHECK_PLOG_S_ENABLED(WARNING);
+}
+
+TEST(logging, PLOG_S_INFO_disabled) {
+ CHECK_PLOG_S_DISABLED(INFO);
+}
+
+TEST(logging, PLOG_S_INFO_enabled) {
+ CHECK_PLOG_S_ENABLED(INFO);
+}
+
+TEST(logging, PLOG_S_DEBUG_disabled) {
+ CHECK_PLOG_S_DISABLED(DEBUG);
+}
+
+TEST(logging, PLOG_S_DEBUG_enabled) {
+ CHECK_PLOG_S_ENABLED(DEBUG);
+}
+
+TEST(logging, PLOG_S_VERBOSE_disabled) {
+ CHECK_PLOG_S_DISABLED(VERBOSE);
+}
+
+TEST(logging, PLOG_S_VERBOSE_enabled) {
+ CHECK_PLOG_S_ENABLED(VERBOSE);
+}
+
+#undef CHECK_PLOG_S_DISABLED
+#undef CHECK_PLOG_S_ENABLED
+
+
TEST(logging, UNIMPLEMENTED) {
std::string expected = android::base::StringPrintf("%s unimplemented ", __PRETTY_FUNCTION__);