redirecting clog

S

Scott

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
 
R

red floyd

Scott said:
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.
 
D

Dietmar Kuehl

Scott said:
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.
 
S

Scott

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top