]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
log/json: Convert log output to use JsonBuilder
authorJeff Lucovsky <jeff@lucovsky.org>
Sun, 4 Apr 2021 15:25:39 +0000 (11:25 -0400)
committerVictor Julien <vjulien@oisf.net>
Tue, 20 Dec 2022 07:51:33 +0000 (08:51 +0100)
This commit converts the file logging logic to use JsonBuilder instead
of libjansson.

src/util-debug.c

index bb437f16b6e2aba14b6169ad7a1345a48b6c8e6e..3903669b9028f1341445d60485f697e81897a0c1 100644 (file)
@@ -200,31 +200,29 @@ static int SCLogMessageJSON(struct timeval *tval, char *buffer, size_t buffer_si
         SCLogLevel log_level, const char *file, unsigned line, const char *function,
         const char *module, const char *message)
 {
-    json_t *js = json_object();
+    JsonBuilder *js = jb_new_object();
     if (unlikely(js == NULL))
         goto error;
-    json_t *ejs = json_object();
-    if (unlikely(ejs == NULL))
-        goto error;
 
     char timebuf[64];
     CreateIsoTimeString(tval, timebuf, sizeof(timebuf));
-    json_object_set_new(js, "timestamp", json_string(timebuf));
+    jb_set_string(js, "timestamp", timebuf);
 
     const char *s = SCMapEnumValueToName(log_level, sc_log_level_map);
     if (s != NULL) {
-        json_object_set_new(js, "log_level", json_string(s));
+        jb_set_string(js, "log_level", s);
     } else {
-        json_object_set_new(js, "log_level", json_string("INVALID"));
+        JB_SET_STRING(js, "log_level", "INVALID");
     }
 
-    json_object_set_new(js, "event_type", json_string("engine"));
+    JB_SET_STRING(js, "event_type", "engine");
+    jb_open_object(js, "engine");
 
     if (message)
-        json_object_set_new(ejs, "message", json_string(message));
+        jb_set_string(js, "message", message);
 
     if (t_thread_name[0] != '\0') {
-        json_object_set_new(ejs, "thread_name", json_string(t_thread_name));
+        jb_set_string(js, "thread_name", t_thread_name);
     }
 
     if (module) {
@@ -232,33 +230,28 @@ static int SCLogMessageJSON(struct timeval *tval, char *buffer, size_t buffer_si
         int dn_len = 0;
         const char *dn_name;
         dn_name = SCTransformModule(module, &dn_len);
-        json_object_set_new(ejs, "module", json_string(dn_name));
+        jb_set_string(js, "module", dn_name);
     }
 
     if (log_level >= SC_LOG_DEBUG) {
         if (function)
-            json_object_set_new(ejs, "function", json_string(function));
+            jb_set_string(js, "function", function);
 
         if (file)
-            json_object_set_new(ejs, "file", json_string(file));
+            jb_set_string(js, "file", file);
 
         if (line > 0)
-            json_object_set_new(ejs, "line", json_integer(line));
+            jb_set_uint(js, "line", line);
     }
+    jb_close(js); // engine
 
-    json_object_set_new(js, "engine", ejs);
-
-    char *js_s = json_dumps(js,
-            JSON_PRESERVE_ORDER|JSON_COMPACT|JSON_ENSURE_ASCII|
-            JSON_ESCAPE_SLASH);
-    snprintf(buffer, buffer_size, "%s", js_s);
-    free(js_s);
+    jb_close(js);
+    memcpy(buffer, jb_ptr(js), MIN(buffer_size, jb_len(js)));
 
-    json_object_del(js, "engine");
-    json_object_clear(js);
-    json_decref(js);
+    jb_free(js);
 
     return 0;
+
 error:
     return -1;
 }