C/C++日志写入系统log(/var/log/syslog)
openlog的參數:
?第一個參數ident將是一個標記,ident所表示的字符串將固定地加在每行日志的前面以標識這個日志,通常就寫成當前程序的名稱以作標記。
第二個參數option是下列值取與運算的結果:LOG_CONS,LOG_NDELAY, LOG_NOWAIT, LOG_ODELAY, LOG_PERROR,LOG_PID,各值意義請參考man openlog手冊:
????LOG_CONS
???????Writedirectly to system console if there is an error while sending tosystem logger.
????LOG_NDELAY
???????Openthe connection immediately (normally, the connection is opened whenthe first message is logged).
????LOG_NOWAIT
???????Don’t wait for childprocesses that may have been created while logging themessage.??(The GNU C library does not createa child process, so this option has no effect onLinux.)
????LOG_ODELAY
???????The converseof LOG_NDELAY; opening of the connection is delayed until syslog()is called.??(This is the default,??and need not be specified.)
????LOG_PERROR
???????(Notin SUSv3.) Print to stderr as well.
????LOG_PID
????????IncludePID with each message.
第三個參數facility指明記錄日志的程序的類型。
????????The facility argument is used to specify what type ofprogram??is logging??the??message.
????????This??lets the configuration file specify thatmessages from different facilities will be
????????handled differently.
????????LOG_AUTH??????security/authorization messages (DEPRECATED Use LOG_AUTHPRIVinstead)
????????
????????LOG_AUTHPRIV??security/authorization messages (private)
????????
????????LOG_CRON??????clock daemon (cron and at)
????????
????????LOG_DAEMON????system daemons without separate facility value
????????
????????LOG_FTP???????ftp daemon
????????
????????LOG_KERN??????kernel messages (these can't be generage from user processes)
????????
????????LOG_LOCAL0 through LOG_LOCAL7
???????????????????????reserved for local use
????????
????????LOG_LPR???????line printer subsystem
????????
????????LOG_MAIL??????mail subsystem
????????
????????LOG_NEWS??????USENET news subsystem
????????
????????LOG_SYSLOG????messages generated internally by syslogd(8)
????????
????????LOG_USER (default)
???????????????????????generic user-level messages
????????
????????LOG_UUCP??????UUCP subsystem
syslog函數及參數
syslog函數用于把日志消息發給系統程序syslogd去記錄,此函數原型是:
void syslog(int priority, const char *format, ...);
第一個參數是消息的緊急級別
第二個參數是消息的格式,之后是格式對應的參數。就是printf函數一樣使用。
????????如果我們的程序要使用系統日志功能,只需要在程序啟動時使用openlog函數來連接syslogd程序,后面隨時用syslog函數寫日志就行了。
level
????????This determines the importance of the message. The levels are, in??order of??decreasing
????????importance:
????????
????????LOG_EMERG?????system is unusable
????????
????????LOG_ALERT?????action must be taken immediately
????????
????????LOG_CRIT??????critical conditions
????????
????????LOG_ERR???????error conditions
????????
????????LOG_WARNING???warning conditions
????????
????????LOG_NOTICE????normal, but significant, condition
????????
????????LOG_INFO??????informational message
????????
????????LOG_DEBUG?????debug-level message
????????
????????The function setlogmask(3) can be used to restrict logging tospecified levels only.
NOTES
????????The??argument ident??in??the call??of openlog() is probably storedas-is.??Thus, if the
????????string it points to is changed, syslog() may start prepending thechanged string, and??if
????????the??string it points to ceases to exist, theresults are undefined.??Most portable is to
????????use a string constant.
????????
????????Never pass a string with user-supplied data as a format, use thefollowing instead:
????????
????????????syslog(priority, "%s", string);
實例
#include <stdio.h> #include <syslog.h>int main ( int argc, char *argv[] ) {openlog("project/filter", LOG_PID, LOG_MAIL);syslog ( LOG_DEBUG, "argc: %d\n", argc );syslog ( LOG_NOTICE, "argv[0]: %s\n", argv[0] );closelog();return 0; }總結
以上是生活随笔為你收集整理的C/C++日志写入系统log(/var/log/syslog)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python2、3字典比较函数
- 下一篇: Web完整渗透测试实例(windows)