scc  2022.4.0
SystemC components library
io-redirector.h
1 /*******************************************************************************
2  * Copyright 2019 MINRES Technologies GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************/
16 
17 #ifndef _UTIL_IO_REDIRECTOR_H_
18 #define _UTIL_IO_REDIRECTOR_H_
19 
20 #include <fcntl.h>
21 #include <mutex>
22 #include <stdio.h>
23 
28 namespace util {
33 class IoRedirector {
34  enum { bufSize = 1024 };
35 
36 public:
37  void start();
38 
39  void stop();
40 
41  bool is_active();
42 
43  std::string get_output(bool blocking = false);
44 
45  static IoRedirector& get() {
46  static IoRedirector inst;
47  return inst;
48  }
49 
50 private:
51  enum PIPES { READ, WRITE };
52 
53  IoRedirector();
54 
55  int copy_fd(int fd);
56  void create_pipes();
57  void copy_fd_to(int src_fd, int destfd);
58  void close_fd(int& fd);
59 
60  int m_pipe[2]{};
61  int m_oldStdOut;
62  int m_oldStdErr;
63  bool m_capturing;
64  std::mutex m_mutex;
65  std::string m_captured;
66 };
67 } // namespace util
69 #endif /* _UTIL_IO_REDIRECTOR_H_ */
allows to capture the strings written to std::cout and std::cerr (MT-safe)
Definition: io-redirector.h:33
SCC common utilities.
Definition: bit_field.h:30