9 #ifndef CConsoleRedirector_H 10 #define CConsoleRedirector_H 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()
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 );
60 char *ptr =
new char[bufferSize];
61 setp(ptr, ptr + bufferSize);
67 sbOld = std::cout.rdbuf();
68 std::cout.rdbuf(
this );
72 sbOld_cerr = std::cerr.rdbuf();
73 std::cerr.rdbuf(
this );
81 std::cout.rdbuf(sbOld);
82 if (sbOld_cerr!=NULL) std::cerr.rdbuf( sbOld_cerr );
83 if (pbase())
delete[] pbase();
93 if (m_also_to_console) sbOld->sputn(str.c_str(),str.size());
105 if (pbase() == epptr())
122 if (pbase() != pptr())
124 int len = int(pptr() - pbase());
125 std::string temp(pbase(), len);
127 setp(pbase(), epptr());
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 ~CConsoleRedirector()
virtual void writeString(const std::string &str)
std::streambuf * sbOld
The "old" std::cout.
#define THROW_EXCEPTION_CUSTOM_MSG1(msg, param1)