feat: Process with multithreaded log pipes
This commit is contained in:
parent
8cf2876ff0
commit
9416794101
2 changed files with 70 additions and 0 deletions
44
pkgr/src/process/mod.rs
Normal file
44
pkgr/src/process/mod.rs
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
pub mod output;
|
||||||
|
|
||||||
|
pub struct Process {
|
||||||
|
pub command: Vec<String>,
|
||||||
|
pub cwd: Option<String>,
|
||||||
|
pub env: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Process {
|
||||||
|
pub fn new(command: Vec<String>, cwd: Option<String>, env: Vec<String>) -> Process {
|
||||||
|
Process {
|
||||||
|
command,
|
||||||
|
cwd,
|
||||||
|
env,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn command<S: Into<String>>(cmd: Vec<S>) -> Process {
|
||||||
|
Process::new(cmd.into_iter().map(|v| v.into()).collect(), None, vec![])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn spawn(&self) -> std::io::Result<std::process::Child> {
|
||||||
|
let mut child = Command::new(&self.command[0])
|
||||||
|
.args(&self.command[1..])
|
||||||
|
.current_dir(self.cwd.clone().unwrap_or(".".to_string()))
|
||||||
|
.envs(self.env.iter().map(|v| {
|
||||||
|
let mut split = v.split('=');
|
||||||
|
(split.next().unwrap().to_string(), split.next().unwrap().to_string())
|
||||||
|
}))
|
||||||
|
.stdout(std::process::Stdio::piped())
|
||||||
|
.stderr(std::process::Stdio::piped())
|
||||||
|
.spawn()?;
|
||||||
|
output::spawn_output_handlers(&mut child);
|
||||||
|
Ok(child)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Process {
|
||||||
|
fn default() -> Process {
|
||||||
|
Process::new(vec![], None, vec![])
|
||||||
|
}
|
||||||
|
}
|
26
pkgr/src/process/output.rs
Normal file
26
pkgr/src/process/output.rs
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
// create functions that spawn threads for Stdout and Stderr
|
||||||
|
// that print the output to info!(target: "command:stdout", ...) and info!(target: "command:stderr", ...) respectively
|
||||||
|
|
||||||
|
use std::process::Child;
|
||||||
|
use std::io::{BufRead, BufReader};
|
||||||
|
|
||||||
|
use log::{info, error};
|
||||||
|
|
||||||
|
pub fn spawn_output_handlers(child: &mut Child) {
|
||||||
|
let stdout = child.stdout.take().unwrap();
|
||||||
|
let stderr = child.stderr.take().unwrap();
|
||||||
|
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
let reader = BufReader::new(stdout);
|
||||||
|
for line in reader.lines() {
|
||||||
|
info!(target: "command:stdout", "{}", line.unwrap());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
let reader = BufReader::new(stderr);
|
||||||
|
for line in reader.lines() {
|
||||||
|
error!(target: "command:stderr", "{}", line.unwrap());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in a new issue