Browse Source

Send all log messages to console while console logger hasn't been set up.

master
ckolivas 8 years ago committed by Con Kolivas
parent
commit
7d4c99b647
  1. 45
      src/ckpool.c

45
src/ckpool.c

@ -82,28 +82,19 @@ static void proclog(ckpool_t *ckp, char *msg)
} }
/* Log everything to the logfile, but display warnings on the console as well */ /* Log everything to the logfile, but display warnings on the console as well */
void logmsg(int loglevel, const char *fmt, ...) { void logmsg(int loglevel, const char *fmt, ...)
if (global_ckp->loglevel >= loglevel && fmt) { {
int logfd = global_ckp->logfd; int logfd = global_ckp->logfd;
char *buf = NULL, *log; char *log, *buf = NULL;
char stamp[128];
struct tm tm; struct tm tm;
tv_t now_tv; tv_t now_tv;
int ms;
va_list ap; va_list ap;
char stamp[128]; int ms;
va_start(ap, fmt);
VASPRINTF(&buf, fmt, ap);
va_end(ap);
if (unlikely(!buf)) { if (global_ckp->loglevel < loglevel || !fmt)
fprintf(stderr, "Null buffer sent to logmsg\n");
return; return;
}
if (unlikely(!strlen(buf))) {
fprintf(stderr, "Zero length string sent to logmsg\n");
return;
}
tv_time(&now_tv); tv_time(&now_tv);
ms = (int)(now_tv.tv_usec / 1000); ms = (int)(now_tv.tv_usec / 1000);
localtime_r(&(now_tv.tv_sec), &tm); localtime_r(&(now_tv.tv_sec), &tm);
@ -114,18 +105,36 @@ void logmsg(int loglevel, const char *fmt, ...) {
tm.tm_hour, tm.tm_hour,
tm.tm_min, tm.tm_min,
tm.tm_sec, ms); tm.tm_sec, ms);
va_start(ap, fmt);
VASPRINTF(&buf, fmt, ap);
va_end(ap);
if (unlikely(!buf)) {
fprintf(stderr, "Null buffer sent to logmsg\n");
return;
}
if (unlikely(!strlen(buf))) {
fprintf(stderr, "Zero length string sent to logmsg\n");
goto out;
}
if (loglevel <= LOG_ERR && errno != 0) if (loglevel <= LOG_ERR && errno != 0)
ASPRINTF(&log, "%s %s with errno %d: %s\n", stamp, buf, errno, strerror(errno)); ASPRINTF(&log, "%s %s with errno %d: %s\n", stamp, buf, errno, strerror(errno));
else else
ASPRINTF(&log, "%s %s\n", stamp, buf); ASPRINTF(&log, "%s %s\n", stamp, buf);
if (unlikely(!global_ckp->console_logger)) {
fprintf(stderr, "%s", log);
goto out_free;
}
if (loglevel <= LOG_WARNING) if (loglevel <= LOG_WARNING)
ckmsgq_add(global_ckp->console_logger, strdup(log)); ckmsgq_add(global_ckp->console_logger, strdup(log));
if (logfd > 0) if (logfd > 0)
ckmsgq_add(global_ckp->logger, strdup(log)); ckmsgq_add(global_ckp->logger, strdup(log));
free(buf); out_free:
free(log); free(log);
} out:
free(buf);
} }
/* Generic function for creating a message queue receiving and parsing thread */ /* Generic function for creating a message queue receiving and parsing thread */

Loading…
Cancel
Save