From 515ba6c8a2fd2f428f2d448827443bce2521142d Mon Sep 17 00:00:00 2001 From: FroVolod Date: Sat, 30 Nov 2024 13:45:41 +0200 Subject: [PATCH] feat: Trace configuration for loading wait indicators and --teach-me flag moved to library (#417) Created a setup_tracing() function in the library. It provides tracing configuration for loading wait indicators and the --teach-me flag. Co-authored-by: FroVolod --- src/lib.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 52 +-------------------------------------------------- 2 files changed, 55 insertions(+), 51 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ad273c6b4..95fd79eca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,3 +18,57 @@ pub struct GlobalContext { pub offline: bool, pub teach_me: bool, } + +pub fn setup_tracing(teach_me_flag_is_set: bool) -> CliResult { + use indicatif::ProgressStyle; + use tracing_indicatif::IndicatifLayer; + use tracing_subscriber::layer::SubscriberExt; + use tracing_subscriber::util::SubscriberInitExt; + use tracing_subscriber::EnvFilter; + + if teach_me_flag_is_set { + let env_filter = EnvFilter::from_default_env() + .add_directive(tracing::Level::WARN.into()) + .add_directive("near_teach_me=info".parse()?) + .add_directive("near_cli_rs=info".parse()?); + tracing_subscriber::registry() + .with( + tracing_subscriber::fmt::layer() + .without_time() + .with_target(false), + ) + .with(env_filter) + .init(); + } else { + let indicatif_layer = IndicatifLayer::new() + .with_progress_style( + ProgressStyle::with_template( + "{spinner:.blue}{span_child_prefix} {span_name} {msg} {span_fields}", + ) + .unwrap() + .tick_strings(&[ + "▹▹▹▹▹", + "▸▹▹▹▹", + "▹▸▹▹▹", + "▹▹▸▹▹", + "▹▹▹▸▹", + "▹▹▹▹▸", + "▪▪▪▪▪", + ]), + ) + .with_span_child_prefix_symbol("↳ "); + let env_filter = EnvFilter::from_default_env() + .add_directive(tracing::Level::WARN.into()) + .add_directive("near_cli_rs=info".parse()?); + tracing_subscriber::registry() + .with( + tracing_subscriber::fmt::layer() + .without_time() + .with_writer(indicatif_layer.get_stderr_writer()), + ) + .with(indicatif_layer) + .with(env_filter) + .init(); + }; + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 969b34b27..06de48f62 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,12 +9,6 @@ use color_eyre::eyre::WrapErr; use color_eyre::owo_colors::OwoColorize; use interactive_clap::ToCliArgs; -use indicatif::ProgressStyle; -use tracing_indicatif::IndicatifLayer; -use tracing_subscriber::layer::SubscriberExt; -use tracing_subscriber::util::SubscriberInitExt; -use tracing_subscriber::EnvFilter; - pub use near_cli_rs::commands; pub use near_cli_rs::common::{self, CliResult}; pub use near_cli_rs::config; @@ -25,7 +19,6 @@ pub use near_cli_rs::network_view_at_block; pub use near_cli_rs::transaction_signature_options; pub use near_cli_rs::types; pub use near_cli_rs::utils_command; - pub use near_cli_rs::GlobalContext; type ConfigContext = (crate::config::Config,); @@ -120,50 +113,7 @@ fn main() -> crate::common::CliResult { } }, }; - if cli.teach_me { - let env_filter = EnvFilter::from_default_env() - .add_directive(tracing::Level::WARN.into()) - .add_directive("near_teach_me=info".parse()?) - .add_directive("near_cli_rs=info".parse()?); - tracing_subscriber::registry() - .with( - tracing_subscriber::fmt::layer() - .without_time() - .with_target(false), - ) - .with(env_filter) - .init(); - } else { - let indicatif_layer = IndicatifLayer::new() - .with_progress_style( - ProgressStyle::with_template( - "{spinner:.blue}{span_child_prefix} {span_name} {msg} {span_fields}", - ) - .unwrap() - .tick_strings(&[ - "▹▹▹▹▹", - "▸▹▹▹▹", - "▹▸▹▹▹", - "▹▹▸▹▹", - "▹▹▹▸▹", - "▹▹▹▹▸", - "▪▪▪▪▪", - ]), - ) - .with_span_child_prefix_symbol("↳ "); - let env_filter = EnvFilter::from_default_env() - .add_directive(tracing::Level::WARN.into()) - .add_directive("near_cli_rs=info".parse()?); - tracing_subscriber::registry() - .with( - tracing_subscriber::fmt::layer() - .without_time() - .with_writer(indicatif_layer.get_stderr_writer()), - ) - .with(indicatif_layer) - .with(env_filter) - .init(); - }; + near_cli_rs::setup_tracing(cli.teach_me)?; let cli_cmd = match ::from_cli(Some(cli), (config,)) { interactive_clap::ResultFromCli::Ok(cli_cmd)