debugmac.ph.html

来自「this is a mirrored site c-faq. thought 」· HTML 代码 · 共 112 行

HTML
112
字号
<html><!-- Mirrored from c-faq.com/cpp/debugmac.ph.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:02:59 GMT --><head><title></title></head><body>From: Phil Howard<br>Subject: Re: how to give variable number of args for macro<br>Newsgroups: comp.lang.c<br>Message-ID: &lt;Vv0D2.339$v8.10430@newsfeed.slurp.net&gt;<br>Date: Wed, 03 Mar 1999 01:46:29 GMT<p>Here's another way that's not in the FAQ.  It does depend on the <TT>vfprintf</TT>function being in your library and on a compiler that optimizes outexpressions that cannot be executed.  Most modern libraries and compilersdo have this.  Even if the optimization is absent, the trick will stillwork although the executeable will have some dead code in it.<p>I start by defining the symbol "debug" according to the state of thesymbol "DEBUG" like so:   (this can be in your .h file)<pre>#ifdef DEBUG#define debug                   _debug_call#else#define debug                   1?0:#endif</pre><p>Then I define a function called "_debug_call()" which accepts one or morefixed arguments followed by a variable number of arguments.  For example:<pre>        int _debug_call( int msglev, const char *msgfmt, ... )</pre><p>That function will need to include stdarg.h and have a variable defined:<pre>        va_list arg_list;</pre><p>Then following any code which does things like deciding whether the messageshould even be printed (I have varying debug levels, for example), I have<pre>        va_start( arg_list, msgfmt );        vfprintf( stderr, msgfmt , arg_list );        putc( '\n', stderr );</pre><p>Now if the symbol DEBUG is not defined, then the symbol debug is definewith the string "1?0:".  When the pre-processor sees something like<pre>        debug( 1, "begin %s", argv[0] );</pre>It turns it into:<pre>        1?0:(1,"begin %s",argv[0]);</pre>which is now a <TT>?:</TT> expression which evaluates the left side of <TT>:</TT> but notthe right side.  The right side is merely a parenthesized expressionwhich, if it were evaluated, would discard all comma separated expressionsbut the last, yielding the last.  But the <TT>?:</TT> causes it to not be evaluatedand a good compiler will recognize that since 1 is a constant, the rightside of <TT>:</TT> cannot ever be evaluated, and not even compile it it.  This itbecomes a free standing value.  If you did choose to assign <TT>debug()</TT> tosomething line:<pre>        result = debug( 1, "begin %s", argv[0] );</pre>Then you get:<pre>        result = 1?0:(1,"begin %s",argv[0]);</pre>which has the effect of:<pre>        result = 0;</pre>Now if <TT>DEBUG</TT> is defined, then it becomes:<pre>        _debug_call(1,"begin %s",argv[0]);</pre>and voila, it is compiled as a function call.  You do have to place thedefinition of "DEBUG" before the pre-processor code that decides how todefine "debug".  I do this by having the part the decides how to define"debug" in a header file, called debug.h, and I simply define, or not,the symbol "DEBUG" before including debug.h.<p>Above I have given a simple version.  My actual debug package is a bitmore complex, having features to manage debug levels, which files tooutput to, and a separately named duplication called <TT>trace</TT> (the samething all over again, but with a new name which I can use for a differentgroup of concepts, and a separate messaging level state).<p>If anyone wants to include the above trick in an FAQ or publish it orwhatever, feel free to do so.<p><pre>-- --    *-----------------------------*      Phil Howard KA9WGN       *    --  --   | Inturnet, Inc.              | Director of Internet Services |   --   --  | Business Internet Solutions |       eng at intur.net        |  --    -- *-----------------------------*      phil at intur.net        * --</pre></body><!-- Mirrored from c-faq.com/cpp/debugmac.ph.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:02:59 GMT --></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?