我尝试使用syslog.h
库在我的项目中记录消息。我注意到,如果我尝试记录相同的消息,在/var/log/syslog
中它只出现一次;这是我的示例代码和设置:
int main(int argc, char *argv[])
{
//Initialize syslog
setlogmask(LOG_UPTO(LOG_NOTICE));
openlog("example_process", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);
while(main_loop == LOOP_CONTINUE)
{
syslog(LOG_ERR, "Data Logged!\n");
sleep(60);
}
closelog();
}
这就是结果:
Feb 11 23:27:19 andrea-Aspire-5745G example_process[17332]: Data Logged!
andrea@andrea-Aspire-5745G:~$
但如果我改变
中的代码int main(int argc, char *argv[])
{
//Initialize syslog
setlogmask(LOG_UPTO(LOG_NOTICE));
openlog("example_process", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);
while(main_loop == LOOP_CONTINUE)
{
syslog(LOG_ERR, "Data Logged!\n");
syslog(LOG_ERR, "Data Logged2!\n");
sleep(60);
}
closelog();
}
它写下了每一个重复:
Feb 11 17:43:06 andrea-Aspire-5745G example_process[7645]: Data Logged!
Feb 11 17:43:06 andrea-Aspire-5745G example_process[7645]: Data Logged2!
Feb 11 17:43:10 andrea-Aspire-5745G example_process[7645]: Data Logged!
Feb 11 17:43:10 andrea-Aspire-5745G example_process[7645]: Data Logged2!
Feb 11 17:43:13 andrea-Aspire-5745G example_process[7645]: Data Logged!
Feb 11 17:43:13 andrea-Aspire-5745G example_process[7645]: Data Logged2!
我明白,记录每个相同的消息并不是一个好主意,但这是我的问题:
是否有方法或选项使其成为可能?