referrerpolicy=no-referrer-when-downgrade

sc_tracing/logging/layers/
prefix_layer.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19use tracing::{span::Attributes, Id, Subscriber};
20use tracing_subscriber::{layer::Context, registry::LookupSpan, Layer};
21
22/// Span name used for the logging prefix. See macro `sc_tracing::logging::prefix_logs_with!`
23pub const PREFIX_LOG_SPAN: &str = "substrate-log-prefix";
24
25/// A `Layer` that captures the prefix span ([`PREFIX_LOG_SPAN`]) which is then used by
26/// [`crate::logging::EventFormat`] to prefix the log lines by customizable string.
27///
28/// See the macro `sc_cli::prefix_logs_with!` for more details.
29pub struct PrefixLayer;
30
31impl<S> Layer<S> for PrefixLayer
32where
33	S: Subscriber + for<'a> LookupSpan<'a>,
34{
35	fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) {
36		let span = match ctx.span(id) {
37			Some(span) => span,
38			None => {
39				// this shouldn't happen!
40				debug_assert!(
41					false,
42					"newly created span with ID {:?} did not exist in the registry; this is a bug!",
43					id
44				);
45				return
46			},
47		};
48
49		if span.name() != PREFIX_LOG_SPAN {
50			return
51		}
52
53		let mut extensions = span.extensions_mut();
54
55		if extensions.get_mut::<Prefix>().is_none() {
56			let mut s = String::new();
57			let mut v = PrefixVisitor(&mut s);
58			attrs.record(&mut v);
59
60			if !s.is_empty() {
61				let fmt_fields = Prefix(s);
62				extensions.insert(fmt_fields);
63			}
64		}
65	}
66}
67
68struct PrefixVisitor<'a, W: std::fmt::Write>(&'a mut W);
69
70macro_rules! write_node_name {
71	($method:ident, $type:ty, $format:expr) => {
72		fn $method(&mut self, field: &tracing::field::Field, value: $type) {
73			if field.name() == "name" {
74				let _ = write!(self.0, $format, value);
75			}
76		}
77	};
78}
79
80impl<'a, W: std::fmt::Write> tracing::field::Visit for PrefixVisitor<'a, W> {
81	write_node_name!(record_debug, &dyn std::fmt::Debug, "[{:?}] ");
82	write_node_name!(record_str, &str, "[{}] ");
83	write_node_name!(record_i64, i64, "[{}] ");
84	write_node_name!(record_u64, u64, "[{}] ");
85	write_node_name!(record_bool, bool, "[{}] ");
86}
87
88#[derive(Debug)]
89pub(crate) struct Prefix(String);
90
91impl Prefix {
92	pub(crate) fn as_str(&self) -> &str {
93		self.0.as_str()
94	}
95}