Skip to content
Snippets Groups Projects
Commit 38d5c3c3 authored by Seemant Aggarwal's avatar Seemant Aggarwal
Browse files

messy attempt to re‑exports the logging module so that it’s available as sc_tracing::logging

parent f0128f50
No related merge requests found
Pipeline #518042 waiting for manual action with stages
in 31 minutes and 39 seconds
......@@ -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",
......
......@@ -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 }
......
......@@ -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
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)),
}
}
}
}
......@@ -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`.
......
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