From 15109c72d1c5e8804318c507b042d3e331f92c01 Mon Sep 17 00:00:00 2001 From: Alex Young Date: Fri, 13 Jul 2012 12:18:19 +0100 Subject: [PATCH] Add a newline to log messages at macro expansion This simplifies building the log output because it means we don't have to malloc a buffer to append a newline, and we keep the atomic write property we're after. It also takes advantage of the C constant string concatenation which we already require to work to prepend the thread and pid data. --- src/util.c | 22 ++++------------------ src/util.h | 2 +- 2 files changed, 5 insertions(+), 19 deletions(-) diff --git a/src/util.c b/src/util.c index cf7c4f6..2c44e86 100644 --- a/src/util.c +++ b/src/util.c @@ -40,32 +40,18 @@ void exit_err( const char *msg ) } - void mylog(int line_level, const char* format, ...) { - va_list argptr; - char * format_n = NULL; - int format_len; - - if (line_level < log_level) { return; } - - /* Copy the format sideways so that we can append a "\n" to it - * and avoid a second fprintf. This stops log lines from getting - * interleaved. - */ - format_len = strlen( format ); - format_n = xmalloc( format_len + 2 ); - memcpy( format_n, format, format_len+1 ); - format_n[format_len] = '\n'; + + va_list argptr; va_start(argptr, format); - vfprintf(stderr, format_n, argptr); + vfprintf(stderr, format, argptr); va_end(argptr); - - free( format_n ); } + void* xrealloc(void* ptr, size_t size) { void* p = realloc(ptr, size); diff --git a/src/util.h b/src/util.h index 6d0f57b..15181a5 100644 --- a/src/util.h +++ b/src/util.h @@ -87,7 +87,7 @@ void mylog(int line_level, const char* format, ...); #define levstr(i) (i==0?'D':(i==1?'I':(i==2?'W':(i==3?'E':'F')))) -#define myloglev(level, msg, ...) mylog( level, "%c:%d %p %s:%d: "msg, levstr(level), getpid(),pthread_self(), __FILE__, __LINE__, ##__VA_ARGS__ ) +#define myloglev(level, msg, ...) mylog( level, "%c:%d %p %s:%d: "msg"\n", levstr(level), getpid(),pthread_self(), __FILE__, __LINE__, ##__VA_ARGS__ ) #ifdef DEBUG # define debug(msg, ...) myloglev(0, msg, ##__VA_ARGS__)