summaryrefslogtreecommitdiffstats
path: root/template.c
diff options
context:
space:
mode:
authorRainer Gerhards <rgerhards@adiscon.com>2012-08-29 14:19:28 +0200
committerRainer Gerhards <rgerhards@adiscon.com>2012-08-29 14:19:28 +0200
commitf9dc2f787630ec9729b02ef73dd8dc5614160f13 (patch)
tree40e2d55ff76ee82bcbb57ffe6a95070de65ef3d1 /template.c
parent2a4e1c12f235d6914d650dd29d56df152b014209 (diff)
parenta5e51ced933cb73bd3755bffb81aceceb3f0f953 (diff)
downloadrsyslog-f9dc2f787630ec9729b02ef73dd8dc5614160f13.tar.gz
rsyslog-f9dc2f787630ec9729b02ef73dd8dc5614160f13.tar.bz2
rsyslog-f9dc2f787630ec9729b02ef73dd8dc5614160f13.zip
Merge branch 'master' into v7-devel
Diffstat (limited to 'template.c')
-rw-r--r--template.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/template.c b/template.c
index 115b5ec2..e49dac1d 100644
--- a/template.c
+++ b/template.c
@@ -34,6 +34,7 @@
#include <string.h>
#include <ctype.h>
#include <assert.h>
+#include <json/json.h>
#include "stringbuf.h"
#include "syslogd-types.h"
#include "template.h"
@@ -270,6 +271,50 @@ finalize_it:
}
+/* This functions converts a template into a json object.
+ * For further general details, see the very similar funtion
+ * tpltoString().
+ * rgerhards, 2012-08-29
+ */
+rsRetVal tplToJSON(struct template *pTpl, msg_t *pMsg, struct json_object **pjson)
+{
+ struct templateEntry *pTpe;
+ char *cstr;
+ size_t propLen;
+ unsigned short bMustBeFreed;
+ uchar *pVal;
+ struct json_object *json, *jsonf;
+ DEFiRet;
+
+ assert(pTpl != NULL);
+ assert(pMsg != NULL);
+ assert(json != NULL);
+
+ json = json_object_new_object();
+ for(pTpe = pTpl->pEntryRoot ; pTpe != NULL ; pTpe = pTpe->pNext) {
+ if(pTpe->eEntryType == CONSTANT) {
+ if(pTpe->fieldName == NULL)
+ continue;
+ jsonf = json_object_new_string((char*) pTpe->data.constant.pConstant);
+ } else if(pTpe->eEntryType == FIELD) {
+ pVal = (uchar*) MsgGetProp(pMsg, pTpe, pTpe->data.field.propid,
+ pTpe->data.field.propName, &propLen, &bMustBeFreed);
+ jsonf = json_object_new_string_len((char*)pVal, propLen);
+ if(bMustBeFreed) { /* json-c makes its own private copy! */
+ free(pVal);
+ }
+ }
+ /* TODO: unify strings, handling currently quite inefficient! */
+ cstr = es_str2cstr(pTpe->fieldName, NULL);
+ json_object_object_add(json, cstr, jsonf);
+ free(cstr);
+ }
+
+ *pjson = (iRet == RS_RET_OK) ? json : NULL;
+ RETiRet;
+}
+
+
/* Helper to doEscape. This is called if doEscape
* runs out of memory allocating the escaped string.
* Then we are in trouble. We can