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.
|