From b04dcd94283e9b824cd63f55de772ed6a438a1a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= <ondrej@sury.org> Date: Fri, 11 May 2012 02:55:30 +0200 Subject: [PATCH] Add nanoseconds to time logging and change date-time format to RFC3339. fixes #1873 @30m --- src/common/log.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/common/log.c b/src/common/log.c index 6dd8ba8237..d3616659ec 100644 --- a/src/common/log.c +++ b/src/common/log.c @@ -14,11 +14,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#define _POSIX_C_SOURCE 199506L +#define _BSD_SOURCE #include <config.h> #include <stdarg.h> #include <stdio.h> #include <string.h> #include <stdlib.h> +#include <unistd.h> +#if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 +#include <time.h> +#else +#include <sys/time.h> +#endif #include "common/log.h" #include "common/lists.h" @@ -212,14 +220,22 @@ static int _log_msg(logsrc_t src, int level, const char *msg) /* Prefix date and time. */ char tstr[128] = {0}; int tlen = 0; - time_t t = time(NULL); - struct tm *lt = localtime(&t); - if (lt != NULL) { + struct tm lt; + struct timespec ts; +#if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 + clock_gettime(CLOCK_REALTIME, &ts); +#else + struct timeval tv; + gettimeofday(&tv, NULL); + ts.tv_sec = tv.tv_sec; + ts.tv_nsec = tv.tv_usec * 1000; +#endif + if (localtime_r(&ts.tv_sec, <) != NULL) { tlen = strftime(tstr, sizeof(tstr) - 1, - "%d-%m-%Y %H:%M:%S", lt); + "%Y-%m-%dT%H:%M:%S", <); if (tlen > 0) { - tstr[tlen] = ' '; - tstr[tlen + 1] = '\0'; + char pm = (lt.tm_gmtoff > 0)?'+':'-'; + snprintf(tstr+tlen, 128-tlen-1, ".%.9lu%c%.2u:%.2u ", (unsigned long)ts.tv_nsec, pm, (unsigned int)lt.tm_gmtoff/3600, (unsigned int)(lt.tm_gmtoff/60)%60); } } -- GitLab