feat: Process with multithreaded log pipes
All checks were successful
/ check (push) Successful in 38s

This commit is contained in:
Didier Slof 2023-07-17 20:40:22 +02:00
parent 7a56816472
commit b83d44768d
Signed by: didier
GPG key ID: 01E71F18AA4398E5
2 changed files with 70 additions and 0 deletions

44
pkgr/src/process/mod.rs Normal file
View 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![])
}
}

View 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());
}
});
}