Skip to content

Commit 1307c95

Browse files
committed
bootstrap: add tracing and tracing-tree based tracing setup
1 parent 97d59c5 commit 1307c95

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/bootstrap/src/bin/main.rs

+36-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,18 @@ use bootstrap::{
1515
human_readable_changes, t,
1616
};
1717
use build_helper::ci::CiEnv;
18-
18+
#[cfg(feature = "tracing")]
19+
use tracing::*;
20+
#[cfg(feature = "tracing")]
21+
use tracing_subscriber::EnvFilter;
22+
#[cfg(feature = "tracing")]
23+
use tracing_subscriber::prelude::*;
24+
25+
#[cfg_attr(feature = "tracing", instrument(level = "trace", name = "main"))]
1926
fn main() {
27+
#[cfg(feature = "tracing")]
28+
setup_tracing();
29+
2030
let args = env::args().skip(1).collect::<Vec<_>>();
2131

2232
if Flags::try_parse_verbose_help(&args) {
@@ -187,3 +197,28 @@ fn check_version(config: &Config) -> Option<String> {
187197

188198
Some(msg)
189199
}
200+
201+
// # Note on `tracing` usage in bootstrap
202+
//
203+
// Due to the conditional compilation via the `tracing` cargo feature, this means that `tracing`
204+
// usages in bootstrap need to be also gated behind the `tracing` feature:
205+
//
206+
// - `tracing` macros (like `trace!`) and anything from `tracing`, `tracing_subscriber` and
207+
// `tracing-tree` will need to be gated by `#[cfg(feature = "tracing")]`.
208+
// - `tracing`'s `#[instrument(..)]` macro will need to be gated like `#![cfg_attr(feature =
209+
// "tracing", instrument(..))]`.
210+
#[cfg(feature = "tracing")]
211+
fn setup_tracing() {
212+
let filter = EnvFilter::from_env("BOOTSTRAP_TRACING");
213+
let layer = tracing_tree::HierarchicalLayer::default()
214+
.with_writer(std::io::stderr)
215+
.with_ansi(true)
216+
.with_targets(true)
217+
.with_bracketed_fields(true)
218+
.with_indent_amount(2)
219+
.with_indent_lines(true);
220+
let subscriber = tracing_subscriber::registry().with(filter).with(layer);
221+
222+
tracing::subscriber::set_global_default(subscriber).unwrap();
223+
trace!("tracing subscriber setup");
224+
}

0 commit comments

Comments
 (0)