【问题标题】:Reporting information using syslog使用 syslog 报告信息
【发布时间】:2013-09-06 12:58:26
【问题描述】:

我正在尝试编写一个函数,它将优先级和可变数量的字符串作为参数来记录应用程序中的信息。

到目前为止,该函数看起来像这样:

int _logf(int priority, char *fmt, ...)
{
    if (log.priority >= priority) {
        syslog(priority,  "LOG:%s", fmt);
    }
    /* stderr and syslog */
}

log.priority 是在运行时设置的int,可以是LOG_INFO / LOG_DEBUG / LOG_ERR

并在使用中:

_logf(LOG_INFO, "Starting app version %s", "1.0");

这是向syslog发送日志消息的可接受方式吗?

【问题讨论】:

  • 变量log在哪里定义?

标签: c logging syslog


【解决方案1】:

这不起作用,因为您不涉及可能传递给函数的可变数量的参数。

关于如何执行此操作,您可以查看以下使用 vsyslog() 的示例:

#include <syslog.h>
#include <stdarg.h> 

...

int _logf(int priority, const char * fmt, ...)
{
  if (log.priority >= priority) 
  {
    va_list ap;

    va_start(ap, fmt);

    vsyslog(priority, fmt, ap);

    va_end(ap);
  }
}

然后像这样调用它:

_logf(LOG_INFO, "LOG %s:%d (%s) - %s", __FILE__, __LINE__, __func__, "Just some info.");

更新:

要另外登录stderr,您可能需要查看功能vfprintf()

【讨论】:

    猜你喜欢
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多