/** * This snmp logging function allows variable argument list given the * specified priority, format and a populated va_list structure. * The default logfile this function writes to is /var/log/snmpd.log. * * @param priority is an integer representing the type of message to be written * to the snmp log file. The types are errors, warning, and information. * - The error types are: * - LOG_EMERG system is unusable * - LOG_ALERT action must be taken immediately * - LOG_CRIT critical conditions * - LOG_ERR error conditions * - The warning type is: * - LOG_WARNING warning conditions * - The information types are: * - LOG_NOTICE normal but significant condition * - LOG_INFO informational * - LOG_DEBUG debug-level messages * * @param format is a pointer to a char representing the variable argument list * format used. * * @param ap is a va_list type used to traverse the list of arguments. * * @return Returns 0 on success, -1 when the code could not format the log- * string, -2 when dynamic memory could not be allocated if the length * of the log buffer is greater then 1024 bytes. For each of these * errors a LOG_ERR messgae is written to the logfile. * * @see snmp_log */ int snmp_vlog(int priority, const char *format, va_list ap) { char buffer[LOGLENGTH]; int length; char *dynamic; va_list aq; va_copy(aq, ap); length = vsnprintf(buffer, LOGLENGTH, format, ap); va_end(ap); if (length == 0) { #ifdef NEED_VA_END_AFTER_VA_COPY va_end(aq); #endif return (0); /* Empty string */ } if (length == -1) { snmp_log_string(LOG_ERR, "Could not format log-string\n"); #ifdef NEED_VA_END_AFTER_VA_COPY va_end(aq); #endif return (-1); } if (length < LOGLENGTH) { snmp_log_string(priority, buffer); #ifdef NEED_VA_END_AFTER_VA_COPY va_end(aq); #endif return (0); } dynamic = (char *) malloc(length + 1); if (dynamic == NULL) { snmp_log_string(LOG_ERR, "Could not allocate memory for log-message\n"); snmp_log_string(priority, buffer); #ifdef NEED_VA_END_AFTER_VA_COPY va_end(aq); #endif return (-2); } vsnprintf(dynamic, length + 1, format, aq); snmp_log_string(priority, dynamic); free(dynamic); va_end(aq); return 0; }
/** * This snmp logging function allows variable argument list given the * specified priority, format and a populated va_list structure. * The default logfile this function writes to is /var/log/snmpd.log. * * @param priority is an integer representing the type of message to be written * to the snmp log file. The types are errors, warning, and information. * - The error types are: * - LOG_EMERG system is unusable * - LOG_ALERT action must be taken immediately * - LOG_CRIT critical conditions * - LOG_ERR error conditions * - The warning type is: * - LOG_WARNING warning conditions * - The information types are: * - LOG_NOTICE normal but significant condition * - LOG_INFO informational * - LOG_DEBUG debug-level messages * * @param format is a pointer to a char representing the variable argument list * format used. * * @param ap is a va_list type used to traverse the list of arguments. * * @return Returns 0 on success, -1 when the code could not format the log- * string, -2 when dynamic memory could not be allocated if the length * of the log buffer is greater then 1024 bytes. For each of these * errors a LOG_ERR messgae is written to the logfile. * * @see snmp_log */ int snmp_vlog(int priority, const char *format, va_list ap) { char *buffer = NULL; int length; length = vasprintf(&buffer, format, ap); if (length < 0) { snmp_log_string(LOG_ERR, "Could not format log-string\n"); return -1; } snmp_log_string(priority, buffer); free(buffer); return 0; }
void snmp_log_string(int priority, const char *str) { netsnmp_log_handler *logh; /* * We've got to be able to log messages *somewhere*! * If you don't want stderr logging, then enable something else. */ if (!logh_head) { snmp_enable_stderrlog(); snmp_log_string(LOG_WARNING, "No log handling enabled - turning on stderr logging\n"); } /* * Start at the given priority, and work "upwards".... */ logh = logh_priorities[priority]; for ( ; logh; logh = logh->next ) { /* * ... but skipping any handlers with a "maximum priority" * that we have already exceeded. And don't forget to * ensure this logging is turned on (see snmp_disable_stderrlog * and its cohorts). */ if (logh->enabled && (priority >= logh->pri_max)) logh->handler( logh, priority, str ); } }
int snmp_vlog (int priority, const char *format, va_list ap) { char buffer[LOGLENGTH]; int length; #if HAVE_VSNPRINTF char *dynamic; length=vsnprintf(buffer, LOGLENGTH, format, ap); #else length=vsprintf(buffer, format, ap); #endif if (length == 0) return(0); /* Empty string */ if (length == -1) { snmp_log_string(LOG_ERR, "Could not format log-string\n"); return(-1); } if (length < LOGLENGTH) { snmp_log_string(priority, buffer); return(0); } #if HAVE_VSNPRINTF dynamic=malloc(length+1); if (dynamic==NULL) { snmp_log_string(LOG_ERR, "Could not allocate memory for log-message\n"); snmp_log_string(priority, buffer); return(-2); } vsnprintf(dynamic, length+1, format, ap); snmp_log_string(priority, dynamic); free(dynamic); return(0); #else snmp_log_string(priority, buffer); snmp_log_string(LOG_ERR, "Log-message too long!\n"); return(-3); #endif }