base logging: fix errno restoring, severity conditionality, dangling ifs
Fix LOG() to properly save and restore errno. Test this properly.
Only do logging if severity is >= the minimum.
Fix dangling if statements in CHECK(), CHECK_STR{EQ,NE}(). Test this
properly.
Fix base logging tests on Windows. All libbase_tests now pass on
Windows.
Change place to lock, so the lock can protect logging of all data in
LogMessage.
Change-Id: I7ff531c67ae10a99ef0a2bbfe279aa77282d5ae9
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
diff --git a/base/logging_test.cpp b/base/logging_test.cpp
index c12dfa5..9cf1aad 100644
--- a/base/logging_test.cpp
+++ b/base/logging_test.cpp
@@ -18,6 +18,10 @@
#include <libgen.h>
+#if defined(_WIN32)
+#include <signal.h>
+#endif
+
#include <regex>
#include <string>
@@ -49,6 +53,11 @@
private:
void init() {
+#if defined(_WIN32)
+ // On Windows, stderr is often buffered, so make sure it is unbuffered so
+ // that we can immediately read back what was written to stderr.
+ ASSERT_EQ(0, setvbuf(stderr, NULL, _IONBF, 0));
+#endif
old_stderr_ = dup(STDERR_FILENO);
ASSERT_NE(-1, old_stderr_);
ASSERT_NE(-1, dup2(fd(), STDERR_FILENO));
@@ -57,21 +66,58 @@
void reset() {
ASSERT_NE(-1, dup2(old_stderr_, STDERR_FILENO));
ASSERT_EQ(0, close(old_stderr_));
+ // Note: cannot restore prior setvbuf() setting.
}
TemporaryFile temp_file_;
int old_stderr_;
};
+#if defined(_WIN32)
+static void ExitSignalAbortHandler(int) {
+ _exit(3);
+}
+#endif
+
+static void SuppressAbortUI() {
+#if defined(_WIN32)
+ // We really just want to call _set_abort_behavior(0, _CALL_REPORTFAULT) to
+ // suppress the Windows Error Reporting dialog box, but that API is not
+ // available in the OS-supplied C Runtime, msvcrt.dll, that we currently
+ // use (it is available in the Visual Studio C runtime).
+ //
+ // Instead, we setup a SIGABRT handler, which is called in abort() right
+ // before calling Windows Error Reporting. In the handler, we exit the
+ // process just like abort() does.
+ ASSERT_NE(SIG_ERR, signal(SIGABRT, ExitSignalAbortHandler));
+#endif
+}
+
TEST(logging, CHECK) {
- ASSERT_DEATH(CHECK(false), "Check failed: false ");
+ ASSERT_DEATH({SuppressAbortUI(); CHECK(false);}, "Check failed: false ");
CHECK(true);
- ASSERT_DEATH(CHECK_EQ(0, 1), "Check failed: 0 == 1 ");
+ ASSERT_DEATH({SuppressAbortUI(); CHECK_EQ(0, 1);}, "Check failed: 0 == 1 ");
CHECK_EQ(0, 0);
- ASSERT_DEATH(CHECK_STREQ("foo", "bar"), R"(Check failed: "foo" == "bar")");
+ ASSERT_DEATH({SuppressAbortUI(); CHECK_STREQ("foo", "bar");},
+ R"(Check failed: "foo" == "bar")");
CHECK_STREQ("foo", "foo");
+
+ // Test whether CHECK() and CHECK_STREQ() have a dangling if with no else.
+ bool flag = false;
+ if (true)
+ CHECK(true);
+ else
+ flag = true;
+ EXPECT_FALSE(flag) << "CHECK macro probably has a dangling if with no else";
+
+ flag = false;
+ if (true)
+ CHECK_STREQ("foo", "foo");
+ else
+ flag = true;
+ EXPECT_FALSE(flag) << "CHECK_STREQ probably has a dangling if with no else";
}
std::string make_log_pattern(android::base::LogSeverity severity,
@@ -85,7 +131,7 @@
}
TEST(logging, LOG) {
- ASSERT_DEATH(LOG(FATAL) << "foobar", "foobar");
+ ASSERT_DEATH({SuppressAbortUI(); LOG(FATAL) << "foobar";}, "foobar");
// We can't usefully check the output of any of these on Windows because we
// don't have std::regex, but we can at least make sure we printed at least as
@@ -148,6 +194,50 @@
ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
#endif
}
+
+ // Test whether LOG() saves and restores errno.
+ {
+ CapturedStderr cap;
+ errno = 12345;
+ LOG(INFO) << (errno = 67890);
+ EXPECT_EQ(12345, errno) << "errno was not restored";
+
+ ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0));
+
+ std::string output;
+ android::base::ReadFdToString(cap.fd(), &output);
+ EXPECT_NE(nullptr, strstr(output.c_str(), "67890")) << output;
+
+#if !defined(_WIN32)
+ std::regex message_regex(
+ make_log_pattern(android::base::INFO, "67890"));
+ ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
+#endif
+ }
+
+ // Test whether LOG() has a dangling if with no else.
+ {
+ CapturedStderr cap;
+
+ // Do the test two ways: once where we hypothesize that LOG()'s if
+ // will evaluate to true (when severity is high enough) and once when we
+ // expect it to evaluate to false (when severity is not high enough).
+ bool flag = false;
+ if (true)
+ LOG(INFO) << "foobar";
+ else
+ flag = true;
+
+ EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
+
+ flag = false;
+ if (true)
+ LOG(VERBOSE) << "foobar";
+ else
+ flag = true;
+
+ EXPECT_FALSE(flag) << "LOG macro probably has a dangling if with no else";
+ }
}
TEST(logging, PLOG) {