summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_log/src/lib.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:25:56 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:25:56 +0000
commit018c4950b9406055dec02ef0fb52f132e2bb1e2c (patch)
treea835ebdf2088ef88fa681f8fad45f09922c1ae9a /compiler/rustc_log/src/lib.rs
parentAdding debian version 1.75.0+dfsg1-5. (diff)
downloadrustc-018c4950b9406055dec02ef0fb52f132e2bb1e2c.tar.xz
rustc-018c4950b9406055dec02ef0fb52f132e2bb1e2c.zip
Merging upstream version 1.76.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_log/src/lib.rs')
-rw-r--r--compiler/rustc_log/src/lib.rs45
1 files changed, 34 insertions, 11 deletions
diff --git a/compiler/rustc_log/src/lib.rs b/compiler/rustc_log/src/lib.rs
index 0c9ec5565..e3464cb8a 100644
--- a/compiler/rustc_log/src/lib.rs
+++ b/compiler/rustc_log/src/lib.rs
@@ -14,7 +14,7 @@
//!
//! ```
//! fn main() {
-//! rustc_log::init_env_logger("LOG").unwrap();
+//! rustc_log::init_logger(rustc_log::LoggerConfig::from_env("LOG")).unwrap();
//!
//! let edition = rustc_span::edition::Edition::Edition2021;
//! rustc_span::create_session_globals_then(edition, || {
@@ -52,13 +52,36 @@ use tracing_subscriber::fmt::{
};
use tracing_subscriber::layer::SubscriberExt;
-pub fn init_env_logger(env: &str) -> Result<(), Error> {
- let filter = match env::var(env) {
+/// The values of all the environment variables that matter for configuring a logger.
+/// Errors are explicitly preserved so that we can share error handling.
+pub struct LoggerConfig {
+ pub filter: Result<String, VarError>,
+ pub color_logs: Result<String, VarError>,
+ pub verbose_entry_exit: Result<String, VarError>,
+ pub verbose_thread_ids: Result<String, VarError>,
+ pub backtrace: Result<String, VarError>,
+}
+
+impl LoggerConfig {
+ pub fn from_env(env: &str) -> Self {
+ LoggerConfig {
+ filter: env::var(env),
+ color_logs: env::var(format!("{env}_COLOR")),
+ verbose_entry_exit: env::var(format!("{env}_ENTRY_EXIT")),
+ verbose_thread_ids: env::var(format!("{env}_THREAD_IDS")),
+ backtrace: env::var(format!("{env}_BACKTRACE")),
+ }
+ }
+}
+
+/// Initialize the logger with the given values for the filter, coloring, and other options env variables.
+pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
+ let filter = match cfg.filter {
Ok(env) => EnvFilter::new(env),
_ => EnvFilter::default().add_directive(Directive::from(LevelFilter::WARN)),
};
- let color_logs = match env::var(String::from(env) + "_COLOR") {
+ let color_logs = match cfg.color_logs {
Ok(value) => match value.as_ref() {
"always" => true,
"never" => false,
@@ -69,14 +92,14 @@ pub fn init_env_logger(env: &str) -> Result<(), Error> {
Err(VarError::NotUnicode(_value)) => return Err(Error::NonUnicodeColorValue),
};
- let verbose_entry_exit = match env::var_os(String::from(env) + "_ENTRY_EXIT") {
- None => false,
- Some(v) => &v != "0",
+ let verbose_entry_exit = match cfg.verbose_entry_exit {
+ Ok(v) => &v != "0",
+ Err(_) => false,
};
- let verbose_thread_ids = match env::var_os(String::from(env) + "_THREAD_IDS") {
- None => false,
- Some(v) => &v == "1",
+ let verbose_thread_ids = match cfg.verbose_thread_ids {
+ Ok(v) => &v == "1",
+ Err(_) => false,
};
let layer = tracing_tree::HierarchicalLayer::default()
@@ -91,7 +114,7 @@ pub fn init_env_logger(env: &str) -> Result<(), Error> {
.with_thread_names(verbose_thread_ids);
let subscriber = tracing_subscriber::Registry::default().with(filter).with(layer);
- match env::var(format!("{env}_BACKTRACE")) {
+ match cfg.backtrace {
Ok(str) => {
let fmt_layer = tracing_subscriber::fmt::layer()
.with_writer(io::stderr)