Move CapturedStderr to test_util library

Bug: 32181382
Test: run /data/nativetest(64)/binderTextOutputTest
Change-Id: Ifb2e1f6af2c3f57b5cbed7dde65efb31253c52a2
This commit is contained in:
Wei Wang 2016-10-21 09:23:39 -07:00
parent c75a32aae3
commit 8c176302e6
3 changed files with 46 additions and 36 deletions

View File

@ -48,4 +48,21 @@ class TemporaryDir {
DISALLOW_COPY_AND_ASSIGN(TemporaryDir);
};
class CapturedStderr {
public:
CapturedStderr();
~CapturedStderr();
int fd() const;
private:
void init();
void reset();
TemporaryFile temp_file_;
int old_stderr_;
DISALLOW_COPY_AND_ASSIGN(CapturedStderr);
};
#endif // ANDROID_BASE_TEST_UTILS_H

View File

@ -37,42 +37,6 @@
#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() {
#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));
}
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);

View File

@ -102,3 +102,32 @@ bool TemporaryDir::init(const std::string& tmp_dir) {
OS_PATH_SEPARATOR);
return (mkdtemp(path) != nullptr);
}
CapturedStderr::CapturedStderr() : old_stderr_(-1) {
init();
}
CapturedStderr::~CapturedStderr() {
reset();
}
int CapturedStderr::fd() const {
return temp_file_.fd;
}
void CapturedStderr::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.
CHECK_EQ(0, setvbuf(stderr, NULL, _IONBF, 0));
#endif
old_stderr_ = dup(STDERR_FILENO);
CHECK_NE(-1, old_stderr_);
CHECK_NE(-1, dup2(fd(), STDERR_FILENO));
}
void CapturedStderr::reset() {
CHECK_NE(-1, dup2(old_stderr_, STDERR_FILENO));
CHECK_EQ(0, close(old_stderr_));
// Note: cannot restore prior setvbuf() setting.
}