@@ -19,10 +19,8 @@ use proc_macro::TokenStream;
19
19
use proc_macro2:: Span ;
20
20
use proc_macro_crate:: { crate_name, FoundCrate } ;
21
21
use quote:: quote;
22
- use syn:: { Error , Expr , Ident , ItemFn } ;
22
+ use syn:: { Error , Expr , ItemFn , Path , Result } ;
23
23
24
- /// Add a log prefix to the function.
25
- ///
26
24
/// This prefixes all the log lines with `[<name>]` (after the timestamp). It works by making a
27
25
/// tracing's span that is propagated to all the child calls and child tasks (futures) if they are
28
26
/// 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};
104
102
/// ```
105
103
#[ proc_macro_attribute]
106
104
pub fn prefix_logs_with ( arg : TokenStream , item : TokenStream ) -> TokenStream {
107
- let item_fn = syn:: parse_macro_input!( item as ItemFn ) ;
108
-
105
+ // Ensure an argument was provided.
109
106
if arg. is_empty ( ) {
110
107
return Error :: new (
111
- Span :: call_site ( ) ,
112
- "missing argument: name of the node . Example: sc_cli:: prefix_logs_with(<expr> )" ,
108
+ proc_macro2 :: Span :: call_site ( ) ,
109
+ "missing argument: prefix . Example: prefix_logs_with(\" Relaychain \" )" ,
113
110
)
114
111
. to_compile_error ( )
115
- . into ( )
112
+ . into ( ) ;
116
113
}
117
114
118
- let name = syn:: parse_macro_input!( arg as Expr ) ;
115
+ let prefix_expr = syn:: parse_macro_input!( arg as Expr ) ;
116
+ let item_fn = syn:: parse_macro_input!( item as ItemFn ) ;
119
117
120
- let crate_name = match crate_name ( "sc-tracing" ) {
121
- Ok ( FoundCrate :: Itself ) => Ident :: new ( "sc_tracing" , Span :: call_site ( ) ) ,
122
- Ok ( FoundCrate :: Name ( crate_name ) ) => Ident :: new ( & crate_name , Span :: call_site ( ) ) ,
123
- Err ( e ) => return Error :: new ( Span :: call_site ( ) , e ) . to_compile_error ( ) . into ( ) ,
118
+ // Resolve the proper sc_tracing path.
119
+ let crate_name = match resolve_sc_tracing ( ) {
120
+ Ok ( path ) => path ,
121
+ Err ( err ) => return err . to_compile_error ( ) . into ( ) ,
124
122
} ;
125
123
126
- let ItemFn { attrs, vis, sig, block } = item_fn;
124
+ let syn :: ItemFn { attrs, vis, sig, block } = item_fn;
127
125
128
126
( quote ! {
129
127
#( #attrs) *
130
128
#vis #sig {
131
129
let span = #crate_name:: tracing:: info_span!(
132
130
#crate_name:: logging:: PREFIX_LOG_SPAN ,
133
- name = #name ,
131
+ name = #prefix_expr ,
134
132
) ;
135
133
let _enter = span. enter( ) ;
136
134
@@ -139,3 +137,18 @@ pub fn prefix_logs_with(arg: TokenStream, item: TokenStream) -> TokenStream {
139
137
} )
140
138
. into ( )
141
139
}
140
+
141
+ /// Resolve the correct path for sc_tracing:
142
+ /// - If `polkadot-sdk` is in scope, returns a Path corresponding to `polkadot_sdk::sc_tracing`
143
+ /// - Otherwise, falls back to `sc_tracing`
144
+ fn resolve_sc_tracing ( ) -> Result < Path > {
145
+ match crate_name ( "polkadot-sdk" ) {
146
+ Ok ( FoundCrate :: Itself ) => syn:: parse_str ( "polkadot_sdk::sc_tracing" ) ,
147
+ Ok ( FoundCrate :: Name ( sdk_name) ) => syn:: parse_str ( & format ! ( "{}::sc_tracing" , sdk_name) ) ,
148
+ Err ( _) => match crate_name ( "sc-tracing" ) {
149
+ Ok ( FoundCrate :: Itself ) => syn:: parse_str ( "sc_tracing" ) ,
150
+ Ok ( FoundCrate :: Name ( name) ) => syn:: parse_str ( & name) ,
151
+ Err ( e) => Err ( syn:: Error :: new ( Span :: call_site ( ) , e) ) ,
152
+ } ,
153
+ }
154
+ }
0 commit comments