Chinaunix首页 | 论坛 | 博客
  • 博客访问: 335570
  • 博文数量: 82
  • 博客积分: 2602
  • 博客等级: 少校
  • 技术积分: 660
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-10 08:48
文章分类

全部博文(82)

文章存档

2008年(17)

2007年(65)

分类: C/C++

2007-10-16 16:13:37

函数声明:
#include 
void syslog(int priority, const char *message, arguments...);

priority参数的格式(severity level|facility code)
示例: 
LOG_ERR|LOG_USER

severity level:
Priority Level               Description
LOG_EMERG                    An emergency situation
LOG_ALERT                    High-priority problem, such as database corruption
LOG_CRIT                     Critical error, such as hardware failure
LOG_ERR                      Errors
LOG_WARNING                  Warning
LOG_NOTICE                   Special conditions requiring attention
LOG_INFO                     Informational messages
LOG_DEBUG                    Debug messages

facility value(转自syslog.h头文件):
/* facility codes */
#define LOG_KERN        (0<<3)  /* kernel messages */
#define LOG_USER        (1<<3)  /* random user-level messages */
#define LOG_MAIL        (2<<3)  /* mail system */
#define LOG_DAEMON      (3<<3)  /* system daemons */
#define LOG_AUTH        (4<<3)  /* security/authorization messages */
#define LOG_SYSLOG      (5<<3)  /* messages generated internally by syslogd */
#define LOG_LPR         (6<<3)  /* line printer subsystem */
#define LOG_NEWS        (7<<3)  /* network news subsystem */
#define LOG_UUCP        (8<<3)  /* UUCP subsystem */
#define LOG_CRON        (9<<3)  /* clock daemon */
#define LOG_AUTHPRIV    (10<<3) /* security/authorization messages (private) */
#define LOG_FTP         (11<<3) /* ftp daemon */

示例代码:
#include 
#include 

int main(void)
{
        FILE *f;

        f = fopen("abc","r");
        if(!f)                                    
                syslog(LOG_ERR|LOG_USER,"test - %m\n");       
}

上面的日志信息由系统自动给出,我们也可过滤日志信息。用到以下函数:

#include 
void closelog(void);
void openlog(const char *ident, int logopt, int facility);
int setlogmask(int maskpri);

logopt参数的选项:
logopt Parameter    Description
LOG_PID             Includes the process identifier, a unique number allocated to each process by the system, in the messages.
LOG_CONS            Sends messages to the console if they can’t be logged.
LOG_ODELAY          Opens the log facility at first call to .
LOG_NDELAY          Opens the log facility immediately, rather than at first log.

示例代码:
#include 
#include 
#include 

int main(void)
{
        int logmask;

        openlog("logmask", LOG_PID|LOG_CONS, LOG_USER); /*日志信息会包含进程id。*/
        syslog(LOG_INFO, "informative message, pid=%d", getpid()); 
        syslog(LOG_DEBUG,"debug message, should appear");   /*记录该日志信息。*/
        logmask = setlogmask(LOG_UPTO(LOG_NOTICE));     /*设置屏蔽低于NOTICE级别的日志信息。*/
        syslog(LOG_DEBUG, "debug message, should not appear");  /*该日志信息被屏蔽,不记录。*/
}

不同安全级别的日志信息存放在/var/log目录下的哪个文件中是由/etc/syslog.conf文件控制的,下面是我系统中syslog.conf文件的内容:

#  /etc/syslog.conf     Configuration file for syslogd.
#
#                       For more information see syslog.conf(5)
#                       manpage.

#
# First some standard logfiles.  Log by facility.
#

auth,authpriv.*                 /var/log/auth.log
*.*;auth,authpriv.none          -/var/log/syslog
#cron.*                         /var/log/cron.log
daemon.*                        -/var/log/daemon.log
kern.*                          -/var/log/kern.log
lpr.*                           -/var/log/lpr.log
mail.*                          -/var/log/mail.log
user.*                          -/var/log/user.log
uucp.*                          /var/log/uucp.log

#
# Logging for the mail system.  Split it up so that
# it is easy to write scripts to parse these files.
#
mail.info                       -/var/log/mail.info
mail.warn                       -/var/log/mail.warn
mail.err                        /var/log/mail.err

# Logging for INN news system
#
news.crit                       /var/log/news/news.crit
news.err                        /var/log/news/news.err
news.notice                     -/var/log/news/news.notice

