Skip to content

Commit

Permalink
Add a Service Configuration's field + adapt informant + provide means…
Browse files Browse the repository at this point in the history
… to CLI
  • Loading branch information
cecton committed May 28, 2020
1 parent 6c3a2c0 commit 9c2e726
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 18 deletions.
7 changes: 7 additions & 0 deletions client/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -402,6 +403,12 @@ macro_rules! substrate_cli_subcommands {
$($enum::$variant(cmd) => cmd.log_filters()),*
}
}

fn informant_prefix<C: SubstrateCli>(&self) -> $crate::Result<String> {
match self {
$($enum::$variant(cmd) => cmd.informant_prefix::<C>()),*
}
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions client/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,11 @@ pub trait CliConfiguration: Sized {
Ok(true)
}

/// A prefix for the informant's logs
fn informant_prefix<C: SubstrateCli>(&self) -> Result<String> {
Ok(C::informant_prefix().to_string())
}

/// Create a Configuration object from the current object
fn create_configuration<C: SubstrateCli>(
&self,
Expand Down Expand Up @@ -464,6 +469,7 @@ pub trait CliConfiguration: Sized {
max_runtime_instances,
announce_block: self.announce_block()?,
role,
informant_prefix: self.informant_prefix::<C>()?,
})
}

Expand Down
5 changes: 5 additions & 0 deletions client/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ pub trait SubstrateCli: Sized {
/// Chain spec factory
fn load_spec(&self, id: &str) -> std::result::Result<Box<dyn ChainSpec>, 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`.
Expand Down
5 changes: 4 additions & 1 deletion client/cli/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,12 @@ impl<C: SubstrateCli> Runner<C> {
F: FnOnce(Configuration) -> std::result::Result<T, sc_service::error::Error>,
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,
Expand Down
27 changes: 15 additions & 12 deletions client/informant/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,18 @@ impl<B: BlockT> InformantDisplay<B> {
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)),
Expand All @@ -86,11 +88,12 @@ impl<B: BlockT> InformantDisplay<B> {
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,
Expand All @@ -100,7 +103,7 @@ impl<B: BlockT> InformantDisplay<B> {
info.chain.finalized_hash,
TransferRateFormat(net_status.average_download_per_sec),
TransferRateFormat(net_status.average_upload_per_sec),
);
),
}
}
}
Expand Down
25 changes: 20 additions & 5 deletions client/informant/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,22 @@ 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.
pub fn build(service: &impl AbstractService, format: OutputFormat) -> impl futures::Future<Output = ()> {
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))
Expand Down Expand Up @@ -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(())
});

Expand Down
2 changes: 2 additions & 0 deletions client/service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 9c2e726

Please sign in to comment.