I need to redirect output of a spawned child process. This is what I tried but it doesn't work:
Command::new(cmd)
.args(&["--testnet",
"--fast",
format!("&>> {}", log_path).as_str()])
.stdin(Stdio::piped())
.stdout(Stdio::inherit())
.spawn()
To directly use a file as output without intermediate copying from a pipe, you have to pass the file descriptor. The code is platform-specific, but with conditional compilation you can make it work on Windows too.
let f = File::create("foo.log").unwrap();
let fd = f.as_raw_fd();
// from_raw_fd is only considered unsafe if the file is used for mmap
let out = unsafe {Stdio::from_raw_fd(fd)};
let child = Command::new("foo")
.stdout(out)
.spawn().unwrap();