On 02-11-2012 18:31, Rob T wrote:
Thanks to input from the D community, I've managed to implement a
reasonable way to log the name of a calling function. This is used for
basic execution monitoring and for automated logging of exception errors.

Here's what I did.

template __FUNCTION()
{
    const char[] __FUNCTION = "__traits(identifier, __traits(parent, {}))";
}

Example use in code:

throw new Exception( "Error: Function ", mixin(__FUNCTION!()) );

writefln( "File: %s, Func: %s, Line: %d", __FILE__,
mixin(__FUNCTION!()), __LINE__ );


The ONLY thing left that I would like to have, is ability to display the
function signature along with the name. The signature will be very
useful to show which version of an overloaded or templated function was
called.

If anyone can suggest imporvements, like how to get rid of need to
explicitly call mixin, and better yet a solution to get the function
signature, please post away. Thanks!

I have to mention that we need a real solution that can only be provided
through improved reflection support, eg __scope.function, __scope.line,
__scope.file, etc, or whatever the D community thinks will fit in best.

--rt



You should totally submit this for inclusion into std.traits in Phobos.

(Though, to follow naming conventions, it should be functionName and functionSignature or so.)

--
Alex Rønne Petersen
a...@lycus.org
http://lycus.org

Reply via email to