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

ANSI broken on Windows 10 #445

Open
andrewbanchich opened this issue Nov 21, 2019 · 9 comments
Open

ANSI broken on Windows 10 #445

andrewbanchich opened this issue Nov 21, 2019 · 9 comments
Labels
crate/subscriber Related to the `tracing-subscriber` crate

Comments

@andrewbanchich
Copy link

ansi_term requires Windows 10 programs to explicitly enable ANSI support.

Without doing this, using cargo run or cargo run --release display tracing logs correctly, but when running the binary installed with cargo install --path . it results in broken ANSI strings.

Is this something you think tracing should fix out of the box, should just be communicated in the docs?

@hawkw
Copy link
Member

hawkw commented Nov 22, 2019

@andrewbanchich hmm. We're currently using the ansi_term crate for ANSI colors. My initial thought is that it should handle this behavior — maybe the best solution is a patch upstream?

@andrewbanchich
Copy link
Author

@hawkw It does handle it (see the link I posted) but just not by default.

@hawkw
Copy link
Member

hawkw commented Nov 22, 2019

Ah, sorry, I misinterpreted your comment (and didn't click the link), my bad. I think we probably want to do that when constructing the subscriber if the target OS is windows, then?

@andrewbanchich
Copy link
Author

That makes sense to me. I'll give this a shot and have a pull request soon.

@hawkw hawkw added the crate/subscriber Related to the `tracing-subscriber` crate label Nov 26, 2019
@andrewbanchich
Copy link
Author

@hawkw Any thoughts on me swapping out ansi_term for colored? colored has full Windows support and doesn't look like you need to call any special functions conditionally. Plus, it looks nicer overall.

@hawkw
Copy link
Member

hawkw commented Feb 5, 2020

I'm fine with switching to colored. Currently, we do use ansi-term's Style type to isolate the feature-flagging of ANSI support:

fn bold(&self) -> Style {
#[cfg(feature = "ansi")]
{
if self.ansi {
return Style::new().bold();
}
}
Style::new()
}

with a no-op Style type when ansi-term is disabled:
#[cfg(not(feature = "ansi"))]
struct Style;
#[cfg(not(feature = "ansi"))]
impl Style {
fn new() -> Self {
Style
}
fn paint(&self, d: impl fmt::Display) -> impl fmt::Display {
d
}
}

We can probably do a similar thing with colored's Colorize trait?

@hawkw
Copy link
Member

hawkw commented Feb 5, 2020

hmm, upon taking a closer look, it looks like colored's methods always return a ColoredString struct which appears to always allocate a String, even when the input is borrowed. This also means that formatting Debug/Display happens eagerly, writing into the string, rather than lazily (allowing us to write directly into the per-thread buffer that tracing-subscriber::fmt creates for each thread that logs).

A big part of how we've optimized the performance of logging events is by avoiding additional string allocations, and writing directly to the buffer, which is reused without deallocating — in many cases, once the buffer is "warmed up" (i.e. it has previously allocated enough length to hold the line we are currently writing), logging an event should be allocation-free. It seems like switching to colored breaks this property, which might have a noticeable performance impact...

@andrewhickman
Copy link

andrewhickman commented Feb 20, 2021

termcolor has a buffer type which supports writing colored text to a buffer and clearing afterwards to preserve the allocations. Maybe that would be suitable here?

@Chaoses-Ib
Copy link

Chaoses-Ib commented Jun 26, 2024

termcolor also supports Windows 7 and 8 according to rust-lang/rust#55769, while ansi-term only works on Windows 10+.

Edit: anstream also has legacy Windows support, and is possibly a better choice than termcolor: rust-lang/cargo#12627

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crate/subscriber Related to the `tracing-subscriber` crate
Projects
None yet
Development

No branches or pull requests

4 participants