【问题标题】:Logging Guard to limit semi-constant log messagesLogging Guard 限制半常量日志消息
【发布时间】:2010-10-31 14:06:41
【问题描述】:

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

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

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

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

如果由于某种原因“帧缓冲区”很长时间没有收到任何帧,日志记录将发送大量日志消息。

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

【问题讨论】:

    标签: c++ logging boost


    【解决方案1】:

    来点简单的,如果你愿意,你可以把它封装起来:

    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”变量上的整数溢出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-05
      • 2020-05-02
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多