NET-SNMP開發——日志輸出
net-snmp的日志輸出功能是很強大的,與日志輸出相關函數聲明在net-snmp-5.7.3\include\net-snmp\library\snmp_logging.h
文件中,定義在net-snmp-5.7.3\snmplib\snmp_logging.c
文件中。
具體實現就不說了,可以自己看源碼。
net-snmp的日志功能默認將日志寫入到/var/log/snmpd.log
文件中(linux/unix下,windows下沒有)
snmp log 類型
net-snmp中將日志分為8個類型(一個警告warning,三個信息information,四個錯誤error),具體的宏定義如下
/* error types */
#define LOG_EMERG 0 /* system is unusable */
#define LOG_ALERT 1 /* action must be taken immediately */
#define LOG_CRIT 2 /* critical conditions 臨界條件*/
#define LOG_ERR 3 /* error conditions 錯誤條件*/
/* warning type */
#define LOG_WARNING 4 /* warning conditions 預警條件*/
/* information types */
#define LOG_NOTICE 5 /* normal but significant condition 正常但意義重大*/
#define LOG_INFO 6 /* informational 信息*/
#define LOG_DEBUG 7 /* debug-level messages 調試級別的消息*/
注意:這些嚴重等級的分類是與標准的 UNIX/LINUX 中的 syslog 一致的。
System is unusable emergencies alerts
極其緊急的錯誤 Action must be taken immediately
需立即糾正的錯誤 Critical conditions
關鍵信息 Error conditions
需關注但不關鍵的錯誤 Warning conditions
警告,可能存在某種差錯 Normal but significant condition
需注意的信息 Informational
一般提示信息 Debug-level messages
調試信息
日志輸出函數
snmp_log
int snmp_log(int priority, const char *format, ...)
這個函數實際上調用的是snmp_vlog
函數,這個函數成功返回0,當不能格式化日志字符串時返回1,當動態內存不能分配返回2,如果日志緩沖區的長度大於1024字節。這些消息將以LOG_ERR類型直接寫入日志文件。
調用示例
snmp_log(LOG_ERR, "%s: Error %d out-of-range\n", s, errno);
snmp_log_perror
void snmp_log_perror(const char *s)
實際上是先調用char* error = strerror(errno);
獲取errno
的信息,然后使用snmp_log
來輸出參數s
和error
字符串。 調用示例
snmp_log_perror("init error");
snmp_log_string
void snmp_log_string(int priority, const char *str)
調用示例
snmp_log_string(LOG_ERR, "Could not format log-string\n");