gpt4 book ai didi

c++ - 在每个系统日志 C++ 之前调用 openlog

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:53:22 24 4
gpt4 key购买 nike

我已经创建了我自己的 C 库,我的同事打算使用它。在这个包装器中,我打算使用 syslog 并根据输入参数,我希望在 LOCAL0LOCAL1 之间切换。

我发现最简单的方法是使用 LOCAL0LOCAL1 执行 openlog(),具体取决于输入参数,然后执行 syslog() 和 `closelog()。

我在同一个包装器 API 中拥有所有 3 个(类似于下面的内容):

void syslog_wrap_api(int flag, const char *msg)
{
setlogmask(LOG_UPTO (LOG_INFO));
if(flag == 0)
openlog("myapplog",LOG_NDELAY,LOG_LOCAL0);
else
openlog("myapplog",LOG_NDELAY,LOG_LOCAL1);
syslog(LOG_INFO,"%s",msg);
closelog()
}

我的问题是,此 API 是否会在压力下导致问题(性能问题)?

最佳答案

在我看来,这是错误的做法,但文档令人困惑。

您是否在压力下遇到问题将取决于您的 C 库中 openlog 的实现。这很可能是有问题的。更重要的是,在多线程环境中,一个线程可能会在另一个线程发出 openlog 之后发出 syslog,这意味着使用了错误的优先级。

如何正确执行此操作的关键在手册页中:

The facility argument establishes a default to be used if none is specified in subsequent calls to syslog(). Values for option and facility are given below ... The priority argument is formed by OR-ing the facility and the level values (explained below).

关于如何利用它的线索在这里:

The use of openlog() is optional; it will automatically be called by syslog() if necessary, in which case ident will default to NULL.

如果您不使用openlog,则必须有某种方式来表达facility。答案是您可以将设施指定为零到 openlog(或根本不使用它),或者在设施中指定 syslog 的 priority 参数

所以,像这样:

syslog(LOG_INFO | (flag?LOG_LOCAL1:LOG_LOCAL0),"%s",msg);

关于c++ - 在每个系统日志 C++ 之前调用 openlog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24005666/

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