Skip to content

Use of std::flush in std::cerr #935

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

Closed
zixin96 opened this issue Sep 30, 2021 · 3 comments
Closed

Use of std::flush in std::cerr #935

zixin96 opened this issue Sep 30, 2021 · 3 comments

Comments

@zixin96
Copy link

zixin96 commented Sep 30, 2021

The book: Ray Tracing in One Weekend
Specific location: 2.3. Adding a Progress Indicator, Listing 3

Hello everyone! I recently started to learn about the usage of std::flush and found that our code used
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;.

However, since std::cerr is not buffered like std::cout, wouldn't this make our std::flush redundant? If not, what is the purpose of std::flush here?

@hollasch
Copy link
Collaborator

Thank you! I didn't realize that std::cerr is inherently unbuffered. Given that, we have two solutions:

std::cerr << "\rScanlines remaining: " << j << ' ';

and

std::clog << "\rScanlines remaining: " << j << ' ' << std::flush;.

I'd never heard of std::clog until now, but it actually looks like a more accurate channel for this particular content (that is, logging the current scanline in progress). Given that, I lean toward keeping the flush and using std::clog instead. Thoughts?

@zixin96
Copy link
Author

zixin96 commented Oct 1, 2021

Thanks for the quick reply. I agree with you on using std::clog. It makes it clear that we want to log our progress.

@hollasch
Copy link
Collaborator

Fixed in #936 and #942.

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

2 participants