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

How to print progressbar when redirecting output to file? #530

Open
c19 opened this issue Apr 7, 2023 · 6 comments
Open

How to print progressbar when redirecting output to file? #530

c19 opened this issue Apr 7, 2023 · 6 comments

Comments

@c19
Copy link

c19 commented Apr 7, 2023

I'm using this to run my program: nohup ./target/release/main > main.log &.

But this cause all the progressbars are gone from the logfile. Any method to preserve it?

@chris-laplante
Copy link
Collaborator

I don't think there is anything built-in to indicatif to support this. You may however give unbuffer (https://linux.die.net/man/1/unbuffer) a try. That might do what you want.

@djc
Copy link
Member

djc commented May 9, 2023

indicatif more or less inherits this behavior from the console crate which it uses under the cover. An indicatif ProgressDrawTarget can be initialized using a caller-provided console::Term (but a cursory reading of the console API did not allow me to find an option to override their detection of whether the used file descriptor is a terminal).

@nyurik
Copy link

nyurik commented Nov 25, 2023

@djc I think most of the CLI apps are also used in non-interactive (headless?) mode, and could use some support from indicatif. Even without the auto-detection support, would it make sense to have a "linear log" formatter? It could print current status: 5% out of 100% style messages instead of using ansi control characters once per (second/minute/10min/...?). I have seen this behavior with aria2c, wget, and possibly others.

@djc
Copy link
Member

djc commented Dec 13, 2023

Well, if someone wants to propose a design for something like that I'd be willing to review it.

@spoutn1k
Copy link
Contributor

I believe in both this issue and #651 the root problem cause has not been explained.

Bar printing works by printing special characters to control the cursor position on a tty and overwrite previous output. The bar, in most cases, gets redrawn over and over and that gives the illusion of movement.

This is a special property of the virtual terminal you are using. When the output is redirected to a file, docker log, or anything other than a tty, those properties are gone. What you would see is a very long line of partial bars separated by \r characters. This is also true for colors, styling, and other terminal features.

This is not an indicatif issue. This is a consequence of the design of computer systems.

Indicatif seems to refresh 15 times a second. Even if you change the behaviour to write the bar line per line, if your process takes 1mn you have ~900 lines of just bar in your output, cluttering any kind of log or file you are producing. At this point you really need to ask yourself if a bar is needed.

You can do this check yourself and change the behaviour of your program to print differently when the output is not a terminal. @nyurik's suggestion is the best possible way to go about this in indicatif, but I wanted to make sure everybody is on the same page about why indicatif stops working sometimes when I pipe to a file.

@djc
Copy link
Member

djc commented Oct 7, 2024

@spoutn1k thanks for making the additional context explicit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants