From c274457caaa378b9a54940e8b37136036fca5559 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Tue, 29 Apr 2014 10:17:42 +1000 Subject: [PATCH] Add loglevel support with default of notice level, configurable on command line --- src/ckpool.c | 11 ++++++++++- src/ckpool.h | 40 ++++++++++++++++++++++++++++++++++++++++ src/libckpool.h | 48 ------------------------------------------------ 3 files changed, 50 insertions(+), 49 deletions(-) diff --git a/src/ckpool.c b/src/ckpool.c index 72965287..0ed45ebd 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -303,7 +303,9 @@ int main(int argc, char **argv) global_ckp = &ckp; memset(&ckp, 0, sizeof(ckp)); - while ((c = getopt(argc, argv, "c:n:s:")) != -1) { + ckp.loglevel = LOG_NOTICE; + + while ((c = getopt(argc, argv, "c:n:s:l:")) != -1) { switch (c) { case 'c': ckp.config = optarg; @@ -314,6 +316,13 @@ int main(int argc, char **argv) case 's': ckp.socket_dir = strdup(optarg); break; + case 'l': + ckp.loglevel = atoi(optarg); + if (ckp.loglevel < LOG_EMERG || ckp.loglevel > LOG_DEBUG) { + quit(1, "Invalid loglevel (range %d - %d): %d", + LOG_EMERG, LOG_DEBUG, ckp.loglevel); + } + break; } } diff --git a/src/ckpool.h b/src/ckpool.h index 6756df9b..79bf2e72 100644 --- a/src/ckpool.h +++ b/src/ckpool.h @@ -65,4 +65,44 @@ struct ckpool_instance { ckpool_t *global_ckp; +/* Placeholders for when we have more comprehensive logging facilities */ + +#define LOGMSG(_loglevel, fmt, ...) do { \ + if (global_ckp->loglevel >= _loglevel && fmt) { \ + fprintf(stderr, fmt, ##__VA_ARGS__); \ + fprintf(stderr, "\n"); \ + if (_loglevel <= LOG_ERR) \ + fprintf(stderr, " with errno %d: %s", errno, strerror(errno)); \ + fflush(stderr); \ + } \ +} while (0) + +#define LOGEMERG(fmt, ...) LOGMSG(LOG_EMERG, fmt, ##__VA_ARGS__) +#define LOGALERT(fmt, ...) LOGMSG(LOG_ALERT, fmt, ##__VA_ARGS__) +#define LOGCRIT(fmt, ...) LOGMSG(LOG_CRIT, fmt, ##__VA_ARGS__) +#define LOGERR(fmt, ...) LOGMSG(LOG_ERR, fmt, ##__VA_ARGS__) +#define LOGWARNING(fmt, ...) LOGMSG(LOG_WARNING, fmt, ##__VA_ARGS__) +#define LOGNOTICE(fmt, ...) LOGMSG(LOG_NOTICE, fmt, ##__VA_ARGS__) +#define LOGINFO(fmt, ...) LOGMSG(LOG_INFO, fmt, ##__VA_ARGS__) +#define LOGDEBUG(fmt, ...) LOGMSG(LOG_DEBUG, fmt, ##__VA_ARGS__) + +#define IN_FMT_FFL " in %s %s():%d" +#define quitfrom(status, _file, _func, _line, fmt, ...) do { \ + if (fmt) { \ + fprintf(stderr, fmt IN_FMT_FFL, ##__VA_ARGS__, _file, _func, _line); \ + fprintf(stderr, "\n"); \ + fflush(stderr); \ + } \ + exit(status); \ +} while (0) + +#define quit(status, fmt, ...) do { \ + if (fmt) { \ + fprintf(stderr, fmt, ##__VA_ARGS__); \ + fprintf(stderr, "\n"); \ + fflush(stderr); \ + } \ + exit(status); \ +} while (0) + #endif /* CKPOOL_H */ diff --git a/src/libckpool.h b/src/libckpool.h index d1b3e272..88c8010d 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -173,54 +173,6 @@ static inline void flip_80(void *dest_p, const void *src_p) #define LOG_DEBUG 7 /* debug-level messages */ #endif -/* Placeholders for when we have more comprehensive logging facilities */ -#define LOGERR(fmt, ...) do { \ - if (fmt) { \ - fprintf(stderr, fmt, ##__VA_ARGS__); \ - if (errno)\ - fprintf(stderr, " with errno %d:%s", errno, strerror(errno)); \ - fprintf(stderr, "\n"); \ - fflush(stderr); \ - } \ -} while (0) - -#define LOGMSG(fmt, ...) do { \ - if (fmt) { \ - fprintf(stderr, fmt, ##__VA_ARGS__); \ - fprintf(stderr, "\n"); \ - fflush(stderr); \ - } \ -} while (0) - -#define LOGEMERG(fmt, ...) LOGMSG(fmt, ##__VA_ARGS__) -#define LOGALERT(fmt, ...) LOGMSG(fmt, ##__VA_ARGS__) -#define LOGCRIT(fmt, ...) LOGMSG(fmt, ##__VA_ARGS__) -#define LOGWARNING(fmt, ...) LOGMSG(fmt, ##__VA_ARGS__) -#define LOGNOTICE(fmt, ...) LOGMSG(fmt, ##__VA_ARGS__) -#define LOGINFO(fmt, ...) LOGMSG(fmt, ##__VA_ARGS__) -#define LOGDEBUG(fmt, ...) LOGMSG(fmt, ##__VA_ARGS__) - -#define IN_FMT_FFL " in %s %s():%d" -#define quitfrom(status, _file, _func, _line, fmt, ...) do { \ - if (fmt) { \ - fprintf(stderr, fmt IN_FMT_FFL, ##__VA_ARGS__, _file, _func, _line); \ - fprintf(stderr, "\n"); \ - fflush(stderr); \ - } \ - exit(status); \ -} while (0) - -#define quit(status, fmt, ...) do { \ - if (fmt) { \ - fprintf(stderr, fmt, ##__VA_ARGS__); \ - if (status || errno)\ - fprintf(stderr, " with errno %d:%s", errno, strerror(errno)); \ - fprintf(stderr, "\n"); \ - fflush(stderr); \ - } \ - exit(status); \ -} while (0) - #define PAGESIZE (4096) /* ck locks, a write biased variant of rwlocks */