Re: __FUNCTION__ and debug.

From: Henrik Nordstrom <hno@dont-contact.us>
Date: Tue, 5 Nov 2002 09:55:00 +0100

On Tuesday 05 November 2002 09.00, Francesco Chemolli wrote:

> Why not just using a function and hinting the compiler to inline
> it? Inline-hints are less intrusive than macro varargs (BTW: I used
> macro varargs for the debug-messages in some helpers, with #ifdef
> trickery to fall back to not emitting the debug messages if I
> wasn't sure that the compiler supported them - more a style
> exercise than something useful, but still...)

Because the code needs to be expanded in place for __FILE__, __LINE__,
or __func__ to give anything interesting.. In a inlined function
these would all give the location where the inlined function is
defined, not the location of the debug message.

As we can not use macro varargs we have to pass the extra arguments in
another way. Global variables suggested. And the same syntax as for
debug() to allow for quick "macro inlined" level filter code.

After some consideration I agree on the use of __FILE__ if
__FUNCTION__ is not available. Drawback is that __FILE__ is a little
overly verbose (full path), but the called log function should be
able to work around this with not too much effort.

Which brings me back to the original suggestion of simply mutating the
existing debug() macro to add "__func__:" or "__FILE__:__LINE__:" to
each message.

#if HAVE___FUNC__
#define trace(LEVEL) \
        ((LEVEL) > debugLevels[DEBUG_SECTION]) ? (void) 0 : \
        ((_db_func = __func__), (_db_line = __LINE__), (_db_level =
(LEVEL))), \
        ((LEVEL) > debugLevels[DEBUG_SECTION]) ? (void) 0 : \
        db_print
#else
#define trace(LEVEL) \
        ((LEVEL) > debugLevels[DEBUG_SECTION]) ? (void) 0 : \
        ((_db_file = __FILE__), (_db_line = __LINE__), (_db_level =
(LEVEL))), \
        ((LEVEL) > debugLevels[DEBUG_SECTION]) ? (void) 0 : \
        db_print
#endif

And then in a module:

static const int DEBUG_SECTION = nn;

...

   trace(4)("format", ...);

If you find that there is need for different DEBUG_SECTION in the same
compilation unit or class then code should most likely be separated
in different files. (note: for C++ classes DEBUG_SECTION should be a
static const class global)

We have already seen that supplying the debug section on each call
makes a big room for error on code reshuffling / copy-paste, so lets
try to avoid that.

Regards
Henrik
Received on Tue Nov 05 2002 - 01:55:08 MST

This archive was generated by hypermail pre-2.1.9 : Tue Dec 09 2003 - 16:18:37 MST