mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-30 09:37:55 +00:00
Remove redundent logging code (#4059)
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>
This commit is contained in:
@@ -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
|
||||
@@ -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.
|
||||
@@ -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
|
||||
@@ -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)?;
|
||||
}
|
||||
|
||||
@@ -86,7 +81,7 @@ where
|
||||
}
|
||||
|
||||
if self.display_target {
|
||||
write!(writer, "{}: ", meta.target())?;
|
||||
write!(writer, "{}: ", event.metadata().target())?;
|
||||
}
|
||||
|
||||
// Custom code to display node name
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user