17#if defined(CATCH_CONFIG_NEW_CAPTURE)
22 #define fileno _fileno
32 : m_originalStream( originalStream ),
33 m_redirectionStream( redirectionStream ),
34 m_prevBuf( m_originalStream.rdbuf() )
36 m_originalStream.rdbuf( m_redirectionStream.rdbuf() );
40 m_originalStream.rdbuf( m_prevBuf );
53 : m_redirectedCout(redirectedCout),
54 m_redirectedCerr(redirectedCerr)
58 m_redirectedCout += m_redirectedStdOut.
str();
59 m_redirectedCerr += m_redirectedStdErr.
str();
62#if defined(CATCH_CONFIG_NEW_CAPTURE)
65 TempFile::TempFile() {
66 if (tmpnam_s(m_buffer)) {
69 if (fopen_s(&m_file, m_buffer,
"w")) {
71 if (strerror_s(buffer, errno)) {
74 CATCH_RUNTIME_ERROR(
"Could not open the temp file: '" << m_buffer <<
"' because: " << buffer);
78 TempFile::TempFile() {
79 m_file = std::tmpfile();
87 TempFile::~TempFile() {
93 std::remove(m_buffer);
98 FILE* TempFile::getFile() {
102 std::string TempFile::getContents() {
103 std::stringstream sstr;
104 char buffer[100] = {};
106 while (std::fgets(buffer,
sizeof(buffer), m_file)) {
112 OutputRedirect::OutputRedirect(std::string& stdout_dest, std::string& stderr_dest) :
113 m_originalStdout(dup(1)),
114 m_originalStderr(dup(2)),
115 m_stdoutDest(stdout_dest),
116 m_stderrDest(stderr_dest) {
117 dup2(fileno(m_stdoutFile.getFile()), 1);
118 dup2(fileno(m_stderrFile.getFile()), 2);
121 OutputRedirect::~OutputRedirect() {
130 dup2(m_originalStdout, 1);
131 dup2(m_originalStderr, 2);
133 m_stdoutDest += m_stdoutFile.getContents();
134 m_stderrDest += m_stderrFile.getContents();
141#if defined(CATCH_CONFIG_NEW_CAPTURE)
142 #if defined(_MSC_VER)
#define CATCH_RUNTIME_ERROR(msg)
auto str() const -> std::string
auto str() const -> std::string
RedirectedStream(std::ostream &originalStream, std::ostream &redirectionStream)
RedirectedStreams(RedirectedStreams const &)=delete
auto str() const -> std::string