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



Page generated by Doxygen 1.8.11 for MRPT 1.3.2 SVN:Unversioned directory at Sun May 1 08:45:24 UTC 2016