Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(log): Only show the Zebra logo & intro for the start command #7075

Merged
merged 2 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions zebrad/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 20 additions & 6 deletions zebrad/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
52 changes: 32 additions & 20 deletions zebrad/src/components/tracing/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, FrameworkError> {
pub fn new(network: Network, config: Config, uses_intro: bool) -> Result<Self, FrameworkError> {
// 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();
Expand All @@ -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!(
"Thank you for running a {} zebrad {} node!",
network.lowercase_name(),
build_version()
);
eprintln!(
"{}",
std::str::from_utf8(&ZEBRA_ART)
.expect("should always work on a UTF-8 encoded constant")
"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.
Expand All @@ -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 {
Expand Down