Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 9c2e726

Browse files
committed
Add a Service Configuration's field + adapt informant + provide means to CLI
1 parent 6c3a2c0 commit 9c2e726

File tree

7 files changed

+59
-18
lines changed

7 files changed

+59
-18
lines changed

client/cli/src/commands/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub use self::purge_chain_cmd::PurgeChainCmd;
3232
pub use self::revert_cmd::RevertCmd;
3333
pub use self::run_cmd::RunCmd;
3434
pub use self::export_state_cmd::ExportStateCmd;
35+
use crate::SubstrateCli;
3536
use std::fmt::Debug;
3637
use structopt::StructOpt;
3738

@@ -402,6 +403,12 @@ macro_rules! substrate_cli_subcommands {
402403
$($enum::$variant(cmd) => cmd.log_filters()),*
403404
}
404405
}
406+
407+
fn informant_prefix<C: SubstrateCli>(&self) -> $crate::Result<String> {
408+
match self {
409+
$($enum::$variant(cmd) => cmd.informant_prefix::<C>()),*
410+
}
411+
}
405412
}
406413
}
407414
}

client/cli/src/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,11 @@ pub trait CliConfiguration: Sized {
393393
Ok(true)
394394
}
395395

396+
/// A prefix for the informant's logs
397+
fn informant_prefix<C: SubstrateCli>(&self) -> Result<String> {
398+
Ok(C::informant_prefix().to_string())
399+
}
400+
396401
/// Create a Configuration object from the current object
397402
fn create_configuration<C: SubstrateCli>(
398403
&self,
@@ -464,6 +469,7 @@ pub trait CliConfiguration: Sized {
464469
max_runtime_instances,
465470
announce_block: self.announce_block()?,
466471
role,
472+
informant_prefix: self.informant_prefix::<C>()?,
467473
})
468474
}
469475

client/cli/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ pub trait SubstrateCli: Sized {
8484
/// Chain spec factory
8585
fn load_spec(&self, id: &str) -> std::result::Result<Box<dyn ChainSpec>, String>;
8686

87+
/// A prefix for the informant's logs
88+
fn informant_prefix() -> &'static str {
89+
""
90+
}
91+
8792
/// Helper function used to parse the command line arguments. This is the equivalent of
8893
/// `structopt`'s `from_iter()` except that it takes a `VersionInfo` argument to provide the name of
8994
/// the application, author, "about" and version. It will also set `AppSettings::GlobalVersion`.

