Browse Source

Use a weak logmsg function within libckpool allowing it to be included standalone without touching ckpool code

master
Con Kolivas 11 years ago
parent
commit
b77e481c7e
  1. 44
      src/ckpool.c
  2. 73
      src/ckpool.h
  3. 15
      src/libckpool.c
  4. 51
      src/libckpool.h

44
src/ckpool.c

@ -30,6 +30,50 @@
ckpool_t *global_ckp; ckpool_t *global_ckp;
/* Log everything to the logfile, but display warnings on the console as well */
void logmsg(int loglevel, const char *fmt, ...) {
if (global_ckp->loglevel >= loglevel && fmt) {
int logfd = global_ckp->logfd;
char *buf = NULL;
struct tm *tm;
time_t now_t;
va_list ap;
va_start(ap, fmt);
VASPRINTF(&buf, fmt, ap);
va_end(ap);
now_t = time(NULL);
tm = localtime(&now_t);
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);
}
}
static void *listener(void *arg) static void *listener(void *arg)
{ {
proc_instance_t *pi = (proc_instance_t *)arg; proc_instance_t *pi = (proc_instance_t *)arg;

73
src/ckpool.h

@ -119,77 +119,4 @@ json_t *json_rpc_call(connsock_t *cs, const char *rpc_req);
int process_exit(ckpool_t *ckp, proc_instance_t *pi, int ret); int process_exit(ckpool_t *ckp, proc_instance_t *pi, int ret);
#define ASPRINTF(strp, fmt, ...) do { \
if (unlikely(asprintf(strp, fmt, ##__VA_ARGS__) < 0)) \
quitfrom(1, __FILE__, __func__, __LINE__, "Failed to asprintf"); \
} while (0)
/* Log everything to the logfile, but display warnings on the console as well */
#define LOGMSG(_loglevel, fmt, ...) do { \
if (global_ckp->loglevel >= _loglevel && fmt) { \
struct tm *tm; \
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); \
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)
#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 */ #endif /* CKPOOL_H */

15
src/libckpool.c

@ -37,6 +37,21 @@
#define UNIX_PATH_MAX 108 #define UNIX_PATH_MAX 108
#endif #endif
/* We use a weak function as a simple printf within the library that can be
* overridden by however the outside executable wishes to do its logging. */
void __attribute__((weak)) logmsg(int __maybe_unused loglevel, const char *fmt, ...)
{
va_list ap;
char *buf;
va_start(ap, fmt);
VASPRINTF(&buf, fmt, ap);
va_end(ap);
printf("%s\n", buf);
free(buf);
}
void rename_proc(const char *name) void rename_proc(const char *name)
{ {
char buf[16]; char buf[16];

51
src/libckpool.h

@ -161,18 +161,45 @@ static inline void flip_80(void *dest_p, const void *src_p)
#define dealloc(ptr) _dealloc((void *)&(ptr)) #define dealloc(ptr) _dealloc((void *)&(ptr))
/* Should be defined in syslog, but keeping them here is a reminder of their #define VASPRINTF(strp, fmt, ...) do { \
* values. */ if (unlikely(vasprintf(strp, fmt, ##__VA_ARGS__) < 0)) \
#ifndef LOG_ERR quitfrom(1, __FILE__, __func__, __LINE__, "Failed to asprintf"); \
#define LOG_EMERG 0 /* system is unusable */ } while (0)
#define LOG_ALERT 1 /* action must be taken immediately */
#define LOG_CRIT 2 /* critical conditions */ #define ASPRINTF(strp, fmt, ...) do { \
#define LOG_ERR 3 /* error conditions */ if (unlikely(asprintf(strp, fmt, ##__VA_ARGS__) < 0)) \
#define LOG_WARNING 4 /* warning conditions */ quitfrom(1, __FILE__, __func__, __LINE__, "Failed to asprintf"); \
#define LOG_NOTICE 5 /* normal but significant condition */ } while (0)
#define LOG_INFO 6 /* informational */
#define LOG_DEBUG 7 /* debug-level messages */ void logmsg(int loglevel, const char *fmt, ...);
#endif
#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)
#define PAGESIZE (4096) #define PAGESIZE (4096)

Loading…
Cancel
Save