9 #ifndef CConsoleRedirector_H 10 #define CConsoleRedirector_H 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());
127 setp(pbase(), epptr());
This class provides simple critical sections functionality.
mrpt::synch::CCriticalSection m_cs
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
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,...)
void leave() const MRPT_OVERRIDE
Leave.
void enter() const MRPT_OVERRIDE
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.
GLsizei const GLchar ** string
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)
int overflow(int c) MRPT_OVERRIDE
std::streambuf * sbOld
The "old" std::cout.