18
18
use proc_macro:: TokenStream ;
19
19
use quote:: quote;
20
20
use syn:: { Error , Expr , ItemFn } ;
21
+ use proc_macro2:: Span ;
22
+ use proc_macro_crate:: { crate_name, FoundCrate } ;
23
+ use syn:: { Path , Result } ;
21
24
22
- /// Add a log prefix to the function.
23
- ///
24
25
/// This prefixes all the log lines with `[<name>]` (after the timestamp). It works by making a
25
26
/// tracing's span that is propagated to all the child calls and child tasks (futures) if they are
26
27
/// spawned properly with the `SpawnHandle` (see `TaskManager` in sc-cli) or if the futures use
@@ -100,7 +101,6 @@ use syn::{Error, Expr, ItemFn};
100
101
/// 2020-10-16 08:12:58 [open-harbor-1619] 〽️ Prometheus server started at 127.0.0.1:9615
101
102
/// 2020-10-16 08:12:58 [open-harbor-1619] Listening for new connections on 127.0.0.1:9944.
102
103
/// ```
103
- mod utils;
104
104
105
105
#[ proc_macro_attribute]
106
106
pub fn prefix_logs_with ( arg : TokenStream , item : TokenStream ) -> TokenStream {
@@ -118,7 +118,7 @@ pub fn prefix_logs_with(arg: TokenStream, item: TokenStream) -> TokenStream {
118
118
let item_fn = syn:: parse_macro_input!( item as ItemFn ) ;
119
119
120
120
// Resolve the proper sc_tracing path.
121
- let crate_name = match utils :: resolve_sc_tracing ( ) {
121
+ let crate_name = match resolve_sc_tracing ( ) {
122
122
Ok ( path) => path,
123
123
Err ( err) => return err. to_compile_error ( ) . into ( ) ,
124
124
} ;
@@ -139,3 +139,18 @@ pub fn prefix_logs_with(arg: TokenStream, item: TokenStream) -> TokenStream {
139
139
} )
140
140
. into ( )
141
141
}
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
+ }
0 commit comments