digitalmars.D - Trick for function trace printing
- Dicebot (55/57) Sep 10 2014 Noticing requests for addition of @trace attribute I decided to
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); } ========= Printstest.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 ;)
Sep 10 2014