Stdout formatting is broken running with cargo install, but works with cargo run

I'm running a CLI program on Windows and noticed that the tracing logs display correctly with colored log levels when I runcargo run or cargo run --release:

Nov 20 11:59:57.923 DEBUG my_program::consumer: Connected to RabbitMQ!
Nov 20 11:59:57.929 DEBUG my_program::consumer: RabbitMQ successfully connected!

but when I run cargo install --path . --force and then my_program I get:

?[2mNov 20 11:59:03.767 ?[0m?[34mDEBUG?[0m my_program::consumer: Connected to RabbitMQ!
?[2mNov 20 11:59:03.768 ?[0m?[34mDEBUG?[0m my_program::consumer: RabbitMQ successfully connected!

Any ideas what could be causing this? I thought at first it was something Windows related but if it were, I would think it would be consistently broken (even when running cargo run).

I don't run Windows, but those look like terminal control codes for coloured output. By default cargo run removes colours if the terminal doesn't support them (this can be controlled with the --color option, see the documentation). I expect it has nothing to do with cargo install specifically and you would get the same output from running the program directly without installing.

The best way to handle this is probably to see if your logging library has a way of deactivating coloured output, and use a terminal control library to check if you can output colours (or just use uncoloured output all the time).

I'll look into that, but the strange thing is, the library (tracing) DOES color output for cargo run or cargo run --release. If cargo run stripped colors, why is it displaying them by default?

Yeah, looking into it further, it seems the --color flag just controls the compiler colour output. What happens if you run target/debug/my_program without involving cargo?

It does not work if I run /target/debug/my_program directly without Cargo

Do you mean it doesn't run or that it produces the messed up output? It should be able to run on its own. The version running on its own should function similarly to the version installed by cargo install. However, as I said, I'm not running Windows, so I can't say for certain how it works.

It does run. I just mean the output is messed up. I think I found the issue in one of tracing dependencies though: https://github.com/ogham/rust-ansi-term#basic-usage

I'll ask tracing about that.

Thanks!

1 Like

That problem comes up a lot when trying to write coloured output to a terminal. Most libraries assume ANSI escape codes are the only way to emit colour, then when people run the program on Windows normal output ends up interleaved with escape code garbage.

If tracing wants correct color output they should switch to a crate like termcolor.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.