How to get output of piped process?

Jedi Return at Lucifer.com
Wed Feb 17 06:58:55 UTC 2021


I an using pipeShell, I have redirected stdout, stderr, and stdin.

I am trying to read from the output and display it in my app. I 
have followed this code almost exactly except I use try wait and 
flush because the app is continuously updating the output. (it 
outputs a progress text on the same line and I'm trying to poll 
it to report to the user)


auto pipes = pipeProcess("my_application", Redirect.stdout | 
Redirect.stderr);
scope(exit) wait(pipes.pid);

// Store lines of output.
string[] output;
foreach (line; pipes.stdout.byLine) output ~= line.idup;

// Store lines of errors.
string[] errors;
foreach (line; pipes.stderr.byLine) errors ~= line.idup;


My code

auto p = pipeShell(`app.exe "`~f.name~`"`, Redirect.stdout | 
Redirect.stdin | Redirect.stderr);

		
			while(!tryWait(p.pid).terminated)
			{
				string[] output;
				foreach (line; p.stdout.byLine)
				{
					output ~= line.idup;
					writeln(line);
				}

				string[] errors;
				foreach (line; p.stderr.byLine)
				{
					errors ~= line.idup;
					writeln("Err:"~line);
				}
			}

wait(p.pid);

None of this works though. What is strange is that when I close 
out the debugger the app starts working(no console output but I 
able to see that it is doing something) but is very slow.

auto p = executeShell(`app.exe "`~f.name~`"`);

Does work, except I have no output or input. I have another app 
that I do the exact same code and I can get the output and parse 
it, but this is after the app terminates. I imagine the issue 
here is that I'm trying to get the output while the app is 
running.


I want to be able to get the output so I can reduce much of the 
clutter and give a progress report. I am ok with simply hooking 
up the in and out of the console of the app to mine just as if I 
ran app.exe directly.


More information about the Digitalmars-d-learn mailing list