www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Redirecting stdout

reply Robert Fraser <fraserofthenight gmail.com> writes:
I'd like at a particular point in my program to temporarily ignore all 
stdout (i.e. not have it written to the stream)... is there a way to do 
this? I want my program to work under both Phobos & Tango, so a way for 
both libraries would be great.

Thanks,
Robert
Dec 04 2007
next sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Robert Fraser wrote:
 I'd like at a particular point in my program to temporarily ignore all 
 stdout (i.e. not have it written to the stream)... is there a way to do 
 this? I want my program to work under both Phobos & Tango, so a way for 
 both libraries would be great.
 
Phobos just uses the underlying C stream objects in the end, so you can use freopen on those. Note that std.cstream functions will throw exceptions if the standard output streams are not open, so the code below freopens them to point to /dev/null or Nul. Don't know if there's a better way, but this way has been working for me. version(Tango) { ?? } else { static this() { // redefine dout,derr,dlog to prevent IO exceptions version(Windows) { std.c.stdio.freopen("Nul", "w", dout.file); std.c.stdio.freopen("Nul", "w", derr.file); } else { std.c.stdio.freopen("/dev/null", "w", dout.file); std.c.stdio.freopen("/dev/null", "w", derr.file); } } }
Dec 04 2007
parent reply Robert Fraser <fraserofthenight gmail.com> writes:
Bill Baxter wrote:
 Robert Fraser wrote:
 I'd like at a particular point in my program to temporarily ignore all 
 stdout (i.e. not have it written to the stream)... is there a way to 
 do this? I want my program to work under both Phobos & Tango, so a way 
 for both libraries would be great.
Phobos just uses the underlying C stream objects in the end, so you can use freopen on those. Note that std.cstream functions will throw exceptions if the standard output streams are not open, so the code below freopens them to point to /dev/null or Nul. Don't know if there's a better way, but this way has been working for me. version(Tango) { ?? } else { static this() { // redefine dout,derr,dlog to prevent IO exceptions version(Windows) { std.c.stdio.freopen("Nul", "w", dout.file); std.c.stdio.freopen("Nul", "w", derr.file); } else { std.c.stdio.freopen("/dev/null", "w", dout.file); std.c.stdio.freopen("/dev/null", "w", derr.file); } } }
Thanks; that's what I needed! But how do I capture the current stdout/stderr so I can get them back (I've never done these scary stream thing before, obviously).
Dec 04 2007
parent Nathan Reed <nathaniel.reed gmail.com> writes:
Robert Fraser wrote:
 Bill Baxter wrote:
 Robert Fraser wrote:
 I'd like at a particular point in my program to temporarily ignore 
 all stdout (i.e. not have it written to the stream)... is there a way 
 to do this? I want my program to work under both Phobos & Tango, so a 
 way for both libraries would be great.
Phobos just uses the underlying C stream objects in the end, so you can use freopen on those. Note that std.cstream functions will throw exceptions if the standard output streams are not open, so the code below freopens them to point to /dev/null or Nul. Don't know if there's a better way, but this way has been working for me. version(Tango) { ?? } else { static this() { // redefine dout,derr,dlog to prevent IO exceptions version(Windows) { std.c.stdio.freopen("Nul", "w", dout.file); std.c.stdio.freopen("Nul", "w", derr.file); } else { std.c.stdio.freopen("/dev/null", "w", dout.file); std.c.stdio.freopen("/dev/null", "w", derr.file); } } }
Thanks; that's what I needed! But how do I capture the current stdout/stderr so I can get them back (I've never done these scary stream thing before, obviously).
I think you can just save the old values of dout.file and derr.file, and restore them later on (remembering to close the 'reopened' streams when you do so). Thanks, Nathan Reed
Dec 04 2007
prev sibling parent "Kris" <foo bar.com> writes:
With Tango you set either Stdout.stream() or Cout.output() to an output 
stream, which can be a file, socket, chunk of memory, or whatever. For 
example, to place all stdout and cout content into a file:





"Robert Fraser" <fraserofthenight gmail.com> wrote in message 
news:fj4stk$2c1t$1 digitalmars.com...
 I'd like at a particular point in my program to temporarily ignore all 
 stdout (i.e. not have it written to the stream)... is there a way to do 
 this? I want my program to work under both Phobos & Tango, so a way for 
 both libraries would be great.

 Thanks,
 Robert 
Dec 04 2007