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. 58
      src/ckpool.h

58
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; \
tm->tm_year + 1900, \ \
tm->tm_mon + 1, \ flock(LOGFD, LOCK_EX); \
tm->tm_mday, \ fprintf(LOGFP, "[%d-%02d-%02d %02d:%02d:%02d] %s", \
tm->tm_hour, \ tm->tm_year + 1900, \
tm->tm_min, \ tm->tm_mon + 1, \
tm->tm_sec, \ tm->tm_mday, \
buf); \ tm->tm_hour, \
if (_loglevel <= LOG_ERR) \ tm->tm_min, \
fprintf(global_ckp->logfp, " with errno %d: %s", errno, strerror(errno)); \ tm->tm_sec, \
fprintf(global_ckp->logfp, "\n"); \ BUF); \
if (_loglevel <= LOG_WARNING) \ if (_loglevel <= LOG_ERR) \
fprintf(stderr, "%s\n", buf); \ fprintf(LOGFP, " with errno %d: %s", errno, strerror(errno)); \
fflush(NULL); \ fprintf(LOGFP, "\n"); \
flock(global_ckp->logfd, LOCK_UN); \ fflush(LOGFP); \
free(buf); \ flock(LOGFD, LOCK_UN); \
} \ } \
} 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