watch for stdout and stderr

master
rasul 5 years ago
parent 837967c241
commit 132bc6311f

@ -10,7 +10,7 @@ use std::error::Error;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::{Child, Command, ExitStatus}; use std::process::{Child, Command, ExitStatus, Stdio};
type SupResult<T> = Result<T, Box<dyn Error>>; type SupResult<T> = Result<T, Box<dyn Error>>;
@ -36,25 +36,22 @@ impl App {
match command.output() { match command.output() {
Ok(output) => { Ok(output) => {
if !output.stdout.is_empty() { if !output.stdout.is_empty() {
info!( let s = String::from_utf8(output.stdout)?;
"[{}] stdout: {}", for line in s.lines() {
&self.name, info!("[{}] stdout: {}", &self.name, line);
String::from_utf8(output.stdout)?
);
} }
if !output.stderr.is_empty() {
info!(
"[{}] stderr: {}",
&self.name,
String::from_utf8(output.stderr)?
);
} }
if !output.stderr.is_empty() {
let s = String::from_utf8(output.stderr)?;
for line in s.lines() {
info!("[{}] stderr: {}", &self.name, line);
} }
Err(e) => error!("Error starting app: {}: {:?}", &self.name, e),
} }
Ok(()) Ok(())
} }
Err(e) => Err(Box::from(e)),
}
}
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -85,12 +82,56 @@ impl Proc {
info!("starting application {}", &app.name); info!("starting application {}", &app.name);
let mut command = Command::new(&app.command); let mut command = Command::new(&app.command);
command.stdout(Stdio::piped());
command.stderr(Stdio::piped());
command.args(&app.args); command.args(&app.args);
let child = command.spawn()?; let child = command.spawn()?;
Ok(Proc { app, child }) Ok(Proc { app, child })
} }
fn check_stdout(&mut self) -> Option<String> {
let child_stdout = self.child.stdout.as_mut();
if let Some(stdout) = child_stdout {
let mut buf = String::new();
match stdout.read_to_string(&mut buf) {
Ok(size) => {
if size > 0 {
return Some(buf);
} else {
return None;
}
}
Err(e) => {
error!("couldn't read stdout from {}: {:?}", &self.app.name, e);
return None;
}
}
}
None
}
fn check_stderr(&mut self) -> Option<String> {
let child_stderr = self.child.stderr.as_mut();
if let Some(stderr) = child_stderr {
let mut buf = String::new();
match stderr.read_to_string(&mut buf) {
Ok(size) => {
if size > 0 {
return Some(buf);
} else {
return None;
}
}
Err(e) => {
error!("couldn't read stderr from {}: {:?}", &self.app.name, e);
return None;
}
}
}
None
}
} }
fn main() { fn main() {
@ -105,6 +146,7 @@ fn main() {
} }
} }
#[allow(clippy::cognitive_complexity)]
fn run() -> SupResult<()> { fn run() -> SupResult<()> {
let config = Config::load("sup.toml")?; let config = Config::load("sup.toml")?;
info!("loaded config: sup.toml"); info!("loaded config: sup.toml");
@ -159,12 +201,21 @@ fn run() -> SupResult<()> {
} }
} }
Ok(None) => { Ok(None) => {
if let Some(s) = &proc.check_stdout() {
for line in s.lines() {
info!("[{}] stdout: {}", &proc.app.name, line);
}
}
if let Some(s) = &proc.check_stderr() {
for line in s.lines() {
info!("[{}] stderr: {}", &proc.app.name, line);
}
}
newprocs.push(proc); newprocs.push(proc);
} }
Err(e) => error!("error: {:?}", e), Err(e) => error!("error: {:?}", e),
} }
} }
procs = newprocs; procs = newprocs;
} }

Loading…
Cancel
Save