scc  2024.06
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 #include <string>
24 
29 namespace util {
34 class IoRedirector {
35  enum { bufSize = 1024 };
36 
37 public:
38  void start();
39 
40  void stop();
41 
42  bool is_active();
43 
44  std::string get_output(bool blocking = false);
45 
46  static IoRedirector& get() {
47  static IoRedirector inst;
48  return inst;
49  }
50 
51 private:
52  enum PIPES { READ, WRITE };
53 
54  IoRedirector();
55 
56  int copy_fd(int fd);
57  void create_pipes();
58  void copy_fd_to(int src_fd, int destfd);
59  void close_fd(int& fd);
60 
61  int m_pipe[2]{};
62  int m_oldStdOut;
63  int m_oldStdErr;
64  bool m_capturing;
65  std::mutex m_mutex;
66  std::string m_captured;
67 };
68 } // namespace util
70 #endif /* _UTIL_IO_REDIRECTOR_H_ */
allows to capture the strings written to std::cout and std::cerr (MT-safe)
Definition: io-redirector.h:34
SCC common utilities.
Definition: bit_field.h:30