473,770 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7697
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.co m> <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
955
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 in less than 20k of python - yep another great way to avoid working... mix up the names of friends and colleagues, great fun.
4
5087
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 run the mencoder with the needed params, but now I want to integrate mencoder as a subprocess in my app. What already works: the starting of the subprocess.Popen and the subsequent killing of the process if I want it to.
3
2336
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 main page from the code behind the contents page. I want to do this to allow the use of asp "linkbuttons" instead of hyperlinks so I can do a little processing before redirecting. The main reason for this to prevent hyperlinks of pages that have...
1
3115
by: Gurikar | last post by:
How to redirect information to file usinf cerr or clog. Regards
4
1497
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 are redirecting to somewhere that doesn't exist. Worse, if you do type it correctly, but then later the page name changes or the page moves, you still won't find out until run time. I have a framework that solves this problem and guarantees,...
8
5788
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 technically create an executable that calls the function and then using a command prompt call the exexutable using the > output.txt redirector but I would prefer to do everything within the application itself. Is there a way for force an output...
1
4357
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 warning statements based on an input, and I would like to know whether these should both go to cerr, or whether clog for warnings is a more appropriate choice (or I suppose, just cout)? Thank-you,
5
1498
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 the web hosting server because of my program? MD
17
9468
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 redirect: Response.Redirect("/website/pdffiles/myreport.pdf"); Response.End; IE opens a "File Download" box and if you click the "open" button then
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.