From 82ccbf0a7e2da16110d5ba0e41562dc682022378 Mon Sep 17 00:00:00 2001 From: finga Date: Wed, 3 Mar 2021 16:14:54 +0100 Subject: [PATCH] Implement proper logging The `log` and `env_logger` crates are used for logging. --- Cargo.lock | 30 ++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/main.rs | 44 +++++++++++++++++++++++++++----------------- 3 files changed, 59 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bacf9b6..cbdd8bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -306,6 +306,19 @@ version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "88d7ed2934d741c6b37e33e3832298e8850b53fd2d2bea03873375596c7cea4e" +[[package]] +name = "env_logger" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17392a012ea30ef05a610aa97dfb49496e71c9f676b27879922ea5bdf60d9d3f" +dependencies = [ + "atty", + "humantime", + "log 0.4.14", + "regex", + "termcolor", +] + [[package]] name = "filetime" version = "0.2.14" @@ -442,6 +455,12 @@ version = "1.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "615caabe2c3160b313d52ccc905335f4ed5f10881dd63dc5699d47e90be85691" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "hyper" version = "0.10.16" @@ -1062,6 +1081,15 @@ dependencies = [ "unicode-xid 0.2.1", ] +[[package]] +name = "termcolor" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" +dependencies = [ + "winapi-util", +] + [[package]] name = "thread_local" version = "1.1.3" @@ -1224,6 +1252,8 @@ version = "0.1.0" dependencies = [ "anyhow", "dirs", + "env_logger", + "log 0.4.14", "regex", "rocket", "rocket_contrib", diff --git a/Cargo.toml b/Cargo.toml index 2097353..874bff4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,5 @@ serde_yaml = "0.8" regex = "1.4" dirs = "3.0" anyhow = "1.0" +log = "0.4" +env_logger = "0.8" diff --git a/src/main.rs b/src/main.rs index 6d9899d..60f8d72 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,13 @@ #![feature(proc_macro_hygiene, decl_macro)] use anyhow::{anyhow, bail, Result}; +use log::{debug, info, trace}; use regex::Regex; use rocket::{fairing::AdHoc, get, post, routes, State}; use rocket_contrib::json::Json; use serde::{Deserialize, Serialize}; -use std::collections::HashMap; -use std::fs::File; -use std::io::BufReader; -use std::process::Command; +use std::{collections::HashMap, fs::File, io::BufReader, net::SocketAddr, process::Command}; #[derive(Debug, Deserialize, Serialize)] struct Config { @@ -37,23 +35,20 @@ fn index() -> &'static str { } fn execute_hook(name: &str, hook: &Hook, data: &serde_json::Value) -> Result<()> { - println!( - "hook: {:?} action: {:?} filters: {:?}", - name, hook.action, hook.filters - ); + debug!("Running hook `{}`", name); for (filter_name, filter) in hook.filters.iter() { - println!("filter: {:?}", filter); + debug!("Matching filter `{}`", filter_name); if let Some(value) = data.pointer(&filter.pointer) { let regex = Regex::new(&filter.regex)?; if let Some(value) = value.as_str() { if !regex.is_match(value) { + info!("Filter `{}` in hook `{}` did not match", filter_name, name); return Ok(()); } } else { - println!("Error could not parse pointer"); anyhow!( "Could not parse pointer in hook `{}` from filter `{}`", name, @@ -63,10 +58,8 @@ fn execute_hook(name: &str, hook: &Hook, data: &serde_json::Value) -> Result<()> } } - println!("execute action"); - if let Some(action) = &hook.action { - println!("execute {}", action); + info!("Execute `{}` from hook `{}`", action, name); Command::new(action).spawn()?; } @@ -75,9 +68,13 @@ fn execute_hook(name: &str, hook: &Hook, data: &serde_json::Value) -> Result<()> } #[post("/", format = "json", data = "")] -fn receive_hook(config: State, data: Json) -> Result { +fn receive_hook(address: SocketAddr, config: State, data: Json) -> Result { + info!("POST request received from: {}", address); + let data = serde_json::to_value(data.0)?; + trace!("Data received from: {}\n{}", address, data); + for (hook_name, hook) in config.hooks.iter() { execute_hook(&hook_name, &hook, &data)?; } @@ -87,18 +84,27 @@ fn receive_hook(config: State, data: Json) -> Result { fn get_config() -> Result { if let Ok(config) = File::open("/etc/webhookey/config.yml") { + info!("Loading configuration from `/etc/webhookey/config.yml`"); + return Ok(config); } if let Some(mut path) = dirs::config_dir() { path.push("webhookey/config.yml"); - if let Ok(config) = File::open(path) { + if let Ok(config) = File::open(&path) { + info!( + "Loading configuration from `{}`", + path.to_str().unwrap_or("path not printable"), + ); + return Ok(config); } } if let Ok(config) = File::open("config.yml") { + info!("Loading configuration from `./config.yml`"); + return Ok(config); } @@ -106,9 +112,13 @@ fn get_config() -> Result { } fn main() -> Result<()> { - let config: Config = serde_yaml::from_reader(BufReader::new(get_config()?))?; + env_logger::init(); - println!("{}", serde_yaml::to_string(&config)?); + let config = get_config()?; + + let config: Config = serde_yaml::from_reader(BufReader::new(config))?; + + trace!("Parsed configuration:\n{}", serde_yaml::to_string(&config)?); rocket::ignite() .mount("/", routes![index, receive_hook])