openbmc/build/tmp/deploy/sdk/witherspoon-2019-08-08/sysroots/armv6-openbmc-linux-gnueabi/usr/src/debug/rsyslog/8.1903.0-r0/rsyslog-8.1903.0/template.c
rsRetVal tplToJSON(struct template* pTpl, smsg_t* pMsg,
                   struct json_object** pjson, struct syslogTime* ttNow)
{
    struct templateEntry* pTpe;
    rs_size_t propLen;
    unsigned short bMustBeFreed;
    uchar* pVal;
    struct json_object *json, *jsonf;
    rsRetVal localRet;
    DEFiRet;

    if (pTpl->bHaveSubtree)
    {
        if (jsonFind(pMsg->json, &pTpl->subtree, pjson) != RS_RET_OK)
            *pjson = NULL;
        if (*pjson == NULL)
        {
            /* we need to have a root object! */
            *pjson = json_object_new_object();
        }
        else
        {
            json_object_get(*pjson); /* inc refcount */
        }
        FINALIZE;
    }

    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);
            json_object_object_add(json, (char*)pTpe->fieldName, jsonf);
        }
        else if (pTpe->eEntryType == FIELD)
        {
            if (pTpe->data.field.msgProp.id == PROP_CEE ||
                pTpe->data.field.msgProp.id == PROP_LOCAL_VAR ||
                pTpe->data.field.msgProp.id == PROP_GLOBAL_VAR)
            {
                localRet =
                    msgGetJSONPropJSON(pMsg, &pTpe->data.field.msgProp, &jsonf);
                if (localRet == RS_RET_OK)
                {
                    json_object_object_add(json, (char*)pTpe->fieldName,
                                           json_object_get(jsonf));
                }
                else
                {
                    DBGPRINTF("tplToJSON: error %d looking up property %s\n",
                              localRet, pTpe->fieldName);
                    if (pTpe->data.field.options.bMandatory)
                    {
                        json_object_object_add(json, (char*)pTpe->fieldName,
                                               NULL);
                    }
                }
            }
            else
            {
                pVal = (uchar*)MsgGetProp(pMsg, pTpe, &pTpe->data.field.msgProp,
                                          &propLen, &bMustBeFreed, ttNow);
                if (pTpe->data.field.options.bMandatory || propLen > 0)
                {
                    jsonf =
                        json_object_new_string_len((char*)pVal, propLen + 1);
                    json_object_object_add(json, (char*)pTpe->fieldName, jsonf);
                }
                if (bMustBeFreed)
                { /* json-c makes its own private copy! */
                    free(pVal);
                }
            }
        }
    }
    assert(iRet == RS_RET_OK);
    *pjson = json;

finalize_it:
    RETiRet;
}
/* This functions converts a template into a json object.
 * For further general details, see the very similar funtion
 * tpltoString().
 * rgerhards, 2012-08-29
 */