#
# Some `catch-all' logfiles.
#
*.=debug;\
        auth,authpriv.none;\
        news.none;mail.none     -/var/log/debug
*.=info;*.=notice;*.=warn;\
        auth,authpriv.none;\
        cron,daemon.none;\
        mail,news.none          -/var/log/messages

#
# Emergencies are sent to everybody logged in.
#
*.emerg                         *

#
# I like to have messages displayed on the console, but only on a virtual
# console I usually leave idle.
#
#daemon,mail.*;\
#       news.=crit;news.=err;news.=notice;\
#       *.=debug;*.=info;\
#       *.=notice;*.=warn       /dev/tty8

# The named pipe /dev/xconsole is for the `xconsole' utility.  To use it,
# you must invoke `xconsole' with the `-file' option:
#
#    $ xconsole -file /dev/xconsole [...]
#
# NOTE: adjust the list below, or you'll go crazy if you have a reasonably
#      busy site..
#
daemon.*;mail.*;\
        news.crit;news.err;news.notice;\
        *.=debug;*.=info;\
        *.=notice;*.=warn       |/dev/xconsole
实例代码:
 

1 #include <string>
      2 #include <iostream>
      3 #include <algorithm>
      4 #include <list>
      5 #include <syslog.h>
      6 using namespace std ;
      7
      8 //===============================================

      9
     10 class TLOG
     11 {
     12 public:
     13 TLOG() ;
     14 ~TLOG() ;
     15 TLOG& operator<<( const char * p ) ;
     16 TLOG& operator<<( string & s ) ;
     17 TLOG& operator<<( TLOG& (* pfun)(TLOG&) ) ;
     18
     19 void debug( const char * p ) ;
     20 void debug( string & s ) ;
     21
     22 void log( const char * p ) ;
     23 void log( string & s ) ;
     24 } ;
     25 TLOG& endl( TLOG& object ) ;
     26
     27 const char * LOG_PREFIX = "root_log : " ;
     28
     29 TLOG glog ;
     30
     31 TLOG::TLOG()
     32 {
     33 #ifdef LOG_SYSLOGD
     34 openlog( LOG_PREFIX, LOG_PID, LOG_USER ) ;
 35 #endif
     36 }
     37 TLOG::~TLOG()
     38 {
     39 #ifdef LOG_SYSLOGD
     40 closelog() ;
     41 #endif
     42 }
     43
     44 TLOG& TLOG::operator<<( const char * p )
     45 {
     46 #ifdef LOG_STDOUT
     47 cout << p ;
     48 #endif
     49 return *this ;
     50 }
     51
     52 TLOG& TLOG::operator<<( string & s )
     53 {
     54 #ifdef LOG_STDOUT
     55 cout << s ;
     56 #endif
     57 return *this ;
     58 }
     59 TLOG& TLOG::operator<<( TLOG& (* pfun)(TLOG&) )
     60 {
     61 return pfun(*this) ;
     62 }
     63 TLOG& endl( TLOG& object )
     64 {
     65 #ifdef LOG_STDOUT
     66 cout << endl ;
     67 #endif
     68 return object ;
69 }
     70
     71 void TLOG::debug( const char * p )
     72 {
     73 *this << p ;
     74 return ;
     75 }
     76 void TLOG::debug( string & s )
     77 {
     78 *this << s ;
     79 return ;
     80 }
     81
     82 void TLOG::log( const char * p )
     83 {
     84 *this << LOG_PREFIX << p << endl ;
     85
     86 #ifdef LOG_SYSLOG
     87 syslog( LOG_INFO, p ) ;
     88 #endif
     89 return ;
     90 }
     91 void TLOG::log( string & s )
     92 {
     93 *this << LOG_PREFIX << s << endl ;
     94
     95 #ifdef LOG_SYSLOG
     96 syslog( LOG_INFO, s.c_str() ) ;
     97 #endif
     98 return ;
     99 }
    100
    101 int
    102 main(void)
103 {
    104 glog.log("abcde" );
    105 // glog.debug(__func__);

    106 string word;
    107 cout<<"Enter a line:";
    108 cin>>word;
    109 while(cin.get()!='\n')
    110 continue;
    111 cout<<word<<"is all"<<"wanted!\n";
    112
    113 string line;
    114 cout<<"Enter a line:(really)";
    115 getline(cin,line);
    116 cout<<"line:"<<line<<endl;
    117 return 0;
    118 }

结果:

 

ct 16 16:01:42 zerk a.out: abcde

阅读(5607) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~