Skip to content

Commit 6152d15

Browse files
committed
Extract init_env_logger to crate
1 parent 399ba6b commit 6152d15

File tree

5 files changed

+114
-57
lines changed

5 files changed

+114
-57
lines changed

Diff for: Cargo.lock

+11-3
Original file line numberDiff line numberDiff line change
@@ -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,16 @@ dependencies = [
40774075
"libc",
40784076
]
40794077

4078+
[[package]]
4079+
name = "rustc_log"
4080+
version = "0.0.0"
4081+
dependencies = [
4082+
"atty",
4083+
"tracing",
4084+
"tracing-subscriber",
4085+
"tracing-tree",
4086+
]
4087+
40804088
[[package]]
40814089
name = "rustc_macros"
40824090
version = "0.1.0"

Diff for: 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']

Diff for: 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")))]

Diff for: 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+
[lib]
7+
doctest = false
8+
9+
[dependencies]
10+
atty = "0.2"
11+
tracing = "0.1.28"
12+
tracing-subscriber = { version = "0.3.3", default-features = false, features = ["fmt", "env-filter", "smallvec", "parking_lot", "ansi"] }
13+
tracing-tree = "0.2.0"
14+
15+
[features]
16+
max_level_info = ['tracing/max_level_info']

Diff for: compiler/rustc_log/src/lib.rs

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//! This crate allows tools to enable rust logging without having to magically
2+
//! match rustc's tracing crate version.
3+
4+
use std::env::{self, VarError};
5+
use std::fmt::{self, Display};
6+
use std::io;
7+
use tracing_subscriber::filter::{Directive, EnvFilter, LevelFilter};
8+
use tracing_subscriber::layer::SubscriberExt;
9+
10+
pub fn init_rustc_env_logger() -> Result<(), Error> {
11+
init_env_logger("RUSTC_LOG")
12+
}
13+
14+
/// In contrast to `init_rustc_env_logger` this allows you to choose an env var
15+
/// other than `RUSTC_LOG`.
16+
pub fn init_env_logger(env: &str) -> Result<(), Error> {
17+
let filter = match env::var(env) {
18+
Ok(env) => EnvFilter::new(env),
19+
_ => EnvFilter::default().add_directive(Directive::from(LevelFilter::WARN)),
20+
};
21+
22+
let color_logs = match env::var(String::from(env) + "_COLOR") {
23+
Ok(value) => match value.as_ref() {
24+
"always" => true,
25+
"never" => false,
26+
"auto" => stderr_isatty(),
27+
_ => return Err(Error::InvalidColorValue(value)),
28+
},
29+
Err(VarError::NotPresent) => stderr_isatty(),
30+
Err(VarError::NotUnicode(_value)) => return Err(Error::NonUnicodeColorValue),
31+
};
32+
33+
let layer = tracing_tree::HierarchicalLayer::default()
34+
.with_writer(io::stderr)
35+
.with_indent_lines(true)
36+
.with_ansi(color_logs)
37+
.with_targets(true)
38+
.with_indent_amount(2);
39+
#[cfg(parallel_compiler)]
40+
let layer = layer.with_thread_ids(true).with_thread_names(true);
41+
42+
let subscriber = tracing_subscriber::Registry::default().with(filter).with(layer);
43+
tracing::subscriber::set_global_default(subscriber).unwrap();
44+
45+
Ok(())
46+
}
47+
48+
pub fn stdout_isatty() -> bool {
49+
atty::is(atty::Stream::Stdout)
50+
}
51+
52+
pub fn stderr_isatty() -> bool {
53+
atty::is(atty::Stream::Stderr)
54+
}
55+
56+
#[derive(Debug)]
57+
pub enum Error {
58+
InvalidColorValue(String),
59+
NonUnicodeColorValue,
60+
}
61+
62+
impl std::error::Error for Error {}
63+
64+
impl Display for Error {
65+
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
66+
match self {
67+
Error::InvalidColorValue(value) => write!(
68+
formatter,
69+
"invalid log color value '{}': expected one of always, never, or auto",
70+
value,
71+
),
72+
Error::NonUnicodeColorValue => write!(
73+
formatter,
74+
"non-Unicode log color value: expected one of always, never, or auto",
75+
),
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)