diff options
author | Wei Wang | 2016-10-21 11:23:39 -0500 |
---|---|---|
committer | Wei Wang | 2016-10-22 12:35:21 -0500 |
commit | 8c176302e6a6b4de40079af42c30d8d00da89f30 (patch) | |
tree | c063a1d84d1fbb8a9c4b70d2339aeea0063aa08d /base/test_utils.cpp | |
parent | c75a32aae30f8cadbb5608d2fb680ef326708fcd (diff) | |
download | platform-system-core-8c176302e6a6b4de40079af42c30d8d00da89f30.tar.gz platform-system-core-8c176302e6a6b4de40079af42c30d8d00da89f30.tar.xz platform-system-core-8c176302e6a6b4de40079af42c30d8d00da89f30.zip |
Move CapturedStderr to test_util library
Bug: 32181382
Test: run /data/nativetest(64)/binderTextOutputTest
Change-Id: Ifb2e1f6af2c3f57b5cbed7dde65efb31253c52a2
Diffstat (limited to 'base/test_utils.cpp')
-rw-r--r-- | base/test_utils.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/base/test_utils.cpp b/base/test_utils.cpp index 635af6c6e..3b3d698cc 100644 --- a/base/test_utils.cpp +++ b/base/test_utils.cpp | |||
@@ -102,3 +102,32 @@ bool TemporaryDir::init(const std::string& tmp_dir) { | |||
102 | OS_PATH_SEPARATOR); | 102 | OS_PATH_SEPARATOR); |
103 | return (mkdtemp(path) != nullptr); | 103 | return (mkdtemp(path) != nullptr); |
104 | } | 104 | } |
105 | |||
106 | CapturedStderr::CapturedStderr() : old_stderr_(-1) { | ||
107 | init(); | ||
108 | } | ||
109 | |||
110 | CapturedStderr::~CapturedStderr() { | ||
111 | reset(); | ||
112 | } | ||
113 | |||
114 | int CapturedStderr::fd() const { | ||
115 | return temp_file_.fd; | ||
116 | } | ||
117 | |||
118 | void CapturedStderr::init() { | ||
119 | #if defined(_WIN32) | ||
120 | // On Windows, stderr is often buffered, so make sure it is unbuffered so | ||
121 | // that we can immediately read back what was written to stderr. | ||
122 | CHECK_EQ(0, setvbuf(stderr, NULL, _IONBF, 0)); | ||
123 | #endif | ||
124 | old_stderr_ = dup(STDERR_FILENO); | ||
125 | CHECK_NE(-1, old_stderr_); | ||
126 | CHECK_NE(-1, dup2(fd(), STDERR_FILENO)); | ||
127 | } | ||
128 | |||
129 | void CapturedStderr::reset() { | ||
130 | CHECK_NE(-1, dup2(old_stderr_, STDERR_FILENO)); | ||
131 | CHECK_EQ(0, close(old_stderr_)); | ||
132 | // Note: cannot restore prior setvbuf() setting. | ||
133 | } | ||