From 40807602c9dc79ba54d6ee2eafa8d1e4837e89ca Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sat, 24 May 2014 10:18:51 +1000 Subject: [PATCH] Fix macro variable name clashing and crash on logging prior to logfile being set --- src/ckpool.h | 58 +++++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/src/ckpool.h b/src/ckpool.h index 130b9090..b8b0db2f 100644 --- a/src/ckpool.h +++ b/src/ckpool.h @@ -114,37 +114,39 @@ int process_exit(ckpool_t *ckp, proc_instance_t *pi, int ret); #define LOGMSG(_loglevel, fmt, ...) do { \ if (global_ckp->loglevel >= _loglevel && fmt) { \ struct tm *tm; \ - char *buf = NULL; \ - asprintf(&buf, fmt, ##__VA_ARGS__); \ + char *BUF = NULL; \ time_t now_t; \ + int LOGFD = global_ckp->logfd; \ + \ + asprintf(&BUF, fmt, ##__VA_ARGS__); \ now_t = time(NULL); \ tm = localtime(&now_t); \ - flock(global_ckp->logfd, LOCK_EX); \ - fprintf(global_ckp->logfp, "[%d-%02d-%02d %02d:%02d:%02d] %s", \ - tm->tm_year + 1900, \ - tm->tm_mon + 1, \ - tm->tm_mday, \ - tm->tm_hour, \ - tm->tm_min, \ - tm->tm_sec, \ - buf); \ - if (_loglevel <= LOG_ERR) \ - fprintf(global_ckp->logfp, " with errno %d: %s", errno, strerror(errno)); \ - fprintf(global_ckp->logfp, "\n"); \ - if (_loglevel <= LOG_WARNING) \ - fprintf(stderr, "%s\n", buf); \ - fflush(NULL); \ - flock(global_ckp->logfd, LOCK_UN); \ - free(buf); \ - } \ -} while (0) - -#define LOGBOTH(_loglevel, fmt, ...) do { \ - if (global_ckp->loglevel >= _loglevel && fmt) { \ - char *buf = NULL; \ - asprintf(&buf, fmt, ##__VA_ARGS__); \ - printf("%s\n", buf); \ - free(buf); \ + if (LOGFD) { \ + FILE *LOGFP = global_ckp->logfp; \ + \ + flock(LOGFD, LOCK_EX); \ + fprintf(LOGFP, "[%d-%02d-%02d %02d:%02d:%02d] %s", \ + tm->tm_year + 1900, \ + tm->tm_mon + 1, \ + tm->tm_mday, \ + tm->tm_hour, \ + tm->tm_min, \ + tm->tm_sec, \ + BUF); \ + if (_loglevel <= LOG_ERR) \ + fprintf(LOGFP, " with errno %d: %s", errno, strerror(errno)); \ + fprintf(LOGFP, "\n"); \ + fflush(LOGFP); \ + flock(LOGFD, LOCK_UN); \ + } \ + if (_loglevel <= LOG_WARNING) {\ + fprintf(stderr, "%s", BUF); \ + if (_loglevel <= LOG_ERR) \ + fprintf(stderr, " with errno %d: %s", errno, strerror(errno)); \ + fprintf(stderr, "\n"); \ + fflush(stderr); \ + } \ + free(BUF); \ } \ } while (0)