Skip to content
Snippets Groups Projects
Commit b04dcd94 authored by Ondřej Surý's avatar Ondřej Surý
Browse files

Add nanoseconds to time logging and change date-time format to RFC3339.

fixes #1873 @30m
parent c6d11a9e
No related branches found
No related tags found
No related merge requests found
......@@ -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, &lt) != NULL) {
tlen = strftime(tstr, sizeof(tstr) - 1,
"%d-%m-%Y %H:%M:%S", lt);
"%Y-%m-%dT%H:%M:%S", &lt);
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);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment