Add an experimental new way of capturing stdout/stderr
Unlike the relatively non-invasive old way of capturing stdout/stderr,
this new way is also able to capture output from C's stdlib functions
such as `printf`. This is done by redirecting stdout and stderr file
descriptors to a file, and then reading this file back.
This approach has two sizeable drawbacks:
1) Performance, obviously. Previously an installed capture made the
program run faster (as long as it was then discarded), because a call
to `std::cout` did not result in text output to the console. This new
capture method in fact forces disk IO. While it is likely that any
modern OS will keep this file in memory-cache and might never actually
issue the IO to the backing storage, it is still a possibility and
calls to the file system are not free.
2) Nonportability. While POSIX is usually assumed portable, and this
implementation relies only on a very common parts of it, it is no
longer standard C++ (or just plain C) and thus might not be available
on some obscure platforms. Different C libs might also implement the
relevant functions in a less-than-useful ways (e.g. MS's `tmpfile`
generates a temp file inside system folder, so it will not work
without elevated privileges and thus is useless).
These two drawbacks mean that, at least for now, the new capture is
opt-in. To opt-in, `CATCH_CONFIG_EXPERIMENTAL_REDIRECT` needs to be
defined in the implementation file.
Closes #1243
2018-04-29 22:14:41 +02:00
|
|
|
/*
|
|
|
|
* Created by Martin on 28/04/2018.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
|
|
|
#ifndef TWOBLUECUBES_CATCH_OUTPUT_REDIRECT_H
|
|
|
|
#define TWOBLUECUBES_CATCH_OUTPUT_REDIRECT_H
|
|
|
|
|
|
|
|
#include "catch_platform.h"
|
|
|
|
#include "catch_stream.h"
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <iosfwd>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace Catch {
|
|
|
|
|
|
|
|
class RedirectedStream {
|
|
|
|
std::ostream& m_originalStream;
|
|
|
|
std::ostream& m_redirectionStream;
|
|
|
|
std::streambuf* m_prevBuf;
|
|
|
|
|
|
|
|
public:
|
|
|
|
RedirectedStream( std::ostream& originalStream, std::ostream& redirectionStream );
|
|
|
|
~RedirectedStream();
|
|
|
|
};
|
|
|
|
|
|
|
|
class RedirectedStdOut {
|
|
|
|
ReusableStringStream m_rss;
|
|
|
|
RedirectedStream m_cout;
|
|
|
|
public:
|
|
|
|
RedirectedStdOut();
|
|
|
|
auto str() const -> std::string;
|
|
|
|
};
|
|
|
|
|
|
|
|
// StdErr has two constituent streams in C++, std::cerr and std::clog
|
|
|
|
// This means that we need to redirect 2 streams into 1 to keep proper
|
|
|
|
// order of writes
|
|
|
|
class RedirectedStdErr {
|
|
|
|
ReusableStringStream m_rss;
|
|
|
|
RedirectedStream m_cerr;
|
|
|
|
RedirectedStream m_clog;
|
|
|
|
public:
|
|
|
|
RedirectedStdErr();
|
|
|
|
auto str() const -> std::string;
|
|
|
|
};
|
|
|
|
|
2019-03-02 21:18:48 +01:00
|
|
|
class RedirectedStreams {
|
|
|
|
public:
|
|
|
|
RedirectedStreams(RedirectedStreams const&) = delete;
|
|
|
|
RedirectedStreams& operator=(RedirectedStreams const&) = delete;
|
|
|
|
RedirectedStreams(RedirectedStreams&&) = delete;
|
|
|
|
RedirectedStreams& operator=(RedirectedStreams&&) = delete;
|
|
|
|
|
|
|
|
RedirectedStreams(std::string& redirectedCout, std::string& redirectedCerr);
|
|
|
|
~RedirectedStreams();
|
|
|
|
private:
|
|
|
|
std::string& m_redirectedCout;
|
|
|
|
std::string& m_redirectedCerr;
|
|
|
|
RedirectedStdOut m_redirectedStdOut;
|
|
|
|
RedirectedStdErr m_redirectedStdErr;
|
|
|
|
};
|
Add an experimental new way of capturing stdout/stderr
Unlike the relatively non-invasive old way of capturing stdout/stderr,
this new way is also able to capture output from C's stdlib functions
such as `printf`. This is done by redirecting stdout and stderr file
descriptors to a file, and then reading this file back.
This approach has two sizeable drawbacks:
1) Performance, obviously. Previously an installed capture made the
program run faster (as long as it was then discarded), because a call
to `std::cout` did not result in text output to the console. This new
capture method in fact forces disk IO. While it is likely that any
modern OS will keep this file in memory-cache and might never actually
issue the IO to the backing storage, it is still a possibility and
calls to the file system are not free.
2) Nonportability. While POSIX is usually assumed portable, and this
implementation relies only on a very common parts of it, it is no
longer standard C++ (or just plain C) and thus might not be available
on some obscure platforms. Different C libs might also implement the
relevant functions in a less-than-useful ways (e.g. MS's `tmpfile`
generates a temp file inside system folder, so it will not work
without elevated privileges and thus is useless).
These two drawbacks mean that, at least for now, the new capture is
opt-in. To opt-in, `CATCH_CONFIG_EXPERIMENTAL_REDIRECT` needs to be
defined in the implementation file.
Closes #1243
2018-04-29 22:14:41 +02:00
|
|
|
|
2018-07-13 20:27:00 +02:00
|
|
|
#if defined(CATCH_CONFIG_NEW_CAPTURE)
|
|
|
|
|
Add an experimental new way of capturing stdout/stderr
Unlike the relatively non-invasive old way of capturing stdout/stderr,
this new way is also able to capture output from C's stdlib functions
such as `printf`. This is done by redirecting stdout and stderr file
descriptors to a file, and then reading this file back.
This approach has two sizeable drawbacks:
1) Performance, obviously. Previously an installed capture made the
program run faster (as long as it was then discarded), because a call
to `std::cout` did not result in text output to the console. This new
capture method in fact forces disk IO. While it is likely that any
modern OS will keep this file in memory-cache and might never actually
issue the IO to the backing storage, it is still a possibility and
calls to the file system are not free.
2) Nonportability. While POSIX is usually assumed portable, and this
implementation relies only on a very common parts of it, it is no
longer standard C++ (or just plain C) and thus might not be available
on some obscure platforms. Different C libs might also implement the
relevant functions in a less-than-useful ways (e.g. MS's `tmpfile`
generates a temp file inside system folder, so it will not work
without elevated privileges and thus is useless).
These two drawbacks mean that, at least for now, the new capture is
opt-in. To opt-in, `CATCH_CONFIG_EXPERIMENTAL_REDIRECT` needs to be
defined in the implementation file.
Closes #1243
2018-04-29 22:14:41 +02:00
|
|
|
// Windows's implementation of std::tmpfile is terrible (it tries
|
|
|
|
// to create a file inside system folder, thus requiring elevated
|
|
|
|
// privileges for the binary), so we have to use tmpnam(_s) and
|
|
|
|
// create the file ourselves there.
|
|
|
|
class TempFile {
|
|
|
|
public:
|
|
|
|
TempFile(TempFile const&) = delete;
|
|
|
|
TempFile& operator=(TempFile const&) = delete;
|
|
|
|
TempFile(TempFile&&) = delete;
|
|
|
|
TempFile& operator=(TempFile&&) = delete;
|
|
|
|
|
|
|
|
TempFile();
|
|
|
|
~TempFile();
|
|
|
|
|
|
|
|
std::FILE* getFile();
|
|
|
|
std::string getContents();
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::FILE* m_file = nullptr;
|
2018-04-30 23:06:26 +02:00
|
|
|
#if defined(_MSC_VER)
|
Add an experimental new way of capturing stdout/stderr
Unlike the relatively non-invasive old way of capturing stdout/stderr,
this new way is also able to capture output from C's stdlib functions
such as `printf`. This is done by redirecting stdout and stderr file
descriptors to a file, and then reading this file back.
This approach has two sizeable drawbacks:
1) Performance, obviously. Previously an installed capture made the
program run faster (as long as it was then discarded), because a call
to `std::cout` did not result in text output to the console. This new
capture method in fact forces disk IO. While it is likely that any
modern OS will keep this file in memory-cache and might never actually
issue the IO to the backing storage, it is still a possibility and
calls to the file system are not free.
2) Nonportability. While POSIX is usually assumed portable, and this
implementation relies only on a very common parts of it, it is no
longer standard C++ (or just plain C) and thus might not be available
on some obscure platforms. Different C libs might also implement the
relevant functions in a less-than-useful ways (e.g. MS's `tmpfile`
generates a temp file inside system folder, so it will not work
without elevated privileges and thus is useless).
These two drawbacks mean that, at least for now, the new capture is
opt-in. To opt-in, `CATCH_CONFIG_EXPERIMENTAL_REDIRECT` needs to be
defined in the implementation file.
Closes #1243
2018-04-29 22:14:41 +02:00
|
|
|
char m_buffer[L_tmpnam] = { 0 };
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class OutputRedirect {
|
|
|
|
public:
|
|
|
|
OutputRedirect(OutputRedirect const&) = delete;
|
|
|
|
OutputRedirect& operator=(OutputRedirect const&) = delete;
|
|
|
|
OutputRedirect(OutputRedirect&&) = delete;
|
|
|
|
OutputRedirect& operator=(OutputRedirect&&) = delete;
|
|
|
|
|
|
|
|
|
|
|
|
OutputRedirect(std::string& stdout_dest, std::string& stderr_dest);
|
|
|
|
~OutputRedirect();
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_originalStdout = -1;
|
|
|
|
int m_originalStderr = -1;
|
|
|
|
TempFile m_stdoutFile;
|
|
|
|
TempFile m_stderrFile;
|
|
|
|
std::string& m_stdoutDest;
|
|
|
|
std::string& m_stderrDest;
|
|
|
|
};
|
|
|
|
|
2018-07-13 20:27:00 +02:00
|
|
|
#endif
|
Add an experimental new way of capturing stdout/stderr
Unlike the relatively non-invasive old way of capturing stdout/stderr,
this new way is also able to capture output from C's stdlib functions
such as `printf`. This is done by redirecting stdout and stderr file
descriptors to a file, and then reading this file back.
This approach has two sizeable drawbacks:
1) Performance, obviously. Previously an installed capture made the
program run faster (as long as it was then discarded), because a call
to `std::cout` did not result in text output to the console. This new
capture method in fact forces disk IO. While it is likely that any
modern OS will keep this file in memory-cache and might never actually
issue the IO to the backing storage, it is still a possibility and
calls to the file system are not free.
2) Nonportability. While POSIX is usually assumed portable, and this
implementation relies only on a very common parts of it, it is no
longer standard C++ (or just plain C) and thus might not be available
on some obscure platforms. Different C libs might also implement the
relevant functions in a less-than-useful ways (e.g. MS's `tmpfile`
generates a temp file inside system folder, so it will not work
without elevated privileges and thus is useless).
These two drawbacks mean that, at least for now, the new capture is
opt-in. To opt-in, `CATCH_CONFIG_EXPERIMENTAL_REDIRECT` needs to be
defined in the implementation file.
Closes #1243
2018-04-29 22:14:41 +02:00
|
|
|
|
|
|
|
} // end namespace Catch
|
|
|
|
|
|
|
|
#endif // TWOBLUECUBES_CATCH_OUTPUT_REDIRECT_H
|