Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:05

0001 
0002 //              Copyright Catch2 Authors
0003 // Distributed under the Boost Software License, Version 1.0.
0004 //   (See accompanying file LICENSE.txt or copy at
0005 //        https://www.boost.org/LICENSE_1_0.txt)
0006 
0007 // SPDX-License-Identifier: BSL-1.0
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     // StdErr has two constituent streams in C++, std::cerr and std::clog
0040     // This means that we need to redirect 2 streams into 1 to keep proper
0041     // order of writes
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     // Windows's implementation of std::tmpfile is terrible (it tries
0070     // to create a file inside system folder, thus requiring elevated
0071     // privileges for the binary), so we have to use tmpnam(_s) and
0072     // create the file ourselves there.
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 } // end namespace Catch
0117 
0118 #endif // CATCH_OUTPUT_REDIRECT_HPP_INCLUDED