Skip to content
Snippets Groups Projects
Unverified Commit 5f44a778 authored by seemantaggarwal's avatar seemantaggarwal Committed by GitHub
Browse files

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: default avatarIulian Barbu <iulian.barbu@parity.io>
Co-authored-by: default avatarOliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: default avatarSerban Iorga <serban@parity.io>
Co-authored-by: default avatarcmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: default avatarIulian Barbu <iulian.barbu@parity.io>
Co-authored-by: default avatarSebastian Kunert <skunert49@gmail.com>
parent 38d2fa85
No related merge requests found
Pipeline #519263 waiting for manual action with stages
in 1 hour, 24 minutes, and 13 seconds
......@@ -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",
......
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
......@@ -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)),
},
}
}
......@@ -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"] }
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment