diff options
author | Andreas Gampe <agampe@google.com> | 2016-09-23 13:31:52 -0700 |
---|---|---|
committer | Andreas Gampe <agampe@google.com> | 2016-09-23 13:31:52 -0700 |
commit | 19ff8f1e57154686eeb51375fbcee5bf0f6457e3 (patch) | |
tree | e77af3421812e95e6e0753f9a1fb97a9938fc49c /base | |
parent | 58af713d8a2b15eb211466aa51fa7acfb6356671 (diff) | |
download | core-19ff8f1e57154686eeb51375fbcee5bf0f6457e3.tar.gz core-19ff8f1e57154686eeb51375fbcee5bf0f6457e3.tar.bz2 core-19ff8f1e57154686eeb51375fbcee5bf0f6457e3.zip |
Base: Check severity in destructor
Bug: 31338270
Test: m
Test: mmma system/core/base && $ANDROID_HOST_OUT/nativetest64/libbase_test/libbase_test64
Change-Id: Ic077822bcfd591bd11f4fa9d4b6e440b461c70c1
Diffstat (limited to 'base')
-rw-r--r-- | base/logging.cpp | 5 | ||||
-rw-r--r-- | base/logging_test.cpp | 118 |
2 files changed, 95 insertions, 28 deletions
diff --git a/base/logging.cpp b/base/logging.cpp index 77a0a43c3..2ca224650 100644 --- a/base/logging.cpp +++ b/base/logging.cpp @@ -390,6 +390,11 @@ LogMessage::LogMessage(const char* file, unsigned int line, LogId id, } LogMessage::~LogMessage() { + // Check severity again. This is duplicate work wrt/ LOG macros, but not LOG_STREAM. + if (!WOULD_LOG_SEVERITY(data_->GetSeverity())) { + return; + } + // Finish constructing the message. if (data_->GetError() != -1) { data_->GetBuffer() << ": " << strerror(data_->GetError()); diff --git a/base/logging_test.cpp b/base/logging_test.cpp index a1ed05b82..dccb77488 100644 --- a/base/logging_test.cpp +++ b/base/logging_test.cpp @@ -317,69 +317,131 @@ static void CheckMessage(const CapturedStderr& cap, #endif } -#define CHECK_LOG_STREAM(severity) \ +#define CHECK_LOG_STREAM_DISABLED(severity) \ + android::base::ScopedLogSeverity sls1(android::base::FATAL); \ + CapturedStderr cap1; \ + LOG_STREAM(severity) << "foo bar"; \ + ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); + +#define CHECK_LOG_STREAM_ENABLED(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_FATAL_WITHOUT_ABORT_disabled) { + CHECK_LOG_STREAM_DISABLED(FATAL_WITHOUT_ABORT); +} + +TEST(logging, LOG_STREAM_FATAL_WITHOUT_ABORT_enabled) { + CHECK_LOG_STREAM_ENABLED(FATAL_WITHOUT_ABORT); +} + +TEST(logging, LOG_STREAM_ERROR_disabled) { + CHECK_LOG_STREAM_DISABLED(ERROR); +} + +TEST(logging, LOG_STREAM_ERROR_enabled) { + CHECK_LOG_STREAM_ENABLED(ERROR); +} + +TEST(logging, LOG_STREAM_WARNING_disabled) { + CHECK_LOG_STREAM_DISABLED(WARNING); +} + +TEST(logging, LOG_STREAM_WARNING_enabled) { + CHECK_LOG_STREAM_ENABLED(WARNING); +} + +TEST(logging, LOG_STREAM_INFO_disabled) { + CHECK_LOG_STREAM_DISABLED(INFO); } -TEST(logging, LOG_STREAM_ERROR) { - CHECK_LOG_STREAM(ERROR); +TEST(logging, LOG_STREAM_INFO_enabled) { + CHECK_LOG_STREAM_ENABLED(INFO); } -TEST(logging, LOG_STREAM_WARNING) { - CHECK_LOG_STREAM(WARNING); +TEST(logging, LOG_STREAM_DEBUG_disabled) { + CHECK_LOG_STREAM_DISABLED(DEBUG); } -TEST(logging, LOG_STREAM_INFO) { - CHECK_LOG_STREAM(INFO); +TEST(logging, LOG_STREAM_DEBUG_enabled) { + CHECK_LOG_STREAM_ENABLED(DEBUG); } -TEST(logging, LOG_STREAM_DEBUG) { - CHECK_LOG_STREAM(DEBUG); +TEST(logging, LOG_STREAM_VERBOSE_disabled) { + CHECK_LOG_STREAM_DISABLED(VERBOSE); } -TEST(logging, LOG_STREAM_VERBOSE) { - CHECK_LOG_STREAM(VERBOSE); +TEST(logging, LOG_STREAM_VERBOSE_enabled) { + CHECK_LOG_STREAM_ENABLED(VERBOSE); } -#undef CHECK_LOG_STREAM +#undef CHECK_LOG_STREAM_DISABLED +#undef CHECK_LOG_STREAM_ENABLED -#define CHECK_LOG_STREAM_S(severity) \ +#define CHECK_LOG_STREAM_S_DISABLED(severity) \ + android::base::ScopedLogSeverity sls1(android::base::FATAL); \ + CapturedStderr cap1; \ + LOG_STREAM_S(android::base::severity) << "foobar"; \ + ASSERT_EQ(0, lseek(cap1.fd(), 0, SEEK_CUR)); + +#define CHECK_LOG_STREAM_S_ENABLED(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_FATAL_WITHOUT_ABORT_disabled) { + CHECK_LOG_STREAM_S_DISABLED(FATAL_WITHOUT_ABORT); +} + +TEST(logging, LOG_STREAM_S_FATAL_WITHOUT_ABORT_enabled) { + CHECK_LOG_STREAM_S_ENABLED(FATAL_WITHOUT_ABORT); +} + +TEST(logging, LOG_STREAM_S_ERROR_disabled) { + CHECK_LOG_STREAM_S_DISABLED(ERROR); +} + +TEST(logging, LOG_STREAM_S_ERROR_enabled) { + CHECK_LOG_STREAM_S_ENABLED(ERROR); +} + +TEST(logging, LOG_STREAM_S_WARNING_disabled) { + CHECK_LOG_STREAM_S_DISABLED(WARNING); +} + +TEST(logging, LOG_STREAM_S_WARNING_enabled) { + CHECK_LOG_STREAM_S_ENABLED(WARNING); +} + +TEST(logging, LOG_STREAM_S_INFO_disabled) { + CHECK_LOG_STREAM_S_DISABLED(INFO); } -TEST(logging, LOG_STREAM_S_ERROR) { - CHECK_LOG_STREAM_S(ERROR); +TEST(logging, LOG_STREAM_S_INFO_enabled) { + CHECK_LOG_STREAM_S_ENABLED(INFO); } -TEST(logging, LOG_STREAM_S_WARNING) { - CHECK_LOG_STREAM_S(WARNING); +TEST(logging, LOG_STREAM_S_DEBUG_disabled) { + CHECK_LOG_STREAM_S_DISABLED(DEBUG); } -TEST(logging, LOG_STREAM_S_INFO) { - CHECK_LOG_STREAM_S(INFO); +TEST(logging, LOG_STREAM_S_DEBUG_enabled) { + CHECK_LOG_STREAM_S_ENABLED(DEBUG); } -TEST(logging, LOG_STREAM_S_DEBUG) { - CHECK_LOG_STREAM_S(DEBUG); +TEST(logging, LOG_STREAM_S_VERBOSE_disabled) { + CHECK_LOG_STREAM_S_DISABLED(VERBOSE); } -TEST(logging, LOG_STREAM_S_VERBOSE) { - CHECK_LOG_STREAM_S(VERBOSE); +TEST(logging, LOG_STREAM_S_VERBOSE_enabled) { + CHECK_LOG_STREAM_S_ENABLED(VERBOSE); } -#undef CHECK_LOG_STREAM_S +#undef CHECK_LOG_STREAM_S_DISABLED +#undef CHECK_LOG_STREAM_S_ENABLED #define CHECK_LOG_DISABLED(severity) \ |