Convenient debug printing

ezneh petitv.isat at gmail.com
Sat Feb 2 00:33:55 UTC 2019


On Saturday, 2 February 2019 at 00:25:02 UTC, Per Nordlöw wrote:
> On Saturday, 2 February 2019 at 00:06:58 UTC, Rubn wrote:
>> void dbg(alias a)(string file = __FILE__, int line = __LINE__) 
>> {
>> 	writeln( file, "(", line, "): ", __traits(identifier, a), " = 
>> ", a );
>> }
>
> Nice!
>
> I wasn't aware of `__traits(identifier, a)`. What role does it 
> play here?
>
> Further, can we make this variadic with respect to `alias a`?

I got this working without using the alias a, but working with 
variadic:

```
template dbg(args...)
{
	alias dbgImpl!(args).print dbg;
}

template dbgImpl(args...)
{
	void print(string file = __FILE__, uint line = __LINE__, string 
fun = __FUNCTION__)
	{
		static foreach (int i, a; args)
		{
			debug stderr.writefln("[%s:%s (%s)] %s = %s", file, line, fun,
					__traits(identifier, args[i]), args[i]);
		}
	}
}

void main()
{
	int i;
	float f = 3.14;
	string s = "some string";

	dbg!(i, f, s);
}
```
prints:
[dbg.d:28 (dbg.main)] i = 0
[dbg.d:28 (dbg.main)] f = 3.14
[dbg.d:28 (dbg.main)] s = some string


More information about the Digitalmars-d mailing list