C Syslog.h未写入日志
嗨,那里有Stackoverflow黑客!
在极少数情况下,我与C syslog()函数之间存在严重问题或误解。
该代码编译得很好,我可以看到它正在执行其“虚拟作业”(ping 8.8.8.8),但是定义的日志不会追加。我对此完全感到困惑,也不知道可能出什么问题了。已经使用了SMAO(已关闭“搜索我的屁股-试图普及”),但无法使其正常运行。
代码在这里:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
// Application settings # TODO: import these from a .ini file
#define WORKDIR \"/var/run/management\"
#define LOGDIR \"/var/log/management\"
#define LOGFILE \"/var/log/management.log\"
#define SCRIPTDIR \"/var/spool/management\"
#define PIDFILE \"/var/run/management/daemon.pid\"
int main(void) {
printf(\"Management Daemon\\nInitializing...\");
pid_t pid, sid;
setlogmask(LOG_UPTO (LOG_NOTICE));
openlog(LOGFILE, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
syslog(LOG_NOTICE, \"Management Daemon started by User %d\", getuid());
closelog();
printf(\"Done.\\nForking...\\n\");
pid = fork();
if(pid < 0) {
printf(\"Fork failed! Exiting...\\n\");
// TODO: syslog facility
syslog(LOG_EMERG, \"Forking failed, exiting.\");
closelog();
exit(EXIT_FAILURE);
}
if(pid > 0) {
FILE *pidfile;
pidfile = fopen(PIDFILE, \"w\");
fprintf(pidfile, \"%d\\n\", pid);
fclose(pidfile);
printf(\"PID written to %s\\nUsing log file %s\\nGoing silent...\\n\", PIDFILE, LOGFILE);
// TODO: syslog facility
openlog(LOGFILE, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
syslog(LOG_NOTICE, \"Fork returned with valid PID: %d. PID file: %s\", pid, PIDFILE);
exit(EXIT_SUCCESS);
}
umask(0);
sid = setsid();
if(sid < 0) {
// TODO: syslog facility
printf(\"SID is corrupt\\n\");
exit(EXIT_FAILURE);
}
if(sid > 0) {
printf(\"Acquired valid SID!\\n\");
}
if((chdir(WORKDIR)) < 0) {
// TODO: syslog facility
printf(\"Directory change failed. Got permissions?\\n\");
exit(EXIT_FAILURE);
}
// Going Silent
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
// daemon init here
FILE *fp;
// The big loop
closelog();
while(1) {
// Do your thing...
// TODO: implement daemon executing mechanics.
ret = system(\"ping 8.8.8.8 -c 1\");
fp = fopen(\"/var/run/management.output\", \"a\");
if(ret == 0) {
fprintf(fp, \"Success!\\n\");
fclose(fp);
}
if(ret == 512) {
fprintf(fp, \"Failure!\\n\");
fclose(fp);
}
// Sleep till the next heartbeat
// TODO: notice level log about heartbeats if verbosity is set to high
sleep(30);
}
exit(EXIT_SUCCESS);
}
所有帮助将不胜感激!
雌二醇
解决方案:
在syslog-ng.conf中添加了以下几行:
destination d_management { file(\"/var/log/management/management.log\"); };
filter f_management { match(\"MD:\" value(\"MESSAGE\")); };
log { source(src); filter(f_management); destination(d_management); };
包含MD:序列的所有日志消息都将被重定向到management.log文件。
奇迹般有效。再次感谢您指出正确的方向。
没有找到相关结果
已邀请:
1 个回复
芦歉竭皑
中找不到任何东西。 日志文件的名称通常在logger守护程序的配置文件中设置。该文件的名称和位置取决于您使用的守护程序(例如,在我的计算机上为
)。