Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
catch_output_redirect.h
Go to the documentation of this file.
1/*
2 * Created by Martin on 28/04/2018.
3 *
4 * Distributed under the Boost Software License, Version 1.0. (See accompanying
5 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 */
7#ifndef TWOBLUECUBES_CATCH_OUTPUT_REDIRECT_H
8#define TWOBLUECUBES_CATCH_OUTPUT_REDIRECT_H
9
10#include "catch_platform.h"
11#include "catch_stream.h"
12
13#include <cstdio>
14#include <iosfwd>
15#include <string>
16
17namespace Catch {
18
20 std::ostream& m_originalStream;
21 std::ostream& m_redirectionStream;
22 std::streambuf* m_prevBuf;
23
24 public:
25 RedirectedStream( std::ostream& originalStream, std::ostream& redirectionStream );
27 };
28
31 RedirectedStream m_cout;
32 public:
34 auto str() const -> std::string;
35 };
36
37 // StdErr has two constituent streams in C++, std::cerr and std::clog
38 // This means that we need to redirect 2 streams into 1 to keep proper
39 // order of writes
42 RedirectedStream m_cerr;
43 RedirectedStream m_clog;
44 public:
46 auto str() const -> std::string;
47 };
48
50 public:
55
56 RedirectedStreams(std::string& redirectedCout, std::string& redirectedCerr);
58 private:
59 std::string& m_redirectedCout;
60 std::string& m_redirectedCerr;
61 RedirectedStdOut m_redirectedStdOut;
62 RedirectedStdErr m_redirectedStdErr;
63 };
64
65#if defined(CATCH_CONFIG_NEW_CAPTURE)
66
67 // Windows's implementation of std::tmpfile is terrible (it tries
68 // to create a file inside system folder, thus requiring elevated
69 // privileges for the binary), so we have to use tmpnam(_s) and
70 // create the file ourselves there.
71 class TempFile {
72 public:
73 TempFile(TempFile const&) = delete;
74 TempFile& operator=(TempFile const&) = delete;
75 TempFile(TempFile&&) = delete;
76 TempFile& operator=(TempFile&&) = delete;
77
78 TempFile();
79 ~TempFile();
80
81 std::FILE* getFile();
82 std::string getContents();
83
84 private:
85 std::FILE* m_file = nullptr;
86 #if defined(_MSC_VER)
87 char m_buffer[L_tmpnam] = { 0 };
88 #endif
89 };
90
91
92 class OutputRedirect {
93 public:
94 OutputRedirect(OutputRedirect const&) = delete;
95 OutputRedirect& operator=(OutputRedirect const&) = delete;
96 OutputRedirect(OutputRedirect&&) = delete;
97 OutputRedirect& operator=(OutputRedirect&&) = delete;
98
99
100 OutputRedirect(std::string& stdout_dest, std::string& stderr_dest);
101 ~OutputRedirect();
102
103 private:
104 int m_originalStdout = -1;
105 int m_originalStderr = -1;
106 TempFile m_stdoutFile;
107 TempFile m_stderrFile;
108 std::string& m_stdoutDest;
109 std::string& m_stderrDest;
110 };
111
112#endif
113
114} // end namespace Catch
115
116#endif // TWOBLUECUBES_CATCH_OUTPUT_REDIRECT_H
auto str() const -> std::string
auto str() const -> std::string
RedirectedStream(std::ostream &originalStream, std::ostream &redirectionStream)
RedirectedStreams(RedirectedStreams &&)=delete
RedirectedStreams & operator=(RedirectedStreams &&)=delete
RedirectedStreams(RedirectedStreams const &)=delete
RedirectedStreams & operator=(RedirectedStreams const &)=delete
FILE * TempFile(char *filename)
Definition unittest.h:80