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 181e915

Browse files
committedJan 5, 2022
Auto merge of #92580 - matthiaskrgr:rollup-nzyn65y, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - #92182 (Label more build steps) - #92188 (rustdoc: Clean up NestedAttributesExt trait/implementation) - #92322 (Add another implementation example to Debug trait) - #92448 (Set font size proportional to user's font size) - #92517 (Explicitly pass `PATH` to the Windows exe resolver) - #92545 (Extract init_env_logger to crate) - #92579 (update Miri) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 936ce3d + a067346 commit 181e915

File tree

13 files changed

+299
-191
lines changed

13 files changed

+299
-191
lines changed
 

‎Cargo.lock

+14-14
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ version = "0.1.0"
368368
dependencies = [
369369
"directories",
370370
"rustc-workspace-hack",
371-
"rustc_version 0.3.3",
371+
"rustc_version",
372372
"serde",
373373
"serde_json",
374374
"vergen",
@@ -2276,7 +2276,7 @@ dependencies = [
22762276
"measureme 9.1.2",
22772277
"rand 0.8.4",
22782278
"rustc-workspace-hack",
2279-
"rustc_version 0.4.0",
2279+
"rustc_version",
22802280
"shell-escape",
22812281
"smallvec",
22822282
]
@@ -3808,7 +3808,6 @@ dependencies = [
38083808
name = "rustc_driver"
38093809
version = "0.0.0"
38103810
dependencies = [
3811-
"atty",
38123811
"libc",
38133812
"rustc_ast",
38143813
"rustc_ast_pretty",
@@ -3822,6 +3821,7 @@ dependencies = [
38223821
"rustc_hir_pretty",
38233822
"rustc_interface",
38243823
"rustc_lint",
3824+
"rustc_log",
38253825
"rustc_metadata",
38263826
"rustc_middle",
38273827
"rustc_parse",
@@ -3833,8 +3833,6 @@ dependencies = [
38333833
"rustc_target",
38343834
"rustc_typeck",
38353835
"tracing",
3836-
"tracing-subscriber",
3837-
"tracing-tree",
38383836
"winapi",
38393837
]
38403838

@@ -4077,6 +4075,17 @@ dependencies = [
40774075
"libc",
40784076
]
40794077

4078+
[[package]]
4079+
name = "rustc_log"
4080+
version = "0.0.0"
4081+
dependencies = [
4082+
"atty",
4083+
"rustc_span",
4084+
"tracing",
4085+
"tracing-subscriber",
4086+
"tracing-tree",
4087+
]
4088+
40804089
[[package]]
40814090
name = "rustc_macros"
40824091
version = "0.1.0"
@@ -4577,15 +4586,6 @@ dependencies = [
45774586
"tracing",
45784587
]
45794588

4580-
[[package]]
4581-
name = "rustc_version"
4582-
version = "0.3.3"
4583-
source = "registry+https://github.com/rust-lang/crates.io-index"
4584-
checksum = "f0dfe2087c51c460008730de8b57e6a320782fbfb312e1f4d520e6c6fae155ee"
4585-
dependencies = [
4586-
"semver 0.11.0",
4587-
]
4588-
45894589
[[package]]
45904590
name = "rustc_version"
45914591
version = "0.4.0"

‎compiler/rustc_driver/Cargo.toml

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ crate-type = ["dylib"]
88

99
[dependencies]
1010
libc = "0.2"
11-
atty = "0.2"
1211
tracing = { version = "0.1.28" }
13-
tracing-subscriber = { version = "0.3.3", default-features = false, features = ["fmt", "env-filter", "smallvec", "parking_lot", "ansi"] }
14-
tracing-tree = "0.2.0"
12+
rustc_log = { path = "../rustc_log" }
1513
rustc_middle = { path = "../rustc_middle" }
1614
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1715
rustc_target = { path = "../rustc_target" }
@@ -40,4 +38,4 @@ winapi = { version = "0.3", features = ["consoleapi", "debugapi", "processenv"]
4038

4139
[features]
4240
llvm = ['rustc_interface/llvm']
43-
max_level_info = ['tracing/max_level_info']
41+
max_level_info = ['rustc_log/max_level_info']

‎compiler/rustc_driver/src/lib.rs

+7-50
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc_feature::find_gated_cfg;
2424
use rustc_interface::util::{self, collect_crate_types, get_codegen_backend};
2525
use rustc_interface::{interface, Queries};
2626
use rustc_lint::LintStore;
27+
use rustc_log::stdout_isatty;
2728
use rustc_metadata::locator;
2829
use rustc_save_analysis as save;
2930
use rustc_save_analysis::DumpHandler;
@@ -514,14 +515,6 @@ impl Compilation {
514515
#[derive(Copy, Clone)]
515516
pub struct RustcDefaultCalls;
516517

517-
fn stdout_isatty() -> bool {
518-
atty::is(atty::Stream::Stdout)
519-
}
520-
521-
fn stderr_isatty() -> bool {
522-
atty::is(atty::Stream::Stderr)
523-
}
524-
525518
fn handle_explain(registry: Registry, code: &str, output: ErrorOutputType) {
526519
let upper_cased_code = code.to_ascii_uppercase();
527520
let normalised = if upper_cased_code.starts_with('E') {
@@ -1254,54 +1247,18 @@ pub fn install_ice_hook() {
12541247
/// This allows tools to enable rust logging without having to magically match rustc's
12551248
/// tracing crate version.
12561249
pub fn init_rustc_env_logger() {
1257-
init_env_logger("RUSTC_LOG")
1250+
if let Err(error) = rustc_log::init_rustc_env_logger() {
1251+
early_error(ErrorOutputType::default(), &error.to_string());
1252+
}
12581253
}
12591254

12601255
/// This allows tools to enable rust logging without having to magically match rustc's
12611256
/// tracing crate version. In contrast to `init_rustc_env_logger` it allows you to choose an env var
12621257
/// other than `RUSTC_LOG`.
12631258
pub fn init_env_logger(env: &str) {
1264-
use tracing_subscriber::{
1265-
filter::{self, EnvFilter, LevelFilter},
1266-
layer::SubscriberExt,
1267-
};
1268-
1269-
let filter = match std::env::var(env) {
1270-
Ok(env) => EnvFilter::new(env),
1271-
_ => EnvFilter::default().add_directive(filter::Directive::from(LevelFilter::WARN)),
1272-
};
1273-
1274-
let color_logs = match std::env::var(String::from(env) + "_COLOR") {
1275-
Ok(value) => match value.as_ref() {
1276-
"always" => true,
1277-
"never" => false,
1278-
"auto" => stderr_isatty(),
1279-
_ => early_error(
1280-
ErrorOutputType::default(),
1281-
&format!(
1282-
"invalid log color value '{}': expected one of always, never, or auto",
1283-
value
1284-
),
1285-
),
1286-
},
1287-
Err(std::env::VarError::NotPresent) => stderr_isatty(),
1288-
Err(std::env::VarError::NotUnicode(_value)) => early_error(
1289-
ErrorOutputType::default(),
1290-
"non-Unicode log color value: expected one of always, never, or auto",
1291-
),
1292-
};
1293-
1294-
let layer = tracing_tree::HierarchicalLayer::default()
1295-
.with_writer(io::stderr)
1296-
.with_indent_lines(true)
1297-
.with_ansi(color_logs)
1298-
.with_targets(true)
1299-
.with_indent_amount(2);
1300-
#[cfg(parallel_compiler)]
1301-
let layer = layer.with_thread_ids(true).with_thread_names(true);
1302-
1303-
let subscriber = tracing_subscriber::Registry::default().with(filter).with(layer);
1304-
tracing::subscriber::set_global_default(subscriber).unwrap();
1259+
if let Err(error) = rustc_log::init_env_logger(env) {
1260+
early_error(ErrorOutputType::default(), &error.to_string());
1261+
}
13051262
}
13061263

13071264
#[cfg(all(unix, any(target_env = "gnu", target_os = "macos")))]

‎compiler/rustc_log/Cargo.toml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "rustc_log"
3+
version = "0.0.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
atty = "0.2"
8+
tracing = "0.1.28"
9+
tracing-subscriber = { version = "0.3.3", default-features = false, features = ["fmt", "env-filter", "smallvec", "parking_lot", "ansi"] }
10+
tracing-tree = "0.2.0"
11+
12+
[dev-dependencies]
13+
rustc_span = { path = "../rustc_span" }
14+
15+
[features]
16+
max_level_info = ['tracing/max_level_info']

‎compiler/rustc_log/src/lib.rs

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//! This crate allows tools to enable rust logging without having to magically
2+
//! match rustc's tracing crate version.
3+
//!
4+
//! For example if someone is working on rustc_ast and wants to write some
5+
//! minimal code against it to run in a debugger, with access to the `debug!`
6+
//! logs emitted by rustc_ast, that can be done by writing:
7+
//!
8+
//! ```toml
9+
//! [dependencies]
10+
//! rustc_ast = { path = "../rust/compiler/rustc_ast" }
11+
//! rustc_log = { path = "../rust/compiler/rustc_log" }
12+
//! rustc_span = { path = "../rust/compiler/rustc_span" }
13+
//! ```
14+
//!
15+
//! ```
16+
//! fn main() {
17+
//! rustc_log::init_rustc_env_logger().unwrap();
18+
//!
19+
//! let edition = rustc_span::edition::Edition::Edition2021;
20+
//! rustc_span::create_session_globals_then(edition, || {
21+
//! /* ... */
22+
//! });
23+
//! }
24+
//! ```
25+
//!
26+
//! Now `RUSTC_LOG=debug cargo run` will run your minimal main.rs and show
27+
//! rustc's debug logging. In a workflow like this, one might also add
28+
//! `std::env::set_var("RUSTC_LOG", "debug")` to the top of main so that `cargo
29+
//! run` by itself is sufficient to get logs.
30+
//!
31+
//! The reason rustc_log is a tiny separate crate, as opposed to exposing the
32+
//! same things in rustc_driver only, is to enable the above workflow. If you
33+
//! had to depend on rustc_driver in order to turn on rustc's debug logs, that's
34+
//! an enormously bigger dependency tree; every change you make to rustc_ast (or
35+
//! whichever piece of the compiler you are interested in) would involve
36+
//! rebuilding all the rest of rustc up to rustc_driver in order to run your
37+
//! main.rs. Whereas by depending only on rustc_log and the few crates you are
38+
//! debugging, you can make changes inside those crates and quickly run main.rs
39+
//! to read the debug logs.
40+
41+
use std::env::{self, VarError};
42+
use std::fmt::{self, Display};
43+
use std::io;
44+
use tracing_subscriber::filter::{Directive, EnvFilter, LevelFilter};
45+
use tracing_subscriber::layer::SubscriberExt;
46+
47+
pub fn init_rustc_env_logger() -> Result<(), Error> {
48+
init_env_logger("RUSTC_LOG")
49+
}
50+
51+
/// In contrast to `init_rustc_env_logger` this allows you to choose an env var
52+
/// other than `RUSTC_LOG`.
53+
pub fn init_env_logger(env: &str) -> Result<(), Error> {
54+
let filter = match env::var(env) {
55+
Ok(env) => EnvFilter::new(env),
56+
_ => EnvFilter::default().add_directive(Directive::from(LevelFilter::WARN)),
57+
};
58+
59+
let color_logs = match env::var(String::from(env) + "_COLOR") {
60+
Ok(value) => match value.as_ref() {
61+
"always" => true,
62+
"never" => false,
63+
"auto" => stderr_isatty(),
64+
_ => return Err(Error::InvalidColorValue(value)),
65+
},
66+
Err(VarError::NotPresent) => stderr_isatty(),
67+
Err(VarError::NotUnicode(_value)) => return Err(Error::NonUnicodeColorValue),
68+
};
69+
70+
let layer = tracing_tree::HierarchicalLayer::default()
71+
.with_writer(io::stderr)
72+
.with_indent_lines(true)
73+
.with_ansi(color_logs)
74+
.with_targets(true)
75+
.with_indent_amount(2);
76+
#[cfg(parallel_compiler)]
77+
let layer = layer.with_thread_ids(true).with_thread_names(true);
78+
79+
let subscriber = tracing_subscriber::Registry::default().with(filter).with(layer);
80+
tracing::subscriber::set_global_default(subscriber).unwrap();
81+
82+
Ok(())
83+
}
84+
85+
pub fn stdout_isatty() -> bool {
86+
atty::is(atty::Stream::Stdout)
87+
}
88+
89+
pub fn stderr_isatty() -> bool {
90+
atty::is(atty::Stream::Stderr)
91+
}
92+
93+
#[derive(Debug)]
94+
pub enum Error {
95+
InvalidColorValue(String),
96+
NonUnicodeColorValue,
97+
}
98+
99+
impl std::error::Error for Error {}
100+
101+
impl Display for Error {
102+
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
103+
match self {
104+
Error::InvalidColorValue(value) => write!(
105+
formatter,
106+
"invalid log color value '{}': expected one of always, never, or auto",
107+
value,
108+
),
109+
Error::NonUnicodeColorValue => write!(
110+
formatter,
111+
"non-Unicode log color value: expected one of always, never, or auto",
112+
),
113+
}
114+
}
115+
}

‎library/core/src/fmt/mod.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -570,11 +570,30 @@ impl Display for Arguments<'_> {
570570
/// There are a number of helper methods on the [`Formatter`] struct to help you with manual
571571
/// implementations, such as [`debug_struct`].
572572
///
573+
/// [`debug_struct`]: Formatter::debug_struct
574+
///
575+
/// Types that do not wish to use the standard suite of debug representations
576+
/// provided by the `Formatter` trait (`debug_struct`, `debug_tuple`,
577+
/// `debut_list`, `debug_set`, `debug_map`) can do something totally custom by
578+
/// manually writing an arbitrary representation to the `Formatter`.
579+
///
580+
/// ```
581+
/// # use std::fmt;
582+
/// # struct Point {
583+
/// # x: i32,
584+
/// # y: i32,
585+
/// # }
586+
/// #
587+
/// impl fmt::Debug for Point {
588+
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
589+
/// write!(f, "Point [{} {}]", self.x, self.y)
590+
/// }
591+
/// }
592+
/// ```
593+
///
573594
/// `Debug` implementations using either `derive` or the debug builder API
574595
/// on [`Formatter`] support pretty-printing using the alternate flag: `{:#?}`.
575596
///
576-
/// [`debug_struct`]: Formatter::debug_struct
577-
///
578597
/// Pretty-printing with `#?`:
579598
///
580599
/// ```
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.