From f9a17d04c3ad40b2a683e6355cf6da5b69ab4009 Mon Sep 17 00:00:00 2001 From: kanoi Date: Sun, 12 Jul 2015 14:33:21 +1000 Subject: [PATCH] Update log messages to millisecond accuracy --- src/ckdb.c | 12 +++++++----- src/ckpool.c | 14 ++++++++------ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/ckdb.c b/src/ckdb.c index d8eba810..e568ccdd 100644 --- a/src/ckdb.c +++ b/src/ckdb.c @@ -561,7 +561,8 @@ void logmsg(int loglevel, const char *fmt, ...) int logfd = 0; char *buf = NULL; struct tm tm; - time_t now_t; + tv_t now_tv; + int ms; va_list ap; char stamp[128]; char *extra = EMPTY; @@ -572,8 +573,9 @@ void logmsg(int loglevel, const char *fmt, ...) if (loglevel > global_ckp->loglevel) return; - now_t = time(NULL); - localtime_r(&now_t, &tm); + tv_time(&now_tv); + ms = (int)(now_tv.tv_usec / 1000); + localtime_r(&(now_tv.tv_sec), &tm); minoff = tm.tm_gmtoff / 60; if (minoff < 0) { tzch = '-'; @@ -591,13 +593,13 @@ void logmsg(int loglevel, const char *fmt, ...) tzch, hroff); } snprintf(stamp, sizeof(stamp), - "[%d-%02d-%02d %02d:%02d:%02d%s]", + "[%d-%02d-%02d %02d:%02d:%02d.%03d%s]", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, - tm.tm_sec, + tm.tm_sec, ms, tzinfo); if (!fmt) { diff --git a/src/ckpool.c b/src/ckpool.c index 4d00084e..7f2473b3 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -64,7 +64,8 @@ void logmsg(int loglevel, const char *fmt, ...) { int logfd = global_ckp->logfd; char *buf = NULL; struct tm tm; - time_t now_t; + tv_t now_tv; + int ms; va_list ap; char stamp[128]; @@ -72,16 +73,17 @@ void logmsg(int loglevel, const char *fmt, ...) { VASPRINTF(&buf, fmt, ap); va_end(ap); - now_t = time(NULL); - localtime_r(&now_t, &tm); - sprintf(stamp, "[%d-%02d-%02d %02d:%02d:%02d]", + tv_time(&now_tv); + ms = (int)(now_tv.tv_usec / 1000); + localtime_r(&(now_tv.tv_sec), &tm); + sprintf(stamp, "[%d-%02d-%02d %02d:%02d:%02d.%03d]", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, - tm.tm_sec); - if (loglevel <= LOG_WARNING) {\ + tm.tm_sec, ms); + if (loglevel <= LOG_WARNING) { if (loglevel <= LOG_ERR && errno != 0) fprintf(stderr, "%s %s with errno %d: %s\n", stamp, buf, errno, strerror(errno)); else