Skip to content

Commit

Permalink
Respect user settings for tracing coloring (#9733)
Browse files Browse the repository at this point in the history
Previously, `-vvv --color never` would still emit ANSI sequences to
stderr.

Ref #9668 (comment)
  • Loading branch information
konstin authored Dec 9, 2024
1 parent b17902d commit 2f49a8e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
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

0 comments on commit 2f49a8e

Please sign in to comment.