Trick for function trace printing
Dicebot via Digitalmars-d
digitalmars-d at puremagic.com
Wed Sep 10 22:36:42 PDT 2014
Noticing requests for addition of @trace attribute I decided to
share one small snippet of mine:
=========
immutable trace =`
import std.stdio : writefln;
import std.string : format;
import std.traits : ParameterIdentifierTuple;
mixin(format(
q{enum args = ParameterIdentifierTuple!(%s);},
__FUNCTION__
));
import std.algorithm : map, joiner;
enum args_fmt = [args].map!(a => "%s").joiner(", ");
mixin(format(
q{writefln("> %s(%s)", %s);},
__FUNCTION__,
args_fmt,
[args].joiner(", ")
));
scope(exit)
{
mixin(format(
q{writefln("< %s(%s)", %s);},
__FUNCTION__,
args_fmt,
[args].joiner(", ")
));
}
`;
=========
Used like this:
=========
module test;
void foo(int a)
{
mixin(trace);
bar(0.43, null);
}
int bar(double x, int* ptr)
{
mixin(trace);
return 444;
}
void main()
{
foo(42);
}
=========
Prints
> test.foo(42)
> test.bar(0.43, null)
< test.bar(0.43, null)
< test.foo(42)
http://dpaste.dzfl.pl/3dd33973bc99
Is not optimized at all but gets job done for simple cases when I
usually want it.
Hope it can be useful for someone else ;)
More information about the Digitalmars-d
mailing list