client/cli/src/runner.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,12 @@ impl<C: SubstrateCli> Runner<C> {
209209
F: FnOnce(Configuration) -> std::result::Result<T, sc_service::error::Error>,
210210
T: AbstractService + Unpin,
211211
{
212+
let prefix = self.config.informant_prefix.clone();
212213
let service = service_builder(self.config)?;
213214

214-
let informant_future = sc_informant::build(&service, sc_informant::OutputFormat::Coloured);
215+
let informant_future = sc_informant::build(&service, sc_informant::OutputFormat::Coloured {
216+
prefix,
217+
});
215218
let _informant_handle = self.tokio_runtime.spawn(informant_future);
216219

217220
// we eagerly drop the service so that the internal exit future is fired,

client/informant/src/display.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,18 @@ impl<B: BlockT> InformantDisplay<B> {
6767
self.last_update = Instant::now();
6868
self.last_number = Some(best_number);
6969

70-
let (status, target) = match (net_status.sync_state, net_status.best_seen_block) {
71-
(SyncState::Idle, _) => ("💤 Idle".into(), "".into()),
72-
(SyncState::Downloading, None) => (format!("⚙️ Preparing{}", speed), "".into()),
73-
(SyncState::Downloading, Some(n)) => (format!("⚙️ Syncing{}", speed), format!(", target=#{}", n)),
70+
let (level, status, target) = match (net_status.sync_state, net_status.best_seen_block) {
71+
(SyncState::Idle, _) => ("💤", "Idle".into(), "".into()),
72+
(SyncState::Downloading, None) => ("⚙️ ", format!("Preparing{}", speed), "".into()),
73+
(SyncState::Downloading, Some(n)) => ("⚙️ ", format!("Syncing{}", speed), format!(", target=#{}", n)),
7474
};
7575

76-
if self.format == OutputFormat::Coloured {
77-
info!(
76+
match &self.format {
77+
OutputFormat::Coloured { prefix } => info!(
7878
target: "substrate",
79-
"{}{} ({} peers), best: #{} ({}), finalized #{} ({}), {} {}",
79+
"{} {}{}{} ({} peers), best: #{} ({}), finalized #{} ({}), {} {}",
80+
level,
81+
prefix,
8082
Colour::White.bold().paint(&status),
8183
target,
8284
Colour::White.bold().paint(format!("{}", num_connected_peers)),
@@ -86,11 +88,12 @@ impl<B: BlockT> InformantDisplay<B> {
8688
info.chain.finalized_hash,
8789
Colour::Green.paint(format!("⬇ {}", TransferRateFormat(net_status.average_download_per_sec))),
8890
Colour::Red.paint(format!("⬆ {}", TransferRateFormat(net_status.average_upload_per_sec))),
89-
);
90-
} else {
91-
info!(
91+
),
92+
OutputFormat::Plain { prefix } => info!(
9293
target: "substrate",
93-
"{}{} ({} peers), best: #{} ({}), finalized #{} ({}), ⬇ {} ⬆ {}",
94+
"{} {}{}{} ({} peers), best: #{} ({}), finalized #{} ({}), ⬇ {} ⬆ {}",
95+
level,
96+
prefix,
9497
status,
9598
target,
9699
num_connected_peers,
@@ -100,7 +103,7 @@ impl<B: BlockT> InformantDisplay<B> {
100103
info.chain.finalized_hash,
101104
TransferRateFormat(net_status.average_download_per_sec),
102105
TransferRateFormat(net_status.average_upload_per_sec),
103-
);
106+
),
104107
}
105108
}
106109
}

client/informant/src/lib.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,22 @@ use std::time::Duration;
2929
mod display;
3030

3131
/// The format to print telemetry output in.
32-
#[derive(PartialEq)]
32+
#[derive(Clone)]
3333
pub enum OutputFormat {
34-
Coloured,
35-
Plain,
34+
Coloured {
35+
prefix: String,
36+
},
37+
Plain {
38+
prefix: String,
39+
},
3640
}
3741

3842
/// Creates an informant in the form of a `Future` that must be polled regularly.
3943
pub fn build(service: &impl AbstractService, format: OutputFormat) -> impl futures::Future<Output = ()> {
4044
let client = service.client();
4145
let pool = service.transaction_pool();
4246

43-
let mut display = display::InformantDisplay::new(format);
47+
let mut display = display::InformantDisplay::new(format.clone());
4448

4549
let display_notifications = service
4650
.network_status(Duration::from_millis(5000))
@@ -97,7 +101,18 @@ pub fn build(service: &impl AbstractService, format: OutputFormat) -> impl futur
97101
last_best = Some((n.header.number().clone(), n.hash.clone()));
98102
}
99103

100-
info!(target: "substrate", "✨ Imported #{} ({})", Colour::White.bold().paint(format!("{}", n.header.number())), n.hash);
104+
match &format {
105+
OutputFormat::Coloured { prefix } => info!(
106+
target: "substrate",
107+
"✨ {}Imported #{} ({})",
108+
prefix, Colour::White.bold().paint(format!("{}", n.header.number())), n.hash,
109+
),
110+
OutputFormat::Plain { prefix } => info!(
111+
target: "substrate", "✨ {}Imported #{} ({})",
112+
prefix, format!("{}", n.header.number()), n.hash,
113+
),
114+
}
115+
101116
future::ready(())
102117
});
103118

client/service/src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ pub struct Configuration {
102102
pub max_runtime_instances: usize,
103103
/// Announce block automatically after they have been imported
104104
pub announce_block: bool,
105+
/// A prefix for the informant's logs
106+
pub informant_prefix: String,
105107
}
106108

107109
/// Type for tasks spawned by the executor.

0 commit comments

Comments
 (0)