void logger_error_tests() { { std::stringstream stream; logger<> l( stream ); l.error( "error message" ); const std::string stream_content = stream.str(); boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:ERROR \\]\\: error message\n" ); BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true ); } { std::stringstream stream; logger<> l( stream ); l.error() << "error message"; const std::string stream_content = stream.str(); boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:ERROR \\]\\: error message\n" ); BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true ); } }
void logger_note_tests() { { std::stringstream stream; logger<> l( stream ); l.note( "note message" ); const std::string stream_content = stream.str(); boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:NOTE \\]\\: note message\n" ); BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true ); } { std::stringstream stream; logger<> l( stream ); l.note() << "note message"; const std::string stream_content = stream.str(); boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:NOTE \\]\\: note message\n" ); BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true ); } }
void logger_warn_tests() { { std::stringstream stream; logger<> l( stream ); l.warn( "not important message" ); const std::string stream_content = stream.str(); boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:WARNING\\]\\: not important message\n" ); BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true ); } { std::stringstream stream; logger<> l( stream ); l.warn() << "not important message"; const std::string stream_content = stream.str(); boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:WARNING\\]\\: not important message\n" ); BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true ); } }
void logger_fatal_tests() { { std::stringstream stream; logger<> l( stream ); l.fatal( "fatal message" ); const std::string stream_content = stream.str(); boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:FATAL \\]\\: fatal message\n" ); BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true ); } { std::stringstream stream; logger<> l( stream ); l.fatal() << "fatal message"; const std::string stream_content = stream.str(); boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:FATAL \\]\\: fatal message\n" ); BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true ); } }
void logger_debug_tests() { { std::stringstream stream; logger<> l( stream ); l.debug( "debug message" ); const std::string stream_content = stream.str(); boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:DEBUG \\]\\: debug message\n" ); BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true ); } { std::stringstream stream; logger<> l( stream ); l.debug() << "debug message"; const std::string stream_content = stream.str(); boost::regex message_regex( "\\[\\d{4}\\-\\w{3}\\-\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{6}\\:DEBUG \\]\\: debug message\n" ); BOOST_CHECK_EQUAL( boost::regex_match( stream_content, message_regex ), true ); } }
TEST(logging, PLOG) { { CapturedStderr cap; errno = ENOENT; PLOG(INFO) << "foobar"; ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0)); std::string output; android::base::ReadFdToString(cap.fd(), &output); ASSERT_GT(output.length(), strlen("foobar")); #if !defined(_WIN32) std::regex message_regex(make_log_pattern( android::base::INFO, "foobar: No such file or directory")); ASSERT_TRUE(std::regex_search(output, message_regex)); #endif } }
TEST(logging, UNIMPLEMENTED) { { CapturedStderr cap; errno = ENOENT; UNIMPLEMENTED(ERROR); ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0)); std::string output; android::base::ReadFdToString(cap.fd(), &output); ASSERT_GT(output.length(), strlen("unimplemented")); #if !defined(_WIN32) std::string expected_message = android::base::StringPrintf("%s unimplemented ", __PRETTY_FUNCTION__); std::regex message_regex( make_log_pattern(android::base::ERROR, expected_message.c_str())); ASSERT_TRUE(std::regex_search(output, message_regex)); #endif } }
TEST(logging, LOG) { ASSERT_DEATH(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 // many characters are in the log message. { CapturedStderr cap; LOG(WARNING) << "foobar"; ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0)); std::string output; android::base::ReadFdToString(cap.fd(), &output); ASSERT_GT(output.length(), strlen("foobar")); #if !defined(_WIN32) std::regex message_regex( make_log_pattern(android::base::WARNING, "foobar")); ASSERT_TRUE(std::regex_search(output, message_regex)); #endif } { CapturedStderr cap; LOG(INFO) << "foobar"; ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0)); std::string output; android::base::ReadFdToString(cap.fd(), &output); ASSERT_GT(output.length(), strlen("foobar")); #if !defined(_WIN32) std::regex message_regex( make_log_pattern(android::base::INFO, "foobar")); ASSERT_TRUE(std::regex_search(output, message_regex)); #endif } { CapturedStderr cap; LOG(DEBUG) << "foobar"; ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0)); std::string output; android::base::ReadFdToString(cap.fd(), &output); ASSERT_TRUE(output.empty()); } { android::base::ScopedLogSeverity severity(android::base::DEBUG); CapturedStderr cap; LOG(DEBUG) << "foobar"; ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0)); std::string output; android::base::ReadFdToString(cap.fd(), &output); ASSERT_GT(output.length(), strlen("foobar")); #if !defined(_WIN32) std::regex message_regex( make_log_pattern(android::base::DEBUG, "foobar")); ASSERT_TRUE(std::regex_search(output, message_regex)); #endif } }