title: Conditional macro expansion
body: <p>Heads up: This is a weird question.</p>
  
  <p>I've got some really useful macros that I like to use to simplify some logging.  For example I can do <code>Log(@"My message with arguments: %@, %@, %@", @"arg1", @"arg2", @"arg3")</code>, and that will get expanded into a more complex method invocation that includes things like <code>self</code>, <code>_cmd</code>, <code>__FILE__</code>, <code>__LINE__</code>, etc, so that I can easily track where things are getting logged.  This works great.</p>
  
  <p>Now I'd like to expand my macros to not only work with Objective-C methods, but general C functions.  The problem is the <code>self</code> and <code>_cmd</code> portions that are in the macro expansion.  These two parameters don't exist in C functions.  Ideally, I'd like to be able to use this same set of macros within C functions, but I'm running into problems.  When I use (for example) my <code>Log()</code> macro, I get compiler warnings about <code>self</code> and <code>_cmd</code> being undeclared (which makes total sense).</p>
  
  <p>My first thought was to do something like the following (in my macro):</p>
  
  <pre><code>if (thisFunctionIsACFunction) {
    DoLogging(nil, nil, format, ##__VA_ARGS__);
  } else {
    DoLogging(self, _cmd, format, ##__VA_ARGS__);
  }
  </code></pre>
  
  <p>This still produces compiler warnings, since the entire if() statement is substituted in place of the macro, resulting in errors with the <code>self</code> and <code>_cmd</code> keywords (even though they will never be executed during function execution).</p>
  
  <p>My next thought was to do something like this (in my macro):</p>
  
  <pre><code>if (thisFunctionIsACFunction) {
    #define SELF nil
    #define CMD nil
  } else {
    #define SELF self
    #define CMD _cmd
  }
  DoLogging(SELF, CMD, format, ##__VA_ARGS__);
  </code></pre>
  
  <p>That doesn't work, unfortunately.  I get "error: '#' is not followed by a macro parameter" on my first <code>#define</code>.</p>
  
  <p>My other thought was to create a second set of macros, specifically for use in C functions.  This reeks of a bad code smell, and I <em>really</em> don't want to do this.</p>
  
  <p>Is there some way I can use the same set of macros from within both Objective-C methods and C functions, and only reference <code>self</code> and <code>_cmd</code> if the macro is in an Objective-C method?</p>
  
  <p><strong>edit</strong> more information:</p>
  
  <p><code>thisFunctionIsACFunction</code> is determined in a pretty rudimentary way (and I'm definitely open to improvements and suggestions).  Basically it's this:</p>
  
  <pre><code>BOOL thisFunctionIsACFunction == (__PRETTY_FUNCTION__[0] != '-' &amp;&amp; __PRETTY_FUNCTION__[0] != '+');
  </code></pre>
  
  <p>It's relying on the behavior of the compiler to prepend a '-' or '+' for instance and class methods on Objective-C objects.  Anything else must be a C function (since C functions can't have names that begin with '-' or '+').</p>
  
  <p>I understand that this check is technically a runtime check, since <code>__PRETTY_FUNCTION__</code> gets replaced with a <code>char*</code>, and this is probably the major roadblock to my request for help.</p>