diff --git a/substrate/client/cli/src/lib.rs b/substrate/client/cli/src/lib.rs index e63e379533..d64f016131 100644 --- a/substrate/client/cli/src/lib.rs +++ b/substrate/client/cli/src/lib.rs @@ -423,6 +423,11 @@ mod tests { #[test] fn prefix_in_log_lines() { + let re = regex::Regex::new(&format!( + r"^\d{{4}}-\d{{2}}-\d{{2}} \d{{2}}:\d{{2}}:\d{{2}} \[{}\] {}$", + EXPECTED_NODE_NAME, + EXPECTED_LOG_MESSAGE, + )).unwrap(); let executable = env::current_exe().unwrap(); let output = Command::new(executable) .env("ENABLE_LOGGING", "1") @@ -431,7 +436,10 @@ mod tests { .unwrap(); let output = String::from_utf8(output.stderr).unwrap(); - assert!(output.contains(&format!(" [{}] ", EXPECTED_NODE_NAME))); + assert!( + re.is_match(output.trim()), + format!("Expected:\n{}\nGot:\n{}", re, output), + ); } /// This is no actual test, it will be used by the `prefix_in_log_lines` test. @@ -448,6 +456,6 @@ mod tests { #[crate::prefix_logs_with(EXPECTED_NODE_NAME)] fn prefix_in_log_lines_process() { - log::info!("Hello World!"); + log::info!("{}", EXPECTED_LOG_MESSAGE); } } diff --git a/substrate/client/cli/src/logging.rs b/substrate/client/cli/src/logging.rs index 3b87d95fe0..e1fc90505b 100644 --- a/substrate/client/cli/src/logging.rs +++ b/substrate/client/cli/src/logging.rs @@ -16,12 +16,9 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use ansi_term::{Colour, Style}; -use std::{fmt::{self, Write as _}, iter}; -use tracing::{ - span::{self, Attributes}, - Event, Id, Level, Subscriber, -}; +use ansi_term::Colour; +use std::fmt; +use tracing::{span::Attributes, Event, Id, Level, Subscriber}; use tracing_log::NormalizeEvent; use tracing_subscriber::{ fmt::{ @@ -93,8 +90,6 @@ where } } - let fmt_ctx = { FmtCtx::new(&ctx, event.parent(), self.ansi) }; - write!(writer, "{}", fmt_ctx)?; if self.display_target { write!(writer, "{}:", meta.target())?; } @@ -247,70 +242,6 @@ impl<'a> fmt::Display for FmtThreadName<'a> { } } -struct FmtCtx<'a, S, N> { - ctx: &'a FmtContext<'a, S, N>, - span: Option<&'a span::Id>, - ansi: bool, -} - -impl<'a, S, N: 'a> FmtCtx<'a, S, N> -where - S: Subscriber + for<'lookup> LookupSpan<'lookup>, - N: for<'writer> FormatFields<'writer> + 'static, -{ - pub(crate) fn new( - ctx: &'a FmtContext<'_, S, N>, - span: Option<&'a span::Id>, - ansi: bool, - ) -> Self { - Self { ctx, ansi, span } - } - - fn bold(&self) -> Style { - if self.ansi { - return Style::new().bold(); - } - - Style::new() - } -} - -// NOTE: the following code took inspiration from tracing-subscriber -// -// https://github.com/tokio-rs/tracing/blob/2f59b32/tracing-subscriber/src/fmt/format/mod.rs#L711 -impl<'a, S, N: 'a> fmt::Display for FmtCtx<'a, S, N> -where - S: Subscriber + for<'lookup> LookupSpan<'lookup>, - N: for<'writer> FormatFields<'writer> + 'static, -{ - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let bold = self.bold(); - let mut seen = false; - - let span = self - .span - .and_then(|id| self.ctx.span(&id)) - .or_else(|| self.ctx.lookup_current()); - - let scope = span - .into_iter() - .flat_map(|span| span.from_root().chain(iter::once(span))); - - for name in scope - .map(|span| span.metadata().name()) - .filter(|&x| x != "substrate-node") - { - seen = true; - write!(f, "{}:", bold.paint(name))?; - } - - if seen { - f.write_char(' ')?; - } - Ok(()) - } -} - // NOTE: the following code has been duplicated from tracing-subscriber // // https://github.com/tokio-rs/tracing/blob/2f59b32/tracing-subscriber/src/fmt/time/mod.rs#L252