473,323 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

redirecting clog

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
Mar 13 '06 #1
3 7658
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.
Mar 13 '06 #2
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:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 13 '06 #3
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
Mar 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Fuzzyman | last post by:
Nappy clog month... no, not the usual USENET abuse - it's an anagram of comp.lang.python...... http://www.voidspace.org.uk/atlantibots/nanagram.html An anagram finder with Tkinter front end...
4
by: Ivo Woltring | last post by:
Hi Pythoneers, I am trying to make my own gui for mencoder.exe (windows port of the terrific linux mencoder/mplayer) to convert divx to Pocket_PC size. My current app creates a batch script to...
3
by: lozd | last post by:
Would appreciate any solutions people could offer for this. Basically I wan't to use a frameset with an aspx page as the contents rather than a htm page and I'd like to be able to redirect the...
1
by: Gurikar | last post by:
How to redirect information to file usinf cerr or clog. Regards
4
by: Greg Smalter | last post by:
Redirecting from page to page within a web project is pretty easy. However, all Redirect methods take strings as arguments, as if you mistype the string, you don't find out until run time that you...
8
by: Morpheus | last post by:
I am trying to test a function that outputs text to the standard output, presumably using a cout call. I do not have access to the source, but need to test the output. Is this possible? I can...
1
by: Scott | last post by:
Hello, I am not entirely clear on the difference between cerr and clog. When it is more appropriate to use one versus the other? I am coding a simple C++ application that will issue error and...
5
by: mich dobelman | last post by:
I am developing a web crawler which collects info from more than 50,0000 pages. but i am afraid this is going to clog the rental web server if I run it every single day. Is it possible to stop...
17
by: mansb2002 | last post by:
Hi, We recently moved our webserver from Win2K to Win2003. The application works fine. Only problem is that when user views a report as a PDF, IE does not show it. The following code is used to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.