sc-informant: Respect --disable-log-color (#3009)

Changes `sc-informant` to respect the `--disable-log-color` CLI flag.

Closes: https://github.com/paritytech/polkadot-sdk/issues/2795

---------

Co-authored-by: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com>
This commit is contained in:
Bastian Köcher
2024-01-22 11:31:37 +01:00
committed by GitHub
parent d53534c49e
commit deb72f432d
5 changed files with 82 additions and 45 deletions
+14 -31
View File
@@ -142,37 +142,20 @@ impl<B: BlockT> InformantDisplay<B> {
("⚙️ ", format!("Preparing{}", speed), format!(", target=#{target}")),
};
if self.format.enable_color {
info!(
target: "substrate",
"{} {}{} ({} peers), best: #{} ({}), finalized #{} ({}), {} {}",
level,
Colour::White.bold().paint(&status),
target,
Colour::White.bold().paint(format!("{}", num_connected_peers)),
Colour::White.bold().paint(format!("{}", best_number)),
best_hash,
Colour::White.bold().paint(format!("{}", finalized_number)),
info.chain.finalized_hash,
Colour::Green.paint(format!(" {}", TransferRateFormat(avg_bytes_per_sec_inbound))),
Colour::Red.paint(format!("{}", TransferRateFormat(avg_bytes_per_sec_outbound))),
)
} else {
info!(
target: "substrate",
"{} {}{} ({} peers), best: #{} ({}), finalized #{} ({}), ⬇ {} ⬆ {}",
level,
status,
target,
num_connected_peers,
best_number,
best_hash,
finalized_number,
info.chain.finalized_hash,
TransferRateFormat(avg_bytes_per_sec_inbound),
TransferRateFormat(avg_bytes_per_sec_outbound),
)
}
info!(
target: "substrate",
"{} {}{} ({} peers), best: #{} ({}), finalized #{} ({}), {} {}",
level,
self.format.print_with_color(Colour::White.bold(), status),
target,
self.format.print_with_color(Colour::White.bold(), num_connected_peers),
self.format.print_with_color(Colour::White.bold(), best_number),
best_hash,
self.format.print_with_color(Colour::White.bold(), finalized_number),
info.chain.finalized_hash,
self.format.print_with_color(Colour::Green, format!("{}", TransferRateFormat(avg_bytes_per_sec_inbound))),
self.format.print_with_color(Colour::Red, format!(" {}", TransferRateFormat(avg_bytes_per_sec_outbound))),
)
}
}
+51 -7
View File
@@ -18,7 +18,7 @@
//! Console informant. Prints sync progress and block events. Runs on the calling thread.
use ansi_term::Colour;
use ansi_term::{Colour, Style};
use futures::prelude::*;
use futures_timer::Delay;
use log::{debug, info, trace};
@@ -51,6 +51,47 @@ impl Default for OutputFormat {
}
}
enum ColorOrStyle {
Color(Colour),
Style(Style),
}
impl From<Colour> for ColorOrStyle {
fn from(value: Colour) -> Self {
Self::Color(value)
}
}
impl From<Style> for ColorOrStyle {
fn from(value: Style) -> Self {
Self::Style(value)
}
}
impl ColorOrStyle {
fn paint(&self, data: String) -> impl Display {
match self {
Self::Color(c) => c.paint(data),
Self::Style(s) => s.paint(data),
}
}
}
impl OutputFormat {
/// Print with color if `self.enable_color == true`.
fn print_with_color(
&self,
color: impl Into<ColorOrStyle>,
data: impl ToString,
) -> impl Display {
if self.enable_color {
color.into().paint(data.to_string()).to_string()
} else {
data.to_string()
}
}
}
/// Builds the informant and returns a `Future` that drives the informant.
pub async fn build<B: BlockT, C, N, S>(client: Arc<C>, network: N, syncing: S, format: OutputFormat)
where
@@ -89,11 +130,14 @@ where
futures::select! {
() = display_notifications.fuse() => (),
() = display_block_import(client).fuse() => (),
() = display_block_import(client, format).fuse() => (),
};
}
fn display_block_import<B: BlockT, C>(client: Arc<C>) -> impl Future<Output = ()>
fn display_block_import<B: BlockT, C>(
client: Arc<C>,
format: OutputFormat,
) -> impl Future<Output = ()>
where
C: UsageProvider<B> + HeaderMetadata<B> + BlockchainEvents<B>,
<C as HeaderMetadata<B>>::Error: Display,
@@ -117,11 +161,11 @@ where
match maybe_ancestor {
Ok(ref ancestor) if ancestor.hash != *last_hash => info!(
"♻️ Reorg on #{},{} to #{},{}, common ancestor #{},{}",
Colour::Red.bold().paint(format!("{}", last_num)),
format.print_with_color(Colour::Red.bold(), last_num),
last_hash,
Colour::Green.bold().paint(format!("{}", n.header.number())),
format.print_with_color(Colour::Green.bold(), n.header.number()),
n.hash,
Colour::White.bold().paint(format!("{}", ancestor.number)),
format.print_with_color(Colour::White.bold(), ancestor.number),
ancestor.hash,
),
Ok(_) => {},
@@ -146,7 +190,7 @@ where
info!(
target: "substrate",
"✨ Imported #{} ({})",
Colour::White.bold().paint(format!("{}", n.header.number())),
format.print_with_color(Colour::White.bold(), n.header.number()),
n.hash,
);
}