Using BufRead to stream read both stdout and stderr of the spawned child

Another noob question which remained unanswered in the discord user chat, and on which I couldn't really find any specific answer, nor could I manage to answer myself.

I have a child: Child which is spawned using .spawn() and I want to stream read both stdout and stderr from it using BufReader::new(stdout).lines(..).

Creating a BufReader from child.stdout moves child into BufReader and I can't use it anywhere else after that.

Borrowing with &child.stdout doesn't work as BufReader seems to be needing to own the emitter.

I haven't found any examples of using both stdout and stderr with a Child using a streamed reader, only using a single .output or .wait_with_output and getting the whole stdout and stderr contents at once, which may be quite a large block of text.

Why I want it to work this way - I want to read stdout line by line to parse it for valuable info (upload links of the appcenter CLI for example) - the main reason. I also want to append the log files with those lines to robustly check for any output of running commands in case of something stops abruptly.

There are really two questions:

  1. How do I read both stdout and stderr of the Child and then wait() for a Child to end.
  2. Is it the correct way to approach solving the problem of parsing lines and using log files?

You can use Option::take to take the stdout and stderr out of the Child struct.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.