Simple implementation of __FUNCTION

Rob T rob at ucora.com
Fri Nov 2 10:31:54 PDT 2012


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




More information about the Digitalmars-d mailing list