From: Jason Ish Date: Thu, 22 Dec 2022 15:55:13 +0000 (-0600) Subject: logging: add new date format: YYYY-MM-DD HH:MM:SS X-Git-Tag: suricata-7.0.0-rc1~176 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bac8b8d26de53dc817a8153e81fdb82fcd0e281a;p=thirdparty%2Fsuricata.git logging: add new date format: YYYY-MM-DD HH:MM:SS Adds a new logging format character, %z that uses a more standard time format of "YYYY-MM-DD HH:MM:SS". Ticket #5764 --- diff --git a/src/util-debug.c b/src/util-debug.c index fb91c6a4b3..bfac174852 100644 --- a/src/util-debug.c +++ b/src/util-debug.c @@ -383,8 +383,9 @@ static SCError SCLogMessageGetBuffer(struct timeval *tval, int color, SCLogOPTyp strlcat(local_format, "%M", sizeof(local_format)); char *temp_fmt = local_format; char *substr = temp_fmt; + struct tm local_tm; - while ( (temp_fmt = strchr(temp_fmt, SC_LOG_FMT_PREFIX)) ) { + while ((temp_fmt = strchr(temp_fmt, SC_LOG_FMT_PREFIX))) { if ((temp - buffer) > SC_LOG_MAX_LOG_MSG_LEN) { return 0; } @@ -392,7 +393,23 @@ static SCError SCLogMessageGetBuffer(struct timeval *tval, int color, SCLogOPTyp case SC_LOG_FMT_TIME: temp_fmt[0] = '\0'; - struct tm local_tm; + tms = SCLocalTime(tval->tv_sec, &local_tm); + + cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), + "%s%s%04d-%02d-%02d %02d:%02d:%02d%s", substr, green, tms->tm_year + 1900, + tms->tm_mon + 1, tms->tm_mday, tms->tm_hour, tms->tm_min, tms->tm_sec, + reset); + if (cw < 0) + return -1; + temp += cw; + temp_fmt++; + substr = temp_fmt; + substr++; + break; + + case SC_LOG_FMT_TIME_LEGACY: + temp_fmt[0] = '\0'; + tms = SCLocalTime(tval->tv_sec, &local_tm); cw = snprintf(temp, SC_LOG_MAX_LOG_MSG_LEN - (temp - buffer), @@ -588,7 +605,7 @@ static SCError SCLogMessageGetBuffer(struct timeval *tval, int color, SCLogOPTyp } } temp_fmt++; - } + } if ((temp - buffer) > SC_LOG_MAX_LOG_MSG_LEN) { return 0; } diff --git a/src/util-debug.h b/src/util-debug.h index 1e6e880da3..aab6c84d3d 100644 --- a/src/util-debug.h +++ b/src/util-debug.h @@ -188,7 +188,8 @@ typedef struct SCLogConfig_ { } SCLogConfig; /* The different log format specifiers supported by the API */ -#define SC_LOG_FMT_TIME 't' /* Timestamp in standard format */ +#define SC_LOG_FMT_TIME 'z' /* Timestamp in RFC3339 like format */ +#define SC_LOG_FMT_TIME_LEGACY 't' /* Timestamp in legacy format */ #define SC_LOG_FMT_PID 'p' /* PID */ #define SC_LOG_FMT_TID 'i' /* Thread ID */ #define SC_LOG_FMT_TM 'm' /* Thread module name */