iutest  1.17.99.14
iutest_stream_capture.hpp
[詳解]
1 //======================================================================
2 //-----------------------------------------------------------------------
13 //-----------------------------------------------------------------------
14 //======================================================================
15 #ifndef INCG_IRIS_IUTEST_STREAM_CAPTURE_HPP_B2DEE478_BEA9_435D_A11A_86888DAE8740_
16 #define INCG_IRIS_IUTEST_STREAM_CAPTURE_HPP_B2DEE478_BEA9_435D_A11A_86888DAE8740_
17 
18 //======================================================================
19 // include
20 // IWYU pragma: begin_exports
21 #include "iutest_file.hpp"
22 // IWYU pragma: end_exports
23 
24 #if IUTEST_HAS_STREAM_CAPTURE
25 
26 namespace iutest
27 {
28 namespace detail
29 {
30 
34 class IUStreamCapture
35 {
36 public:
37  explicit IUStreamCapture(int fd)
38  : m_fd(fd), m_prev_fd(internal::posix::Dup(fd)), m_new_fd(-1)
39  {
40  if( m_prev_fd == -1 )
41  {
42  IUTEST_LOG_(WARNING) << "file descriptor dup failed";
43  }
44  else
45  {
46  if( !m_tempfile.Create("iutest_stream_capture") )
47  {
48  IUTEST_LOG_(WARNING) << "temp file open failed";
49  }
50  m_new_fd = m_tempfile.GetDescriptor();
51  if( internal::posix::Dup2(m_new_fd, fd) == -1 )
52  {
53  IUTEST_LOG_(WARNING) << "file descriptor dup2 failed";
54  Close();
55  }
56  m_tempfile.Close();
57  }
58  }
59  ~IUStreamCapture()
60  {
61  Delete();
62  }
63 
64 public:
65  ::std::string GetStreamString()
66  {
67  if( !m_tempfile.Open(iutest::IFile::OpenRead) )
68  {
69  IUTEST_LOG_(WARNING) << "temp file open failed: " << m_tempfile.GetFileName();
70  return "";
71  }
72  return m_tempfile.ReadAll();
73  }
74 
75 private:
76  void Close()
77  {
78  if( m_prev_fd != -1 )
79  {
80  internal::posix::Dup2(m_prev_fd, m_fd);
81  internal::posix::FdClose(m_prev_fd);
82  m_prev_fd = -1;
83  m_new_fd = -1;
84  }
85  }
86  void Delete()
87  {
88  Close();
89  m_tempfile.Delete();
90  }
91 
92 private:
93  TempFile m_tempfile;
94  int m_fd;
95  int m_prev_fd;
96  int m_new_fd;
97 
98  IUTEST_PP_DISALLOW_COPY_AND_ASSIGN(IUStreamCapture);
99 };
100 
101 #if IUTEST_HAS_STREAM_BUFFER && defined(IUTEST_OS_WINDOWS_MINGW)
102 class IUStreamCaputreWithBuffer : public IUStreamCapture
103 {
104 public:
105  IUStreamCaputreWithBuffer(int fd, FILE* fp)
106  : IUStreamCapture(fd)
107  , m_buffering(fp)
108  {
109  }
110  ::std::string GetStreamString()
111  {
112  ::std::string str = IUStreamCapture::GetStreamString();
113  if( str.empty() )
114  {
115  return m_buffering.GetStreamString();
116  }
117  return str;
118  }
119 private:
120  IUStreamBuffer<> m_buffering;
121 };
122 #else
123 class IUStreamCaputreWithBuffer : public IUStreamCapture
124 {
125 public:
126  IUStreamCaputreWithBuffer(int fd, FILE*) : IUStreamCapture(fd) {}
127 };
128 #endif
129 
130 class IUStreamCaptureStdout : public IUStreamCaputreWithBuffer
131 {
132 public:
133  IUStreamCaptureStdout()
134 #if defined(STDOUT_FILENO)
135  : IUStreamCaputreWithBuffer(STDOUT_FILENO, stdout)
136 #else
137  : IUStreamCaputreWithBuffer(1, stdout)
138 #endif
139  {
140  }
141 };
142 
143 class IUStreamCaptureStderr : public IUStreamCaputreWithBuffer
144 {
145 public:
146  IUStreamCaptureStderr()
147 #if defined(STDERR_FILENO)
148  : IUStreamCaputreWithBuffer(STDERR_FILENO, stderr)
149 #else
150  : IUStreamCaputreWithBuffer(2, stderr)
151 #endif
152  {
153  }
154 };
155 
156 } // end of namespace detail
157 } // end of namespace iutest
158 
159 #endif
160 
161 #endif // INCG_IRIS_IUTEST_STREAM_CAPTURE_HPP_B2DEE478_BEA9_435D_A11A_86888DAE8740_
@ OpenRead
読み込み
Definition: iutest_file.hpp:46
iris unit test ファイルクラス ファイル
#define IUTEST_LOG_(level)
ログメッセージストリーム
Definition: iutest_port.hpp:56
#define IUTEST_PP_DISALLOW_COPY_AND_ASSIGN(TypeName)
コピー禁止定義
Definition: iutest_pp.hpp:31
iutest root namespace
Definition: iutest_charcode.hpp:33