Thread overview
Debug output functions
Jul 03, 2012
Wouter Verhelst
Jul 03, 2012
nazriel
Jul 03, 2012
nazriel
Jul 03, 2012
Wouter Verhelst
Jul 03, 2012
Mike Parker
Jul 03, 2012
Wouter Verhelst
July 03, 2012
So, I wanted to create a number of functions that would call write(),
writef(), writefln(), or writeln() with whatever arguments they were
given, but only if the user had used a 'enable debugging' command-line
option (or some such).

What I first did was this:

module debugout;

int debuglevel;

void set_level(int level) {
	debuglevel = level;
}

auto tdebug(func)(int level, ...) {
	va_list va;
	va_start(va, level);
	if(debuglevel <= level) {
		return func(va);
	}
	va_end(va);
}

alias tdebug!(write) wdebug;
alias tdebug!(writef) wdebugf;
(... and so on...)

but that didn't work:

debugout.d(20): Error: template instance tdebug!(write) tdebug!(write) does not match template declaration tdebug(func)

I had a short look at http://dlang.org/variadic-function-templates.html which would seem to be a somewhat better way of implementing this, but I can't figure out how exactly I should go forward, then.

I do believe that something like the following would do what I want:

auto wdebug(level, ...) {
	va_list va;
	va_start(va, level);
	writefx(stdout, ???, va);
	va_end(va);
}

except the va_list stuff doesn't seem to be necessary, I don't know what to put in place of the "???", and I'd have to write this four times rather than being able to template it four times.

How should I do this instead?

-- 
The volume of a pizza of thickness a and radius z can be described by the following formula:

pi zz a
July 03, 2012
On Tuesday, 3 July 2012 at 03:15:02 UTC, Wouter Verhelst wrote:
>
> So, I wanted to create a number of functions that would call write(),
> writef(), writefln(), or writeln() with whatever arguments they were
> given, but only if the user had used a 'enable debugging' command-line
> option (or some such).
>
> What I first did was this:
>
> module debugout;
>
> int debuglevel;
>
> void set_level(int level) {
> 	debuglevel = level;
> }
>
> auto tdebug(func)(int level, ...) {
> 	va_list va;
> 	va_start(va, level);
> 	if(debuglevel <= level) {
> 		return func(va);
> 	}
> 	va_end(va);
> }
>
> alias tdebug!(write) wdebug;
> alias tdebug!(writef) wdebugf;
> (... and so on...)
>
> but that didn't work:
>
> debugout.d(20): Error: template instance tdebug!(write) tdebug!(write) does not match template declaration tdebug(func)
>
> I had a short look at http://dlang.org/variadic-function-templates.html
> which would seem to be a somewhat better way of implementing this, but I
> can't figure out how exactly I should go forward, then.
>
> I do believe that something like the following would do what I want:
>
> auto wdebug(level, ...) {
> 	va_list va;
> 	va_start(va, level);
> 	writefx(stdout, ???, va);
> 	va_end(va);
> }
>
> except the va_list stuff doesn't seem to be necessary, I don't know what
> to put in place of the "???", and I'd have to write this four times
> rather than being able to template it four times.
>
> How should I do this instead?

http://dpaste.dzfl.pl/3efd9c1b - try this.
July 03, 2012
On Tuesday, 3 July 2012 at 03:15:02 UTC, Wouter Verhelst wrote:
>
> So, I wanted to create a number of functions that would call write(),
> writef(), writefln(), or writeln() with whatever arguments they were
> given, but only if the user had used a 'enable debugging' command-line
> option (or some such).
>
> What I first did was this:
>
> module debugout;
>
> int debuglevel;
>
> void set_level(int level) {
> 	debuglevel = level;
> }
>
> auto tdebug(func)(int level, ...) {
> 	va_list va;
> 	va_start(va, level);
> 	if(debuglevel <= level) {
> 		return func(va);
> 	}
> 	va_end(va);
> }
>
> alias tdebug!(write) wdebug;
> alias tdebug!(writef) wdebugf;
> (... and so on...)
>
> but that didn't work:
>
> debugout.d(20): Error: template instance tdebug!(write) tdebug!(write) does not match template declaration tdebug(func)
>
> I had a short look at http://dlang.org/variadic-function-templates.html
> which would seem to be a somewhat better way of implementing this, but I
> can't figure out how exactly I should go forward, then.
>
> I do believe that something like the following would do what I want:
>
> auto wdebug(level, ...) {
> 	va_list va;
> 	va_start(va, level);
> 	writefx(stdout, ???, va);
> 	va_end(va);
> }
>
> except the va_list stuff doesn't seem to be necessary, I don't know what
> to put in place of the "???", and I'd have to write this four times
> rather than being able to template it four times.
>
> How should I do this instead?

Try this - http://dpaste.dzfl.pl/3efd9c1b
July 03, 2012
On 7/3/2012 12:12 PM, Wouter Verhelst wrote:
>
> So, I wanted to create a number of functions that would call write(),
> writef(), writefln(), or writeln() with whatever arguments they were
> given, but only if the user had used a 'enable debugging' command-line
> option (or some such).
>
> What I first did was this:
>
> module debugout;
>
> int debuglevel;


Are you aware of debug conditions?

http://dlang.org/version.html#debug

They can be set on the command line or in code at module level. In the former case, it's global. In the latter, only module scope.
July 03, 2012
Mike Parker <aldacron@gmail.com> writes:

> On 7/3/2012 12:12 PM, Wouter Verhelst wrote:
>>
>> So, I wanted to create a number of functions that would call write(),
>> writef(), writefln(), or writeln() with whatever arguments they were
>> given, but only if the user had used a 'enable debugging' command-line
>> option (or some such).
>>
>> What I first did was this:
>>
>> module debugout;
>>
>> int debuglevel;
>
>
> Are you aware of debug conditions?
>
> http://dlang.org/version.html#debug
>
> They can be set on the command line or in code at module level. In the former case, it's global. In the latter, only module scope.

Yes, but these are defined at compile time, if I understand things correctly. That's useful, but not what I'm after; the idea would be that a user of my application could use a '-v' option on the command line to get more output.

-- 
The volume of a pizza of thickness a and radius z can be described by the following formula:

pi zz a
July 03, 2012
"nazriel" <nazriel6969@gmail.com> writes:
> Try this - http://dpaste.dzfl.pl/3efd9c1b

Yes, that's exactly what I need. Thanks.

Looks like I was making it far too difficult for myself :-)

-- 
The volume of a pizza of thickness a and radius z can be described by the following formula:

pi zz a