diff --git a/Cargo.lock b/Cargo.lock index 0664351fd508e8c5bf7018690c2efed341d75eb4..88442d3702e94881b25e10c0f6132e1ac5d7fe3e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6558,9 +6558,9 @@ dependencies = [ [[package]] name = "frame-decode" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02d3379df61ff3dd871e2dde7d1bcdc0263e613c21c7579b149fd4f0ad9b1dc2" +checksum = "6027a409bac4fe95b4d107f965fcdbc252fc89d884a360d076b3070b6128c094" dependencies = [ "frame-metadata 17.0.0", "parity-scale-codec", @@ -13673,11 +13673,8 @@ dependencies = [ "jsonrpsee", "log", "parachain-template-runtime", - "parity-scale-codec", "polkadot-sdk 0.1.0", - "sc-tracing", "serde", - "serde_json", "substrate-prometheus-endpoint", ] @@ -25070,7 +25067,7 @@ dependencies = [ "base58", "blake2 0.10.6", "derive-where", - "frame-decode 0.5.0", + "frame-decode 0.5.1", "frame-metadata 17.0.0", "hashbrown 0.14.5", "hex", @@ -25192,7 +25189,7 @@ version = "0.38.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ee13e6862eda035557d9a2871955306aff540d2b89c06e0a62a1136a700aed28" dependencies = [ - "frame-decode 0.5.0", + "frame-decode 0.5.1", "frame-metadata 17.0.0", "hashbrown 0.14.5", "parity-scale-codec", diff --git a/prdoc/pr_7464.prdoc b/prdoc/pr_7464.prdoc new file mode 100644 index 0000000000000000000000000000000000000000..3be6a87df35ed33c4e8d70e35c23161130cbb466 --- /dev/null +++ b/prdoc/pr_7464.prdoc @@ -0,0 +1,9 @@ +title: 'Enable importing sc-tracing macros through polkadot-sdk' +doc: +- audience: Node Dev + description: |- + This PR makes it possible to use the sc-tracing macros when they are imported through the umbrella crate. + +crates: +- name: parachain-template-node + bump: minor diff --git a/substrate/client/tracing/proc-macro/src/lib.rs b/substrate/client/tracing/proc-macro/src/lib.rs index f9ed7d67d829a71225b9cf879eba845fdb74e6db..5bfe66cda3ee247504fd4c029a272267de18c8c3 100644 --- a/substrate/client/tracing/proc-macro/src/lib.rs +++ b/substrate/client/tracing/proc-macro/src/lib.rs @@ -19,10 +19,8 @@ use proc_macro::TokenStream; use proc_macro2::Span; use proc_macro_crate::{crate_name, FoundCrate}; use quote::quote; -use syn::{Error, Expr, Ident, ItemFn}; +use syn::{Error, Expr, ItemFn, Path, Result}; -/// Add a log prefix to the function. -/// /// This prefixes all the log lines with `[<name>]` (after the timestamp). It works by making a /// tracing's span that is propagated to all the child calls and child tasks (futures) if they are /// 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}; /// ``` #[proc_macro_attribute] pub fn prefix_logs_with(arg: TokenStream, item: TokenStream) -> TokenStream { - let item_fn = syn::parse_macro_input!(item as ItemFn); - + // Ensure an argument was provided. if arg.is_empty() { return Error::new( - Span::call_site(), - "missing argument: name of the node. Example: sc_cli::prefix_logs_with(<expr>)", + proc_macro2::Span::call_site(), + "missing argument: prefix. Example: prefix_logs_with(\"Relaychain\")", ) .to_compile_error() - .into() + .into(); } - let name = syn::parse_macro_input!(arg as Expr); + let prefix_expr = syn::parse_macro_input!(arg as Expr); + let item_fn = syn::parse_macro_input!(item as ItemFn); - let crate_name = match crate_name("sc-tracing") { - Ok(FoundCrate::Itself) => Ident::new("sc_tracing", Span::call_site()), - Ok(FoundCrate::Name(crate_name)) => Ident::new(&crate_name, Span::call_site()), - Err(e) => return Error::new(Span::call_site(), e).to_compile_error().into(), + // Resolve the proper sc_tracing path. + let crate_name = match resolve_sc_tracing() { + Ok(path) => path, + Err(err) => return err.to_compile_error().into(), }; - let ItemFn { attrs, vis, sig, block } = item_fn; + let syn::ItemFn { attrs, vis, sig, block } = item_fn; (quote! { #(#attrs)* #vis #sig { let span = #crate_name::tracing::info_span!( #crate_name::logging::PREFIX_LOG_SPAN, - name = #name, + name = #prefix_expr, ); let _enter = span.enter(); @@ -139,3 +137,18 @@ pub fn prefix_logs_with(arg: TokenStream, item: TokenStream) -> TokenStream { }) .into() } + +/// Resolve the correct path for sc_tracing: +/// - If `polkadot-sdk` is in scope, returns a Path corresponding to `polkadot_sdk::sc_tracing` +/// - Otherwise, falls back to `sc_tracing` +fn resolve_sc_tracing() -> Result<Path> { + match crate_name("polkadot-sdk") { + Ok(FoundCrate::Itself) => syn::parse_str("polkadot_sdk::sc_tracing"), + Ok(FoundCrate::Name(sdk_name)) => syn::parse_str(&format!("{}::sc_tracing", sdk_name)), + Err(_) => match crate_name("sc-tracing") { + Ok(FoundCrate::Itself) => syn::parse_str("sc_tracing"), + Ok(FoundCrate::Name(name)) => syn::parse_str(&name), + Err(e) => Err(syn::Error::new(Span::call_site(), e)), + }, + } +} diff --git a/templates/parachain/node/Cargo.toml b/templates/parachain/node/Cargo.toml index ec4b13b184fcbe1db86a03f04bfee43a6e2bf81b..e7da1b8d60728dfeadfa06a25343bc2a019fee7f 100644 --- a/templates/parachain/node/Cargo.toml +++ b/templates/parachain/node/Cargo.toml @@ -12,14 +12,12 @@ build = "build.rs" [dependencies] clap = { features = ["derive"], workspace = true } -codec = { workspace = true, default-features = true } color-print = { workspace = true } docify = { workspace = true } futures = { workspace = true } jsonrpsee = { features = ["server"], workspace = true } log = { workspace = true, default-features = true } serde = { features = ["derive"], workspace = true, default-features = true } -serde_json = { workspace = true, default-features = true } polkadot-sdk = { workspace = true, features = ["node"] } @@ -27,7 +25,6 @@ parachain-template-runtime = { workspace = true } # Substrate prometheus-endpoint = { workspace = true, default-features = true } -sc-tracing = { workspace = true, default-features = true } [build-dependencies] polkadot-sdk = { workspace = true, features = ["substrate-build-script-utils"] }