I have a function log_info
(copied from printf
's implementation) that accepts variable no. of arguments and passes it to vprintf:
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;
}
I want to prepend and append strings to these arguments. So if a user calls the above function this way:
log_info("at iteration %d, float value is %f", i, f);
instead of printing
at iteration 4, float value is 102.34
I want to print
[INFO] [at iteration 4, float value is 102.34] [timestamp: xxxx]
I could do it in 3 separate steps
fprintf(stdout, "[INFO] [");
vprintf(stdout, format, arg);
fprintf(stdout, "] [timestamp:%f]", ts);
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).
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.
Edit: Performance overhead due to use of mutex is not really an issue, but I dont want to use one unless necessary.
Thanks in advance