File indexing completed on 2025-01-18 09:54:05
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef CATCH_OUTPUT_REDIRECT_HPP_INCLUDED
0009 #define CATCH_OUTPUT_REDIRECT_HPP_INCLUDED
0010
0011 #include <catch2/internal/catch_platform.hpp>
0012 #include <catch2/internal/catch_reusable_string_stream.hpp>
0013 #include <catch2/internal/catch_compiler_capabilities.hpp>
0014
0015 #include <cstdio>
0016 #include <iosfwd>
0017 #include <string>
0018
0019 namespace Catch {
0020
0021 class RedirectedStream {
0022 std::ostream& m_originalStream;
0023 std::ostream& m_redirectionStream;
0024 std::streambuf* m_prevBuf;
0025
0026 public:
0027 RedirectedStream( std::ostream& originalStream, std::ostream& redirectionStream );
0028 ~RedirectedStream();
0029 };
0030
0031 class RedirectedStdOut {
0032 ReusableStringStream m_rss;
0033 RedirectedStream m_cout;
0034 public:
0035 RedirectedStdOut();
0036 auto str() const -> std::string;
0037 };
0038
0039
0040
0041
0042 class RedirectedStdErr {
0043 ReusableStringStream m_rss;
0044 RedirectedStream m_cerr;
0045 RedirectedStream m_clog;
0046 public:
0047 RedirectedStdErr();
0048 auto str() const -> std::string;
0049 };
0050
0051 class RedirectedStreams {
0052 public:
0053 RedirectedStreams(RedirectedStreams const&) = delete;
0054 RedirectedStreams& operator=(RedirectedStreams const&) = delete;
0055 RedirectedStreams(RedirectedStreams&&) = delete;
0056 RedirectedStreams& operator=(RedirectedStreams&&) = delete;
0057
0058 RedirectedStreams(std::string& redirectedCout, std::string& redirectedCerr);
0059 ~RedirectedStreams();
0060 private:
0061 std::string& m_redirectedCout;
0062 std::string& m_redirectedCerr;
0063 RedirectedStdOut m_redirectedStdOut;
0064 RedirectedStdErr m_redirectedStdErr;
0065 };
0066
0067 #if defined(CATCH_CONFIG_NEW_CAPTURE)
0068
0069
0070
0071
0072
0073 class TempFile {
0074 public:
0075 TempFile(TempFile const&) = delete;
0076 TempFile& operator=(TempFile const&) = delete;
0077 TempFile(TempFile&&) = delete;
0078 TempFile& operator=(TempFile&&) = delete;
0079
0080 TempFile();
0081 ~TempFile();
0082
0083 std::FILE* getFile();
0084 std::string getContents();
0085
0086 private:
0087 std::FILE* m_file = nullptr;
0088 #if defined(_MSC_VER)
0089 char m_buffer[L_tmpnam] = { 0 };
0090 #endif
0091 };
0092
0093
0094 class OutputRedirect {
0095 public:
0096 OutputRedirect(OutputRedirect const&) = delete;
0097 OutputRedirect& operator=(OutputRedirect const&) = delete;
0098 OutputRedirect(OutputRedirect&&) = delete;
0099 OutputRedirect& operator=(OutputRedirect&&) = delete;
0100
0101
0102 OutputRedirect(std::string& stdout_dest, std::string& stderr_dest);
0103 ~OutputRedirect();
0104
0105 private:
0106 int m_originalStdout = -1;
0107 int m_originalStderr = -1;
0108 TempFile m_stdoutFile;
0109 TempFile m_stderrFile;
0110 std::string& m_stdoutDest;
0111 std::string& m_stderrDest;
0112 };
0113
0114 #endif
0115
0116 }
0117
0118 #endif