使用syslog()函数处理日志信息
来源:互联网 发布:wordpress 开发 知乎 编辑:程序博客网 时间:2024/06/05 00:28
函数声明:#include <syslog.h>void syslog(int priority, const char *message, arguments...);priority参数的格式(severity level|facility code)示例: LOG_ERR|LOG_USERseverity level:Priority Level DescriptionLOG_EMERG An emergency situationLOG_ALERT High-priority problem, such as database corruptionLOG_CRIT Critical error, such as hardware failureLOG_ERR ErrorsLOG_WARNING WarningLOG_NOTICE Special conditions requiring attentionLOG_INFO Informational messagesLOG_DEBUG Debug messagesfacility 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 <syslog.h>#include <stdio.h>int main(void){ FILE *f; f = fopen("abc","r"); if(!f) syslog(LOG_ERR|LOG_USER,"test - %m\n"); }
上面的日志信息由系统自动给出,我们也可过滤日志信息。用到以下函数:
#include <syslog.h>void closelog(void);void openlog(const char *ident, int logopt, int facility);int setlogmask(int maskpri);logopt参数的选项:logopt Parameter DescriptionLOG_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 <syslog.h>#include <stdio.h>#include <unistd.h>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.logdaemon.* -/var/log/daemon.logkern.* -/var/log/kern.loglpr.* -/var/log/lpr.logmail.* -/var/log/mail.loguser.* -/var/log/user.loguucp.* /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.infomail.warn -/var/log/mail.warnmail.err /var/log/mail.err# Logging for INN news system#news.crit /var/log/news/news.critnews.err /var/log/news/news.errnews.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 namespacestd ;
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
- 使用syslog()函数处理日志信息
- Linux下syslog日志函数使用
- Linux下syslog日志函数使用
- Linux下syslog日志函数使用
- Linux下syslog日志函数使用
- Linux系统日志 -- syslog函数
- syslog函数的使用
- linux syslog日志的使用
- VA_LIST and 系统日志syslog配置信息
- syslog的点滴--集中处理日志
- 系统日志函数syslog(),openlog(),closelog()
- syslog(),closelog()与openlog()--日志操作函数
- syslog(),closelog()与openlog()--日志操作函数
- syslog(),closelog()与openlog()--日志操作函数 .
- syslog(),closelog()与openlog()--日志操作函数
- 系统日志函数syslog(),openlog(),closelog()
- syslog(),closelog()与openlog()--日志操作函数
- 系统日志函数syslog(),openlog(),closelog()
- 为网站添加小图标--favicon
- 代码小工具
- php中使用parse_url()对网址进行解析
- Mysql 跨域查询数据库
- PHP Curl 使用
- 使用syslog()函数处理日志信息
- PDO的基本用法
- 在 PHP 里 使用 CKEditor 和 CKFinder 插件上传图片.
- 一道题
- 维护型项目之殇
- cxf ws_security
- SQL Server JDBC Driver配置方法
- Ubuntu下网络连接图标丢失后获得方法
- Adobe Flex迷你教程 -- 合理使用Module分割项目以及对Module的使用[关于接口]