简体   繁体   English

使用syslog报告信息

[英]Reporting information using syslog

I am trying to write a function which will take a priority level and a variable amount of strings as arguments to log information in an application. 我正在尝试编写一个函数,该函数将优先级和可变数量的字符串作为参数记录到应用程序中的信息。

The function looks something like this so far: 到目前为止,该函数看起来像这样:

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

log.priority is an int set at run time which could be LOG_INFO / LOG_DEBUG / LOG_ERR log.priority是在运行时设置的int值,可以是LOG_INFO / LOG_DEBUG / LOG_ERR

and in use: 并在使用中:

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

Is this an acceptable way to send log messages to syslog ? 这是将日志消息发送到syslog的可接受方法吗?

This won't work as you do not involve the variable number of parameters possibly passed into your function. 这将不起作用,因为您不涉及可能传递到函数中的可变数量的参数。

On how to do this you might take a look a the following example using vsyslog() : 关于如何执行此操作,您可以使用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);
  }
}

Then call it for example like so: 然后像这样调用它:

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

Update: 更新:

To additionally log to stderr you might like to look ath function vfprintf() . 要另外登录到stderr您可能需要查看vfprintf()函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM