www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Idiom for debug printf?

reply Bill Baxter <dnewsgroup billbaxter.com> writes:
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
Sep 30 2007
next sibling parent 0ffh <spam frankhirsch.net> writes:
Bill Baxter wrote:
 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).
No idea what it /should/ be done like, but in the else case I use "void debugfln(...) {}" and rely on compiler optimisation... :) Regards, Frank
Sep 30 2007
prev sibling parent reply BCS <ao pathlink.com> writes:
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); }
Sep 30 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
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
Oct 01 2007
next sibling parent reply BCS <ao pathlink.com> writes:
Reply to Bill,

 BCS wrote:
 
 debug(DebugIdent) writef(__FILE__":("~itoa!(__LINE__)~"): message\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
I would need to use my form regardless (with conditional debugs). Otherwise I'd be completely inundated with logging. (ever try reading though 400k loins of log? ;) I have a tendency to do printf debugging and then never remove the printfs (I just turn them off)
Oct 01 2007
parent 0ffh <spam frankhirsch.net> writes:
BCS wrote:
 Reply to Bill,
 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.
[...] I have a tendency to do printf debugging and then never remove the printfs (I just turn them off)
You're all marked down for 99 sympathy brownie points! :) Regards, Frank
Oct 01 2007
prev sibling parent reply Max Samukha <samukha voliacable.com.removethis> writes:
On Mon, 01 Oct 2007 20:29:45 +0900, Bill Baxter
<dnewsgroup 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); }
Oct 01 2007
next sibling parent Max Samukha <samukha voliacable.com.removethis> writes:
On Tue, 02 Oct 2007 09:21:36 +0300, Max Samukha
<samukha voliacable.com.removethis> wrote:

On Mon, 01 Oct 2007 20:29:45 +0900, Bill Baxter
<dnewsgroup 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.
Oct 01 2007
prev sibling parent "Kris" <foo bar.com> writes:
Better to use the recent Trace.format() instead of Stdout, per the post in 
"announce". Alternatively, consider using tango.util.log instead?

- Kris

"Max Samukha" <samukha voliacable.com.removethis> wrote in message 
news:ldo3g3pbi1tucigvu5fd8reisdh1nd2mvu 4ax.com...
 On Mon, 01 Oct 2007 20:29:45 +0900, Bill Baxter
 <dnewsgroup 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); }
Oct 02 2007