From 38d5c3c3b2aad1ee68adf018327686456b6f04a2 Mon Sep 17 00:00:00 2001 From: Seemant Aggarwal <seemant.aggarwal@parity.io> Date: Thu, 6 Mar 2025 15:32:43 +0530 Subject: [PATCH] =?UTF-8?q?messy=20attempt=20to=20=20re=E2=80=91exports=20?= =?UTF-8?q?the=20logging=20module=20so=20that=20it=E2=80=99s=20available?= =?UTF-8?q?=20as=20sc=5Ftracing::logging?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 1 - .../client/tracing/proc-macro/Cargo.toml | 1 - .../client/tracing/proc-macro/src/lib.rs | 70 +++++++++++-------- .../client/tracing/proc-macro/src/utils.rs | 20 ++++++ substrate/client/tracing/src/logging/mod.rs | 20 ++++++ 5 files changed, 79 insertions(+), 33 deletions(-) create mode 100644 substrate/client/tracing/proc-macro/src/utils.rs diff --git a/Cargo.lock b/Cargo.lock index 4295c8551f2..6e0d2b93ceb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -24128,7 +24128,6 @@ dependencies = [ name = "sc-tracing-proc-macro" version = "11.0.0" dependencies = [ - "frame-support-procedural-tools 10.0.0", "proc-macro-crate 3.1.0", "proc-macro2 1.0.93", "quote 1.0.38", diff --git a/substrate/client/tracing/proc-macro/Cargo.toml b/substrate/client/tracing/proc-macro/Cargo.toml index 6f274dd4f6c..4187235b4e3 100644 --- a/substrate/client/tracing/proc-macro/Cargo.toml +++ b/substrate/client/tracing/proc-macro/Cargo.toml @@ -18,7 +18,6 @@ targets = ["x86_64-unknown-linux-gnu"] proc-macro = true [dependencies] -frame-support-procedural-tools = { workspace = true, default-features = true } proc-macro-crate = { workspace = true } proc-macro2 = { workspace = true } quote = { features = ["proc-macro"], workspace = true } diff --git a/substrate/client/tracing/proc-macro/src/lib.rs b/substrate/client/tracing/proc-macro/src/lib.rs index 9166d461077..dacb0f37769 100644 --- a/substrate/client/tracing/proc-macro/src/lib.rs +++ b/substrate/client/tracing/proc-macro/src/lib.rs @@ -15,9 +15,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use frame_support_procedural_tools::generate_access_from_frame_or_crate; use proc_macro::TokenStream; -use proc_macro2::Span; use quote::quote; use syn::{Error, Expr, ItemFn}; @@ -102,39 +100,49 @@ use syn::{Error, Expr, ItemFn}; /// 2020-10-16 08:12:58 [open-harbor-1619] ã€½ï¸ Prometheus server started at 127.0.0.1:9615 /// 2020-10-16 08:12:58 [open-harbor-1619] Listening for new connections on 127.0.0.1:9944. /// ``` +mod utils; + #[proc_macro_attribute] pub fn prefix_logs_with(arg: TokenStream, item: TokenStream) -> TokenStream { - let item_fn = syn::parse_macro_input!(item as ItemFn); - - if arg.is_empty() { - return Error::new( - Span::call_site(), - "missing argument: prefix. Example: sc_cli::prefix_logs_with(<expr>)", - ) - .to_compile_error() - .into(); - } + // Ensure an argument was provided. + if arg.is_empty() { + return Error::new( + proc_macro2::Span::call_site(), + "missing argument: prefix. Example: prefix_logs_with(\"Relaychain\")", + ) + .to_compile_error() + .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 generate_access_from_frame_or_crate("sc-tracing") { - Ok(ident) => ident, - Err(err) => return err.to_compile_error().into(), - }; + // Resolve the proper sc_tracing path. + let resolved_crate = match utils::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, - ); - let _enter = span.enter(); + // Generate different output based on whether the function is async. + let output = if sig.asyncness.is_some() { + // Async branch: wrap the block in a closure that returns an async block. + quote! { + #(#attrs)* + #vis #sig { + #resolved_crate::logging::apply_prefix_async(#prefix_expr, || async { #block }).await + } + } + } else { + // Sync branch: call the synchronous logging helper. + quote! { + #(#attrs)* + #vis #sig { + #resolved_crate::logging::apply_prefix_sync(#prefix_expr, || { #block }) + } + } + }; - #block - } - }) - .into() -} + output.into() +} \ No newline at end of file diff --git a/substrate/client/tracing/proc-macro/src/utils.rs b/substrate/client/tracing/proc-macro/src/utils.rs new file mode 100644 index 00000000000..c99ee80472c --- /dev/null +++ b/substrate/client/tracing/proc-macro/src/utils.rs @@ -0,0 +1,20 @@ +use proc_macro2::Span; +use proc_macro_crate::{crate_name, FoundCrate}; +use syn::{Path, Result}; + +/// 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` +pub 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/substrate/client/tracing/src/logging/mod.rs b/substrate/client/tracing/src/logging/mod.rs index 33fec2d4188..fd5edfccc18 100644 --- a/substrate/client/tracing/src/logging/mod.rs +++ b/substrate/client/tracing/src/logging/mod.rs @@ -77,6 +77,26 @@ macro_rules! enable_log_reloading { }}; } +/// Synchronous helper: applies the log prefix for the duration of the function call. +pub fn apply_prefix_sync<F, R>(prefix: impl AsRef<str>, f: F) -> R +where + F: FnOnce() -> R, +{ + ::log::info!("Applying log prefix (sync): {}", prefix.as_ref()); + f() +} + +/// Asynchronous helper: applies the log prefix for the duration of the async function call. +pub async fn apply_prefix_async<F, Fut, R>(prefix: impl AsRef<str>, f: F) -> R +where + F: FnOnce() -> Fut, + Fut: std::future::Future<Output = R>, +{ + ::log::info!("Applying log prefix (async): {}", prefix.as_ref()); + f().await +} + + /// Convert a `Option<LevelFilter>` to a [`log::LevelFilter`]. /// /// `None` is interpreted as `Info`. -- GitLab