|
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 | +/// Selects between a human-oriented text formatter and a newline-delimited JSON |
| 17 | +/// formatter. The two are mutually exclusive — pick whichever matches how you'll |
| 18 | +/// consume the output. The choice is orthogonal to |
| 19 | +/// [`setup_logging_and_tracing`]'s `tracing` flag, which controls a separate |
| 20 | +/// Perfetto trace file and is unaffected by the format selected here. |
| 21 | +/// |
| 22 | +/// See the crate-level documentation of `vortex-compressor` for the full |
| 23 | +/// inventory of fields emitted under each tracing target. |
| 24 | +#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, ValueEnum)] |
| 25 | +pub enum LogFormat { |
| 26 | + /// Human-readable single-line records, with level, file, and line prefixes. |
| 27 | + /// ANSI coloring is enabled automatically when stderr is a terminal. This |
| 28 | + /// is the default and matches the historical behavior of this crate. |
| 29 | + #[default] |
| 30 | + Text, |
| 31 | + /// Newline-delimited JSON — one complete JSON object per event, written to |
| 32 | + /// stderr. Each record includes the event fields as typed values plus the |
| 33 | + /// full span stack under the `spans` key, making it suitable for piping |
| 34 | + /// into `jq` or ingestion into a log aggregator. |
| 35 | + /// |
| 36 | + /// Not to be confused with the Perfetto trace format emitted when the |
| 37 | + /// `tracing` flag is set — that is a single-document Chrome Trace Event |
| 38 | + /// file designed to be loaded into the Perfetto UI, and cannot be |
| 39 | + /// meaningfully filtered line-by-line. |
| 40 | + Json, |
| 41 | +} |
| 42 | + |
| 43 | +/// Initialize logging/tracing for a benchmark. |
| 44 | +/// |
| 45 | +/// - `verbose`: when `RUST_LOG` is unset, raises the default filter from `INFO` |
| 46 | +/// to `TRACE`. Has no effect when `RUST_LOG` is set (the env var wins). |
| 47 | +/// - `perfetto`: when `true`, additionally attaches a |
| 48 | +/// [`tracing_perfetto::PerfettoLayer`] that writes span begin/end events to |
| 49 | +/// `trace.json` in the current directory. Intended to be loaded into the |
| 50 | +/// Perfetto UI for flamegraph visualization. |
| 51 | +/// - `format`: controls the primary stderr sink's formatting. See [`LogFormat`]. |
| 52 | +pub fn setup_logging_and_tracing( |
| 53 | + verbose: bool, |
| 54 | + perfetto: bool, |
| 55 | + format: LogFormat, |
| 56 | +) -> anyhow::Result<()> { |
14 | 57 | let filter = default_env_filter(verbose); |
15 | 58 |
|
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()); |
| 59 | + let perfetto_layer = perfetto |
| 60 | + .then(|| { |
| 61 | + Ok::<_, anyhow::Error>( |
| 62 | + PerfettoLayer::new(File::create("trace.json")?).with_debug_annotations(true), |
| 63 | + ) |
| 64 | + }) |
| 65 | + .transpose()?; |
| 66 | + |
| 67 | + // `fmt::layer()` and `fmt::layer().json()` produce different concrete |
| 68 | + // types, so we erase each to a `dyn Layer` via `.boxed()` and keep the |
| 69 | + // registry chain uniform. |
| 70 | + let fmt_layer: Box<dyn Layer<_> + Send + Sync> = match format { |
| 71 | + LogFormat::Text => tracing_subscriber::fmt::layer() |
| 72 | + .with_writer(std::io::stderr) |
| 73 | + .with_level(true) |
| 74 | + .with_file(true) |
| 75 | + .with_line_number(true) |
| 76 | + .with_ansi(std::io::stderr().is_terminal()) |
| 77 | + .boxed(), |
| 78 | + LogFormat::Json => tracing_subscriber::fmt::layer() |
| 79 | + .json() |
| 80 | + .with_writer(std::io::stderr) |
| 81 | + .with_current_span(true) |
| 82 | + .with_span_list(true) |
| 83 | + .boxed(), |
| 84 | + }; |
22 | 85 |
|
23 | 86 | tracing_subscriber::registry() |
24 | 87 | .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 | | - ) |
| 88 | + .with(perfetto_layer) |
35 | 89 | .with(fmt_layer) |
36 | 90 | .init(); |
37 | 91 |
|
|
0 commit comments