Skip to content

Commit

Permalink
feat: Trace configuration for loading wait indicators and --teach-me …
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
FroVolod and FroVolod authored Nov 30, 2024
1 parent 4f19533 commit 515ba6c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 51 deletions.
54 changes: 54 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
52 changes: 1 addition & 51 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,);
Expand Down Expand Up @@ -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 <Cmd as interactive_clap::FromCli>::from_cli(Some(cli), (config,)) {
interactive_clap::ResultFromCli::Ok(cli_cmd)
Expand Down

0 comments on commit 515ba6c

Please sign in to comment.