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

subscriber: add ability to disable ANSI without crate feature #2532

Merged
merged 2 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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.
daxpedda marked this conversation as resolved.
Show resolved Hide resolved
#[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";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message describes what happened, rather than why it happened, which might be more helpful for the user. How about rephrasing it as something like this:

Suggested change
"tracing-subscriber: enabled ANSI terminal colors without the `ansi` crate feature";
"tracing-subscriber: the 'ansi' crate feature is required to enable ANSI terminal colors";

Copy link
Contributor Author

@daxpedda daxpedda Apr 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's way better indeed.

I found that in other parts of the documentation in tracing are using ` instead of ' with crate features, so I used it here too. WDYT?

#[cfg(debug_assertions)]
panic!("{}", ERROR);
#[cfg(not(debug_assertions))]
eprintln!("{}", ERROR);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope this is appropriate.
Should I add some documentation on with_ansi() about the warning? I didn't feel it's needed really.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should document that enabling ANSI formatting requires the feature flag. Now that the method itself is no longer feature gated, there's no obvious way to determine from the documentation that the feature flag is necessary.


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.
daxpedda marked this conversation as resolved.
Show resolved Hide resolved
#[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