Skip to content

Commit 5f44a77

Browse files
seemantaggarwalggwpezserban300github-actions[bot]iulianbarbu
authored
Follow up for: Use the umbrella crate for the parachain template #5993 (#7464)
Resolving sc-tracing not being resolved when imported through the polkadot sdk --------- Signed-off-by: Iulian Barbu <[email protected]> Co-authored-by: Oliver Tale-Yazdi <[email protected]> Co-authored-by: Serban Iorga <[email protected]> Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Iulian Barbu <[email protected]> Co-authored-by: Sebastian Kunert <[email protected]>
1 parent 38d2fa8 commit 5f44a77

File tree

4 files changed

+41
-25
lines changed

4 files changed

+41
-25
lines changed

Cargo.lock

+4-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prdoc/pr_7464.prdoc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
title: 'Enable importing sc-tracing macros through polkadot-sdk'
2+
doc:
3+
- audience: Node Dev
4+
description: |-
5+
This PR makes it possible to use the sc-tracing macros when they are imported through the umbrella crate.
6+
7+
crates:
8+
- name: parachain-template-node
9+
bump: minor

substrate/client/tracing/proc-macro/src/lib.rs

+28-15
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ use proc_macro::TokenStream;
1919
use proc_macro2::Span;
2020
use proc_macro_crate::{crate_name, FoundCrate};
2121
use quote::quote;
22-
use syn::{Error, Expr, Ident, ItemFn};
22+
use syn::{Error, Expr, ItemFn, Path, Result};
2323

24-
/// Add a log prefix to the function.
25-
///
2624
/// This prefixes all the log lines with `[<name>]` (after the timestamp). It works by making a
2725
/// tracing's span that is propagated to all the child calls and child tasks (futures) if they are
2826
/// spawned properly with the `SpawnHandle` (see `TaskManager` in sc-cli) or if the futures use
@@ -104,33 +102,33 @@ use syn::{Error, Expr, Ident, ItemFn};
104102
/// ```
105103
#[proc_macro_attribute]
106104
pub fn prefix_logs_with(arg: TokenStream, item: TokenStream) -> TokenStream {
107-
let item_fn = syn::parse_macro_input!(item as ItemFn);
108-
105+
// Ensure an argument was provided.
109106
if arg.is_empty() {
110107
return Error::new(
111-
Span::call_site(),
112-
"missing argument: name of the node. Example: sc_cli::prefix_logs_with(<expr>)",
108+
proc_macro2::Span::call_site(),
109+
"missing argument: prefix. Example: prefix_logs_with(\"Relaychain\")",
113110
)
114111
.to_compile_error()
115-
.into()
112+
.into();
116113
}
117114

118-
let name = syn::parse_macro_input!(arg as Expr);
115+
let prefix_expr = syn::parse_macro_input!(arg as Expr);
116+
let item_fn = syn::parse_macro_input!(item as ItemFn);
119117

120-
let crate_name = match crate_name("sc-tracing") {
121-
Ok(FoundCrate::Itself) => Ident::new("sc_tracing", Span::call_site()),
122-
Ok(FoundCrate::Name(crate_name)) => Ident::new(&crate_name, Span::call_site()),
123-
Err(e) => return Error::new(Span::call_site(), e).to_compile_error().into(),
118+
// Resolve the proper sc_tracing path.
119+
let crate_name = match resolve_sc_tracing() {
120+
Ok(path) => path,
121+
Err(err) => return err.to_compile_error().into(),
124122
};
125123

126-
let ItemFn { attrs, vis, sig, block } = item_fn;
124+
let syn::ItemFn { attrs, vis, sig, block } = item_fn;
127125

128126
(quote! {
129127
#(#attrs)*
130128
#vis #sig {
131129
let span = #crate_name::tracing::info_span!(
132130
#crate_name::logging::PREFIX_LOG_SPAN,
133-
name = #name,
131+
name = #prefix_expr,
134132
);
135133
let _enter = span.enter();
136134

@@ -139,3 +137,18 @@ pub fn prefix_logs_with(arg: TokenStream, item: TokenStream) -> TokenStream {
139137
})
140138
.into()
141139
}
140+
141+
/// Resolve the correct path for sc_tracing:
142+
/// - If `polkadot-sdk` is in scope, returns a Path corresponding to `polkadot_sdk::sc_tracing`
143+
/// - Otherwise, falls back to `sc_tracing`
144+
fn resolve_sc_tracing() -> Result<Path> {
145+
match crate_name("polkadot-sdk") {
146+
Ok(FoundCrate::Itself) => syn::parse_str("polkadot_sdk::sc_tracing"),
147+
Ok(FoundCrate::Name(sdk_name)) => syn::parse_str(&format!("{}::sc_tracing", sdk_name)),
148+
Err(_) => match crate_name("sc-tracing") {
149+
Ok(FoundCrate::Itself) => syn::parse_str("sc_tracing"),
150+
Ok(FoundCrate::Name(name)) => syn::parse_str(&name),
151+
Err(e) => Err(syn::Error::new(Span::call_site(), e)),
152+
},
153+
}
154+
}

templates/parachain/node/Cargo.toml

-3
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,19 @@ build = "build.rs"
1212

1313
[dependencies]
1414
clap = { features = ["derive"], workspace = true }
15-
codec = { workspace = true, default-features = true }
1615
color-print = { workspace = true }
1716
docify = { workspace = true }
1817
futures = { workspace = true }
1918
jsonrpsee = { features = ["server"], workspace = true }
2019
log = { workspace = true, default-features = true }
2120
serde = { features = ["derive"], workspace = true, default-features = true }
22-
serde_json = { workspace = true, default-features = true }
2321

2422
polkadot-sdk = { workspace = true, features = ["node"] }
2523

2624
parachain-template-runtime = { workspace = true }
2725

2826
# Substrate
2927
prometheus-endpoint = { workspace = true, default-features = true }
30-
sc-tracing = { workspace = true, default-features = true }
3128

3229
[build-dependencies]
3330
polkadot-sdk = { workspace = true, features = ["substrate-build-script-utils"] }

0 commit comments

Comments
 (0)