Idiom for debug printf?

Max Samukha samukha at voliacable.com.removethis
Mon Oct 1 23:53:58 PDT 2007


On Tue, 02 Oct 2007 09:21:36 +0300, Max Samukha
<samukha at voliacable.com.removethis> wrote:

>On Mon, 01 Oct 2007 20:29:45 +0900, Bill Baxter
><dnewsgroup at billbaxter.com> wrote:
>
>>BCS wrote:
>>> 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");
>>
>>Well, I'm not quite so macho, so I'll just stick with the void 
>>debugfln(...){}, and say my prayers to the deities of optimization.
>>
>>--bb
>You could use templates:
>
>import tango.io.Stdout;
>
>void debugfln(A...)(A a)
>{
>    version (Verbose) Stdout.format(a).newline;
>}
>
>void main()
>{
>    debugfln("1: {}, 2: {}", 1, 2);
>}

Ignore. That would only work if the compiler optimized out the empty
function. It's a rephrasing of Frank's solution.


More information about the Digitalmars-d-learn mailing list