From 6311cfbfb3c10e43a6da5339f0bf5843f5a76439 Mon Sep 17 00:00:00 2001 From: teor Date: Wed, 28 Jun 2023 03:38:57 +1000 Subject: [PATCH] fix(log): Only show the Zebra logo & intro for the `start` command (#7075) * Only show the intro for the `start` command * Also disable the log file intro text --- zebrad/src/application.rs | 1 + zebrad/src/commands.rs | 26 ++++++++--- zebrad/src/components/tracing/component.rs | 52 +++++++++++++--------- 3 files changed, 53 insertions(+), 26 deletions(-) diff --git a/zebrad/src/application.rs b/zebrad/src/application.rs index 47f324a2..d701bd80 100644 --- a/zebrad/src/application.rs +++ b/zebrad/src/application.rs @@ -422,6 +422,7 @@ impl Application for ZebradApp { components.push(Box::new(Tracing::new( config.network.network, tracing_config, + command.cmd().uses_intro(), )?)); // Log git metadata and platform info when zebrad starts up diff --git a/zebrad/src/commands.rs b/zebrad/src/commands.rs index 806ac360..d7f4fa33 100644 --- a/zebrad/src/commands.rs +++ b/zebrad/src/commands.rs @@ -64,6 +64,26 @@ impl ZebradCmd { } } + /// Returns true if this command shows the Zebra intro logo and text. + /// + /// For example, `Start` acts as a Zcash node. + pub(crate) fn uses_intro(&self) -> bool { + // List all the commands, so new commands have to make a choice here + match self { + // Commands that need an intro + Start(_) => true, + + // Utility commands + CopyState(_) | Download(_) | Generate(_) | TipHeight(_) => false, + } + } + + /// Returns true if this command should ignore errors when + /// attempting to load a config file. + pub(crate) fn should_ignore_load_config_error(&self) -> bool { + matches!(self, ZebradCmd::Generate(_) | ZebradCmd::Download(_)) + } + /// Returns the default log level for this command, based on the `verbose` command line flag. /// /// Some commands need to be quiet by default. @@ -87,12 +107,6 @@ impl ZebradCmd { "debug" } } - - /// Returns true if this command should ignore errors when - /// attempting to load a config file. - pub(crate) fn should_ignore_load_config_error(&self) -> bool { - matches!(self, ZebradCmd::Generate(_) | ZebradCmd::Download(_)) - } } impl Runnable for ZebradCmd { diff --git a/zebrad/src/components/tracing/component.rs b/zebrad/src/components/tracing/component.rs index 4498b1a1..ff1db585 100644 --- a/zebrad/src/components/tracing/component.rs +++ b/zebrad/src/components/tracing/component.rs @@ -65,9 +65,12 @@ pub struct Tracing { } impl Tracing { - /// Try to create a new [`Tracing`] component with the given `filter`. + /// Try to create a new [`Tracing`] component with the given `config`. + /// + /// If `uses_intro` is true, show a welcome message, the `network`, + /// and the Zebra logo on startup. (If the terminal supports it.) #[allow(clippy::print_stdout, clippy::print_stderr, clippy::unwrap_in_result)] - pub fn new(network: Network, config: Config) -> Result { + pub fn new(network: Network, config: Config, uses_intro: bool) -> Result { // Only use color if tracing output is being sent to a terminal or if it was explicitly // forced to. let use_color = config.use_color_stdout(); @@ -76,28 +79,35 @@ impl Tracing { let filter = config.filter.unwrap_or_default(); let flame_root = &config.flamegraph; - // If it's a terminal and color escaping is enabled: clear screen and - // print Zebra logo (here `use_color` is being interpreted as - // "use escape codes") - if use_color_stderr { - // Clear screen - eprint!("\x1B[2J"); + // Only show the intro for user-focused node server commands like `start` + if uses_intro { + // If it's a terminal and color escaping is enabled: clear screen and + // print Zebra logo (here `use_color` is being interpreted as + // "use escape codes") + if use_color_stderr { + // Clear screen + eprint!("\x1B[2J"); + eprintln!( + "{}", + std::str::from_utf8(&ZEBRA_ART) + .expect("should always work on a UTF-8 encoded constant") + ); + } + eprintln!( - "{}", - std::str::from_utf8(&ZEBRA_ART) - .expect("should always work on a UTF-8 encoded constant") + "Thank you for running a {} zebrad {} node!", + network.lowercase_name(), + build_version() + ); + eprintln!( + "You're helping to strengthen the network and contributing to a social good :)" ); } - eprintln!( - "Thank you for running a {} zebrad {} node!", - network.lowercase_name(), - build_version() - ); - eprintln!("You're helping to strengthen the network and contributing to a social good :)"); - let writer = if let Some(log_file) = config.log_file.as_ref() { - println!("running zebra"); + if uses_intro { + println!("running zebra"); + } // Make sure the directory for the log file exists. // If the log is configured in the current directory, it won't have a parent directory. @@ -122,7 +132,9 @@ impl Tracing { } } - println!("sending logs to {log_file:?}..."); + if uses_intro { + println!("sending logs to {log_file:?}..."); + } let log_file = File::options().append(true).create(true).open(log_file)?; Box::new(log_file) as BoxWrite } else {