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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019