Skip to content

Commit 721173e

Browse files
authored
Rollup merge of rust-lang#136392 - jieyouxu:wrap-tracing, r=onur-ozkan
bootstrap: add wrapper macros for `feature = "tracing"`-gated `tracing` macros Follow-up to rust-lang#136091 (comment). - Add wrapper macros for `error!`, `warn!`, `info!`, `debug!` and `trace!`, which `cfg(feature = "tracing")`-gates the underlying `tracing` macros. They expand to nothing if `"tracing"` feature is not enabled. - This is not done for `span!` or `event!` because they can return span guards, and you can't really wrap that. - This is also not possible for `tracing::instrument` attribute proc-macro unless you use another attribute proc-macro to wrap that. It's not *great*, because `tracing::instrument` and `tracing::{span,event}` can't be wrapped this way. Can test locally with: ```bash $ BOOTSTRAP_TRACING=bootstrap=TRACE ./x check src/bootstrap/ ``` r? ````@onur-ozkan```` (or reroll)
2 parents a2885a6 + acb3bab commit 721173e

File tree

4 files changed

+99
-51
lines changed

4 files changed

+99
-51
lines changed

src/bootstrap/src/bin/main.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ use std::str::FromStr;
1111
use std::{env, process};
1212

1313
use bootstrap::{
14-
Build, CONFIG_CHANGE_HISTORY, Config, Flags, Subcommand, find_recent_config_change_ids,
14+
Build, CONFIG_CHANGE_HISTORY, Config, Flags, Subcommand, debug, find_recent_config_change_ids,
1515
human_readable_changes, t,
1616
};
1717
use build_helper::ci::CiEnv;
1818
#[cfg(feature = "tracing")]
19-
use tracing::{debug, instrument};
19+
use tracing::instrument;
2020

