Skip to content

Commit

Permalink
Auto merge of #92580 - matthiaskrgr:rollup-nzyn65y, r=matthiaskrgr
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Jan 5, 2022
2 parents 936ce3d + a067346 commit 181e915
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 191 deletions.
28 changes: 14 additions & 14 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ version = "0.1.0"
dependencies = [
"directories",
"rustc-workspace-hack",
"rustc_version 0.3.3",
"rustc_version",
"serde",
"serde_json",
"vergen",
Expand Down Expand Up @@ -2276,7 +2276,7 @@ dependencies = [
"measureme 9.1.2",
"rand 0.8.4",
"rustc-workspace-hack",
"rustc_version 0.4.0",
"rustc_version",
"shell-escape",
"smallvec",
]
Expand Down Expand Up @@ -3808,7 +3808,6 @@ dependencies = [
name = "rustc_driver"
version = "0.0.0"
dependencies = [
"atty",
"libc",
"rustc_ast",
"rustc_ast_pretty",
Expand All @@ -3822,6 +3821,7 @@ dependencies = [
"rustc_hir_pretty",
"rustc_interface",
"rustc_lint",
"rustc_log",
"rustc_metadata",
"rustc_middle",
"rustc_parse",
Expand All @@ -3833,8 +3833,6 @@ dependencies = [
"rustc_target",
"rustc_typeck",
"tracing",
"tracing-subscriber",
"tracing-tree",
"winapi",
]

Expand Down Expand Up @@ -4077,6 +4075,17 @@ dependencies = [
"libc",
]

[[package]]
name = "rustc_log"
version = "0.0.0"
dependencies = [
"atty",
"rustc_span",
"tracing",
"tracing-subscriber",
"tracing-tree",
]

[[package]]
name = "rustc_macros"
version = "0.1.0"
Expand Down Expand Up @@ -4577,15 +4586,6 @@ dependencies = [
"tracing",
]

[[package]]
name = "rustc_version"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0dfe2087c51c460008730de8b57e6a320782fbfb312e1f4d520e6c6fae155ee"
dependencies = [
"semver 0.11.0",
]

[[package]]
name = "rustc_version"
version = "0.4.0"
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ crate-type = ["dylib"]

[dependencies]
libc = "0.2"
atty = "0.2"
tracing = { version = "0.1.28" }
tracing-subscriber = { version = "0.3.3", default-features = false, features = ["fmt", "env-filter", "smallvec", "parking_lot", "ansi"] }
tracing-tree = "0.2.0"
rustc_log = { path = "../rustc_log" }
rustc_middle = { path = "../rustc_middle" }
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
rustc_target = { path = "../rustc_target" }
Expand Down Expand Up @@ -40,4 +38,4 @@ winapi = { version = "0.3", features = ["consoleapi", "debugapi", "processenv"]

[features]
llvm = ['rustc_interface/llvm']
max_level_info = ['tracing/max_level_info']
max_level_info = ['rustc_log/max_level_info']
57 changes: 7 additions & 50 deletions compiler/rustc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use rustc_feature::find_gated_cfg;
use rustc_interface::util::{self, collect_crate_types, get_codegen_backend};
use rustc_interface::{interface, Queries};
use rustc_lint::LintStore;
use rustc_log::stdout_isatty;
use rustc_metadata::locator;
use rustc_save_analysis as save;
use rustc_save_analysis::DumpHandler;
Expand Down Expand Up @@ -514,14 +515,6 @@ impl Compilation {
#[derive(Copy, Clone)]
pub struct RustcDefaultCalls;

fn stdout_isatty() -> bool {
atty::is(atty::Stream::Stdout)
}

fn stderr_isatty() -> bool {
atty::is(atty::Stream::Stderr)
}

fn handle_explain(registry: Registry, code: &str, output: ErrorOutputType) {
let upper_cased_code = code.to_ascii_uppercase();
let normalised = if upper_cased_code.starts_with('E') {
Expand Down Expand Up @@ -1254,54 +1247,18 @@ pub fn install_ice_hook() {
/// This allows tools to enable rust logging without having to magically match rustc's
/// tracing crate version.
pub fn init_rustc_env_logger() {
init_env_logger("RUSTC_LOG")
if let Err(error) = rustc_log::init_rustc_env_logger() {
early_error(ErrorOutputType::default(), &error.to_string());
}
}

/// This allows tools to enable rust logging without having to magically match rustc's
/// tracing crate version. In contrast to `init_rustc_env_logger` it allows you to choose an env var
/// other than `RUSTC_LOG`.
pub fn init_env_logger(env: &str) {
use tracing_subscriber::{
filter::{self, EnvFilter, LevelFilter},
layer::SubscriberExt,
};

let filter = match std::env::var(env) {
Ok(env) => EnvFilter::new(env),
_ => EnvFilter::default().add_directive(filter::Directive::from(LevelFilter::WARN)),
};

let color_logs = match std::env::var(String::from(env) + "_COLOR") {
Ok(value) => match value.as_ref() {
"always" => true,
"never" => false,
"auto" => stderr_isatty(),
_ => early_error(
ErrorOutputType::default(),
&format!(
"invalid log color value '{}': expected one of always, never, or auto",
value
),
),
},
Err(std::env::VarError::NotPresent) => stderr_isatty(),
Err(std::env::VarError::NotUnicode(_value)) => early_error(
ErrorOutputType::default(),
"non-Unicode log color value: expected one of always, never, or auto",
),
};

let layer = tracing_tree::HierarchicalLayer::default()
.with_writer(io::stderr)
.with_indent_lines(true)
.with_ansi(color_logs)
.with_targets(true)
.with_indent_amount(2);
#[cfg(parallel_compiler)]
let layer = layer.with_thread_ids(true).with_thread_names(true);

let subscriber = tracing_subscriber::Registry::default().with(filter).with(layer);
tracing::subscriber::set_global_default(subscriber).unwrap();
if let Err(error) = rustc_log::init_env_logger(env) {
early_error(ErrorOutputType::default(), &error.to_string());
}
}

#[cfg(all(unix, any(target_env = "gnu", target_os = "macos")))]
Expand Down
16 changes: 16 additions & 0 deletions compiler/rustc_log/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "rustc_log"
version = "0.0.0"
edition = "2021"

[dependencies]
atty = "0.2"
tracing = "0.1.28"
tracing-subscriber = { version = "0.3.3", default-features = false, features = ["fmt", "env-filter", "smallvec", "parking_lot", "ansi"] }
tracing-tree = "0.2.0"

[dev-dependencies]
rustc_span = { path = "../rustc_span" }

[features]
max_level_info = ['tracing/max_level_info']
115 changes: 115 additions & 0 deletions compiler/rustc_log/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//! This crate allows tools to enable rust logging without having to magically
//! match rustc's tracing crate version.
//!
//! For example if someone is working on rustc_ast and wants to write some
//! minimal code against it to run in a debugger, with access to the `debug!`
//! logs emitted by rustc_ast, that can be done by writing:
//!
//! ```toml
//! [dependencies]
//! rustc_ast = { path = "../rust/compiler/rustc_ast" }
//! rustc_log = { path = "../rust/compiler/rustc_log" }
//! rustc_span = { path = "../rust/compiler/rustc_span" }
//! ```
//!
//! ```
//! fn main() {
//! rustc_log::init_rustc_env_logger().unwrap();
//!
//! let edition = rustc_span::edition::Edition::Edition2021;
//! rustc_span::create_session_globals_then(edition, || {
//! /* ... */
//! });
//! }
//! ```
//!
//! Now `RUSTC_LOG=debug cargo run` will run your minimal main.rs and show
//! rustc's debug logging. In a workflow like this, one might also add
//! `std::env::set_var("RUSTC_LOG", "debug")` to the top of main so that `cargo
//! run` by itself is sufficient to get logs.
//!
//! The reason rustc_log is a tiny separate crate, as opposed to exposing the
//! same things in rustc_driver only, is to enable the above workflow. If you
//! had to depend on rustc_driver in order to turn on rustc's debug logs, that's
//! an enormously bigger dependency tree; every change you make to rustc_ast (or
//! whichever piece of the compiler you are interested in) would involve
//! rebuilding all the rest of rustc up to rustc_driver in order to run your
//! main.rs. Whereas by depending only on rustc_log and the few crates you are
//! debugging, you can make changes inside those crates and quickly run main.rs
//! to read the debug logs.

use std::env::{self, VarError};
use std::fmt::{self, Display};
use std::io;
use tracing_subscriber::filter::{Directive, EnvFilter, LevelFilter};
use tracing_subscriber::layer::SubscriberExt;

pub fn init_rustc_env_logger() -> Result<(), Error> {
init_env_logger("RUSTC_LOG")
}

/// In contrast to `init_rustc_env_logger` this allows you to choose an env var
/// other than `RUSTC_LOG`.
pub fn init_env_logger(env: &str) -> Result<(), Error> {
let filter = match env::var(env) {
Ok(env) => EnvFilter::new(env),
_ => EnvFilter::default().add_directive(Directive::from(LevelFilter::WARN)),
};

let color_logs = match env::var(String::from(env) + "_COLOR") {
Ok(value) => match value.as_ref() {
"always" => true,
"never" => false,
"auto" => stderr_isatty(),
_ => return Err(Error::InvalidColorValue(value)),
},
Err(VarError::NotPresent) => stderr_isatty(),
Err(VarError::NotUnicode(_value)) => return Err(Error::NonUnicodeColorValue),
};

let layer = tracing_tree::HierarchicalLayer::default()
.with_writer(io::stderr)
.with_indent_lines(true)
.with_ansi(color_logs)
.with_targets(true)
.with_indent_amount(2);
#[cfg(parallel_compiler)]
let layer = layer.with_thread_ids(true).with_thread_names(true);

let subscriber = tracing_subscriber::Registry::default().with(filter).with(layer);
tracing::subscriber::set_global_default(subscriber).unwrap();

Ok(())
}

pub fn stdout_isatty() -> bool {
atty::is(atty::Stream::Stdout)
}

pub fn stderr_isatty() -> bool {
atty::is(atty::Stream::Stderr)
}

#[derive(Debug)]
pub enum Error {
InvalidColorValue(String),
NonUnicodeColorValue,
}

impl std::error::Error for Error {}

impl Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::InvalidColorValue(value) => write!(
formatter,
"invalid log color value '{}': expected one of always, never, or auto",
value,
),
Error::NonUnicodeColorValue => write!(
formatter,
"non-Unicode log color value: expected one of always, never, or auto",
),
}
}
}
23 changes: 21 additions & 2 deletions library/core/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,11 +570,30 @@ impl Display for Arguments<'_> {
/// There are a number of helper methods on the [`Formatter`] struct to help you with manual
/// implementations, such as [`debug_struct`].
///
/// [`debug_struct`]: Formatter::debug_struct
///
/// Types that do not wish to use the standard suite of debug representations
/// provided by the `Formatter` trait (`debug_struct`, `debug_tuple`,
/// `debut_list`, `debug_set`, `debug_map`) can do something totally custom by
/// manually writing an arbitrary representation to the `Formatter`.
///
/// ```
/// # use std::fmt;
/// # struct Point {
/// # x: i32,
/// # y: i32,
/// # }
/// #
/// impl fmt::Debug for Point {
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(f, "Point [{} {}]", self.x, self.y)
/// }
/// }
/// ```
///
/// `Debug` implementations using either `derive` or the debug builder API
/// on [`Formatter`] support pretty-printing using the alternate flag: `{:#?}`.
///
/// [`debug_struct`]: Formatter::debug_struct
///
/// Pretty-printing with `#?`:
///
/// ```
Expand Down
Loading

0 comments on commit 181e915

Please sign in to comment.