Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > intercepting output to cout

Reply
Thread Tools

intercepting output to cout

 
 
Marc Schellens
Guest
Posts: n/a
 
      10-13-2004
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

 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      10-13-2004
"Marc Schellens" <(E-Mail Removed)> wrote in message
news:ckih34$3134$(E-Mail Removed)...
> 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)
{
stdstream * 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%20streambuf


hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Brainbench MVP for C++ <> http://www.brainbench.com


 
Reply With Quote
 
 
 
 
Marc Schellens
Guest
Posts: n/a
 
      10-13-2004
Ivan Vecerina wrote:
>
> 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

 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      10-13-2004

"Ivan Vecerina" <(E-Mail Removed)> wrote in message
news:ckijcn$h24$(E-Mail Removed)...
> "Marc Schellens" <(E-Mail Removed)> wrote in message
> news:ckih34$3134$(E-Mail Removed)...
> int main(void)
> {
> stdstream * 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


 
Reply With Quote
 
Ivan Vecerina
Guest
Posts: n/a
 
      10-14-2004
"Howard" <(E-Mail Removed)> wrote in message
news:lBhbd.695712$(E-Mail Removed)...
>
> "Ivan Vecerina" <(E-Mail Removed)> wrote in message
> news:ckijcn$h24$(E-Mail Removed)...
> > "Marc Schellens" <(E-Mail Removed)> wrote in message
> > news:ckih34$3134$(E-Mail Removed)...
> > int main(void)
> > {
> > stdstream * 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")

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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Brainbench MVP for C++ <> http://www.brainbench.com




 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      10-14-2004

"Ivan Vecerina" <(E-Mail Removed)> wrote in message
news:ckktbg$ql4$(E-Mail Removed)...
> "Howard" <(E-Mail Removed)> wrote in message
> news:lBhbd.695712$(E-Mail Removed)...
>>
>> "Ivan Vecerina" <(E-Mail Removed)> wrote in message
>> news:ckijcn$h24$(E-Mail Removed)...
>> > "Marc Schellens" <(E-Mail Removed)> wrote in message
>> > news:ckih34$3134$(E-Mail Removed)...
>> > int main(void)
>> > {
>> > stdstream * 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")

> 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





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: cout vs std::cout Hendrik Schober C++ 7 10-07-2008 12:02 AM
Re: cout vs std::cout Stefan Ram C++ 7 09-30-2008 01:55 PM
can cout<<""; can cout<<""; contribute ? wangzhihuii@yahoo.com.cn C++ 4 09-15-2005 06:16 PM
std::cout vs cout Pmb C++ 2 06-02-2004 03:27 PM
man cout or info cout abi C++ 2 06-28-2003 06:42 PM



Advertisments