Skip to content

Commit

Permalink
Remove redundent logging code (#4059)
Browse files Browse the repository at this point in the history
1. The `CustomFmtContext::ContextWithFormatFields` enum arm isn't
actually used and thus we don't need the enum anymore.

2. We don't do anything with most of the normalized metadata that's
created by calling `event.normalized_metadata();` - the `target` we can
get from `event.metadata.target()` and level we can get from
`event.metadata.level()` - let's just call them direct to simplify
things. (`event.metadata()` is just a field call under the hood)

Changelog: No functional changes, might run a tad faster with lots of
logging turned on.

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
  • Loading branch information
gilescope and bkchr committed Apr 12, 2024
1 parent c963dc2 commit b28ba4a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 53 deletions.
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

0 comments on commit b28ba4a

Please sign in to comment.