Main MRPT website > C++ reference for MRPT 1.4.0
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-2016, 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
14#include <streambuf>
15#include <iostream>
16#include <fstream>
17#include <cstdio> // EOF
18
19namespace 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:
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
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.
void leave() const
Leave.
void enter() const
Enter.
By creating an object of this class, all the output to std::cout (and std::cerr) will be redirected t...
CConsoleRedirector(const std::string &out_file, bool also_to_console=true, bool also_cerr=true, bool append_file=false, int bufferSize=1000)
Constructor.
int overflow(int c) MRPT_OVERRIDE
std::streambuf * sbOld_cerr
The "old" std::cout.
mrpt::synch::CCriticalSection m_cs
virtual void writeString(const std::string &str)
std::streambuf * sbOld
The "old" std::cout.
std::ofstream m_of
The text output file stream.
#define THROW_EXCEPTION_CUSTOM_MSG1(msg, param1)
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
Definition: mrpt_macros.h:28
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.



Page generated by Doxygen 1.9.6 for MRPT 1.4.0 SVN: at Wed Mar 22 06:08:57 UTC 2023