[syslog-ng] [PATCH 2/3] afmongodb: Support type hints

Gergely Nagy algernon at balabit.hu
Thu Nov 15 18:24:17 CET 2012


Since we have type hints available, lets use them! This adds support
for 32- and 64-bit integers, datetime and boolean types (along with
the already existing string type, of course) for the mongodb
destination.

Signed-off-by: Gergely Nagy <algernon at balabit.hu>
---
 modules/afmongodb/afmongodb.c |   46 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)

diff --git a/modules/afmongodb/afmongodb.c b/modules/afmongodb/afmongodb.c
index 5230d8f..ea9c002 100644
--- a/modules/afmongodb/afmongodb.c
+++ b/modules/afmongodb/afmongodb.c
@@ -22,6 +22,7 @@
  */
 
 #include <time.h>
+#include <stdlib.h>
 
 #include "afmongodb.h"
 #include "afmongodb-parser.h"
@@ -369,7 +370,50 @@ afmongodb_vp_process_value(const gchar *name, const gchar *prefix,
   else
     o = (bson *)user_data;
 
-  bson_append_string (o, name, value, -1);
+  switch (type)
+    {
+    case TEMPLATE_TYPE_BOOLEAN:
+      {
+        if (value[0] == 'T' || value[0] == 't' || value[0] == '1')
+          bson_append_boolean (o, name, TRUE);
+        else
+          bson_append_boolean (o, name, FALSE);
+        break;
+      }
+    case TEMPLATE_TYPE_INT32:
+      {
+        gchar *endptr;
+        gint32 i = (gint32)strtoul(value, &endptr, 10);
+
+        if (endptr[0] == '\0')
+          bson_append_int32 (o, name, i);
+
+        break;
+      }
+    case TEMPLATE_TYPE_INT64:
+      {
+        gchar *endptr;
+        gint64 i = (gint64)strtoul(value, &endptr, 10);
+
+        if (endptr[0] == '\0')
+          bson_append_int64 (o, name, i);
+
+        break;
+      }
+    case TEMPLATE_TYPE_DATETIME:
+      {
+        gchar *endptr;
+        gint64 i = (gint64)strtoul(value, &endptr, 10) * 1000;
+
+        if (endptr[0] == '\0')
+          bson_append_utc_datetime (o, name, i);
+
+        break;
+      }
+    default:
+      bson_append_string (o, name, value, -1);
+      break;
+    }
 
   return FALSE;
 }
-- 
1.7.10.4




More information about the syslog-ng mailing list