2015-03-14 14:06:01 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "base/logging.h"
|
|
|
|
|
2015-08-14 11:09:29 +08:00
|
|
|
#include <libgen.h>
|
|
|
|
|
2015-03-14 14:06:01 +08:00
|
|
|
#include <regex>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "base/file.h"
|
|
|
|
#include "base/stringprintf.h"
|
2015-05-07 04:26:00 +08:00
|
|
|
#include "base/test_utils.h"
|
2015-03-14 14:06:01 +08:00
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
#define HOST_TEST(suite, name) TEST(suite, DISABLED_ ## name)
|
|
|
|
#else
|
|
|
|
#define HOST_TEST(suite, name) TEST(suite, name)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class CapturedStderr {
|
|
|
|
public:
|
|
|
|
CapturedStderr() : old_stderr_(-1) {
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
~CapturedStderr() {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd() const {
|
|
|
|
return temp_file_.fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void init() {
|
|
|
|
old_stderr_ = dup(STDERR_FILENO);
|
|
|
|
ASSERT_NE(-1, old_stderr_);
|
|
|
|
ASSERT_NE(-1, dup2(fd(), STDERR_FILENO));
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset() {
|
|
|
|
ASSERT_NE(-1, dup2(old_stderr_, STDERR_FILENO));
|
|
|
|
ASSERT_EQ(0, close(old_stderr_));
|
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryFile temp_file_;
|
|
|
|
int old_stderr_;
|
|
|
|
};
|
|
|
|
|
2015-03-28 02:20:14 +08:00
|
|
|
TEST(logging, CHECK) {
|
2015-03-14 14:06:01 +08:00
|
|
|
ASSERT_DEATH(CHECK(false), "Check failed: false ");
|
|
|
|
CHECK(true);
|
|
|
|
|
|
|
|
ASSERT_DEATH(CHECK_EQ(0, 1), "Check failed: 0 == 1 ");
|
|
|
|
CHECK_EQ(0, 0);
|
|
|
|
|
|
|
|
ASSERT_DEATH(CHECK_STREQ("foo", "bar"), R"(Check failed: "foo" == "bar")");
|
|
|
|
CHECK_STREQ("foo", "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string make_log_pattern(android::base::LogSeverity severity,
|
|
|
|
const char* message) {
|
|
|
|
static const char* log_characters = "VDIWEF";
|
|
|
|
char log_char = log_characters[severity];
|
2015-08-12 07:00:13 +08:00
|
|
|
std::string holder(__FILE__);
|
2015-03-14 14:06:01 +08:00
|
|
|
return android::base::StringPrintf(
|
2015-08-12 07:00:13 +08:00
|
|
|
"%c[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+ %s:[[:digit:]]+] %s",
|
|
|
|
log_char, basename(&holder[0]), message);
|
2015-03-14 14:06:01 +08:00
|
|
|
}
|
|
|
|
|
2015-03-28 02:20:14 +08:00
|
|
|
TEST(logging, LOG) {
|
2015-03-14 14:06:01 +08:00
|
|
|
ASSERT_DEATH(LOG(FATAL) << "foobar", "foobar");
|
|
|
|
|
2015-04-30 02:32:23 +08:00
|
|
|
// 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.
|
2015-03-14 14:06:01 +08:00
|
|
|
{
|
|
|
|
CapturedStderr cap;
|
|
|
|
LOG(WARNING) << "foobar";
|
|
|
|
ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0));
|
|
|
|
|
|
|
|
std::string output;
|
|
|
|
android::base::ReadFdToString(cap.fd(), &output);
|
2015-04-30 02:32:23 +08:00
|
|
|
ASSERT_GT(output.length(), strlen("foobar"));
|
2015-03-14 14:06:01 +08:00
|
|
|
|
2015-04-30 02:32:23 +08:00
|
|
|
#if !defined(_WIN32)
|
2015-03-14 14:06:01 +08:00
|
|
|
std::regex message_regex(
|
|
|
|
make_log_pattern(android::base::WARNING, "foobar"));
|
2015-08-12 07:00:13 +08:00
|
|
|
ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
|
2015-04-30 02:32:23 +08:00
|
|
|
#endif
|
2015-03-14 14:06:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
CapturedStderr cap;
|
|
|
|
LOG(INFO) << "foobar";
|
|
|
|
ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0));
|
|
|
|
|
|
|
|
std::string output;
|
|
|
|
android::base::ReadFdToString(cap.fd(), &output);
|
2015-04-30 02:32:23 +08:00
|
|
|
ASSERT_GT(output.length(), strlen("foobar"));
|
2015-03-14 14:06:01 +08:00
|
|
|
|
2015-04-30 02:32:23 +08:00
|
|
|
#if !defined(_WIN32)
|
2015-03-14 14:06:01 +08:00
|
|
|
std::regex message_regex(
|
|
|
|
make_log_pattern(android::base::INFO, "foobar"));
|
2015-08-12 07:00:13 +08:00
|
|
|
ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
|
2015-04-30 02:32:23 +08:00
|
|
|
#endif
|
2015-03-14 14:06:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
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);
|
2015-04-30 02:32:23 +08:00
|
|
|
ASSERT_GT(output.length(), strlen("foobar"));
|
2015-03-14 14:06:01 +08:00
|
|
|
|
2015-04-30 02:32:23 +08:00
|
|
|
#if !defined(_WIN32)
|
2015-03-14 14:06:01 +08:00
|
|
|
std::regex message_regex(
|
|
|
|
make_log_pattern(android::base::DEBUG, "foobar"));
|
2015-08-12 07:00:13 +08:00
|
|
|
ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
|
2015-04-30 02:32:23 +08:00
|
|
|
#endif
|
2015-03-14 14:06:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-28 02:20:14 +08:00
|
|
|
TEST(logging, PLOG) {
|
2015-03-14 14:06:01 +08:00
|
|
|
{
|
|
|
|
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);
|
2015-04-30 02:32:23 +08:00
|
|
|
ASSERT_GT(output.length(), strlen("foobar"));
|
2015-03-14 14:06:01 +08:00
|
|
|
|
2015-04-30 02:32:23 +08:00
|
|
|
#if !defined(_WIN32)
|
2015-03-14 14:06:01 +08:00
|
|
|
std::regex message_regex(make_log_pattern(
|
|
|
|
android::base::INFO, "foobar: No such file or directory"));
|
2015-08-12 07:00:13 +08:00
|
|
|
ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
|
2015-04-30 02:32:23 +08:00
|
|
|
#endif
|
2015-03-14 14:06:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-28 02:20:14 +08:00
|
|
|
TEST(logging, UNIMPLEMENTED) {
|
2015-03-14 14:06:01 +08:00
|
|
|
{
|
|
|
|
CapturedStderr cap;
|
|
|
|
errno = ENOENT;
|
|
|
|
UNIMPLEMENTED(ERROR);
|
|
|
|
ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0));
|
|
|
|
|
|
|
|
std::string output;
|
|
|
|
android::base::ReadFdToString(cap.fd(), &output);
|
2015-04-30 02:32:23 +08:00
|
|
|
ASSERT_GT(output.length(), strlen("unimplemented"));
|
2015-03-14 14:06:01 +08:00
|
|
|
|
2015-04-30 02:32:23 +08:00
|
|
|
#if !defined(_WIN32)
|
2015-03-14 14:06:01 +08:00
|
|
|
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()));
|
2015-08-12 07:00:13 +08:00
|
|
|
ASSERT_TRUE(std::regex_search(output, message_regex)) << output;
|
2015-04-30 02:32:23 +08:00
|
|
|
#endif
|
2015-03-14 14:06:01 +08:00
|
|
|
}
|
|
|
|
}
|