title: Variadic function: Extract arguments and append string
body: <p>I have a function <code>log_info</code> (copied from <code>printf</code>'s implementation) that accepts variable no. of arguments and passes it to vprintf:</p>
  
  <pre><code>int log_info(const char *format, ...) {
    va_list arg;
    int done;
  
    va_start (arg, format);
    done = vfprintf (stdout, format, arg);
    va_end (arg);
  
    return done;
  }
  </code></pre>
  
  <p>I want to prepend and append strings to these arguments. So if a user calls the above function this way:</p>
  
  <pre><code>log_info("at iteration %d, float value is %f", i, f);
  </code></pre>
  
  <p>instead of printing</p>
  
  <pre><code>at iteration 4, float value is 102.34
  </code></pre>
  
  <p>I want to print</p>
  
  <pre><code>[INFO] [at iteration 4, float value is 102.34] [timestamp: xxxx]
  </code></pre>
  
  <p>I could do it in 3 separate steps</p>
  
  <pre><code>fprintf(stdout, "[INFO] [");
  vprintf(stdout, format, arg);
  fprintf(stdout, "] [timestamp:%f]", ts);
  </code></pre>
  
  <p>But the program is multi-threaded, and hence I want all data to be written in a single call to vprintf (vprintf being thread-safe). </p>
  
  <p>The other option is to lock a mutex and then write it in 3 steps as shown above, but if appending strings to the args is not too complex, I would like to try that.</p>
  
  <p>Edit: Performance overhead due to use of mutex is not really an issue, but I dont want to use one unless necessary. </p>
  
  <p>Thanks in advance</p>