From 30a43cc865f3d7247ec0356566e57b9252e2e6c1 Mon Sep 17 00:00:00 2001 From: Miloslav Trmač Date: Sat, 11 Aug 2012 07:30:36 +0200 Subject: Implement ACT_FIELDS_PASSING, test in mongodb MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The mongodb test contains only debug printfs. Example template, inspired by writeMongoDB_msg: $template MongoTemplate,"%hostname::::sys%%timereported::::time%%timegenerated::::time_rcvd%%msg%%syslogfacility-text::::syslog_fac%%syslogseverity-text::::syslog_server%%syslogtag::::syslog_tag%%programname::::procid%%procid::::pid%%$!foo::::foo%%$!abc::::renamed%" Note that JSON escaping is actually undesirable in this mode (should it be silently ignored?), $!all-json doesn't yet work as expected, and all data is stored as strings. Signed-off-by: Miloslav Trmač --- template.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'template.c') diff --git a/template.c b/template.c index 768608f1..d422f8ee 100644 --- a/template.c +++ b/template.c @@ -269,6 +269,65 @@ finalize_it: RETiRet; } +/* This functions converts a template into an array of templateField structures. + * For further general details, see the very similar funtion + * tpltoString(). + * The caller is repsonsible for destroying that array as well as all of its + * elements (but not the fieldName strings). + */ +rsRetVal tplToFields(struct template *pTpl, msg_t *pMsg, + struct templateField **pFields) +{ + DEFiRet; + struct templateEntry *pTpe; + struct templateField *fields; + size_t i, propLen; + unsigned short bMustBeFreed; + uchar *pVal; + + assert(pTpl != NULL); + assert(pMsg != NULL); + assert(pFields != NULL); + + /* loop through the template. We obtain one value, create a + * private copy (if necessary), add it to the string array + * and then on to the next until we have processed everything. + */ + + /* The zeroization implicitly terminates the output array. */ + CHKmalloc(fields = calloc(pTpl->tpenElements + 1, sizeof(*fields))); + + i = 0; + for(pTpe = pTpl->pEntryRoot; pTpe != NULL; pTpe = pTpe->pNext) { + if(pTpe->eEntryType == CONSTANT) { + /* Completely ingore this - we don't know a field name + to use anyway. */ + } else if(pTpe->eEntryType == FIELD) { + fields[i].fieldName = pTpe->data.field.fieldName; + pVal = MsgGetProp(pMsg, pTpe, pTpe->data.field.propid, + pTpe->data.field.propName, &propLen, + &bMustBeFreed); + if(bMustBeFreed) { /* if it must be freed, it is our own private copy... */ + fields[i].value = pVal; /* ... so we can use it! */ + } else { + CHKmalloc(fields[i].value = (uchar*)strdup((char*) pVal)); + } + i++; + } + } + +finalize_it: + if(iRet != RS_RET_OK && fields != NULL) { + for(i = 0; fields[i].fieldName != NULL; i++) + free(fields[i].value); + free(fields); + fields = NULL; + } + *pFields = fields; + + RETiRet; +} + /* Helper to doEscape. This is called if doEscape * runs out of memory allocating the escaped string. -- cgit