From c1a8908b1e508dbd8760b17afd02666ef62b2fbc Mon Sep 17 00:00:00 2001 From: Andre Lorbach Date: Mon, 11 Feb 2008 10:25:18 +0000 Subject: Added debug function logerrorVar, which can have variable number of parameters. Replaced debug prints in omsnmp with logerrorVar where needed. --- plugins/omsnmp/omsnmp.c | 12 ++++++------ syslogd.c | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/plugins/omsnmp/omsnmp.c b/plugins/omsnmp/omsnmp.c index 2dce5791..d9324135 100644 --- a/plugins/omsnmp/omsnmp.c +++ b/plugins/omsnmp/omsnmp.c @@ -181,7 +181,7 @@ static rsRetVal omsnmp_sendsnmp(instanceData *pData, uchar *psz) if (ss == NULL) { /*TODO diagnose snmp_open errors with the input netsnmp_session pointer */ - dbgprintf("omsnmp_sendsnmp: snmp_open to host '%s' on Port '%d' failed\n", pData->szTarget, pData->iPort); + logerrorVar("omsnmp_sendsnmp: snmp_open to host '%s' on Port '%d' failed\n", pData->szTarget, pData->iPort); return RS_RET_FALSE; } @@ -194,7 +194,7 @@ static rsRetVal omsnmp_sendsnmp(instanceData *pData, uchar *psz) if (!snmp_parse_oid( (char*) pData->szEnterpriseOID, enterpriseoid, &enterpriseoidlen )) { strErr = snmp_api_errstring(snmp_errno); - dbgprintf("omsnmp_sendsnmp: Parsing EnterpriseOID failed '%s' with error '%s' \n", pData->szSyslogMessageOID, strErr); + logerrorVar("omsnmp_sendsnmp: Parsing EnterpriseOID failed '%s' with error '%s' \n", pData->szSyslogMessageOID, strErr); /* CLEANUP */ snmp_free_pdu(pdu); @@ -233,7 +233,7 @@ static rsRetVal omsnmp_sendsnmp(instanceData *pData, uchar *psz) if ( snmp_add_var(pdu, objid_snmptrap, sizeof(objid_snmptrap) / sizeof(oid), 'o', (char*) pData->szSyslogMessageOID ) != 0) { strErr = snmp_api_errstring(snmp_errno); - dbgprintf("omsnmp_sendsnmp: Adding trap OID failed '%s' with error '%s' \n", pData->szSyslogMessageOID, strErr); + logerrorVar("omsnmp_sendsnmp: Adding trap OID failed '%s' with error '%s' \n", pData->szSyslogMessageOID, strErr); /* CLEANUP */ snmp_free_pdu(pdu); @@ -252,7 +252,7 @@ static rsRetVal omsnmp_sendsnmp(instanceData *pData, uchar *psz) if (iErrCode) { const char *str = snmp_api_errstring(iErrCode); - dbgprintf( "omsnmp_sendsnmp: Invalid SyslogMessage OID, error code '%d' - '%s'\n", iErrCode, str ); + logerrorVar( "omsnmp_sendsnmp: Invalid SyslogMessage OID, error code '%d' - '%s'\n", iErrCode, str ); /* CLEANUP */ snmp_free_pdu(pdu); @@ -263,7 +263,7 @@ static rsRetVal omsnmp_sendsnmp(instanceData *pData, uchar *psz) else { strErr = snmp_api_errstring(snmp_errno); - dbgprintf("omsnmp_sendsnmp: Parsing SyslogMessageOID failed '%s' with error '%s' \n", pData->szSyslogMessageOID, strErr); + logerrorVar("omsnmp_sendsnmp: Parsing SyslogMessageOID failed '%s' with error '%s' \n", pData->szSyslogMessageOID, strErr); /* CLEANUP */ snmp_free_pdu(pdu); @@ -282,7 +282,7 @@ static rsRetVal omsnmp_sendsnmp(instanceData *pData, uchar *psz) iErrorCode = session.s_snmp_errno; /* Debug Output! */ - dbgprintf( "omsnmp_sendsnmp: snmp_send failed error '%d', Description='%s'\n", iErrorCode*(-1), api_errors[iErrorCode*(-1)]); + logerrorVar( "omsnmp_sendsnmp: snmp_send failed error '%d', Description='%s'\n", iErrorCode*(-1), api_errors[iErrorCode*(-1)]); /* Important we must free the PDU! */ snmp_free_pdu(pdu); diff --git a/syslogd.c b/syslogd.c index 7434c8b7..de4c311e 100644 --- a/syslogd.c +++ b/syslogd.c @@ -2334,6 +2334,32 @@ void logerrorInt(char *type, int errCode) return; } +/* + * Error Output with variable number of parameters. + * This functions works pretty much like dbgprintf, + * except it logs an error. + */ +void logerrorVar(char *fmt, ...) +{ + va_list ap; + char buf[1024]; + size_t lenBuf; + + /* Format parameters */ + va_start(ap, fmt); + lenBuf = vsnprintf(buf, sizeof(buf), fmt, ap); + if(lenBuf >= sizeof(buf)) { + /* if our buffer was too small, we simply truncate. */ + lenBuf--; + } + va_end(ap); + + /* Log the error now */ + buf[sizeof(buf)/sizeof(char) - 1] = '\0'; /* just to be on the safe side... */ + logerror(buf); + return; +} + /* Print syslogd errors some place. */ void logerror(char *type) -- cgit