Skip to content
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

Respect user settings for tracing coloring #9733

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,24 @@ pub enum ColorChoice {
Never,
}

impl ColorChoice {
/// Combine self (higher priority) with an [`anstream::ColorChoice`] (lower priority).
///
/// This method allows prioritizing the user choice, while using the inferred choice for a
/// stream as default.
#[must_use]
pub fn and_colorchoice(self, next: anstream::ColorChoice) -> Self {
match self {
Self::Auto => match next {
anstream::ColorChoice::Auto => Self::Auto,
anstream::ColorChoice::Always | anstream::ColorChoice::AlwaysAnsi => Self::Always,
anstream::ColorChoice::Never => Self::Never,
},
Self::Always | Self::Never => self,
}
}
}

impl From<ColorChoice> for anstream::ColorChoice {
fn from(value: ColorChoice) -> Self {
match value {
Expand Down
1 change: 1 addition & 0 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
2.. => logging::Level::ExtraVerbose,
},
duration_layer,
globals.color,
)?;

// Configure the `Printer`, which controls user-facing output in the CLI.
Expand Down
17 changes: 10 additions & 7 deletions crates/uv/src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::fmt;
use std::str::FromStr;

use anstream::ColorChoice;
use anyhow::Context;
use jiff::Timestamp;
use owo_colors::OwoColorize;
Expand All @@ -19,6 +18,8 @@ use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{EnvFilter, Layer, Registry};
use tracing_tree::time::Uptime;
use tracing_tree::HierarchicalLayer;

use uv_cli::ColorChoice;
#[cfg(feature = "tracing-durations-export")]
use uv_static::EnvVars;

Expand Down Expand Up @@ -117,6 +118,7 @@ where
pub(crate) fn setup_logging(
level: Level,
durations: impl Layer<Registry> + Send + Sync,
color: ColorChoice,
) -> anyhow::Result<()> {
let default_directive = match level {
Level::Default => {
Expand All @@ -140,6 +142,11 @@ pub(crate) fn setup_logging(
.from_env()
.context("Invalid RUST_LOG directives")?;

let ansi = match color.and_colorchoice(anstream::Stderr::choice(&std::io::stderr())) {
ColorChoice::Always => true,
ColorChoice::Never => false,
ColorChoice::Auto => unreachable!("anstream can't return auto as choice"),
};
match level {
Level::Default | Level::Verbose => {
// Regardless of the tracing level, show messages without any adornment.
Expand All @@ -148,12 +155,7 @@ pub(crate) fn setup_logging(
display_level: true,
show_spans: false,
};
let ansi = match anstream::Stderr::choice(&std::io::stderr()) {
ColorChoice::Always | ColorChoice::AlwaysAnsi => true,
ColorChoice::Never => false,
// We just asked anstream for a choice, that can't be auto
ColorChoice::Auto => unreachable!(),
};

tracing_subscriber::registry()
.with(durations_layer)
.with(
Expand All @@ -174,6 +176,7 @@ pub(crate) fn setup_logging(
.with_targets(true)
.with_timer(Uptime::default())
.with_writer(std::io::stderr)
.with_ansi(ansi)
.with_filter(filter),
)
.init();
Expand Down
Loading