phobos / tango / ares

torhu fake at address.dude
Sat Feb 10 22:30:46 PST 2007


Frits van Bommel wrote:
> On a related note, one of the things that bothers /me/ is that no flush 
> is performed at the end of the program. That causes some or all of the 
> output to be missing if you don't explicitly flush after the last output.
> I'd suggest adding the following to tango.io.Console:
> ---
> static ~this ()
> {
>          Cout.flush();
>          Cerr.flush();
> }
> ---
> 
> That would fix it, I think.

I considered this, but I'll try to explain why I didn't suggest it. 
Basically, it hides that fact that you have forgotten to flush.

Pretend that this example is a big piece of code, that sometimes 
crashes.  To debug, you print some numbers, and where the numbers stop 
should be were the program crashes.

---
import tango.io.Stdout;
import tango.stdc.stdlib;
import tango.stdc.time;

void main()
{
	// some code here

	Stdout("1");

	// silly example of 'sometimes crash'
	if (time(null) & 1)
		free(cast(void*)1);
	
	Stdout("2");	

	// more code here, etc

	Stdout("3");
}
---

The problem is that with auto flush at program end, it will print '123' 
when it doesn't crash, and nothing at all when it does crash.  So the 
flushing happens at the 'wrong' time, unless you're aware of the inner 
workings of Tango.  A crash, exit by calling exit() or abort(), or an 
uncaught exception will cause the auto flush not to happen.  Which seems 
a bit inconsistent.

How realistic or valid this concern is, I'm not sure.  But it's at least 
it made me not suggest auto flushing.

By the way, is there a particular reason why Cerr is buffered?


More information about the Digitalmars-d-learn mailing list