From 0fad6b0eb3ffba834414fff5b1a9c28f3cb79084 Mon Sep 17 00:00:00 2001 From: Shia Date: Wed, 21 Aug 2024 18:03:47 +0900 Subject: [PATCH] Introduce tracing for log --- Cargo.lock | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/cli.rs | 9 ++++++ src/main.rs | 22 +++++++++++++ 4 files changed, 124 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index a021d87..03c3a4e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -713,6 +713,8 @@ dependencies = [ "similar-asserts", "text_io", "tokio", + "tracing", + "tracing-subscriber", ] [[package]] @@ -1074,6 +1076,16 @@ dependencies = [ "syn", ] +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "num-conv" version = "0.1.0" @@ -1135,6 +1147,12 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4030760ffd992bef45b0ae3f10ce1aba99e33464c90d14dd7c039884963ddc7a" +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "parking_lot" version = "0.12.3" @@ -1443,6 +1461,15 @@ dependencies = [ "digest", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "signal-hook-registry" version = "1.4.2" @@ -1547,6 +1574,16 @@ version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d5f0c8eb2ad70c12a6a69508f499b3051c924f4b1cfeae85bfad96e6bc5bba46" +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "time" version = "0.3.36" @@ -1680,6 +1717,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "nu-ansi-term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", ] [[package]] @@ -1762,6 +1825,12 @@ version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "version_check" version = "0.9.4" @@ -1789,6 +1858,28 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index b0d125e..aa8e135 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,3 +24,5 @@ console = "0.15.8" aws-smithy-runtime-api = "1.7.1" aws-smithy-types = "1.2.0" anyhow = "1.0.86" +tracing = "0.1.40" +tracing-subscriber = "0.3.18" diff --git a/src/cli.rs b/src/cli.rs index db0f57d..2526345 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -27,6 +27,9 @@ pub enum Commands { /// Specify path to diff result as json #[clap(long = "diff-as-json", short = 'o', value_name = "output path")] json_diff_path: Option, + /// Emit logs for debugging + #[clap(long = "debug")] + debug_mode: bool, }, /// plan config Plan { @@ -42,6 +45,9 @@ pub enum Commands { /// Specify path to diff result as json #[clap(long = "diff-as-json", short = 'o', value_name = "output path")] json_diff_path: Option, + /// Emit logs for debugging + #[clap(long = "debug")] + debug_mode: bool, }, /// import state machine to specified config file Import { @@ -60,6 +66,9 @@ pub enum Commands { value_name = "group-name/schedule-name" )] schedule_name_with_group: Option, + /// Emit logs for debugging + #[clap(long = "debug")] + debug_mode: bool, }, } diff --git a/src/main.rs b/src/main.rs index d6f57d5..c854445 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,19 @@ use fubura::context::FuburaContext; use fubura::fast_exit; use fubura::types::Config; +fn set_log_level(debug_mode: &bool) { + let level = if *debug_mode { + tracing::Level::DEBUG + } else { + tracing::Level::INFO + }; + + let subscriber = tracing_subscriber::FmtSubscriber::builder() + .with_max_level(level) + .finish(); + tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed"); +} + #[tokio::main] async fn main() { let cli = Cli::parse(); @@ -21,7 +34,10 @@ async fn main() { ext_str, target, json_diff_path, + debug_mode, } => { + set_log_level(debug_mode); + let config = Config::load_from_path(config_path, ext_str); let mut context = FuburaContext::async_default().await; context.targets.clone_from(target); @@ -34,7 +50,10 @@ async fn main() { ext_str, target, json_diff_path, + debug_mode, } => { + set_log_level(debug_mode); + let config = Config::load_from_path(config_path, ext_str); let mut context = FuburaContext::async_default().await; context.targets.clone_from(target); @@ -47,7 +66,10 @@ async fn main() { ext_str, sfn_name, schedule_name_with_group, + debug_mode, } => { + set_log_level(debug_mode); + let config_exist = Path::new(config_path).exists(); let config = if config_exist { Config::load_from_path(config_path, ext_str)