-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Extract init_env_logger to crate #92545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "rustc_log" | ||
version = "0.0.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
doctest = false | ||
|
||
[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" | ||
|
||
[features] | ||
max_level_info = ['tracing/max_level_info'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//! This crate allows tools to enable rust logging without having to magically | ||
//! match rustc's tracing crate version. | ||
|
||
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", | ||
), | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.