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;
}

发布了303 篇原创文章 · 获赞 304 · 访问量 30万+

猜你喜欢

转载自blog.csdn.net/u012206617/article/details/104479017
今日推荐