diff --git a/client/cli/src/commands/mod.rs b/client/cli/src/commands/mod.rs index 62757890ef01d..dfb734644bf2e 100644 --- a/client/cli/src/commands/mod.rs +++ b/client/cli/src/commands/mod.rs @@ -32,6 +32,7 @@ pub use self::purge_chain_cmd::PurgeChainCmd; pub use self::revert_cmd::RevertCmd; pub use self::run_cmd::RunCmd; pub use self::export_state_cmd::ExportStateCmd; +use crate::SubstrateCli; use std::fmt::Debug; use structopt::StructOpt; @@ -402,6 +403,12 @@ macro_rules! substrate_cli_subcommands { $($enum::$variant(cmd) => cmd.log_filters()),* } } + + fn informant_prefix(&self) -> $crate::Result { + match self { + $($enum::$variant(cmd) => cmd.informant_prefix::()),* + } + } } } } diff --git a/client/cli/src/config.rs b/client/cli/src/config.rs index a1ee1b0cc1da9..282352e4a46e7 100644 --- a/client/cli/src/config.rs +++ b/client/cli/src/config.rs @@ -393,6 +393,11 @@ pub trait CliConfiguration: Sized { Ok(true) } + /// A prefix for the informant's logs + fn informant_prefix(&self) -> Result { + Ok(C::informant_prefix().to_string()) + } + /// Create a Configuration object from the current object fn create_configuration( &self, @@ -464,6 +469,7 @@ pub trait CliConfiguration: Sized { max_runtime_instances, announce_block: self.announce_block()?, role, + informant_prefix: self.informant_prefix::()?, }) } diff --git a/client/cli/src/lib.rs b/client/cli/src/lib.rs index 36d3649926f90..75be59ac96b7d 100644 --- a/client/cli/src/lib.rs +++ b/client/cli/src/lib.rs @@ -84,6 +84,11 @@ pub trait SubstrateCli: Sized { /// Chain spec factory fn load_spec(&self, id: &str) -> std::result::Result, String>; + /// A prefix for the informant's logs + fn informant_prefix() -> &'static str { + "" + } + /// Helper function used to parse the command line arguments. This is the equivalent of /// `structopt`'s `from_iter()` except that it takes a `VersionInfo` argument to provide the name of /// the application, author, "about" and version. It will also set `AppSettings::GlobalVersion`. diff --git a/client/cli/src/runner.rs b/client/cli/src/runner.rs index 2d27743163ae4..9587606c45fc1 100644 --- a/client/cli/src/runner.rs +++ b/client/cli/src/runner.rs @@ -209,9 +209,12 @@ impl Runner { F: FnOnce(Configuration) -> std::result::Result, T: AbstractService + Unpin, { + let prefix = self.config.informant_prefix.clone(); let service = service_builder(self.config)?; - let informant_future = sc_informant::build(&service, sc_informant::OutputFormat::Coloured); + let informant_future = sc_informant::build(&service, sc_informant::OutputFormat::Coloured { + prefix, + }); let _informant_handle = self.tokio_runtime.spawn(informant_future); // we eagerly drop the service so that the internal exit future is fired, diff --git a/client/informant/src/display.rs b/client/informant/src/display.rs index 42f498998362e..4a20af281487b 100644 --- a/client/informant/src/display.rs +++ b/client/informant/src/display.rs @@ -67,16 +67,18 @@ impl InformantDisplay { self.last_update = Instant::now(); self.last_number = Some(best_number); - let (status, target) = match (net_status.sync_state, net_status.best_seen_block) { - (SyncState::Idle, _) => ("💤 Idle".into(), "".into()), - (SyncState::Downloading, None) => (format!("⚙️ Preparing{}", speed), "".into()), - (SyncState::Downloading, Some(n)) => (format!("⚙️ Syncing{}", speed), format!(", target=#{}", n)), + let (level, status, target) = match (net_status.sync_state, net_status.best_seen_block) { + (SyncState::Idle, _) => ("💤", "Idle".into(), "".into()), + (SyncState::Downloading, None) => ("⚙️ ", format!("Preparing{}", speed), "".into()), + (SyncState::Downloading, Some(n)) => ("⚙️ ", format!("Syncing{}", speed), format!(", target=#{}", n)), }; - if self.format == OutputFormat::Coloured { - info!( + match &self.format { + OutputFormat::Coloured { prefix } => info!( target: "substrate", - "{}{} ({} peers), best: #{} ({}), finalized #{} ({}), {} {}", + "{} {}{}{} ({} peers), best: #{} ({}), finalized #{} ({}), {} {}", + level, + prefix, Colour::White.bold().paint(&status), target, Colour::White.bold().paint(format!("{}", num_connected_peers)), @@ -86,11 +88,12 @@ impl InformantDisplay { info.chain.finalized_hash, Colour::Green.paint(format!("⬇ {}", TransferRateFormat(net_status.average_download_per_sec))), Colour::Red.paint(format!("⬆ {}", TransferRateFormat(net_status.average_upload_per_sec))), - ); - } else { - info!( + ), + OutputFormat::Plain { prefix } => info!( target: "substrate", - "{}{} ({} peers), best: #{} ({}), finalized #{} ({}), ⬇ {} ⬆ {}", + "{} {}{}{} ({} peers), best: #{} ({}), finalized #{} ({}), ⬇ {} ⬆ {}", + level, + prefix, status, target, num_connected_peers, @@ -100,7 +103,7 @@ impl InformantDisplay { info.chain.finalized_hash, TransferRateFormat(net_status.average_download_per_sec), TransferRateFormat(net_status.average_upload_per_sec), - ); + ), } } } diff --git a/client/informant/src/lib.rs b/client/informant/src/lib.rs index 6eea9c1d0434c..3c3daf559c382 100644 --- a/client/informant/src/lib.rs +++ b/client/informant/src/lib.rs @@ -29,10 +29,14 @@ use std::time::Duration; mod display; /// The format to print telemetry output in. -#[derive(PartialEq)] +#[derive(Clone)] pub enum OutputFormat { - Coloured, - Plain, + Coloured { + prefix: String, + }, + Plain { + prefix: String, + }, } /// Creates an informant in the form of a `Future` that must be polled regularly. @@ -40,7 +44,7 @@ pub fn build(service: &impl AbstractService, format: OutputFormat) -> impl futur let client = service.client(); let pool = service.transaction_pool(); - let mut display = display::InformantDisplay::new(format); + let mut display = display::InformantDisplay::new(format.clone()); let display_notifications = service .network_status(Duration::from_millis(5000)) @@ -97,7 +101,18 @@ pub fn build(service: &impl AbstractService, format: OutputFormat) -> impl futur last_best = Some((n.header.number().clone(), n.hash.clone())); } - info!(target: "substrate", "✨ Imported #{} ({})", Colour::White.bold().paint(format!("{}", n.header.number())), n.hash); + match &format { + OutputFormat::Coloured { prefix } => info!( + target: "substrate", + "✨ {}Imported #{} ({})", + prefix, Colour::White.bold().paint(format!("{}", n.header.number())), n.hash, + ), + OutputFormat::Plain { prefix } => info!( + target: "substrate", "✨ {}Imported #{} ({})", + prefix, format!("{}", n.header.number()), n.hash, + ), + } + future::ready(()) }); diff --git a/client/service/src/config.rs b/client/service/src/config.rs index cc9c742ed68db..d3144b563f44e 100644 --- a/client/service/src/config.rs +++ b/client/service/src/config.rs @@ -102,6 +102,8 @@ pub struct Configuration { pub max_runtime_instances: usize, /// Announce block automatically after they have been imported pub announce_block: bool, + /// A prefix for the informant's logs + pub informant_prefix: String, } /// Type for tasks spawned by the executor.