Line data Source code
1 : use tokio::io::{AsyncBufReadExt, BufReader};
2 : use tokio::process::{ChildStderr, ChildStdout};
3 : use tracing::info;
4 :
5 : /// Asynchronously relays the output from a child process's `stdout` and `stderr` to the tracing log.
6 : /// Each line is read and logged individually, with lossy UTF-8 conversion.
7 : ///
8 : /// # Arguments
9 : ///
10 : /// * `stdout`: An `Option<ChildStdout>` from the child process.
11 : /// * `stderr`: An `Option<ChildStderr>` from the child process.
12 : ///
13 0 : pub(crate) async fn relay_process_output(stdout: Option<ChildStdout>, stderr: Option<ChildStderr>) {
14 0 : let stdout_fut = async {
15 0 : if let Some(stdout) = stdout {
16 0 : let reader = BufReader::new(stdout);
17 0 : let mut lines = reader.lines();
18 0 : while let Ok(Some(line)) = lines.next_line().await {
19 0 : info!(fd = "stdout", "{}", line);
20 : }
21 0 : }
22 0 : };
23 :
24 0 : let stderr_fut = async {
25 0 : if let Some(stderr) = stderr {
26 0 : let reader = BufReader::new(stderr);
27 0 : let mut lines = reader.lines();
28 0 : while let Ok(Some(line)) = lines.next_line().await {
29 0 : info!(fd = "stderr", "{}", line);
30 : }
31 0 : }
32 0 : };
33 :
34 0 : tokio::join!(stdout_fut, stderr_fut);
35 0 : }
|