Simple implementation of __FUNCTION

Regan Heath regan at netmail.co.nz
Fri Nov 2 11:08:32 PDT 2012


On Fri, 02 Nov 2012 18:06:19 -0000, Alex Rønne Petersen <alex at lycus.org>  
wrote:

> 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.)

+1 :)

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d mailing list