Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to print intermediate Command results #30098

Closed
Gankro opened this Issue Nov 28, 2015 · 5 comments

Comments

Projects
None yet
2 participants
@Gankro
Copy link
Contributor

Gankro commented Nov 28, 2015

I have an application that spends about 30 minutes executing benchmarks by invoking Cargo through std::process::Command. It would be really nice to be able to print intermediate results as each bench completes, but Command doesn't seem to expose anything. It would be sufficient for my purposes to be able to tee stdout/err to the parent process' stdout/err, but a more general mechanism may be desirable.

@eefriedman

This comment has been minimized.

Copy link
Contributor

eefriedman commented Nov 28, 2015

It's not impossible with the current API, just kind of inconvenient. Given a piped stdout, you can do whatever you want with the stream (including implementing a tee-like mechanism).

(Did you mean to file this against rust-lang/rfcs?)

@Gankro

This comment has been minimized.

Copy link
Contributor Author

Gankro commented Nov 28, 2015

(I'm always terrible at picking the right repo)

@Gankro

This comment has been minimized.

Copy link
Contributor Author

Gankro commented Nov 28, 2015

@eefriedman can you elaborate on what you're proposing (I'm not super familiar with piping processes)?

@eefriedman

This comment has been minimized.

Copy link
Contributor

eefriedman commented Nov 28, 2015

fn main() {
    use std::process::Command;
    use std::process::Stdio;
    use std::io::Read;
    use std::io::Write;
    use std::io::stdout;

    let child = Command::new("cat")
                         .arg("/usr/include/stdio.h")
                         .stdout(Stdio::piped())
                         .spawn()
                         .unwrap_or_else(|e| { panic!("failed to execute process: {}", e) });
    let mut out = child.stdout.unwrap();
    let mut read_buf = [0u8; 64];
    let mut out_buf: Vec<u8> = Vec::new();
    while let Ok(size) = out.read(&mut read_buf) {
        if size == 0 {
            break;
        }
        stdout().write_all(&read_buf).unwrap();
        out_buf.extend(read_buf.iter());
    }
}

This writes the child's stdout to the parent's stdout, and saves the result. It could easily be packaged up into a crate if someone was interested.

@Gankro

This comment has been minimized.

Copy link
Contributor Author

Gankro commented Nov 29, 2015

Hey awesome, thanks! I think this is sufficient for the job.

@Gankro Gankro closed this Nov 29, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.