Main MRPT website > C++ reference for MRPT 1.9.9
List of all members | Public Member Functions | Protected Attributes | Private Member Functions
mrpt::system::CConsoleRedirector Class Reference

Detailed Description

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.

Based on code from http://www.devmaster.net/forums/showthread.php?t=7037

Definition at line 26 of file CConsoleRedirector.h.

#include <mrpt/system/CConsoleRedirector.h>

Inheritance diagram for mrpt::system::CConsoleRedirector:
Inheritance graph

Public Member Functions

 CConsoleRedirector (const std::string &out_file, bool also_to_console=true, bool also_cerr=true, bool append_file=false, int bufferSize=1000)
 Constructor. More...
 
virtual ~CConsoleRedirector ()
 
void flush ()
 
virtual void writeString (const std::string &str)
 

Protected Attributes

std::ofstream m_of
 The text output file stream. More...
 
std::streambuf * sbOld
 The "old" std::cout. More...
 
std::streambuf * sbOld_cerr
 The "old" std::cout. More...
 
bool m_also_to_console
 
std::mutex m_cs
 

Private Member Functions

int overflow (int c) override
 
int sync () override
 

Constructor & Destructor Documentation

◆ CConsoleRedirector()

mrpt::system::CConsoleRedirector::CConsoleRedirector ( const std::string out_file,
bool  also_to_console = true,
bool  also_cerr = true,
bool  append_file = false,
int  bufferSize = 1000 
)
inline

Constructor.

Parameters
out_fileThe file to create / append
also_to_consoleWhether to redirect data to file and also dump data to the console as usual.
append_fileIf set to false the file will be truncated on open
bufferSizeIt's recommended to buffer the data instead of writing characters one by one.
also_cerrWhether to redirect the output to std::cerr in addition to std::cout.
Exceptions
std::exceptionIf the file cannot be opened.

Definition at line 50 of file CConsoleRedirector.h.

References m_of, sbOld, sbOld_cerr, and THROW_EXCEPTION_FMT.

◆ ~CConsoleRedirector()

virtual mrpt::system::CConsoleRedirector::~CConsoleRedirector ( )
inlinevirtual

Definition at line 86 of file CConsoleRedirector.h.

References sbOld, sbOld_cerr, and sync().

Member Function Documentation

◆ flush()

void mrpt::system::CConsoleRedirector::flush ( )
inline

Definition at line 95 of file CConsoleRedirector.h.

References sync().

◆ overflow()

int mrpt::system::CConsoleRedirector::overflow ( int  c)
inlineoverrideprivate

Definition at line 103 of file CConsoleRedirector.h.

References m_cs, sync(), and writeString().

◆ sync()

int mrpt::system::CConsoleRedirector::sync ( )
inlineoverrideprivate

Definition at line 124 of file CConsoleRedirector.h.

References m_cs, and writeString().

Referenced by flush(), overflow(), and ~CConsoleRedirector().

◆ writeString()

virtual void mrpt::system::CConsoleRedirector::writeString ( const std::string str)
inlinevirtual

Definition at line 96 of file CConsoleRedirector.h.

References m_also_to_console, m_of, and sbOld.

Referenced by overflow(), and sync().

Member Data Documentation

◆ m_also_to_console

bool mrpt::system::CConsoleRedirector::m_also_to_console
protected

Definition at line 35 of file CConsoleRedirector.h.

Referenced by writeString().

◆ m_cs

std::mutex mrpt::system::CConsoleRedirector::m_cs
protected

Definition at line 36 of file CConsoleRedirector.h.

Referenced by overflow(), and sync().

◆ m_of

std::ofstream mrpt::system::CConsoleRedirector::m_of
protected

The text output file stream.

Definition at line 30 of file CConsoleRedirector.h.

Referenced by CConsoleRedirector(), and writeString().

◆ sbOld

std::streambuf* mrpt::system::CConsoleRedirector::sbOld
protected

The "old" std::cout.

Definition at line 32 of file CConsoleRedirector.h.

Referenced by CConsoleRedirector(), writeString(), and ~CConsoleRedirector().

◆ sbOld_cerr

std::streambuf* mrpt::system::CConsoleRedirector::sbOld_cerr
protected

The "old" std::cout.

Definition at line 34 of file CConsoleRedirector.h.

Referenced by CConsoleRedirector(), and ~CConsoleRedirector().




Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019