M
Martin Magnusson
I'd like to create my own output streams, in order to be able to
redirect output strings to a console, a status window or whatever,
depending on the environment. For example, IO::warning << "Bad stuff";
could output a string on stderr to start with, and when I have written
a nice GUI, it could be output to a status window or a log file or
something.
I have written a skeleton file which looks like this:
//---------------------
#include <iostream>
#include <string>
namespace IO
{
template <class charT, class Traits=std::char_traits<charT> >
class My_Ostream : public std::basic_ostream< charT, Traits >
{
public:
My_Ostream( std::basic_streambuf< charT, Traits >* sb =
std::cerr.rdbuf())
: std::basic_ostream< charT, Traits >( sb )
{
its_sb = sb;
}
std::basic_ostream<charT, Traits>& operator <<
( charT& input )
{
its_sb << "[" << input << "]";
}
private:
std::basic_streambuf< charT, Traits >* its_sb;
};
// IO::warning is just std::cerr.
My_Ostream< char, std::char_traits<char> > warning(
std::cerr.rdbuf() );
}
//---------------------
When calling
IO::warning << "Yo";
I'm expecting the output "[Yo]" on cerr, but I'm just getting "Yo".
Can anybody help me with a proper definition of the << operator?
redirect output strings to a console, a status window or whatever,
depending on the environment. For example, IO::warning << "Bad stuff";
could output a string on stderr to start with, and when I have written
a nice GUI, it could be output to a status window or a log file or
something.
I have written a skeleton file which looks like this:
//---------------------
#include <iostream>
#include <string>
namespace IO
{
template <class charT, class Traits=std::char_traits<charT> >
class My_Ostream : public std::basic_ostream< charT, Traits >
{
public:
My_Ostream( std::basic_streambuf< charT, Traits >* sb =
std::cerr.rdbuf())
: std::basic_ostream< charT, Traits >( sb )
{
its_sb = sb;
}
std::basic_ostream<charT, Traits>& operator <<
( charT& input )
{
its_sb << "[" << input << "]";
}
private:
std::basic_streambuf< charT, Traits >* its_sb;
};
// IO::warning is just std::cerr.
My_Ostream< char, std::char_traits<char> > warning(
std::cerr.rdbuf() );
}
//---------------------
When calling
IO::warning << "Yo";
I'm expecting the output "[Yo]" on cerr, but I'm just getting "Yo".
Can anybody help me with a proper definition of the << operator?