From ea9f8f4a7231ce6c9ed48738444dbe2f4441444b Mon Sep 17 00:00:00 2001 From: Anders Olsson Date: Wed, 3 Oct 2018 09:32:55 +0200 Subject: [PATCH] Added some basic logging. Fixes #1. --- Cargo.lock | 32 ++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/main.rs | 45 +++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 17b02e3..e269ff4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -93,8 +93,10 @@ dependencies = [ "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "handlebars 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "humantime 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)", + "stderrlog 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "structopt 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "subprocess 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", @@ -253,6 +255,17 @@ dependencies = [ "serde 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "stderrlog" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", + "termcolor 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "strsim" version = "0.7.0" @@ -325,6 +338,14 @@ dependencies = [ "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "termcolor" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "wincolor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "termion" version = "1.5.1" @@ -423,6 +444,14 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "wincolor" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [metadata] "checksum aho-corasick 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "68f56c7353e5a9547cbd76ed90f7bb5ffc3ba09d4ea9bd1d8c06c8b1142eeb5a" "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" @@ -455,6 +484,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum serde 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)" = "84257ccd054dc351472528c8587b4de2dbf0dc0fe2e634030c1a90bfdacebaa9" "checksum serde_derive 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)" = "31569d901045afbff7a9479f793177fe9259819aff10ab4f89ef69bbc5f567fe" "checksum serde_json 1.0.31 (registry+https://github.com/rust-lang/crates.io-index)" = "bb47a3d5c84320222f66d7db21157c4a7407755de41798f9b4c1c40593397b1a" +"checksum stderrlog 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "61dc66b7ae72b65636dbf36326f9638fb3ba27871bb737a62e2c309b87d91b70" "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" "checksum structopt 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d8e9ad6a11096cbecdcca0cc6aa403fdfdbaeda2fb3323a39c98e6a166a1e45a" "checksum structopt-derive 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4cbce8ccdc62166bd594c14396a3242bf94c337a51dbfa9be1076dd74b3db2af" @@ -463,6 +493,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum syn 0.14.9 (registry+https://github.com/rust-lang/crates.io-index)" = "261ae9ecaa397c42b960649561949d69311f08eeaea86a65696e6e46517cf741" "checksum syn 0.15.7 (registry+https://github.com/rust-lang/crates.io-index)" = "455a6ec9b368f8c479b0ae5494d13b22dc00990d2f00d68c9dc6a2dc4f17f210" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" +"checksum termcolor 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "adc4587ead41bf016f11af03e55a624c06568b5a19db4e90fde573d805074f83" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" @@ -478,3 +509,4 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +"checksum wincolor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eeb06499a3a4d44302791052df005d5232b927ed1a9658146d842165c4de7767" diff --git a/Cargo.toml b/Cargo.toml index 1a4d812..57ca52a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,8 @@ authors = ["Anders Olsson "] chrono = "0.4" handlebars = { version = "1.0", features = ["no_dir_source"], default-features = false} humantime = "1.1" +log = "0.4" +stderrlog = "0.4" serde = "1.0" serde_derive = "1.0" structopt = "0.2" diff --git a/src/main.rs b/src/main.rs index 158c4ff..2b4f9a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,9 @@ extern crate chrono; extern crate handlebars; extern crate humantime; #[macro_use] +extern crate log; +extern crate stderrlog; +#[macro_use] extern crate serde_derive; #[macro_use] extern crate structopt; @@ -18,12 +21,16 @@ use std::time::Duration; use chrono::prelude::*; use handlebars::Handlebars; use humantime::format_duration; +use log::Level::Debug; use structopt::StructOpt; -use subprocess::{Exec, NullFile}; +use subprocess::Exec; #[derive(StructOpt)] #[structopt(name = "ips-uptime")] struct Opt { + #[structopt(short = "v", long = "verbose", parse(from_occurrences))] + verbose: u8, + #[structopt(short = "c", long = "config", parse(from_os_str))] config: PathBuf, } @@ -51,12 +58,36 @@ struct Check { impl Check { fn status(&self) -> bool { + info!("{}: Executing check", self.name); + let result = Exec::shell(&self.cmd) - .stdout(NullFile) - .stderr(NullFile) - .join() + .capture() .expect("failed to execute command"); + if log_enabled!(Debug) { + let stdout = result.stdout_str(); + + if !stdout.is_empty() { + debug!("{} - stdout: '{}'", self.name, result.stdout_str()); + } else { + debug!("{} - stdout: ", self.name); + } + + let stderr = result.stderr_str(); + + if !stderr.is_empty() { + debug!("{} - stderr: '{}'", self.name, result.stderr_str()); + } else { + debug!("{} - stderr: ", self.name); + } + } + + if result.success() { + info!("{}: Check success", self.name); + } else { + info!("{}: Check failed", self.name); + } + result.success() } } @@ -64,6 +95,12 @@ impl Check { fn main() { let opt = Opt::from_args(); + stderrlog::new() + .verbosity(opt.verbose as usize) + .timestamp(stderrlog::Timestamp::Second) + .init() + .unwrap(); + let mut buffer = Vec::new(); fs::File::open(&opt.config)