Browse Source

Fix macro variable name clashing and crash on logging prior to logfile being set

master
Con Kolivas 11 years ago
parent
commit
40807602c9
  1. 42
      src/ckpool.h

42
src/ckpool.h

@ -114,37 +114,39 @@ int process_exit(ckpool_t *ckp, proc_instance_t *pi, int ret);
#define LOGMSG(_loglevel, fmt, ...) do { \ #define LOGMSG(_loglevel, fmt, ...) do { \
if (global_ckp->loglevel >= _loglevel && fmt) { \ if (global_ckp->loglevel >= _loglevel && fmt) { \
struct tm *tm; \ struct tm *tm; \
char *buf = NULL; \ char *BUF = NULL; \
asprintf(&buf, fmt, ##__VA_ARGS__); \
time_t now_t; \ time_t now_t; \
int LOGFD = global_ckp->logfd; \
\
asprintf(&BUF, fmt, ##__VA_ARGS__); \
now_t = time(NULL); \ now_t = time(NULL); \
tm = localtime(&now_t); \ tm = localtime(&now_t); \
flock(global_ckp->logfd, LOCK_EX); \ if (LOGFD) { \
fprintf(global_ckp->logfp, "[%d-%02d-%02d %02d:%02d:%02d] %s", \ FILE *LOGFP = global_ckp->logfp; \
\
flock(LOGFD, LOCK_EX); \
fprintf(LOGFP, "[%d-%02d-%02d %02d:%02d:%02d] %s", \
tm->tm_year + 1900, \ tm->tm_year + 1900, \
tm->tm_mon + 1, \ tm->tm_mon + 1, \
tm->tm_mday, \ tm->tm_mday, \
tm->tm_hour, \ tm->tm_hour, \
tm->tm_min, \ tm->tm_min, \
tm->tm_sec, \ tm->tm_sec, \
buf); \ BUF); \
if (_loglevel <= LOG_ERR) \ if (_loglevel <= LOG_ERR) \
fprintf(global_ckp->logfp, " with errno %d: %s", errno, strerror(errno)); \ fprintf(LOGFP, " with errno %d: %s", errno, strerror(errno)); \
fprintf(global_ckp->logfp, "\n"); \ fprintf(LOGFP, "\n"); \
if (_loglevel <= LOG_WARNING) \ fflush(LOGFP); \
fprintf(stderr, "%s\n", buf); \ flock(LOGFD, LOCK_UN); \
fflush(NULL); \
flock(global_ckp->logfd, LOCK_UN); \
free(buf); \
} \ } \
} while (0) if (_loglevel <= LOG_WARNING) {\
fprintf(stderr, "%s", BUF); \
#define LOGBOTH(_loglevel, fmt, ...) do { \ if (_loglevel <= LOG_ERR) \
if (global_ckp->loglevel >= _loglevel && fmt) { \ fprintf(stderr, " with errno %d: %s", errno, strerror(errno)); \
char *buf = NULL; \ fprintf(stderr, "\n"); \
asprintf(&buf, fmt, ##__VA_ARGS__); \ fflush(stderr); \
printf("%s\n", buf); \ } \
free(buf); \ free(BUF); \
} \ } \
} while (0) } while (0)

Loading…
Cancel
Save