digitalmars.D.learn - Redirecting stdout
- Robert Fraser (6/6) Dec 04 2007 I'd like at a particular point in my program to temporarily ignore all
- Bill Baxter (22/27) Dec 04 2007 Phobos just uses the underlying C stream objects in the end, so you can
- Robert Fraser (4/34) Dec 04 2007 Thanks; that's what I needed! But how do I capture the current
- Nathan Reed (6/41) Dec 04 2007 I think you can just save the old values of dout.file and derr.file, and...
- Kris (6/12) Dec 04 2007 With Tango you set either Stdout.stream() or Cout.output() to an output
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
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
Bill Baxter wrote:Robert Fraser wrote: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'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
Robert Fraser wrote:Bill Baxter wrote: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 ReedRobert Fraser wrote: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'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
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