From e7a2c67998822740c97285eaabdceb41b6d75387 Mon Sep 17 00:00:00 2001 From: Didier Date: Mon, 8 May 2023 14:39:57 +0200 Subject: [PATCH] feat: add build file and add commit hash and version in debug info --- build.rs | 35 +++++++++++++++++++++++++++++++++++ src/main.rs | 7 +++++-- 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 build.rs diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..0ccc14c --- /dev/null +++ b/build.rs @@ -0,0 +1,35 @@ +use std::process::Command; + +fn main() { + println!("cargo:rustc-env=GIT_COMMIT_HASH={}", git_commit_hash()); + println!("cargo:rustc-env=GIT_TAG={}", git_tag()); +} + +fn git_commit_hash() -> String { + let commit_command = Command::new("git") + .args(&[ + "rev-parse", + "HEAD" + ]) + .output(); + + match commit_command { + Ok(out) => String::from_utf8(out.stdout).unwrap_or(String::from("unknown")), + Err(_) => String::from("unknown") + } +} + +fn git_tag() -> String { + let tag_command = Command::new("git") + .args(&[ + "describe", + "--tags", + "--abbrev=0" + ]) + .output(); + + match tag_command { + Ok(out) => String::from_utf8(out.stdout).unwrap_or(String::from("unknown")), + Err(_) => String::from("unknown") + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 77940ca..31a7f93 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,10 @@ mod error; fn main() { let config = configuration::Config::from_file(&env::var("CONFIG").unwrap_or(String::from("./config.toml"))); setup_logger(&config); - debug!("Config: {:#?}", &config); + trace!("Config: {:#?}", &config); + + debug!("commit: {}", env!("GIT_COMMIT_HASH")); + debug!("version: {}", env!("GIT_TAG")); let input_files = files::get_files(&config.files.input_path) .into_iter() @@ -99,7 +102,7 @@ fn setup_logger(config: &configuration::Config) { .size(10 * 1000) .roll_count(10) .time_format("%Y-%m-%d %H:%M:%S") - .level(if config.is_debug() { "debug" } else { "info" }) + .level(if config.is_debug() { "trace" } else { "info" }) .output_file() .output_console() .build();