gpt4 book ai didi

c - 使用 syslog 报告信息

转载 作者:行者123 更新时间:2023-11-30 15:46:04 28 4
gpt4 key购买 nike

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

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

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 的可接受方式吗?

最佳答案

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

关于如何执行此操作,您可以使用 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()

关于c - 使用 syslog 报告信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18658429/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com