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.
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;
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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With