From e182ab4a4a3bd47e4345a2b7e3d4a44ec8552969 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sat, 26 Jan 2019 10:23:52 +0100 Subject: [PATCH] api2/node/syslog.rs: improve error handling --- src/api2/node/syslog.rs | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/api2/node/syslog.rs b/src/api2/node/syslog.rs index 5de4d42f..9fd5b5fe 100644 --- a/src/api2/node/syslog.rs +++ b/src/api2/node/syslog.rs @@ -18,11 +18,11 @@ fn dump_journal( service: Option<&str>, ) -> Result<(u64, Vec), Error> { - let mut args = vec!["-o", "short", "--no-pager"]; + let mut args = vec!["-o", "short", "--no-pagera"]; - if let Some(service) = service { args.push("--unit"); args.push(service); } - if let Some(since) = since { args.push("--since"); args.push(since); } - if let Some(until) = until { args.push("--until"); args.push(until); } + if let Some(service) = service { args.extend(&["--unit", service]); } + if let Some(since) = since { args.extend(&["--since", since]); } + if let Some(until) = until { args.extend(&["--until", until]); } let mut lines: Vec = vec![]; let mut limit = limit.unwrap_or(50); @@ -38,18 +38,29 @@ fn dump_journal( if let Some(ref mut stdout) = child.stdout { for line in BufReader::new(stdout).lines() { - count += 1; - if count < start { continue }; - if limit <= 0 { continue }; + match line { + Ok(line) => { + count += 1; + if count < start { continue }; + if limit <= 0 { continue }; - let line = line?; // fixme: nom utf8?? - lines.push(json!({ "n": count, "t": line })); + lines.push(json!({ "n": count, "t": line })); - limit -= 1; + limit -= 1; + } + Err(err) => { + eprintln!("reading journal failed: {}", err); + let _ = child.kill(); + break; + } + } } } - let status = child.wait()?; // fixme: check status + let status = child.wait().unwrap(); + if !status.success() { + eprintln!("journalctl failed with {}", status); + } // HACK: ExtJS store.guaranteeRange() does not like empty array // so we add a line @@ -68,7 +79,7 @@ fn get_syslog(param: Value, _info: &ApiMethod) -> Result { param["limit"].as_u64(), param["since"].as_str(), param["until"].as_str(), - param["service"].as_str()); + param["service"].as_str())?; //fixme: $restenv->set_result_attrib('total', $count);