2121
#[cfg_attr(feature = "tracing", instrument(level = "trace", name = "main"))]
2222
fn main() {
@@ -29,10 +29,8 @@ fn main() {
2929
return;
3030
}
3131

32-
#[cfg(feature = "tracing")]
3332
debug!("parsing flags");
3433
let flags = Flags::parse(&args);
35-
#[cfg(feature = "tracing")]
3634
debug!("parsing config based on flags");
3735
let config = Config::parse(flags);
3836

@@ -95,7 +93,6 @@ fn main() {
9593
let dump_bootstrap_shims = config.dump_bootstrap_shims;
9694
let out_dir = config.out.clone();
9795

98-
#[cfg(feature = "tracing")]
9996
debug!("creating new build based on config");
10097
Build::new(config).build();
10198

@@ -207,8 +204,9 @@ fn check_version(config: &Config) -> Option<String> {
207204
// Due to the conditional compilation via the `tracing` cargo feature, this means that `tracing`
208205
// usages in bootstrap need to be also gated behind the `tracing` feature:
209206
//
210-
// - `tracing` macros (like `trace!`) and anything from `tracing`, `tracing_subscriber` and
211-
// `tracing-tree` will need to be gated by `#[cfg(feature = "tracing")]`.
207+
// - `tracing` macros with log levels (`trace!`, `debug!`, `warn!`, `info`, `error`) should not be
208+
// used *directly*. You should use the wrapped `tracing` macros which gate the actual invocations
209+
// behind `feature = "tracing"`.
212210
// - `tracing`'s `#[instrument(..)]` macro will need to be gated like `#![cfg_attr(feature =
213211
// "tracing", instrument(..))]`.
214212
#[cfg(feature = "tracing")]

src/bootstrap/src/lib.rs

+43-44
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ use std::{env, fs, io, str};
2828
use build_helper::ci::gha;
2929
use build_helper::exit;
3030
use termcolor::{ColorChoice, StandardStream, WriteColor};
31-
#[cfg(feature = "tracing")]
32-
use tracing::{debug, instrument, span, trace};
3331
use utils::build_stamp::BuildStamp;
3432
use utils::channel::GitInfo;
3533

@@ -46,6 +44,8 @@ pub use core::builder::PathSet;
4644
pub use core::config::Config;
4745
pub use core::config::flags::{Flags, Subcommand};
4846

47+
#[cfg(feature = "tracing")]
48+
use tracing::{instrument, span};
4949
pub use utils::change_tracker::{
5050
CONFIG_CHANGE_HISTORY, find_recent_config_change_ids, human_readable_changes,
5151
};
@@ -541,72 +541,71 @@ impl Build {
541541
/// Executes the entire build, as configured by the flags and configuration.
542542
#[cfg_attr(feature = "tracing", instrument(level = "debug", name = "Build::build", skip_all))]
543543
pub fn build(&mut self) {
544-
#[cfg(feature = "tracing")]
545544
trace!("setting up job management");
546545
unsafe {
547546
crate::utils::job::setup(self);
548547
}
549548

550-
#[cfg(feature = "tracing")]
551-
trace!("downloading rustfmt early");
552-
553549
// Download rustfmt early so that it can be used in rust-analyzer configs.
550+
trace!("downloading rustfmt early");
554551
let _ = &builder::Builder::new(self).initial_rustfmt();
555552

556-
#[cfg(feature = "tracing")]
557-
let hardcoded_span =
558-
span!(tracing::Level::DEBUG, "handling hardcoded subcommands (Format, Suggest, Perf)")
559-
.entered();
560-
561-
// hardcoded subcommands
562-
match &self.config.cmd {
563-
Subcommand::Format { check, all } => {
564-
return core::build_steps::format::format(
565-
&builder::Builder::new(self),
566-
*check,
567-
*all,
568-
&self.config.paths,
569-
);
570-
}
571-
Subcommand::Suggest { run } => {
572-
return core::build_steps::suggest::suggest(&builder::Builder::new(self), *run);
573-
}
574-
Subcommand::Perf { .. } => {
575-
return core::build_steps::perf::perf(&builder::Builder::new(self));
576-
}
577-
_cmd => {
578-
#[cfg(feature = "tracing")]
579-
debug!(cmd = ?_cmd, "not a hardcoded subcommand; returning to normal handling");
553+
// Handle hard-coded subcommands.
554+
{
555+
#[cfg(feature = "tracing")]
556+
let _hardcoded_span = span!(
557+
tracing::Level::DEBUG,
558+
"handling hardcoded subcommands (Format, Suggest, Perf)"
559+
)
560+
.entered();
561+
562+
match &self.config.cmd {
563+
Subcommand::Format { check, all } => {
564+
return core::build_steps::format::format(
565+
&builder::Builder::new(self),
566+
*check,
567+
*all,
568+
&self.config.paths,
569+
);
570+
}
571+
Subcommand::Suggest { run } => {
572+
return core::build_steps::suggest::suggest(&builder::Builder::new(self), *run);
573+
}
574+
Subcommand::Perf { .. } => {
575+
return core::build_steps::perf::perf(&builder::Builder::new(self));
576+
}
577+
_cmd => {
578+
debug!(cmd = ?_cmd, "not a hardcoded subcommand; returning to normal handling");
579+
}
580580
}
581-
}
582581

583-
#[cfg(feature = "tracing")]
584-
drop(hardcoded_span);
585-
#[cfg(feature = "tracing")]
586-
debug!("handling subcommand normally");
582+
debug!("handling subcommand normally");
583+
}
587584

588585
if !self.config.dry_run() {
589586
#[cfg(feature = "tracing")]
590587
let _real_run_span = span!(tracing::Level::DEBUG, "executing real run").entered();
591588

589+
// We first do a dry-run. This is a sanity-check to ensure that
590+
// steps don't do anything expensive in the dry-run.
592591
{
593592
#[cfg(feature = "tracing")]
594593
let _sanity_check_span =
595594
span!(tracing::Level::DEBUG, "(1) executing dry-run sanity-check").entered();
596-
597-
// We first do a dry-run. This is a sanity-check to ensure that
598-
// steps don't do anything expensive in the dry-run.
599595
self.config.dry_run = DryRun::SelfCheck;
600596
let builder = builder::Builder::new(self);
601597
builder.execute_cli();
602598
}
603599

604-
#[cfg(feature = "tracing")]
605-
let _actual_run_span =
606-
span!(tracing::Level::DEBUG, "(2) executing actual run").entered();
607-
self.config.dry_run = DryRun::Disabled;
608-
let builder = builder::Builder::new(self);
609-
builder.execute_cli();
600+
// Actual run.
601+
{
602+
#[cfg(feature = "tracing")]
603+
let _actual_run_span =
604+
span!(tracing::Level::DEBUG, "(2) executing actual run").entered();
605+
self.config.dry_run = DryRun::Disabled;
606+
let builder = builder::Builder::new(self);
607+
builder.execute_cli();
608+
}
610609
} else {
611610
#[cfg(feature = "tracing")]
612611
let _dry_run_span = span!(tracing::Level::DEBUG, "executing dry run").entered();

src/bootstrap/src/utils/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub(crate) mod render_tests;
1414
pub(crate) mod shared_helpers;
1515
pub(crate) mod tarball;
1616

17+
pub(crate) mod tracing;
18+
1719
#[cfg(feature = "build-metrics")]
1820
pub(crate) mod metrics;
1921

src/bootstrap/src/utils/tracing.rs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//! Wrapper macros for [`tracing`] macros to avoid having to write `cfg(feature = "tracing")`-gated
2+
//! `debug!`/`trace!` everytime, e.g.
3+
//!
4+
//! ```rust,ignore (example)
5+
//! #[cfg(feature = "tracing")]
6+
//! trace!("...");
7+
//! ```
8+
//!
9+
//! When `feature = "tracing"` is inactive, these macros expand to nothing.
10+
11+
#[macro_export]
12+
macro_rules! trace {
13+
($($tokens:tt)*) => {
14+
#[cfg(feature = "tracing")]
15+
::tracing::trace!($($tokens)*)
16+
}
17+
}
18+
19+
#[macro_export]
20+
macro_rules! debug {
21+
($($tokens:tt)*) => {
22+
#[cfg(feature = "tracing")]
23+
::tracing::debug!($($tokens)*)
24+
}
25+
}
26+
27+
#[macro_export]
28+
macro_rules! warn {
29+
($($tokens:tt)*) => {
30+
#[cfg(feature = "tracing")]
31+
::tracing::warn!($($tokens)*)
32+
}
33+
}
34+
35+
#[macro_export]
36+
macro_rules! info {
37+
($($tokens:tt)*) => {
38+
#[cfg(feature = "tracing")]
39+
::tracing::info!($($tokens)*)
40+
}
41+
}
42+
43+
#[macro_export]
44+
macro_rules! error {
45+
($($tokens:tt)*) => {
46+
#[cfg(feature = "tracing")]
47+
::tracing::error!($($tokens)*)
48+
}
49+
}

0 commit comments

Comments
 (0)