Special Token __FUNCTION__

Nick Sabalausky a at a.a
Mon Sep 7 13:25:05 PDT 2009


"Nick Sabalausky" <a at a.a> wrote in message 
news:h83pgs$2f1p$1 at digitalmars.com...
> "Robert Jacques" <sandford at jhu.edu> wrote in message 
> news:op.uzwccwqu26stm6 at sandford.myhome.westell.com...
>> On Mon, 07 Sep 2009 12:41:46 -0400, Byron Heads 
>> <wyverex.cypher at gmail.com> wrote:
>>> Also is there a nice way to replace logging macros?  I often have a 
>>> logger
>>> that looks like
>>> logmsg( int level, uint line, char* func, char* file, char[] msg, ... );
>>>
>>> then I would write some macros like:
>>> #define LOGDEBUG( m, ... )  logmsg( LVL_DEBUG, __LINE__, __func__, 
>>> __FILE__, msg, __VA_ARGS__ )
>>>
>>> thus i would only have to do
>>> LOGDEBUG( "SOME STRING with formatting", formatting args.. )
>>>
>
> Templates to the rescue! :) From my "SemiTwist D Tools" ( 
> http://www.dsource.org/projects/semitwist ) :
>
> ---------------------------------------------------------
> template trace(char[] prefix="")
> {
> static if(prefix=="")
>  const char[] trace =
>   `Stdout.formatln("{}({}): trace", __FILE__, __LINE__);`;
> else
>  const char[] trace =
>   `Stdout.formatln("{}: {}({}): trace", `~prefix.stringof~`, __FILE__, 
> __LINE__);`;
> //pragma(msg, "trace: " ~ trace);
> }
> ---------------------------------------------------------
>
> That can, of course, be adjusted to do whatever logging you need instead 
> of just tracing to Stdout.
>

Also, keep in mind that because of DMD issue #2887 ( 
http://d.puremagic.com/issues/show_bug.cgi?id=2887 ), __LINE__ needs 
adjusted when used anywhere after the first line of a multi-line string 
mixin:

template foo()
{
  const char[] foo =
`{ auto __foo_line = __LINE__; // must be on first line, and the '{' is 
needed in this case
Stdout.formatln("Foo:");
Stdout.formatln("  File {}", __FILE__);
Stdout.formatln("  Line {}", __foo_line);
}`;
}





More information about the Digitalmars-d mailing list