Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: How to redirect clog to cout and ofstream at the same time?

Reply
Thread Tools

Re: How to redirect clog to cout and ofstream at the same time?

 
 
ksamdev
Guest
Posts: n/a
 
      09-10-2010
On Sep 10, 11:34*am, "Johannes Schaub (litb)" <schaub-johan...@web.de>
wrote:
> ksamdev wrote:
> > Hi,

>
> > Is is possible to make clog output messages into two output stream
> > simultaneously, say: cout and ostream?

>
> > Unfortunately, ostream::rdbuf(...) allows to set only one at a time.

>
> > I'd like to have messages that are sent to clog to be output on the
> > screen and logged in file at the same time.

>
> "ostream"? That's the class type of cout, not an object's stream. You can
> write a rdbuf that writes into two different buffers like
>
> struct mybuf : streambuf {
> * mybuf(streambuf *a, streambuf *b)
> * *:a(a),b(b)
> * { }
>
> protected:
> * int overflow(int c) {
> * * if(a->sputc(c) == EOF) return EOF;
> * * if(b->sputc(c) == EOF) return EOF;
> * * return c;
> * } *
> private:
> * streambuf *a, *b;
>
> };
>
> You can then connect like the following, remembering to later restore clog.
>
> * * filebuf fb;
> * * fb.open(...);
>
> * * mybuf m(cout.rdbuf(), &fb);
> * * streambuf *b = clog.rdbuf(&m);
> * * clog << "i go into cout and into the file!";
> * * clog.rdbuf(b);


Thanks for the advice.

In the original email I was talking about any ostream other than cout:
it might be ofstream, cerr, osstringstream, etc.

Does your code work with <iomanip> (manipulators) ?
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      09-10-2010
On Sep 10, 5:45 pm, ksamdev <ksam...@gmail.com> wrote:
> On Sep 10, 11:34 am, "Johannes Schaub (litb)" <schaub-johan...@web.de>
> wrote:
> > ksamdev wrote:


> > > Is is possible to make clog output messages into two
> > > output stream simultaneously, say: cout and ostream?


> > > Unfortunately, ostream::rdbuf(...) allows to set only one at a time.


> > > I'd like to have messages that are sent to clog to be output on the
> > > screen and logged in file at the same time.


> > "ostream"? That's the class type of cout, not an object's stream. You can
> > write a rdbuf that writes into two different buffers like


> > struct mybuf : streambuf {
> > mybuf(streambuf *a, streambuf *b)
> > :a(a),b(b)
> > { }


> > protected:
> > int overflow(int c) {
> > if(a->sputc(c) == EOF) return EOF;
> > if(b->sputc(c) == EOF) return EOF;
> > return c;
> > }
> > private:
> > streambuf *a, *b;
> > };


> > You can then connect like the following, remembering to later restore clog.


> > filebuf fb;
> > fb.open(...);

>
> > mybuf m(cout.rdbuf(), &fb);
> > streambuf *b = clog.rdbuf(&m);
> > clog << "i go into cout and into the file!";
> > clog.rdbuf(b);


> Thanks for the advice.


> In the original email I was talking about any ostream other
> than cout: it might be ofstream, cerr, osstringstream, etc.


> Does your code work with <iomanip> (manipulators) ?


What do manipulators have to do with streambuf?

--
James Kanze
 
Reply With Quote
 
 
 
 
ksamdev
Guest
Posts: n/a
 
      09-10-2010
On Sep 10, 1:26*pm, James Kanze <james.ka...@gmail.com> wrote:
> On Sep 10, 5:45 pm, ksamdev <ksam...@gmail.com> wrote:
>
>
>
>
>
> > On Sep 10, 11:34 am, "Johannes Schaub (litb)" <schaub-johan...@web.de>
> > wrote:
> > > ksamdev wrote:
> > > > Is is possible to make clog output messages into two
> > > > output stream simultaneously, say: cout and ostream?
> > > > Unfortunately, ostream::rdbuf(...) allows to set only one at a time..
> > > > I'd like to have messages that are sent to clog to be output on the
> > > > screen and logged in file at the same time.
> > > "ostream"? That's the class type of cout, not an object's stream. You can
> > > write a rdbuf that writes into two different buffers like
> > > struct mybuf : streambuf {
> > > * mybuf(streambuf *a, streambuf *b)
> > > * *:a(a),b(b)
> > > * { }
> > > protected:
> > > * int overflow(int c) {
> > > * * if(a->sputc(c) == EOF) return EOF;
> > > * * if(b->sputc(c) == EOF) return EOF;
> > > * * return c;
> > > * }
> > > private:
> > > * streambuf *a, *b;
> > > };
> > > You can then connect like the following, remembering to later restore clog.
> > > * * filebuf fb;
> > > * * fb.open(...);

>
> > > * * mybuf m(cout.rdbuf(), &fb);
> > > * * streambuf *b = clog.rdbuf(&m);
> > > * * clog << "i go into cout and into the file!";
> > > * * clog.rdbuf(b);

> > Thanks for the advice.
> > In the original email I was talking about any ostream other
> > than cout: it might be ofstream, cerr, osstringstream, etc.
> > Does your code work with <iomanip> (manipulators) ?

>
> What do manipulators have to do with streambuf?
>
> --
> James Kanze


I don't know. That's why I am asking.
Never mind then. Let me try this approach first.
 
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: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: How to redirect clog to cout and ofstream at the same time? ksamdev C++ 3 09-13-2010 06:28 AM
Re: How to redirect clog to cout and ofstream at the same time? Jeff Flinn C++ 0 09-11-2010 01:51 AM
Use cout as ofstream object Joe Hesse C++ 5 09-26-2007 02:34 PM
ofstream * vs. ofstream Squid Seven C++ 5 07-14-2005 07:34 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57