MRPT  2.0.0
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 24 of file CConsoleRedirector.h.

#include <mrpt/system/CConsoleRedirector.h>

Inheritance diagram for mrpt::system::CConsoleRedirector:

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...
 
 ~CConsoleRedirector () override
 
void flush ()
 
virtual void writeString (const std::string &str)
 

Protected Attributes

std::ofstream m_of
 The text output file stream. More...
 
std::streambuf * sbOld = nullptr
 The "old" std::cout. More...
 
std::streambuf * sbOld_cerr = nullptr
 The "old" std::cout. More...
 
bool m_also_to_console
 
std::mutex m_cs
 
std::vector< char > m_buf
 

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 49 of file CConsoleRedirector.h.

References m_buf, m_of, out, sbOld, sbOld_cerr, and THROW_EXCEPTION_FMT.

◆ ~CConsoleRedirector()

mrpt::system::CConsoleRedirector::~CConsoleRedirector ( )
inlineoverride

Definition at line 81 of file CConsoleRedirector.h.

References sbOld, sbOld_cerr, and sync().

Here is the call graph for this function:

Member Function Documentation

◆ flush()

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

Definition at line 89 of file CConsoleRedirector.h.

References sync().

Here is the call graph for this function:

◆ overflow()

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

Definition at line 97 of file CConsoleRedirector.h.

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

Here is the call graph for this function:

◆ sync()

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

Definition at line 118 of file CConsoleRedirector.h.

References m_cs, and writeString().

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeString()

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

Definition at line 90 of file CConsoleRedirector.h.

References m_also_to_console, m_of, and sbOld.

Referenced by overflow(), and sync().

Here is the caller graph for this function:

Member Data Documentation

◆ m_also_to_console

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

Definition at line 33 of file CConsoleRedirector.h.

Referenced by writeString().

◆ m_buf

std::vector<char> mrpt::system::CConsoleRedirector::m_buf
protected

Definition at line 35 of file CConsoleRedirector.h.

Referenced by CConsoleRedirector().

◆ m_cs

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

Definition at line 34 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 28 of file CConsoleRedirector.h.

Referenced by CConsoleRedirector(), and writeString().

◆ sbOld

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

The "old" std::cout.

Definition at line 30 of file CConsoleRedirector.h.

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

◆ sbOld_cerr

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

The "old" std::cout.

Definition at line 32 of file CConsoleRedirector.h.

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




Page generated by Doxygen 1.8.14 for MRPT 2.0.0 Git: b38439d21 Tue Mar 31 19:58:06 2020 +0200 at miƩ abr 1 00:50:30 CEST 2020