]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
logging: add new date format: YYYY-MM-DD HH:MM:SS
authorJason Ish <jason.ish@oisf.net>
Thu, 22 Dec 2022 15:55:13 +0000 (09:55 -0600)
committerVictor Julien <vjulien@oisf.net>
Tue, 10 Jan 2023 10:29:58 +0000 (11:29 +0100)
Adds a new logging format character, %z that uses a more standard time
format of "YYYY-MM-DD HH:MM:SS".

Ticket #5764

src/util-debug.c
src/util-debug.h

index fb91c6a4b3cf8a2499460e71298879220f699f2a..bfac1748523560b8b8ca19dd1cf80340dd8f9af2 100644 (file)
@@ -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;
     }
index 1e6e880da3acd0d9821ae550293b5b2de35e04c7..aab6c84d3d91f0249c65f4ea0f96ded6cb782750 100644 (file)
@@ -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 */