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



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST