重定向从Rust产生的子进程的输出 [英] Redirect output of child process spawned from Rust
本文介绍了重定向从Rust产生的子进程的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要重定向生成的子进程的输出.这是我尝试过的方法,但是不起作用:
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()
推荐答案
要直接使用文件作为输出而无需从管道进行中间复制,则必须传递文件描述符.该代码是特定于平台的,但是通过条件编译,您也可以使其在Windows上运行.
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();
这篇关于重定向从Rust产生的子进程的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文