@@ -278,6 +278,8 @@ pub struct LoggerConfig {
278
278
pub show_log_origin : Option < bool > ,
279
279
/// Use the new logger format.
280
280
pub new_format : Option < bool > ,
281
+ /// The profile file to output.
282
+ pub profile_file : Option < std:: path:: PathBuf > ,
281
283
}
282
284
283
285
/// Error with actions on the `LoggerConfig`.
@@ -318,7 +320,21 @@ impl LoggerConfig {
318
320
319
321
let fmt_layer = fmt_layer. with_writer ( writer. with_max_level ( level) ) ;
320
322
321
- tracing_subscriber:: registry ( ) . with ( fmt_layer) . init ( ) ;
323
+ if let Some ( profile_file) = & self . profile_file {
324
+ // We can discard the flush guard as
325
+ // > This type is only needed when using
326
+ // > `tracing::subscriber::set_global_default`, which prevents the drop
327
+ // > implementation of layers from running when the program exits.
328
+ // See https://docs.rs/tracing-flame/0.2.0/tracing_flame/struct.FlushGuard.html
329
+ let ( flame_layer, _guard) = tracing_flame:: FlameLayer :: with_file ( profile_file) . unwrap ( ) ;
330
+ tracing_subscriber:: registry ( )
331
+ . with ( fmt_layer)
332
+ . with ( flame_layer)
333
+ . init ( ) ;
334
+ } else {
335
+ tracing_subscriber:: registry ( ) . with ( fmt_layer) . init ( ) ;
336
+ }
337
+
322
338
Ok ( ( ) )
323
339
}
324
340
/// Initializes the logger with the new format.
@@ -353,7 +369,20 @@ impl LoggerConfig {
353
369
354
370
let fmt_layer = fmt_layer. with_writer ( writer. with_max_level ( level) ) ;
355
371
356
- tracing_subscriber:: registry ( ) . with ( fmt_layer) . init ( ) ;
372
+ if let Some ( profile_file) = & self . profile_file {
373
+ // We can discard the flush guard as
374
+ // > This type is only needed when using
375
+ // > `tracing::subscriber::set_global_default`, which prevents the drop
376
+ // > implementation of layers from running when the program exits.
377
+ // See https://docs.rs/tracing-flame/0.2.0/tracing_flame/struct.FlushGuard.html
378
+ let ( flame_layer, _guard) = tracing_flame:: FlameLayer :: with_file ( profile_file) . unwrap ( ) ;
379
+ tracing_subscriber:: registry ( )
380
+ . with ( fmt_layer)
381
+ . with ( flame_layer)
382
+ . init ( ) ;
383
+ } else {
384
+ tracing_subscriber:: registry ( ) . with ( fmt_layer) . init ( ) ;
385
+ }
357
386
358
387
Ok ( ( ) )
359
388
}
0 commit comments