MRPT  2.0.4
CConsoleRedirector.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <cstdio> // EOF
12 #include <fstream>
13 #include <iostream>
14 #include <streambuf>
15 
16 namespace mrpt::system
17 {
18 /** By creating an object of this class, all the output to std::cout (and
19  * std::cerr) will be redirected to a text file, and optionally also shown on
20  * the console.
21  * Based on code from http://www.devmaster.net/forums/showthread.php?t=7037
22  * \ingroup mrpt_base_grp
23  */
24 class CConsoleRedirector : public std::streambuf
25 {
26  protected:
27  /** The text output file stream. */
28  std::ofstream m_of;
29  /** The "old" std::cout */
30  std::streambuf* sbOld = nullptr;
31  /** The "old" std::cout */
32  std::streambuf* sbOld_cerr = nullptr;
34  std::mutex m_cs;
35  std::vector<char> m_buf;
36 
37  public:
38  /** Constructor
39  * \param out_file The file to create / append
40  * \param also_to_console Whether to redirect data to file *and* also dump
41  * data to the console as usual.
42  * \param append_file If set to false the file will be truncated on open
43  * \param bufferSize It's recommended to buffer the data instead of writing
44  * characters one by one.
45  * \param also_cerr Whether to redirect the output to std::cerr in addition
46  * to std::cout.
47  * \exception std::exception If the file cannot be opened.
48  */
50  const std::string& out_file, bool also_to_console = true,
51  bool also_cerr = true, bool append_file = false, int bufferSize = 1000)
52  : m_also_to_console(also_to_console)
53  {
54  // Open the file:
55  std::ios_base::openmode openMode =
56  std::ios_base::binary | std::ios_base::out;
57  if (append_file) openMode |= std::ios_base::app;
58  m_of.open(out_file.c_str(), openMode);
59  if (!m_of.is_open())
60  THROW_EXCEPTION_FMT("Error opening file: %s", out_file.c_str());
61 
62  if (bufferSize)
63  {
64  m_buf.resize(bufferSize);
65  setp(m_buf.data(), m_buf.data() + bufferSize);
66  }
67  else
68  setp(nullptr, nullptr);
69 
70  // Redirect:
71  sbOld = std::cout.rdbuf();
72  std::cout.rdbuf(this);
73 
74  if (also_cerr)
75  {
76  sbOld_cerr = std::cerr.rdbuf();
77  std::cerr.rdbuf(this);
78  }
79  }
80 
82  {
83  sync();
84  // Restore normal output:
85  std::cout.rdbuf(sbOld);
86  if (sbOld_cerr != nullptr) std::cerr.rdbuf(sbOld_cerr);
87  }
88 
89  void flush() { sync(); }
90  virtual void writeString(const std::string& str)
91  {
92  if (m_also_to_console) sbOld->sputn(str.c_str(), str.size());
93  m_of << str;
94  }
95 
96  private:
97  int overflow(int c) override
98  {
99  sync();
100 
101  m_cs.lock();
102  if (c != EOF)
103  {
104  if (pbase() == epptr())
105  {
106  std::string temp;
107  temp += char(c);
108  writeString(temp);
109  }
110  else
111  sputc(c);
112  }
113 
114  m_cs.unlock();
115  return 0;
116  }
117 
118  int sync() override
119  {
120  m_cs.lock();
121  if (pbase() != pptr())
122  {
123  int len = int(pptr() - pbase());
124  std::string temp(pbase(), len);
125  writeString(temp);
126  setp(pbase(), epptr());
127  }
128  m_cs.unlock();
129  return 0;
130  }
131 };
132 } // namespace mrpt::system
std::streambuf * sbOld
The "old" std::cout.
CConsoleRedirector(const std::string &out_file, bool also_to_console=true, bool also_cerr=true, bool append_file=false, int bufferSize=1000)
Constructor.
mrpt::vision::TStereoCalibResults out
std::ofstream m_of
The text output file stream.
virtual void writeString(const std::string &str)
By creating an object of this class, all the output to std::cout (and std::cerr) will be redirected t...
std::streambuf * sbOld_cerr
The "old" std::cout.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 5711e29ae Wed May 27 14:29:47 2020 +0200 at miƩ may 27 14:30:10 CEST 2020