Idiom for debug printf?

BCS ao at pathlink.com
Sun Sep 30 20:03:08 PDT 2007


Reply to Bill,

> Is there a common idiom for a version-specific printf? Something like:
> 
> version(Verbose) {
> alias writefln debugfln;
> }
> else {
> ... what goes here? ...
> }
> The idea is that it should work just like a call to writefln in the
> version=Verbose case, but it should be a complete no-op otherwise
> (meaning it also shouldn't evaluate its arguments).
> 
> I thought there was some way to do this with 'lazy' or somesuch, but I
> don't recall the exact syntax.
> 
> Thanks,
> --bb

I don't bother with switching the function out, I just put each debug line 
in it's own debug statement.

my idium of choice is this:

debug(DebugIdent) writef(__FILE__":("~itoa!(__LINE__)~"): the message to 
print\n");

this uses this little gem:

/**
	Don Clugston's decimalDigit.
	see http://trac.dsource.org/projects/ddl/browser/trunk/meta/conv.d
*/
template decimalDigit(int n)
{
	const char[] decimalDigit = "0123456789"[n..n+1];
}

/**
	Don Clugston's itoa.
	see http://trac.dsource.org/projects/ddl/browser/trunk/meta/conv.d
*/
template itoa(long n)
{
	static if (n < 0)
		const char[] itoa = "-" ~ itoa!(-n);
	else static if (n < 10)
		const char[] itoa = decimalDigit!(n);
	else
		const char[] itoa = itoa!(n/10L) ~ decimalDigit!(n%10L);
}




More information about the Digitalmars-d-learn mailing list