Skip to content

Commit

Permalink
subscriber: add ability to disable ANSI without crate feature
Browse files Browse the repository at this point in the history
Currently it is not possible to disable ANSI in `fmt::Subscriber`
without enabling the `ansi` crate feature. This makes it difficult for
users to implement interoperable settings that are controllable with
crate features without having to pull in the dependencies `ansi` does.

This removes the crate feature requirement on `fmt::Subscriber::with_ansi()`, but will print a warning if trying to enable ANSI terminal colors without the crate feature.
  • Loading branch information
daxpedda committed Apr 14, 2023
1 parent a351b97 commit 2ebcd2b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 10 additions & 2 deletions tracing-subscriber/src/fmt/fmt_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,17 @@ impl<C, N, E, W> Subscriber<C, N, E, W> {
}

/// Enable ANSI terminal colors for formatted output.
#[cfg(feature = "ansi")]
#[cfg_attr(docsrs, doc(cfg(feature = "ansi")))]
pub fn with_ansi(self, ansi: bool) -> Self {
#[cfg(not(feature = "ansi"))]
if ansi {
const ERROR: &str =
"tracing-subscriber: enabled ANSI terminal colors without the `ansi` crate feature";
#[cfg(debug_assertions)]
panic!("{}", ERROR);
#[cfg(not(debug_assertions))]
eprintln!("{}", ERROR);
}

Subscriber {
is_ansi: ansi,
..self
Expand Down
2 changes: 0 additions & 2 deletions tracing-subscriber/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,6 @@ where
}

/// Enable ANSI terminal colors for formatted output.
#[cfg(feature = "ansi")]
#[cfg_attr(docsrs, doc(cfg(feature = "ansi")))]
pub fn with_ansi(self, ansi: bool) -> CollectorBuilder<N, format::Format<L, T>, F, W> {
CollectorBuilder {
inner: self.inner.with_ansi(ansi),
Expand Down

0 comments on commit 2ebcd2b

Please sign in to comment.