【问题标题】:syslog command in C codeC 代码中的 syslog 命令
【发布时间】:2011-12-13 06:41:39
【问题描述】:
#include<syslog.h>
syslog(LOG_INFO, "Start logging");

上面的 syslog 命令没有记录在 syslog 中。所以我尝试了,

openlog("Logs", "", LOG_USER);
syslog(LOG_INFO, "Start logging");
closelog();

这无法记录任何内容,我收到以下错误:

syslog: unknown facility/priority: 8049584

【问题讨论】:

标签: c syslog


【解决方案1】:

这一行是错误的:

openlog("vyatta-conntrack", "", LOG_USER);

“”应该是一个整数:

void openlog(const char *ident, int option, int facility);

整数应该是这些常量中的任何一个或一起:

   LOG_CONS       Write directly to system console if there is
                  an error while sending to system logger.

   LOG_NDELAY     Open the connection immediately (normally, the
                  connection is opened when the first message is
                  logged).

   LOG_NOWAIT     Don't wait for child processes that may have
                  been created while logging the message.  (The
                  GNU C library does not create a child process,
                  so this option has no effect on Linux.)

   LOG_ODELAY     The converse of LOG_NDELAY; opening of the
                  connection is delayed until syslog() is
                  called.  (This is the default, and need not be
                  specified.)

   LOG_PERROR     (Not in POSIX.1-2001.)  Print to stderr as
                  well.

   LOG_PID        Include PID with each message.

用类似的东西再试一次:

openlog("vyatta-conntrack", LOG_PID, LOG_USER);

请注意,您的syslogd 配置可能未设置为保留日志级别LOG_INFO 的消息。尝试使用LOG_ALERT 或其他方法来调试此问题——如果可行,则返回LOG_INFO 并配置您的syslogd 以保留您想要保留的日志消息。添加如下一行:

*.* /var/log/all_messages.log

将为rsyslogd(8) 工作。如果您的系统使用rsyslogd(8),请务必阅读rsyslog.conf(5) 联机帮助页。如果您的系统使用不同的 syslog 守护程序,请查看系统的联机帮助页了解详细信息。

【讨论】:

    【解决方案2】:

    您确实应该在启用和调试所有警告的情况下进行编译,即gcc -Wall -g

    再次阅读openlog man page。它被声明为:

     void openlog(const char *ident, int option, int facility);
    

    所以将"" 作为第二个参数传递是错误的(编译器确实警告过你)。它应该是例如LOG_PERROR|LOG_PID 或其他一些标志。

    【讨论】:

      【解决方案3】:
      openlog("vyatta-conntrack", "", LOG_USER);
      

      这一行是错误的,因为您将字符串作为第二个参数传递。请查看手册页,第二个参数是 int 类型。

      试试这个:

          #include <syslog.h>
            int main() {
            openlog("Logs", LOG_PID, LOG_USER);
            syslog(LOG_INFO, "Start logging");
            closelog();
          }
      

      输出:

      May 20 16:36:19 prabha-VirtualBox Logs[8114]: Start logging
      

      【讨论】:

      • 输出将在 /var/log/syslog 中。
      猜你喜欢
      • 1970-01-01
      • 2017-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多