This functions converts a template into a json object.
For further general details, see the very similar funtion
tpltoString().
This functions converts a template into a json object . For further general details see the very similar funtion tpltoString () .
rsRetVal tplToJSON(struct template* pTpl, smsg_t* pMsg,
                   struct json_object** pjson, struct syslogTime* ttNow)
{
    struct templateEntry* pTpe;
    rs_size_t propLen;
    unsigned short bMustBeFreed;
    uchar* pVal;
    struct json_object *json, *jsonf;
    rsRetVal localRet;
    DEFiRet;
    if (pTpl->bHaveSubtree)
    {
        if (jsonFind(pMsg->json, &pTpl->subtree, pjson) != RS_RET_OK)
            *pjson = NULL;
        if (*pjson == NULL)
        {
            *pjson = json_object_new_object();
        }
        else
        {
            json_object_get(*pjson); 
        }
        FINALIZE;
    }
    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);
            json_object_object_add(json, (char*)pTpe->fieldName, jsonf);
        }
        else if (pTpe->eEntryType == FIELD)
        {
            if (pTpe->data.field.msgProp.id == PROP_CEE ||
                pTpe->data.field.msgProp.id == PROP_LOCAL_VAR ||
                pTpe->data.field.msgProp.id == PROP_GLOBAL_VAR)
            {
                localRet =
                    msgGetJSONPropJSON(pMsg, &pTpe->data.field.msgProp, &jsonf);
                if (localRet == RS_RET_OK)
                {
                    json_object_object_add(json, (char*)pTpe->fieldName,
                                           json_object_get(jsonf));
                }
                else
                {
                    DBGPRINTF("tplToJSON: error %d looking up property %s\n",
                              localRet, pTpe->fieldName);
                    if (pTpe->data.field.options.bMandatory)
                    {
                        json_object_object_add(json, (char*)pTpe->fieldName,
                                               NULL);
                    }
                }
            }
            else
            {
                pVal = (uchar*)MsgGetProp(pMsg, pTpe, &pTpe->data.field.msgProp,
                                          &propLen, &bMustBeFreed, ttNow);
                if (pTpe->data.field.options.bMandatory || propLen > 0)
                {
                    jsonf =
                        json_object_new_string_len((char*)pVal, propLen + 1);
                    json_object_object_add(json, (char*)pTpe->fieldName, jsonf);
                }
                if (bMustBeFreed)
                { 
                    free(pVal);
                }
            }
        }
    }
    assert(iRet == RS_RET_OK);
    *pjson = json;
finalize_it:
    RETiRet;
}
rsRetVal tplToJSON ( struct template * pTpl , smsg_t * pMsg , struct json_object * * pjson , struct syslogTime * ttNow ) { struct templateEntry * pTpe ; rs_size_t propLen ; unsigned short bMustBeFreed ; uchar * pVal ; struct json_object * json , * jsonf ; rsRetVal localRet ; DEFiRet ; if ( pTpl -> bHaveSubtree ) { if ( jsonFind ( pMsg -> json , & pTpl -> subtree , pjson ) != RS_RET_OK ) * pjson = NULL ; if ( * pjson == NULL ) { * pjson = json_object_new_object ( ) ; } else { json_object_get ( * pjson ) ; } FINALIZE ; } 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 ) ; json_object_object_add ( json , ( char * ) pTpe -> fieldName , jsonf ) ; } else if ( pTpe -> eEntryType == FIELD ) { if ( pTpe -> data . field . msgProp . id == PROP_CEE || pTpe -> data . field . msgProp . id == PROP_LOCAL_VAR || pTpe -> data . field . msgProp . id == PROP_GLOBAL_VAR ) { localRet = msgGetJSONPropJSON ( pMsg , & pTpe -> data . field . msgProp , & jsonf ) ; if ( localRet == RS_RET_OK ) { json_object_object_add ( json , ( char * ) pTpe -> fieldName , json_object_get ( jsonf ) ) ; } else { DBGPRINTF ( " \n " , localRet , pTpe -> fieldName ) ; if ( pTpe -> data . field . options . bMandatory ) { json_object_object_add ( json , ( char * ) pTpe -> fieldName , NULL ) ; } } } else { pVal = ( uchar * ) MsgGetProp ( pMsg , pTpe , & pTpe -> data . field . msgProp , & propLen , & bMustBeFreed , ttNow ) ; if ( pTpe -> data . field . options . bMandatory || propLen > 0 ) { jsonf = json_object_new_string_len ( ( char * ) pVal , propLen + 1 ) ; json_object_object_add ( json , ( char * ) pTpe -> fieldName , jsonf ) ; } if ( bMustBeFreed ) { free ( pVal ) ; } } } } assert ( iRet == RS_RET_OK ) ; * pjson = json ; finalize_it : RETiRet ; }
This functions converts a template into a json object.
This functions converts a template into a json object .
/* we need to have a root object! */ /* inc refcount */ /* json-c makes its own private copy! */