gpt4 book ai didi

c++ - Logging Guard 以限制半常量日志消息

转载 作者:行者123 更新时间:2023-11-28 08:26:00 25 4
gpt4 key购买 nike

我在我的应用程序中使用 boost log 进行日志记录。

但是,在我的代码的某些部分中,我有一些日志语句,如果出现问题,这些语句可能会经常出现。我想要某种防护装置,当它检测到相同的日志消息不断出现时可以限制日志消息。

例如(这是一个简化的例子,不是实际实现)

while(!framebuffer.try_pop(frame))
{
BOOST_LOG(trace) << "Buffer underrun.";
}

如果出于某种原因“framebuffer”长时间未收到任何帧,日志记录将发送大量日志消息。

但是我不确定在不丢失任何重要消息的情况下使用什么策略来限制日志消息,以及如何实现它。

最佳答案

简单的东西怎么样,如果你想的话,你可以封装它:

int tooMany = 10;
int count = 0;
while(!framebuffer.try_pop(frame))
{
if(count < tooMany) {
BOOST_LOG(trace) << "Buffer underrun.";
}
count++;
}
if(count >= tooMany) {
BOOST_LOG(trace) << "Message repeated: " << count << " times.";
}

如果您获得绝对增量增量,请注意“count”变量上的整数溢出。

关于c++ - Logging Guard 以限制半常量日志消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4063400/

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