[syslog-ng] [PATCH 4/7] tfjson: Convert to value_pairs_walk()

Gergely Nagy algernon at balabit.hu
Fri Sep 14 11:51:58 CEST 2012


Now that we can process dotted-notation properly, make use of that!

With this change, $(format-json) becomes smart enough to turn
dotted-notation back into proper structures. It does not support
arrays yet, however.

Also added a test case that uses dotted-notation, and tests that it
gets correctly formatted.

Signed-off-by: Gergely Nagy <algernon at balabit.hu>
---
 modules/json/tests/test_json.c |    5 +++-
 modules/json/tfjson.c          |   53 ++++++++++++++++++++++++++++++++++++----
 2 files changed, 52 insertions(+), 6 deletions(-)

diff --git a/modules/json/tests/test_json.c b/modules/json/tests/test_json.c
index 582b264..f188b7f 100644
--- a/modules/json/tests/test_json.c
+++ b/modules/json/tests/test_json.c
@@ -7,7 +7,10 @@ test_format_json(void)
 {
   assert_template_format("$(format-json MSG=$MSG)", "{\"MSG\":\"árvíztűrőtükörfúrógép\"}");
   assert_template_format_with_context("$(format-json MSG=$MSG)", "{\"MSG\":\"árvíztűrőtükörfúrógép\"}{\"MSG\":\"árvíztűrőtükörfúrógép\"}");
-  assert_template_format("$(format-json --scope rfc3164)", "{\"DATE\":\"Feb 11 19:58:35\",\"FACILITY\":\"local3\",\"HOST\":\"bzorp\",\"MESSAGE\":\"árvíztűrőtükörfúrógép\",\"PID\":\"23323\",\"PRIORITY\":\"err\",\"PROGRAM\":\"syslog-ng\"}");
+  assert_template_format("$(format-json --scope rfc3164)", "{\"PROGRAM\":\"syslog-ng\",\"PRIORITY\":\"err\",\"PID\":\"23323\",\"MESSAGE\":\"árvíztűrőtükörfúrógép\",\"HOST\":\"bzorp\",\"FACILITY\":\"local3\",\"DATE\":\"Feb 11 19:58:35\"}");
+  assert_template_format("$(format-json msg.text=$MSG msg.id=42 host=bzorp)", "{\"msg\":{\"text\":\"árvíztűrőtükörfúrógép\",\"id\":\"42\"},\"host\":\"bzorp\"}");
+  assert_template_format("$(format-json msg.text.str=$MSG msg.text.len=42 msg.id=42 host=bzorp)",
+                         "{\"msg\":{\"text\":{\"str\":\"árvíztűrőtükörfúrógép\",\"len\":\"42\"},\"id\":\"42\"},\"host\":\"bzorp\"}");
 }
 
 int
diff --git a/modules/json/tfjson.c b/modules/json/tfjson.c
index 8716559..c8a2472 100644
--- a/modules/json/tfjson.c
+++ b/modules/json/tfjson.c
@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2002-2011 BalaBit IT Ltd, Budapest, Hungary
+ * Copyright (c) 2011-2012 BalaBit IT Ltd, Budapest, Hungary
  * Copyright (c) 2011 Balint Kovacs <blint at balabit.hu>
- * Copyright (c) 2011 Gergely Nagy <algernon at balabit.hu>
+ * Copyright (c) 2011-2012 Gergely Nagy <algernon at balabit.hu>
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 as published
@@ -79,11 +79,52 @@ tf_json_object_to_string (struct json_object *jso,
 }
 
 static gboolean
-tf_json_foreach (const gchar *name, const gchar *value, gpointer user_data)
+tf_json_obj_start(const gchar *name,
+                  const gchar *prefix, gpointer *prefix_data,
+                  const gchar *prev, gpointer *prev_data,
+                  gpointer user_data)
 {
-  struct json_object *root = (struct json_object *)user_data;
+  struct json_object *o;
+
+  if (prefix_data)
+    {
+      o = json_object_new_object();
+      *prefix_data = o;
+      o->_to_json_string = tf_json_object_to_string;
+    }
+  return FALSE;
+}
+
+static gboolean
+tf_json_obj_end(const gchar *name,
+                const gchar *prefix, gpointer *prefix_data,
+                const gchar *prev, gpointer *prev_data,
+                gpointer user_data)
+{
+  struct json_object *root;
+
+  if (prev_data)
+    root = (struct json_object *)*prev_data;
+  else
+    root = (struct json_object *)user_data;
+
+  if (prefix_data)
+    json_object_object_add (root, (gchar *) name, (struct json_object *)*prefix_data);
+  return FALSE;
+}
+
+static gboolean
+tf_json_value(const gchar *name, const gchar *prefix, const gchar *value,
+              gpointer *prefix_data, gpointer user_data)
+{
+  struct json_object *root;
   struct json_object *this;
 
+  if (prefix_data)
+    root = (struct json_object *)*prefix_data;
+  else
+    root = (struct json_object *)user_data;
+
   this = json_object_new_string ((gchar *) value);
   json_object_object_add (root, (gchar *) name, this);
 
@@ -98,7 +139,9 @@ tf_json_append(GString *result, ValuePairs *vp, LogMessage *msg)
   json = json_object_new_object();
   json->_to_json_string = tf_json_object_to_string;
 
-  value_pairs_foreach(vp, tf_json_foreach, msg, 0, json);
+  value_pairs_walk(vp,
+                   tf_json_obj_start, tf_json_value, tf_json_obj_end,
+                   msg, 0, json);
 
   g_string_append(result, json_object_to_json_string (json));
   json_object_put(json);
-- 
1.7.10.4




More information about the syslog-ng mailing list