syslog(),openlog(),closelog()函數介紹

   這裏面的三個函數openlog, syslog, closelog是一套系統日誌寫入接口。另外那個vsyslog和syslog功能一樣,只是參數格式不同
  

  通常,syslog守護進程讀取三種格式的記錄消息。此守護進程在啓動時讀一個配置文件。一般來說,其文件名爲/etc/syslog.conf,該文件決定了不同種類的消息應送向何處。(linux就在/var/log/messages  如果是solaris 就在/var/adm/messages.     不準確,準確的還是看/etc/syslog.conf例如,緊急消息可被送向系統管理員(若已登錄),並在控制檯上顯示,而警告消息則可記錄到一個文件中。該機制提供了syslog函數,其調用格式如下


#include <syslog.h>
void openlog (char*ident,int option ,int facility);
void syslog(int priority,char*format,……)
void closelog();


    調用openlog是可選擇的。如果不調用openlog,則在第一次調用syslog時,自動調用openlog。調用closelog也是可選擇的,它只是關閉被用於與syslog守護進程通信的描述符調用openlog 使我們可以指定一個ident,以後, 此ident 將被加至每則記錄消息中。ident 一般是程序的名稱(例如 ,cron ,ine 等)


程序的用法示例代碼如下:


#include <syslog.h>
int main(int argc, char **argv)
{
    openlog("MyMsgMARK", LOG_CONS | LOG_PID, 0);
    syslog(LOG_DEBUG,
           "This is a syslog test message generated by program '%s'\n",
           argv[0]);
    closelog();
    return 0;
}


 
編譯生成可執行程序後,運行一次程序將向/var/log/message文件添加一行信息如下:
        Feb 12 08:48:38 localhost MyMsgMARK[7085]: This is a syslog test message generated by program './a.out'
 
openlog及closelog函數說明
此函數原型如下:
void openlog(const char *ident, int option, int facility);
此函數用來打開一個到系統日誌記錄程序的連接,打開之後就可以用syslog或vsyslog函數向系統日誌裏添加信息了。而closelog函數就是用來關閉此連接的。
openlog的第一個參數ident將是一個標記,ident所表示的字符串將固定地加在每行日誌的前面以標識這個日誌,通常就寫成當前程序的名稱以作標記。第二個參數option是下列值取與運算的結果:LOG_CONS, LOG_NDELAY, LOG_NOWAIT, LOG_ODELAY, LOG_PERROR, LOG_PID,各值意義請參考man openlog手冊
LOG_CONS
   Write directly to system console if there is an error while sending to system logger.

LOG_NDELAY
   Open the connection immediately (normally, the connection is opened when the first message is logged).

LOG_NOWAIT
   Don’t wait for child processes that may have been created while logging the message.  (The GNU C library does not create a child process, so this option has no effect on Linux.)

LOG_ODELAY
   The converse of LOG_NDELAY; opening of the connection is delayed until syslog() is called.  (This is the  default, and  need not be specified.)

LOG_PERROR
   (Not in SUSv3.) Print to stderr as well.

LOG_PID
    Include PID with each message. 
第三個參數facility指明記錄日誌的程序的類型。

       The facility argument is used to specify what type of program  is  logging  the  message.
       This  lets the configuration file specify that messages from different facilities will be
       handled differently.
       LOG_AUTH       security/authorization messages (DEPRECATED Use LOG_AUTHPRIV instead)

       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 to specified levels only.

 

NOTES
       The  argument  ident  in  the  call  of openlog() is probably stored as-is.  Thus, if the
       string it points to is changed, syslog() may start prepending the changed string, and  if
       the  string it points to ceases to exist, the results are undefined.  Most portable is to
       use a string constant.

       Never pass a string with user-supplied data as a format, use the following instead:

           syslog(priority, "%s", string);

SEE ALSO
       logger(1), setlogmask(3), syslog.conf(5), syslogd(8)

 

 

vsyslog與syslog函數的功能是一樣的。

 #include <stdarg.h>

   void vsyslog(int priority, const char *format, va_list ap);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       vsyslog(): _BSD_SOURCE

發佈了31 篇原創文章 · 獲贊 7 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章