Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doubling a stream

Tags:

c++

stream

I'm using the cerr stream for my error output, but I also wanted to save any of this error output in a stringstream in memory.

I'm looking to turn this:

stringstream errorString;
cerr << " Something went wrong ";
errorString << " Something went wrong ";

Into

myErr << " Something went wrong ";

Where myErr is an instance of a class that stores this in a stringstream and also outputs to cerr.

Thanks for any help.

like image 649
V.S. Avatar asked Sep 07 '11 14:09

V.S.


3 Answers

You could create the type of your myErr class like this:

class stream_fork
{
    std::ostream& _a;
    std::ostream& _b;

public:
    stream_fork( std::ostream& a, std::ostream& b)
        :   _a( a ),
            _b( b )
    {
    }

    template<typename T>
    stream_fork& operator<<( const T& obj )
    {
        _a << obj;
        _b << obj;
        return *this;
    }

    // This lets std::endl work, too!
    stream_fork& operator<<( std::ostream& manip_func( std::ostream& ) )
    {
        _a << manip_func;
        _b << manip_func;
        return *this;
    }
};

Usage:

stream_fork myErr( std::cerr, errorString );
myErr << "Error Message" << std::endl;
like image 193
Timbo Avatar answered Nov 19 '22 09:11

Timbo


You can use Boost.IOStreams.

#include <boost/iostreams/tee.hpp>
#include <boost/iostreams/stream.hpp>
#include <iostream>
#include <sstream>

namespace io = boost::iostreams;

int main() {
    std::stringstream ss;
    io::tee_device<decltype(ss), decltype(std::cerr)> sink(ss, std::cerr);
    io::stream<decltype(sink)> stream(sink);
    stream << "foo" << std::endl;
    std::cout << ss.str().length() << std::endl;
    return 0;
}
like image 40
Cat Plus Plus Avatar answered Nov 19 '22 08:11

Cat Plus Plus


Override operator<< in your MyErr class

MyErr& operator<< ( MyErr& myErr, std::string message)
{
    cerr << message;
    errorString << message; //Where errorString is a member of the MyErr class

    return myErr;
}

Then where you want to log the error:

int main()
{
    MyErr myErr;
    myErr << " Something went wrong. ";

    return 0;
}

You might want to make MyErr a singleton class so that everything written to errorString is in one place.

like image 1
Daniel Avatar answered Nov 19 '22 09:11

Daniel