Logging function name, line, file...

Robert Fraser fraserofthenight at gmail.com
Tue Mar 18 11:39:23 PDT 2008


jaro wrote:
> It's a pitty __FUNCTION__ is not supported, only __FILE__ and __LINE__. Logging file and line without function name is almost useless (who knows what line 2935 does ...).
> 
> __FUNCTION__ in C and C++ was really nice because it was platform independent and worked both in debug and release mode so programs could also log in release mode.
> 
> Your code will work, but only for windows and introduces additional overhead. __FUNCTION__ in C++ was without overhead.

You can get the function name from the address at runtime, too :-). Get 
the symbol, then use something like std.demangle to demangle the symbol 
name and get the function.

As far as overhead is concerned, it's not very significant unless you're 
using a real-time application, and you're only going to be logging in 
debug mode, right?

As far as *nix support is concerned, yes, the code I provided is 
Windows-only. But that is very possible in *nix, too (I just can't 
provide the codes for it). Take a look at Flectioned for how to grab the 
symbols in D, then take a look at addr2line to get info about the 
line/file. Maybe this code has what you need?: 
http://www.koders.com/c/fid79D38B0848AFE045E3A0B6457DE1653F972671E3.aspx



More information about the Digitalmars-d mailing list