openlog()、syslog()、closelog()函数可用于将一些信息写入日志文件,其函数原型如下:
void openlog(const char *ident, int option, int facility);
void syslog(int priority, const char *format, ...);
void closelog(void);
头文件:
#include <syslog.h>
参数说明:
ident ------用于标识日志名
option------LOG_CONS、LOG_NDELAY、LOG_NOWAIT、LOG_ODELAY、LOG_PERROR、LOG_PID.
facility------记录消息程序的类型
priority------LOG_EMERG、LOG_ALERT、LOG_CRIT、LOG_ERR、LOG_WARNING、LOG_NOTICE、LOG_INFO、LOG_DEBUG
format------显示在日志中的说明性文字
实例程序:
#include <syslog.h>
int main(int argc, char *argv[])
{
openlog("testsyslog", LOG_NDELAY|LOG_PID, LOG_DAEMON);
syslog(LOG_ERR, "syslog test message generated in program %s\n", argv[0]);
closelog();
return 0;
}
运行该程序后在/var/log/syslog文件中多了
Jun 27 14:35:53 ubuntu testsyslog[3030]: syslog test message generated in p rogram ./test