Add StdioLogger for command-line tools.
Bug: N/A
Test: ran tests
Change-Id: If366a4ea25aea1becdd3e443eba225e9bd52ebba
diff --git a/base/logging_test.cpp b/base/logging_test.cpp
index 5f689fa..75b4ea0 100644
--- a/base/logging_test.cpp
+++ b/base/logging_test.cpp
@@ -206,11 +206,9 @@
}
#endif
-static void CheckMessage(const CapturedStderr& cap, android::base::LogSeverity severity,
+static void CheckMessage(CapturedStderr& cap, android::base::LogSeverity severity,
const char* expected, const char* expected_tag = nullptr) {
- std::string output;
- ASSERT_EQ(0, lseek(cap.fd(), 0, SEEK_SET));
- android::base::ReadFdToString(cap.fd(), &output);
+ std::string output = cap.str();
// 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
@@ -623,3 +621,23 @@
}
CheckMessage(cap, android::base::LogSeverity::INFO, expected_msg, expected_tag);
}
+
+TEST(logging, StdioLogger) {
+ std::string err_str;
+ std::string out_str;
+ {
+ CapturedStderr cap_err;
+ CapturedStdout cap_out;
+ android::base::SetLogger(android::base::StdioLogger);
+ LOG(INFO) << "out";
+ LOG(ERROR) << "err";
+ err_str = cap_err.str();
+ out_str = cap_out.str();
+ }
+
+ // For INFO we expect just the literal "out\n".
+ ASSERT_EQ("out\n", out_str) << out_str;
+ // Whereas ERROR logging includes the program name.
+ ASSERT_EQ(android::base::Basename(android::base::GetExecutablePath()) + ": err\n", err_str)
+ << err_str;
+}