syslog

 1、void openlog(const char *ident, int option, int facility);

第一个参数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

2、void syslog(int priority, const char *format, ...);

        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.

3、int setlogmask(int maskpri);

进程有一个日志优先级掩码,该掩码决定哪些调用syslog(3)可能被记录。所有其他调用都将被忽略。日志是为在掩码中设置相应位的优先级启用的。最初的掩码是这样的,日志是为所有优先级启用的。
setlogmask()函数为当前的进程设置这个日志掩码,并返回先前的掩码。如果掩码参数为0,那么当前的日志掩码不会被修改。
这8个优先级分别是LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO and LOG_DEBUG.与优先级p相对应的位是LOG_MASK(p)。一些系统还提供了一个宏LOG_UPTO(p),以在上面的列表中包括p的所有优先级。
 

4、调用openlog是可选择的。如果不调用openlog,则在第一次调用syslog时,自动调用openlog。调用closelog也是可选择的,它只是关闭被用于与syslog守护进程通信的描述符。调用openlog使我们可以指定一个ident,以后, 此ident 将被加至每则记录消息中。

5、代码示例

#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'
",  
          argv[0]);  
   closelog();  
   return0;  
}  

编译生成可执行程序后,运行一次程序将向/var/log/message文件添加一行信息如下:
Feb 12 08:48:38 localhost MyMsgMARK[7085]: This is a syslog testmessage generated by program './a.out'  
原文地址:https://www.cnblogs.com/soul-stone/p/8313563.html