intercepting output to cout

Discussion in 'C++' started by Marc Schellens, Oct 13, 2004.

  1. 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
     
    Marc Schellens, Oct 13, 2004
    #1
    1. Advertising

  2. "Marc Schellens" <> wrote in message
    news:ckih34$3134$...
    > 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
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, Oct 13, 2004
    #2
    1. Advertising

  3. 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
     
    Marc Schellens, Oct 13, 2004
    #3
  4. Marc Schellens

    Howard Guest

    "Ivan Vecerina" <> wrote in message
    news:ckijcn$h24$...
    > "Marc Schellens" <> wrote in message
    > news:ckih34$3134$...
    > 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
     
    Howard, Oct 13, 2004
    #4
  5. "Howard" <> wrote in message
    news:lBhbd.695712$...
    >
    > "Ivan Vecerina" <> wrote in message
    > news:ckijcn$h24$...
    > > "Marc Schellens" <> wrote in message
    > > news:ckih34$3134$...
    > > 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")

    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
     
    Ivan Vecerina, Oct 14, 2004
    #5
  6. Marc Schellens

    Howard Guest

    "Ivan Vecerina" <> wrote in message
    news:ckktbg$ql4$...
    > "Howard" <> wrote in message
    > news:lBhbd.695712$...
    >>
    >> "Ivan Vecerina" <> wrote in message
    >> news:ckijcn$h24$...
    >> > "Marc Schellens" <> wrote in message
    >> > news:ckih34$3134$...
    >> > 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")

    > 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
     
    Howard, Oct 14, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. abi

    man cout or info cout

    abi, Jun 27, 2003, in forum: C++
    Replies:
    2
    Views:
    1,665
  2. Pmb

    std::cout vs cout

    Pmb, Jun 2, 2004, in forum: C++
    Replies:
    2
    Views:
    4,439
    Leor Zolman
    Jun 2, 2004
  3. Replies:
    4
    Views:
    333
    Howard
    Sep 15, 2005
  4. Stefan Ram

    Re: cout vs std::cout

    Stefan Ram, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    505
  5. Hendrik Schober

    Re: cout vs std::cout

    Hendrik Schober, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    459
    Jerry Coffin
    Oct 7, 2008
Loading...

Share This Page