2

I want to get the real time output of a command line-by-line and pass the line to another function for further processing. I have the following code for reading the output:

fn output(x: &str) -> Result<(), Error> {
    let stdout = Command::new("strace")
        .args(&["-p", x])
        .stdout(Stdio::piped())
        .spawn()?
        .stdout
        .ok_or_else(|| Error::new(ErrorKind::Other,"Could not capture standard output."))?;

    let reader = BufReader::new(stdout);
    
    reader
        .lines()
        .filter_map(|line| line.ok())
        .for_each(|line| println!("hello {}", line));

     Ok(())
}

I tried changing the .for_each to pass it into the function, but it didn't work. It still printed out the normal output and after adding a println!(); for debugging, I've found that the lines don't even get to the function. I'm stuck. Please help!

Etile
  • 70
  • 3

1 Answers1

1

Following @justinas's comment I replaced the "stdout" with "stderr" in my code and it worked!

Etile
  • 70
  • 3