intercepting output to cout

M

Marc Schellens

I want to create an object,
which replaces std::cout
all output will be put out to std::cout, but optional
also copied to a file.

But the file output needs to be manipulated:
every new line has to be preceded by a ';'

Is there an easy way to do so?
Or do I have to write wrappers for all kind of
<< operators?

And which functions to overload to scan for '\n'
likje ostr.put(...), any other (ie. are operator<<
using put(...) (always)?)

thanks,
marc
 
I

Ivan Vecerina

Marc Schellens said:
I want to create an object,
which replaces std::cout
all output will be put out to std::cout, but optional
also copied to a file.

But the file output needs to be manipulated:
every new line has to be preceded by a ';'

Is there an easy way to do so?
Or do I have to write wrappers for all kind of
<< operators?

And which functions to overload to scan for '\n'
likje ostr.put(...), any other (ie. are operator<<
using put(...) (always)?)
Look for "redirect cout" ...
What you need to do is replace the streambuf associated
with std::cout:

int main(void)
{
std:eek:stream * saveStream = cout.rdbuf();
MyStreamBuffer newBuf(.......);
cout.rdbuf(&newBuf);
try {
... the resto of te program ...

} catch(...) { error reporting... }
cout.rdbuf(saveStream); // restore cout to original state
}

To have output to both the console and a file, you should be
able to find a 'tee' stream buffer (I do not know a specific
implementation, but there are a few floating around on the web):
http://www.google.com/search?&q=Tee streambuf


hth -Ivan
 
M

Marc Schellens

Ivan said:
Look for "redirect cout" ...
What you need to do is replace the streambuf associated
with std::cout:

that was exactly what I needed. Thanks,
marc
 
H

Howard

Ivan Vecerina said:
int main(void)
{
std:eek:stream * saveStream = cout.rdbuf();
MyStreamBuffer newBuf(.......);
cout.rdbuf(&newBuf);
try {
... the resto of te program ...

} catch(...) { error reporting... }
cout.rdbuf(saveStream); // restore cout to original state
}

What if there is no exception? How does cout get restored in that case?
I'd think you'd want to use a constructor/destructor pair for the rdbuf
calls, wouldn't you? (think "RAII")

-Howard
 
I

Ivan Vecerina

Howard said:
What if there is no exception? How does cout get restored in that case?
I'd think you'd want to use a constructor/destructor pair for the rdbuf
calls, wouldn't you? (think "RAII")
I would definitely use RAII -- except I wanted this post to be concise
and there is no RAII class performing the above in the standard library.
This said:
- int main() above does catch(...) { /*code*/ } to always restore the buf
- anyway it is not a good idea to let exceptions propagate out of main()

Cheers -Ivan
 
H

Howard

Ivan Vecerina said:
I would definitely use RAII -- except I wanted this post to be concise
and there is no RAII class performing the above in the standard library.
This said:
- int main() above does catch(...) { /*code*/ } to always restore the buf
- anyway it is not a good idea to let exceptions propagate out of main()

I misread your code. The way you put "{ error reporting... }" on the same
line as catch, I mistook that for a comment. (My old Pascal days coming
back!) And so I then mistook the buffer restore call as being inside the
catch clause. My bad!

But that's one reason I always go down to the next line before starting the
"dependant clause" after a try/catch/if/else, etc. Arranging it like this
always confuses my little mind. :)

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top