Skip to content

Commit 51818d8

Browse files
addressing comments
1 parent abf5bf5 commit 51818d8

File tree

2 files changed

+19
-38
lines changed

2 files changed

+19
-38
lines changed

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

+19-4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
use proc_macro::TokenStream;
1919
use quote::quote;
2020
use syn::{Error, Expr, ItemFn};
21+
use proc_macro2::Span;
22+
use proc_macro_crate::{crate_name, FoundCrate};
23+
use syn::{Path, Result};
2124

22-
/// Add a log prefix to the function.
23-
///
2425
/// This prefixes all the log lines with `[<name>]` (after the timestamp). It works by making a
2526
/// tracing's span that is propagated to all the child calls and child tasks (futures) if they are
2627
/// spawned properly with the `SpawnHandle` (see `TaskManager` in sc-cli) or if the futures use
@@ -100,7 +101,6 @@ use syn::{Error, Expr, ItemFn};
100101
/// 2020-10-16 08:12:58 [open-harbor-1619] 〽️ Prometheus server started at 127.0.0.1:9615
101102
/// 2020-10-16 08:12:58 [open-harbor-1619] Listening for new connections on 127.0.0.1:9944.
102103
/// ```
103-
mod utils;
104104
105105
#[proc_macro_attribute]
106106
pub fn prefix_logs_with(arg: TokenStream, item: TokenStream) -> TokenStream {
@@ -118,7 +118,7 @@ pub fn prefix_logs_with(arg: TokenStream, item: TokenStream) -> TokenStream {
118118
let item_fn = syn::parse_macro_input!(item as ItemFn);
119119

120120
// Resolve the proper sc_tracing path.
121-
let crate_name = match utils::resolve_sc_tracing() {
121+
let crate_name = match resolve_sc_tracing() {
122122
Ok(path) => path,
123123
Err(err) => return err.to_compile_error().into(),
124124
};
@@ -139,3 +139,18 @@ pub fn prefix_logs_with(arg: TokenStream, item: TokenStream) -> TokenStream {
139139
})
140140
.into()
141141
}
142+
143+
/// Resolve the correct path for sc_tracing:
144+
/// - If `polkadot-sdk` is in scope, returns a Path corresponding to `polkadot_sdk::sc_tracing`
145+
/// - Otherwise, falls back to `sc_tracing`
146+
fn resolve_sc_tracing() -> Result<Path> {
147+
match crate_name("polkadot-sdk") {
148+
Ok(FoundCrate::Itself) => syn::parse_str("polkadot_sdk::sc_tracing"),
149+
Ok(FoundCrate::Name(sdk_name)) => syn::parse_str(&format!("{}::sc_tracing", sdk_name)),
150+
Err(_) => match crate_name("sc-tracing") {
151+
Ok(FoundCrate::Itself) => syn::parse_str("sc_tracing"),
152+
Ok(FoundCrate::Name(name)) => syn::parse_str(&name),
153+
Err(e) => Err(syn::Error::new(Span::call_site(), e)),
154+
},
155+
}
156+
}

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

-34
This file was deleted.

0 commit comments

Comments
 (0)