44extern crate proc_macro;
55
66use proc_macro:: TokenStream ;
7- use proc_macro2:: TokenStream as Tokens ;
7+ use proc_macro2:: { Ident , TokenStream as Tokens } ;
88
99use quote:: quote;
1010
@@ -58,7 +58,7 @@ fn try_test(attr: TokenStream, input: ItemFn) -> syn::Result<Tokens> {
5858
5959 let ( attribute_args, ignored_attrs) = parse_attrs ( attrs) ?;
6060 let logging_init = expand_logging_init ( & attribute_args) ;
61- let tracing_init = expand_tracing_init ( & attribute_args) ;
61+ let tracing_init = expand_tracing_init ( & sig . ident , & attribute_args) ;
6262
6363 let result = quote ! {
6464 #[ #inner_test]
@@ -75,13 +75,21 @@ fn try_test(attr: TokenStream, input: ItemFn) -> syn::Result<Tokens> {
7575 // The alternative would be to use fully qualified call syntax in
7676 // all initialization code, but that's much harder to control.
7777 mod init {
78- pub fn init( ) {
78+ pub struct Guard <T > {
79+ tracing: T ,
80+ }
81+
82+ pub fn init( ) -> Guard <impl std:: any:: Any > {
7983 #logging_init
80- #tracing_init
84+ Guard {
85+ tracing: {
86+ #tracing_init
87+ } ,
88+ }
8189 }
8290 }
8391
84- init:: init( ) ;
92+ let _ = init:: init( ) ;
8593
8694 #block
8795 }
@@ -176,7 +184,7 @@ fn expand_logging_init(_attribute_args: &AttributeArgs) -> Tokens {
176184
177185/// Expand the initialization code for the `tracing` crate.
178186#[ cfg( feature = "trace" ) ]
179- fn expand_tracing_init ( attribute_args : & AttributeArgs ) -> Tokens {
187+ fn expand_tracing_init ( name : & Ident , attribute_args : & AttributeArgs ) -> Tokens {
180188 let env_filter = if let Some ( default_log_filter) = & attribute_args. default_log_filter {
181189 quote ! {
182190 :: test_log:: tracing_subscriber:: EnvFilter :: builder( )
@@ -191,12 +199,16 @@ fn expand_tracing_init(attribute_args: &AttributeArgs) -> Tokens {
191199 quote ! { :: test_log:: tracing_subscriber:: EnvFilter :: from_default_env( ) }
192200 } ;
193201
202+ let name = name. to_string ( ) ;
203+
194204 quote ! {
195- :: test_log:: tracing:: init( #env_filter) ;
205+ let base = module_path!( ) . split( "::" ) . map( std:: path:: Path :: new) . collect:: <std:: path:: PathBuf >( ) ;
206+ let name = format!( "{}/{}" , base. display( ) , #name) ;
207+ Some ( :: test_log:: tracing:: init( & name, #env_filter) )
196208 }
197209}
198210
199211#[ cfg( not( feature = "trace" ) ) ]
200- fn expand_tracing_init ( _attribute_args : & AttributeArgs ) -> Tokens {
212+ fn expand_tracing_init ( _name : & Ident , _attribute_args : & AttributeArgs ) -> Tokens {
201213 quote ! { }
202214}
0 commit comments