We build complicated console-based tools that produce a lot of commentary on the console. These tools process a lot of data and so runs are expensive and we don’t like to do them more often than necessary, especially during testing where we already have to run them repeatedly. During such tests, we often log the result to a file to capture this commentary for inspection:
C:> [ourcommand] >log.txt
When the application runs to completion, this works great.
If the application crashes (illegal memory reference, etc.), Windows nastily chops off the log file before the tail end of the commentary so we can’t see what was occuring at the point of the crash. It also chops of the console output at the same place. So, when we get an truncated log file, we have to do it all again without the log file, to see the final answer. Windows doesn’t seem to buffer the console output if not logging.
Is there a way to tell Windows not to buffer the console output while logging? Our application does use the (Windows) C runtime system and awhile back we used “setvbuf”
as follows:
setvbuf(stdout, NULL, _IONBF, 0);
but to no avail.
You can flush the console’s buffer at any point:
This uses TextWriter.Flush().
If you need to make sure this always happens, it’d be fairly easy to write a method for console that worked like WriteLine but included a flush statement at the end.
Edit:
You didn’t specify a language… I’m going to write an example using C#.
In C#, you could easily make a static utility class that provided a method like:
Then just call:
The same approach should work in pretty much any other language, as well.
Edit 2:
Since you’re using C++, there are a couple of options other than setvbuf that may be of interest.
First, you should be able to turn off the buffering in cout by doing:
You also may need to set
sync_with_stdioif you’re mixing stdio (ie: printf) with iostreams.