|
4 | 4 | use std::fs::File; |
5 | 5 | use std::io::IsTerminal; |
6 | 6 |
|
| 7 | +use clap::ValueEnum; |
7 | 8 | use tracing::level_filters::LevelFilter; |
8 | 9 | use tracing_perfetto::PerfettoLayer; |
9 | 10 | use tracing_subscriber::EnvFilter; |
| 11 | +use tracing_subscriber::Layer; |
10 | 12 | use tracing_subscriber::prelude::*; |
11 | 13 |
|
12 | | -/// Initialize logging/tracing for a benchmark |
13 | | -pub fn setup_logging_and_tracing(verbose: bool, tracing: bool) -> anyhow::Result<()> { |
| 14 | +/// Format for the primary stderr log sink. |
| 15 | +/// |
| 16 | +/// `Text` is the default human-readable formatter matching the historical behavior of this crate. |
| 17 | +/// `Json` emits one newline-delimited JSON object per event, suitable for piping into `jq` or a log |
| 18 | +/// aggregator. |
| 19 | +#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, ValueEnum)] |
| 20 | +pub enum LogFormat { |
| 21 | + #[default] |
| 22 | + Text, |
| 23 | + Json, |
| 24 | +} |
| 25 | + |
| 26 | +/// Initialize logging/tracing for a benchmark, hardcoding [`LogFormat::Text`]. |
| 27 | +/// |
| 28 | +/// See [`setup_logging_and_tracing_with_format`] if you want to select JSON |
| 29 | +/// output from a CLI flag. |
| 30 | +pub fn setup_logging_and_tracing(verbose: bool, perfetto: bool) -> anyhow::Result<()> { |
| 31 | + setup_logging_and_tracing_with_format(verbose, perfetto, LogFormat::Text) |
| 32 | +} |
| 33 | + |
| 34 | +/// Initialize logging/tracing for a benchmark with an explicit stderr format. |
| 35 | +/// |
| 36 | +/// - `verbose`: when `RUST_LOG` is unset, raises the default filter from `INFO` to `TRACE`. Has no |
| 37 | +/// effect when `RUST_LOG` is set (the env var wins). |
| 38 | +/// - `perfetto`: when `true`, additionally attaches a [`tracing_perfetto::PerfettoLayer`] that |
| 39 | +/// writes span begin/end events to `trace.json` in the current directory. Intended to be loaded |
| 40 | +/// into the Perfetto UI for flamegraph visualization. |
| 41 | +/// - `format`: controls the primary stderr sink's formatting. See [`LogFormat`]. |
| 42 | +pub fn setup_logging_and_tracing_with_format( |
| 43 | + verbose: bool, |
| 44 | + perfetto: bool, |
| 45 | + format: LogFormat, |
| 46 | +) -> anyhow::Result<()> { |
14 | 47 | let filter = default_env_filter(verbose); |
15 | 48 |
|
16 | | - let fmt_layer = tracing_subscriber::fmt::layer() |
17 | | - .with_writer(std::io::stderr) |
18 | | - .with_level(true) |
19 | | - .with_file(true) |
20 | | - .with_line_number(true) |
21 | | - .with_ansi(std::io::stderr().is_terminal()); |
| 49 | + let perfetto_layer = perfetto |
| 50 | + .then(|| { |
| 51 | + Ok::<_, anyhow::Error>( |
| 52 | + PerfettoLayer::new(File::create("trace.json")?).with_debug_annotations(true), |
| 53 | + ) |
| 54 | + }) |
| 55 | + .transpose()?; |
| 56 | + |
| 57 | + // `fmt::layer()` and `fmt::layer().json()` produce different concrete types, |
| 58 | + // so erase each to a `dyn Layer` via `.boxed()` and keep the registry uniform. |
| 59 | + let fmt_layer: Box<dyn Layer<_> + Send + Sync> = match format { |
| 60 | + LogFormat::Text => tracing_subscriber::fmt::layer() |
| 61 | + .with_writer(std::io::stderr) |
| 62 | + .with_level(true) |
| 63 | + .with_file(true) |
| 64 | + .with_line_number(true) |
| 65 | + .with_ansi(std::io::stderr().is_terminal()) |
| 66 | + .boxed(), |
| 67 | + LogFormat::Json => tracing_subscriber::fmt::layer() |
| 68 | + .json() |
| 69 | + .with_writer(std::io::stderr) |
| 70 | + .with_current_span(true) |
| 71 | + .with_span_list(true) |
| 72 | + .boxed(), |
| 73 | + }; |
22 | 74 |
|
23 | 75 | tracing_subscriber::registry() |
24 | 76 | .with(filter) |
25 | | - .with( |
26 | | - tracing |
27 | | - .then(|| { |
28 | | - Ok::<_, anyhow::Error>( |
29 | | - PerfettoLayer::new(File::create("trace.json")?) |
30 | | - .with_debug_annotations(true), |
31 | | - ) |
32 | | - }) |
33 | | - .transpose()?, |
34 | | - ) |
| 77 | + .with(perfetto_layer) |
35 | 78 | .with(fmt_layer) |
36 | 79 | .init(); |
37 | 80 |
|
|
0 commit comments