www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - capture stdout or stderr

reply Emil <emilper gmail.com> writes:
is it possible to intercept the  STDOUT or STDERR and capture the 
output into a variable ?


some pseudocode to explain what I mean

string[] output_buffer;
stdout.capture_to(output_buffer);




stdout.release(output_buffer);



Jan 31 2017
next sibling parent reply angel <andrey.gelman gmail.com> writes:
On Wednesday, 1 February 2017 at 01:08:19 UTC, Emil wrote:
 is it possible to intercept the  STDOUT or STDERR and capture 
 the output into a variable ?


 some pseudocode to explain what I mean

 string[] output_buffer;
 stdout.capture_to(output_buffer);




 stdout.release(output_buffer);



No. Please keep in mind, that in Linux, for example, stdout is a file ... writeln() interacts with OS API, which, of course, has nothing to do with internal data structures of your application. What you could probably do is replace writeln() with a custom logger. In your logger implementation you can add such functionality.
Feb 01 2017
parent Emil <emilper gmail.com> writes:
On Wednesday, 1 February 2017 at 14:38:18 UTC, angel wrote:
 On Wednesday, 1 February 2017 at 01:08:19 UTC, Emil wrote:
 is it possible to intercept the  STDOUT or STDERR and capture 
 the output into a variable ?
.....

No. Please keep in mind, that in Linux, for example, stdout is a file ... writeln() interacts with OS API, which, of course, has nothing to do with internal data structures of your application. What you could probably do is replace writeln() with a custom logger. In your logger implementation you can add such functionality.
Thank you.
Feb 02 2017
prev sibling parent sarn <sarn theartofmachinery.com> writes:
On Wednesday, 1 February 2017 at 01:08:19 UTC, Emil wrote:
 is it possible to intercept the  STDOUT or STDERR and capture 
 the output into a variable ?


 some pseudocode to explain what I mean

 string[] output_buffer;
 stdout.capture_to(output_buffer);




 stdout.release(output_buffer);



If you *had* to, you should be able to hack it by reopening the file descriptors for standard output or error (at least on *nix), but is there a specific reason you want to do this, or do you just want formatted output to a variable?
Feb 02 2017