Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 65f0b25

Browse files
committedMar 1, 2021
Always compile rustdoc with debug logging enabled when download-rustc is set
Previously, logging at DEBUG or below would always be silenced, because rustc compiles tracing with the `static_max_level_info` feature. That makes sense for release artifacts, but not for developing rustdoc. Instead, this compiles two different versions of tracing: one in the release artifacts, distributed in the sysroot, and a new version compiled by rustdoc. Since `rustc_driver` is always linked to the version of sysroot, this copy/pastes `init_env_logging` into rustdoc. The builds the second version of tracing unconditionally; see the code for details on why.
1 parent fe1bf8e commit 65f0b25

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed
 

‎Cargo.lock

+3
Original file line numberDiff line numberDiff line change
@@ -4452,6 +4452,9 @@ dependencies = [
44524452
"serde_json",
44534453
"smallvec 1.6.1",
44544454
"tempfile",
4455+
"tracing",
4456+
"tracing-subscriber",
4457+
"tracing-tree",
44554458
]
44564459

44574460
[[package]]

‎src/librustdoc/Cargo.toml

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ tempfile = "3"
1919
itertools = "0.9"
2020
regex = "1"
2121
rustdoc-json-types = { path = "../rustdoc-json-types" }
22+
tracing = "0.1"
23+
tracing-tree = "0.1.6"
24+
25+
[dependencies.tracing-subscriber]
26+
version = "0.2.13"
27+
default-features = false
28+
features = ["fmt", "env-filter", "smallvec", "parking_lot", "ansi"]
2229

2330
[dev-dependencies]
2431
expect-test = "1.0"

‎src/librustdoc/lib.rs

+74
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,88 @@ mod visit_lib;
9595
pub fn main() {
9696
rustc_driver::set_sigpipe_handler();
9797
rustc_driver::install_ice_hook();
98+
99+
// When using CI artifacts (with `download_stage1 = true`), tracing is unconditionally built
100+
// with `--features=static_max_level_info`, which disables almost all rustdoc logging. To avoid
101+
// this, compile our own version of `tracing` that logs all levels.
102+
// NOTE: this compiles both versions of tracing unconditionally, because
103+
// - The compile time hit is not that bad, especially compared to rustdoc's incremental times, and
104+
// - Otherwise, there's no warning that logging is being ignored when `download_stage1 = true`.
105+
// NOTE: The reason this doesn't show double logging when `download_stage1 = false` and
106+
// `debug_logging = true` is because all rustc logging goes to its version of tracing (the one
107+
// in the sysroot), and all of rustdoc's logging goes to its version (the one in Cargo.toml).
108+
init_logging();
98109
rustc_driver::init_env_logger("RUSTDOC_LOG");
110+
99111
let exit_code = rustc_driver::catch_with_exit_code(|| match get_args() {
100112
Some(args) => main_args(&args),
101113
_ => Err(ErrorReported),
102114
});
103115
process::exit(exit_code);
104116
}
105117

118+
fn init_logging() {
119+
use std::io;
120+
121+
// FIXME remove these and use winapi 0.3 instead
122+
// Duplicates: bootstrap/compile.rs, librustc_errors/emitter.rs, rustc_driver/lib.rs
123+
#[cfg(unix)]
124+
fn stdout_isatty() -> bool {
125+
extern crate libc;
126+
unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 }
127+
}
128+
129+
#[cfg(windows)]
130+
fn stdout_isatty() -> bool {
131+
extern crate winapi;
132+
use winapi::um::consoleapi::GetConsoleMode;
133+
use winapi::um::processenv::GetStdHandle;
134+
use winapi::um::winbase::STD_OUTPUT_HANDLE;
135+
136+
unsafe {
137+
let handle = GetStdHandle(STD_OUTPUT_HANDLE);
138+
let mut out = 0;
139+
GetConsoleMode(handle, &mut out) != 0
140+
}
141+
}
142+
143+
let color_logs = match std::env::var("RUSTDOC_LOG_COLOR") {
144+
Ok(value) => match value.as_ref() {
145+
"always" => true,
146+
"never" => false,
147+
"auto" => stdout_isatty(),
148+
_ => early_error(
149+
ErrorOutputType::default(),
150+
&format!(
151+
"invalid log color value '{}': expected one of always, never, or auto",
152+
value
153+
),
154+
),
155+
},
156+
Err(std::env::VarError::NotPresent) => stdout_isatty(),
157+
Err(std::env::VarError::NotUnicode(_value)) => early_error(
158+
ErrorOutputType::default(),
159+
"non-Unicode log color value: expected one of always, never, or auto",
160+
),
161+
};
162+
let filter = tracing_subscriber::EnvFilter::from_env("RUSTDOC_LOG");
163+
let layer = tracing_tree::HierarchicalLayer::default()
164+
.with_writer(io::stderr)
165+
.with_indent_lines(true)
166+
.with_ansi(color_logs)
167+
.with_targets(true)
168+
.with_wraparound(10)
169+
.with_verbose_exit(true)
170+
.with_verbose_entry(true)
171+
.with_indent_amount(2);
172+
#[cfg(parallel_compiler)]
173+
let layer = layer.with_thread_ids(true).with_thread_names(true);
174+
175+
use tracing_subscriber::layer::SubscriberExt;
176+
let subscriber = tracing_subscriber::Registry::default().with(filter).with(layer);
177+
tracing::subscriber::set_global_default(subscriber).unwrap();
178+
}
179+
106180
fn get_args() -> Option<Vec<String>> {
107181
env::args_os()
108182
.enumerate()

0 commit comments

Comments
 (0)
Please sign in to comment.