[syslog-ng] [PATCH] tfjson: Support either json-c or json-glib.

Gergely Nagy algernon at balabit.hu
Sun May 8 14:55:46 CEST 2011


Add the ability to build the module with either json-c, or json-glib
(version 0.12 or later). If both are present, we'll prefer json-c.

Requested-by: Peter Czanik <czanik at balabit.hu>
Signed-off-by: Gergely Nagy <algernon at balabit.hu>
---
 configure.in            |   19 ++++++++++--
 modules/tfjson/tfjson.c |   73 ++++++++++++++++++++++++++++++++++++++---------
 2 files changed, 75 insertions(+), 17 deletions(-)

diff --git a/configure.in b/configure.in
index 9c03b65..87de97f 100644
--- a/configure.in
+++ b/configure.in
@@ -25,7 +25,8 @@ EVTLOG_MIN_VERSION="0.2.12"
 OPENSSL_MIN_VERSION="0.9.8"
 LIBDBI_MIN_VERSION="0.8.0"
 IVYKIS_MIN_VERSION="0.18"
-JSON_MIN_VERSION="0.7"
+JSON_C_MIN_VERSION="0.7"
+JSON_GLIB_MIN_VERSION="0.12"
 PCRE_MIN_VERSION="6.1"
 
 dnl ***************************************************************************
@@ -563,7 +564,10 @@ fi
 dnl ***************************************************************************
 dnl json headers/libraries
 dnl ***************************************************************************
-PKG_CHECK_MODULES(JSON, json >= $JSON_MIN_VERSION,, JSON_LIBS="")
+PKG_CHECK_MODULES(JSON, json >= $JSON_C_MIN_VERSION, JSON_TYPE="C", JSON_LIBS="")
+if test "x$JSON_TYPE" = "x"; then
+   PKG_CHECK_MODULES(JSON, json-glib-1.0 >= $JSON_GLIB_MIN_VERSION, JSON_TYPE="glib", JSON_LIBS="")
+fi
 
 dnl ***************************************************************************
 dnl pcre headers/libraries
@@ -813,7 +817,7 @@ if test "x$enable_json" = "xauto"; then
                 enable_json="yes"
         fi
 elif test "x$enable_json" = "xyes" -a -z "$JSON_LIBS"; then
-        AC_MSG_ERROR(Cannot find json-c version >= $JSON_MIN_VERSION: is pkg-config in path?)
+        AC_MSG_ERROR(Cannot find json-c version >= $JSON_C_MIN_VERSION or json-glib-1.0 >= $JSON_GLIB_MIN_VERSION: is pkg-config in path?)
 fi
 
 if test "x$enable_systemd" = "xauto"; then
@@ -964,6 +968,15 @@ AC_DEFINE_UNQUOTED(ENABLE_PCRE, `enable_value $enable_pcre`, [Enable PCRE suppor
 AC_DEFINE_UNQUOTED(ENABLE_ENV_WRAPPER, `enable_value $enable_env_wrapper`, [Enable environment wrapper support])
 AC_DEFINE_UNQUOTED(ENABLE_SYSTEMD, `enable_value $enable_systemd`, [Enable systemd support])
 
+case "$JSON_TYPE" in
+     "C")
+	AC_DEFINE_UNQUOTED(HAVE_JSON_C, 1, [Have json-c])
+	;;
+     "glib")
+	AC_DEFINE_UNQUOTED(HAVE_JSON_GLIB, 1, [Have json-glib])
+	;;
+esac
+
 AM_CONDITIONAL(ENABLE_ENV_WRAPPER, [test "$enable_env_wrapper" = "yes"])
 AM_CONDITIONAL(ENABLE_SYSTEMD, [test "$enable_systemd" = "yes"])
 AM_CONDITIONAL(ENABLE_SSL, [test "$enable_ssl" = "yes"])
diff --git a/modules/tfjson/tfjson.c b/modules/tfjson/tfjson.c
index 07fc76e..7155f15 100644
--- a/modules/tfjson/tfjson.c
+++ b/modules/tfjson/tfjson.c
@@ -25,7 +25,15 @@
 #include "cfg.h"
 #include "value-pairs.h"
 
+#include "config.h"
+
+#ifdef HAVE_JSON_C
 #include <json.h>
+#endif
+
+#ifdef HAVE_JSON_GLIB
+#include <json-glib/json-glib.h>
+#endif
 
 static gboolean
 tf_json_prepare(LogTemplateFunction *self, LogTemplate *parent,
@@ -35,6 +43,8 @@ tf_json_prepare(LogTemplateFunction *self, LogTemplate *parent,
 {
   ValuePairs *vp;
 
+  g_type_init ();
+
   vp = value_pairs_new_from_cmdline (parent->cfg, argc, argv, error);
   if (!vp)
     return FALSE;
@@ -45,6 +55,7 @@ tf_json_prepare(LogTemplateFunction *self, LogTemplate *parent,
   return TRUE;
 }
 
+#if HAVE_JSON_C
 static gboolean
 tf_json_foreach (const gchar *name, const gchar *value, gpointer user_data)
 {
@@ -57,16 +68,57 @@ tf_json_foreach (const gchar *name, const gchar *value, gpointer user_data)
   return FALSE;
 }
 
-static struct json_object *
-tf_json_format_message(ValuePairs *vp, LogMessage *msg)
+static void
+tf_json_append(GString *result, ValuePairs *vp, LogMessage *msg)
+{
+  struct json_object *json;
+
+  json = json_object_new_object();
+
+  value_pairs_foreach(vp, tf_json_foreach, msg, 0, json);
+
+  g_string_append(result, json_object_to_json_string (json));
+  json_object_put(json);
+}
+#endif
+
+#if HAVE_JSON_GLIB
+static gboolean
+tf_json_foreach (const gchar *name, const gchar *value, gpointer user_data)
 {
-  struct json_object *root;
+  JsonBuilder *builder = (JsonBuilder *)user_data;
+
+  json_builder_set_member_name(builder, name);
+  json_builder_add_string_value(builder, value);
+
+  return FALSE;
+}
+
+static void
+tf_json_append(GString *result, ValuePairs *vp, LogMessage *msg)
+{
+  JsonBuilder *builder;
+  JsonGenerator *gen;
+  gchar *str;
+
+  builder = json_builder_new();
+  json_builder_begin_object(builder);
+
+  value_pairs_foreach(vp, tf_json_foreach, msg, 0, builder);
+
+  json_builder_end_object(builder);
+
+  gen = json_generator_new();
+  json_generator_set_root(gen, json_builder_get_root(builder));
+  str = json_generator_to_data(gen, NULL);
 
-  root = json_object_new_object();
-  value_pairs_foreach (vp, tf_json_foreach, msg, 0, root);
+  g_object_unref(gen);
+  g_object_unref(builder);
 
-  return root;
+  g_string_append(result, str);
+  g_free(str);
 }
+#endif
 
 static void
 tf_json_call(LogTemplateFunction *self, gpointer state, GPtrArray *arg_bufs,
@@ -77,14 +129,7 @@ tf_json_call(LogTemplateFunction *self, gpointer state, GPtrArray *arg_bufs,
   ValuePairs *vp = (ValuePairs *)state;
 
   for (i = 0; i < num_messages; i++)
-    {
-      LogMessage *msg = messages[i];
-      struct json_object *json;
-
-      json = tf_json_format_message(vp, msg);
-      g_string_append(result, json_object_to_json_string (json));
-      json_object_put(json);
-    }
+    tf_json_append(result, vp, messages[i]);
 }
 
 static void
-- 
1.7.2.5



More information about the syslog-ng mailing list