redirecting clog

Discussion in 'C++' started by Scott, Mar 13, 2006.

  1. Scott

    Scott Guest

    Hi All,

    I have a C++ program that uses a library. This library (which I also
    wrote in C++), writes a series of status messages to clog as it goes about
    its work. In my program which utilizes this library, I am attempting to
    redirect clog to a stringstream, to allow me to output the status info as
    I decide it is necessary. So, I have this in my program:

    // redirect clog
    stringbuf *buf;
    stringstream ss;

    buf = ss.rdbuf();
    clog.rdbuf( buf );

    It works fine, but the program segfaults. I did a bit of research, and it
    seems that the problem may be that the flush of the global stream objects
    at the end of the program is trying to access an already destructed
    stringbuf. Does this sound correct? And if it is, how do I go about
    fixing it? It seems that no object I create will live long enough to see
    the destruction of clog. If this doesn't sound like the culprit, could
    anyone else suggest something? If I comment out those lines, the segfault
    goes away (and clog is output normally to stdout).

    On another note, is this a reasonable means of design for my library (i.e.
    writing to clog, and then expecting a user to override this if they wish
    to redirect the output)?

    Thanks for any assistance!

    Best,
    Scott
    Scott, Mar 13, 2006
    #1
    1. Advertising

  2. Scott

    red floyd Guest

    Scott wrote:
    > Hi All,
    >
    > I have a C++ program that uses a library. This library (which I also
    > wrote in C++), writes a series of status messages to clog as it goes about
    > its work. In my program which utilizes this library, I am attempting to
    > redirect clog to a stringstream, to allow me to output the status info as
    > I decide it is necessary. So, I have this in my program:
    >
    > // redirect clog
    > stringbuf *buf;
    > stringstream ss;
    >
    > buf = ss.rdbuf();
    > clog.rdbuf( buf );
    >
    > It works fine, but the program segfaults. I did a bit of research, and it
    > seems that the problem may be that the flush of the global stream objects
    > at the end of the program is trying to access an already destructed
    > stringbuf. Does this sound correct? And if it is, how do I go about
    > fixing it?


    Save the original buffer for clog, and restore it before your program
    terminates. atexit() is your friend.
    red floyd, Mar 13, 2006
    #2
    1. Advertising

  3. Scott wrote:
    > So, I have this in my program:
    >
    > // redirect clog
    > stringbuf *buf;
    > stringstream ss;
    >
    > buf = ss.rdbuf();
    > clog.rdbuf( buf );


    You should do one of two things:
    - Restore the original stream buffer before your replacement is
    destructed.
    - Allocate your replacement on the heap such that it is never
    destructed.

    Essentially, what is going on is that at the end of the program
    the standard stream objects are flushed. This can only be successful
    if their stream buffers are still around.
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.eai-systems.com> - Efficient Artificial Intelligence
    Dietmar Kuehl, Mar 13, 2006
    #3
  4. Scott

    Scott Guest

    On Mon, 13 Mar 2006 10:57:02 +0100, Dietmar Kuehl wrote:
    > You should do one of two things:
    > - Restore the original stream buffer before your replacement is
    > destructed.
    > - Allocate your replacement on the heap such that it is never
    > destructed.


    Thanks - I went with the first option, and all is well now.

    Thanks again,
    Scott
    Scott, Mar 14, 2006
    #4
    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. Fuzzyman

    Nappy Clog Month.........

    Fuzzyman, Feb 9, 2004, in forum: Python
    Replies:
    0
    Views:
    299
    Fuzzyman
    Feb 9, 2004
  2. Scott

    cerr and clog

    Scott, Jun 16, 2006, in forum: C++
    Replies:
    1
    Views:
    1,009
    Gnafu
    Jun 18, 2006
  3. ksamdev
    Replies:
    3
    Views:
    403
    Stuart Redmann
    Sep 13, 2010
  4. ksamdev
    Replies:
    2
    Views:
    1,184
    ksamdev
    Sep 10, 2010
  5. Jeff Flinn
    Replies:
    0
    Views:
    383
    Jeff Flinn
    Sep 11, 2010
Loading...

Share This Page