title: Predefined Macros for function name __func__
body: <p>I am attempting to build a debug log message function that records the file, line, and function of of where the log message was called from. </p>
  
  <pre><code>#define DEBUG_PANIC(p) CLogging::Debuglogf( "Debug marker (%s) - ::%s() in file: %s(%d)", p, __func__ , __FILE__, __LINE__ );
  </code></pre>
  
  <p>The above code works on some compilers but not all. My code needs to be cross compatible with GCC as well as Microsoft Visual studios. I have added the below defines to help with compatibility. </p>
  
  <pre><code>#ifndef __FUNCTION_NAME__
      #if defined __func__ 
          // Undeclared 
          #define __FUNCTION_NAME__   __func__ 
      #elif defined __FUNCTION__ 
          // Undeclared
          #define __FUNCTION_NAME__   __FUNCTION__  
      #elif defined __PRETTY_FUNCTION__
          // Undeclared
          #define __FUNCTION_NAME__   __PRETTY_FUNCTION__
      #else
          // Declared
          #define __FUNCTION_NAME__   "N/A"   
      #endif // __func__ 
  
  #endif // __FUNCTION_NAME__
  
  #define DEBUG_PANIC(p) CLogging::Debuglogf( "Debug marker (%s) - ::%s() in file: %s(%d)", p, __FUNCTION_NAME__, __FILE__, __LINE__ );
  </code></pre>
  
  <p>The problem with the above code snippet is that it is the #else macro is active on all compilers while the other macros are not. in other words <code>#if defined __func__</code> is false on compilers where <code>__func__</code> is a <em>predefined macro</em>.</p>
  
  <p>My question is</p>
  
  <ul>
  <li>How do I create a cross compiler macro to find the function name ?</li>
  <li>How can I tell if a <code>__func__</code> can be used? </li>
  </ul>