Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundent logging code #4059

Merged
merged 7 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions prdoc/pr_4059.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: Remove redundent logging code

doc:
- audience: Node Dev
description: |
Simplified logging code, now does slightly less work while logging.

crates:
- name: sc-tracing
bump: minor
59 changes: 6 additions & 53 deletions substrate/client/tracing/src/logging/event_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ use ansi_term::Colour;
use regex::Regex;
use std::fmt::{self, Write};
use tracing::{Event, Level, Subscriber};
use tracing_log::NormalizeEvent;
use tracing_subscriber::{
field::RecordFields,
fmt::{format, time::FormatTime, FmtContext, FormatEvent, FormatFields},
layer::Context,
registry::{LookupSpan, SpanRef},
registry::LookupSpan,
};

/// A pre-configured event formatter.
Expand Down Expand Up @@ -54,7 +51,7 @@ where
// https://github.com/tokio-rs/tracing/blob/2f59b32/tracing-subscriber/src/fmt/format/mod.rs#L449
pub(crate) fn format_event_custom<'b, 'w, S, N>(
&self,
ctx: CustomFmtContext<'b, S, N>,
ctx: &FmtContext<'b, S, N>,
writer: format::Writer<'w>,
event: &Event,
) -> fmt::Result
Expand All @@ -63,12 +60,10 @@ where
N: for<'a> FormatFields<'a> + 'static,
{
let mut writer = &mut ControlCodeSanitizer::new(!self.enable_color, writer);
let normalized_meta = event.normalized_metadata();
let meta = normalized_meta.as_ref().unwrap_or_else(|| event.metadata());
time::write(&self.timer, &mut format::Writer::new(&mut writer), self.enable_color)?;

if self.display_level {
let fmt_level = { FmtLevel::new(meta.level(), self.enable_color) };
let fmt_level = FmtLevel::new(event.metadata().level(), self.enable_color);
write!(writer, "{} ", fmt_level)?;
}

Expand All @@ -86,7 +81,7 @@ where
}

if self.display_target {
write!(writer, "{}: ", meta.target())?;
write!(writer, "{}: ", event.metadata().target())?;
}

// Custom code to display node name
Expand Down Expand Up @@ -137,12 +132,12 @@ where
{
let mut out = String::new();
let buf_writer = format::Writer::new(&mut out);
self.format_event_custom(CustomFmtContext::FmtContext(ctx), buf_writer, event)?;
self.format_event_custom(ctx, buf_writer, event)?;
writer.write_str(&out)?;
print!("{}", out);
Ok(())
} else {
self.format_event_custom(CustomFmtContext::FmtContext(ctx), writer, event)
self.format_event_custom(ctx, writer, event)
}
}
}
Expand Down Expand Up @@ -261,48 +256,6 @@ mod time {
}
}

// NOTE: `FmtContext`'s fields are private. This enum allows us to make a `format_event` function
// that works with `FmtContext` or `Context` with `FormatFields`
#[allow(dead_code)]
pub(crate) enum CustomFmtContext<'a, S, N> {
FmtContext(&'a FmtContext<'a, S, N>),
ContextWithFormatFields(&'a Context<'a, S>, &'a N),
}

impl<'a, S, N> FormatFields<'a> for CustomFmtContext<'a, S, N>
where
S: Subscriber + for<'lookup> LookupSpan<'lookup>,
N: for<'writer> FormatFields<'writer> + 'static,
{
fn format_fields<R: RecordFields>(&self, writer: format::Writer<'_>, fields: R) -> fmt::Result {
match self {
CustomFmtContext::FmtContext(fmt_ctx) => fmt_ctx.format_fields(writer, fields),
CustomFmtContext::ContextWithFormatFields(_ctx, fmt_fields) =>
fmt_fields.format_fields(writer, fields),
}
}
}

// NOTE: the following code has been duplicated from tracing-subscriber
//
// https://github.com/tokio-rs/tracing/blob/2f59b32/tracing-subscriber/src/fmt/fmt_layer.rs#L788
impl<'a, S, N> CustomFmtContext<'a, S, N>
where
S: Subscriber + for<'lookup> LookupSpan<'lookup>,
N: for<'writer> FormatFields<'writer> + 'static,
{
#[inline]
pub fn lookup_current(&self) -> Option<SpanRef<'_, S>>
where
S: for<'lookup> LookupSpan<'lookup>,
{
match self {
CustomFmtContext::FmtContext(fmt_ctx) => fmt_ctx.lookup_current(),
CustomFmtContext::ContextWithFormatFields(ctx, _) => ctx.lookup_current(),
}
}
}

/// A writer which (optionally) strips out terminal control codes from the logs.
///
/// This is used by [`EventFormat`] to sanitize the log messages.
Expand Down
Loading