class mrpt::system::CConsoleRedirector

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

#include <mrpt/system/CConsoleRedirector.h>

class CConsoleRedirector: public streambuf
{
public:
    // construction

    CConsoleRedirector(
        const std::string& out_file,
        bool also_to_console = true,
        bool also_cerr = true,
        bool append_file = false,
        int bufferSize = 1000
        );

    //
methods

    void flush();
    virtual void writeString(const std::string& str);
};

Construction

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

Constructor.

Parameters:

out_file

The file to create / append

also_to_console

Whether to redirect data to file and also dump data to the console as usual.

append_file

If set to false the file will be truncated on open

bufferSize

It’s recommended to buffer the data instead of writing characters one by one.

also_cerr

Whether to redirect the output to std::cerr in addition to std::cout.

std::exception

If the file cannot be opened.