@@ -7,7 +7,7 @@ use std::fmt::Debug;
77use std:: fs:: OpenOptions ;
88use std:: path:: PathBuf ;
99
10- use tracing:: { Level , trace} ;
10+ use tracing:: { Level , info , trace} ;
1111use tracing_appender:: non_blocking:: WorkerGuard ;
1212use tracing_subscriber:: {
1313 Registry , filter,
@@ -35,6 +35,7 @@ pub fn enable_tracing(
3535 time_style : & TraceTimeStyle ,
3636 console_level : Level ,
3737 json_path : & Option < PathBuf > ,
38+ trace_tmp : bool ,
3839) -> Option < WorkerGuard > {
3940 let time_style = time_style. clone ( ) ;
4041 let console_layer = tracing_subscriber:: fmt:: Layer :: default ( )
@@ -43,6 +44,23 @@ pub fn enable_tracing(
4344 . with_timer ( time_style)
4445 . with_filter ( filter:: Targets :: new ( ) . with_target ( "conserve" , console_level) ) ;
4546 let json_layer;
47+ let ( trace_tmp_layer, tmp_path) = if trace_tmp {
48+ let ( tmp_file, tmp_path) = tempfile:: Builder :: new ( )
49+ . prefix ( & format ! ( "conserve-trace-{}-" , std:: process:: id( ) ) )
50+ . suffix ( ".log" )
51+ . tempfile ( )
52+ . expect ( "create trace temp file" )
53+ . keep ( )
54+ . expect ( "keep trace temp file" ) ;
55+ let layer = tracing_subscriber:: fmt:: Layer :: default ( )
56+ . with_writer ( tmp_file)
57+ . with_ansi ( false )
58+ . with_timer ( time:: uptime ( ) )
59+ . with_filter ( filter:: Targets :: new ( ) . with_target ( "conserve" , Level :: TRACE ) ) ;
60+ ( Some ( layer) , Some ( tmp_path) )
61+ } else {
62+ ( None , None )
63+ } ;
4664 let flush_guard;
4765 if let Some ( json_path) = json_path {
4866 let file_writer = OpenOptions :: new ( )
@@ -65,9 +83,13 @@ pub fn enable_tracing(
6583 Registry :: default ( )
6684 . with ( console_layer)
6785 . with ( json_layer)
86+ . with ( trace_tmp_layer)
6887 . init ( ) ;
6988
7089 trace ! ( "Tracing enabled" ) ;
90+ if let Some ( tmp_path) = tmp_path {
91+ info ! ( "Trace temp file: {}" , tmp_path. display( ) ) ;
92+ }
7193 flush_guard
7294}
7395
0 commit comments