I've been trying to figure out how to redirect and split cout so that I can inspect it. Basically, there is some code that already uses cout that I do not want to modify, but I want to be able to process what is being output to cout, without stopping it from going to cout.
I have found a usenet post that seems appropriate for my problem. However, I am having some difficulty using it. I am fairly new to C++, although not to programming in general, so it may just be syntax trouble.
The method described in that post shows how to redirect cout to another stream using rdbuf. It also gives code for a "teebuf," which is designed to split a stream into two other streams.
I am getting compile errors when I use the teebuf code provided:
The problem seems to be related to the use of m_sb1 and m_sb2 in the constructor. I have tried adding private fields of type streambuf* with those two names. I then needed to change the references to sb1 and sb2 in the other parts of the code to m_sb1 and m_sb2. However, this results in three C2248 errors:Code:#include <streambuf>
class teebuf: public std::streambuf {
public:
typedef std::char_traits<char> traits_type;
typedef traits_type::int_type int_type;
teebuf(std::streambuf* sb1, std::streambuf* sb2):
m_sb1(sb1), m_sb2(sb2) {}
private:
int_type overflow(int_type c) {
if (!traits_type::eq_int_type(c, traits_type::eof()) {
c = sb1->sputc(c);
if (!traits_type::eq_int_type(c, traits_type::eof()))
c = sb2->overflow(c);
return c;
}
else
return traits_type::not_eof(c);
}
int sync() {
int rc = sb1->sync();
if (rc != -1)
rc = sb2->sync();
return rc;
}
};
The other two errors are the same with sync instead of overflow.Quote:
error C2248: 'std::basic_streambuf<_Elem,_Traits>::overflow' : cannot access protected member declared in class 'std::basic_streambuf<_Elem,_Traits>'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
and
[
_Elem=char,
_Traits=std::char_traits<char>
]
Does anyone have any idea what the correct way would be to fix this teebuf code? Or is it correct already but I am just using it wrong? Also, any other ways to solve my overall problem would be appreciated. Thanks.