一、syslog(),openlog(),closelog()一套用于寫系統(tǒng)日志的函數(shù)。
這三個(gè)函數(shù)openlog, syslog, closelog是一套系統(tǒng)日志寫入接口,另外那個(gè)vsyslog和syslog功能一樣,只是參數(shù)格式不同。
通常,syslog守護(hù)進(jìn)程讀取三種格式的記錄消息。此守護(hù)進(jìn)程在啟動(dòng)時(shí)讀一個(gè)配置文件。一般來說,其文件名為/etc/syslog.conf,該文件決定了不同種類的消息應(yīng)送向何處。例如,緊急消息可被送向系統(tǒng)管理員(若已登錄),并在控制臺上顯示,而警告消息則可記錄到一個(gè)文件中。該機(jī)制提供了syslog函數(shù),其調(diào)用格式如下
#include <syslog.h>
void openlog (char*ident,int option ,int facility);
void syslog(int priority,char*format,……)
void closelog();
?
二、openlog
調(diào)用openlog是可選擇的。如果不調(diào)用openlog,則在第一次調(diào)用syslog時(shí),自動(dòng)調(diào)用openlog。調(diào)用closelog也是可選擇的,它只是關(guān)閉被用于與syslog守護(hù)進(jìn)程通信的描述符。調(diào)用openlog 使我們可以指定一個(gè)ident,以后, 此ident 將被加至每則記錄消息中。ident 一般是程序的名稱 。4>openlog及closelog函數(shù)說明
此函數(shù)原型如下:
void openlog(const char *ident, int option, int facility);
此函數(shù)用來打開一個(gè)到系統(tǒng)日志記錄程序的連接,打開之后就可以用syslog或vsyslog函數(shù)向系統(tǒng)日志里添加信息了。而closelog函數(shù)就是用來關(guān)閉此連接的。
第一個(gè)參數(shù)ident將是一個(gè)標(biāo)記,ident所表示的字符串將固定地加在每行日志的前面以標(biāo)識這個(gè)日志,通常就寫成當(dāng)前程序的名稱以作標(biāo)記。
第二個(gè)參數(shù)option是下列值取與運(yùn)算的結(jié)果:LOG_CONS, LOG_NDELAY, LOG_NOWAIT, LOG_ODELAY, LOG_PERROR, LOG_PID,各值意義請參考man openlog手冊:
l LOG_CONS:Write directly to system console if there is an error while sendingto system logger.
l LOG_NDELAY:Open the connection immediately (normally, the connection is openedwhen the first message is logged).
l LOG_NOWAIT:Don’t? wait? for? child processes that may have beencreated while logging the message.? (The GNU C library does not create a childprocess, so this option has no effect on Linux.)
l LOG_ODELAY: The converse of LOG_NDELAY; opening of the connection is delayeduntil syslog() is called.? (This is the? default,? and? neednot be specified.)
l LOG_PERROR:(Not in SUSv3.)Print to stderr as well.
l LOG_PID:Include PID with eachmessage.
第三個(gè)參數(shù)指明記錄日志的程序的類型。
?
三、syslog
syslog函數(shù)用于把日志消息發(fā)給系統(tǒng)程序syslogd去記錄,此函數(shù)原型是:void syslog(int priority, const char *format, ...);
第一個(gè)參數(shù)是消息的緊急級別,第二個(gè)參數(shù)是消息的格式,之后是格式對應(yīng)的參數(shù)。就是printf函數(shù)一樣使用。
如果我們的程序要使用系統(tǒng)日志功能,只需要在程序啟動(dòng)時(shí)使用openlog函數(shù)來連接syslogd程序,后面隨時(shí)用syslog函數(shù)寫日志就行了。
參考:
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
