Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ function call identifier
    text
    copied!<p>Consider the following code:</p> <pre><code>void Foo() { ...... LOG_ERROR("I'm error 1") // call 1 ..... LOG_ERROR("I'm error 2") // call 2 ..... } </code></pre> <p><code>LOG_ERROR()</code> is a macro. <code>LOG_ERROR()</code> should print string identifying it in code, while the assumption is that code can change, but <code>A::Foo()</code> will remain unchanged. The identifier should retain while code changes.</p> <p>This can be solved by adding error code as argument to <code>LOG_ERROR()</code>, but we want to remove from the programmer the burden to manage error codes.</p> <p>Using <code>__LINE__</code> is not an answer, since <code>Foo()</code> can move from build to build.</p> <p>Therefore I thought about identifying <code>LOG_ERROR()</code> relative to start of <code>Foo()</code>:</p> <ul> <li>a. Identify by file name (<code>__FILE__</code>) + function name (<code>__FUNCTION__</code>) + line number of <code>LOG_ERROR()</code> relative to <code>Foo()</code> start.</li> <li>b. Identify by file name (<code>__FILE__</code>) + function name (<code>__FUNCTION__</code>) + <code>LOG_ERROR()</code> call number in <code>Foo()</code>.</li> </ul> <p>The solution should be work with VC++ 2008 and g++ 4.1.1 at least.</p> <p>One proposed solution (<a href="http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/bf4abc4fc9f023a1#" rel="nofollow noreferrer">link text</a>) is: </p> <pre><code>#define ENABLE_LOG_ERROR static const int LOG_ERROR_start_line = __LINE__ #define LOG_ERROR(s) cerr &lt;&lt; "error #" &lt;&lt; (__LINE__ - LOG_ERROR_start_line) \ &lt;&lt; " in " &lt;&lt; __func__ &lt;&lt; ": " &lt;&lt; s &lt;&lt; endl void Foo() { ENABLE_LOG_ERROR; //... LOG_ERROR("error 1"); int i; LOG_ERROR("error 2"); } </code></pre> <p>This will force user to write <code>ENABLE_LOG_ERROR</code> in start of each function containing <code>LOG_ERROR()</code> and there're many such functions.</p> <p>Is there other way to accomplish the task?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload