开发者

Logging Guard to limit semi-constant log messages

I'm using boost log in my application for logging.

However, in some sections of my code I have some log statements that could occur very often if something goes wrong. I'd want some kind of guard that can limit log messages when it detects that the same log message appears constantly.

e.g. (This is a simplified example, not actual implementation)

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

If for some reason "framebuffer" doesn't receive any frames for a long time the logging will send way to much log messages.

Howe开发者_开发技巧ver I'm unsure what strategy to use for limiting log messages, without loosing any important messages, and how to implement it.


How about something simple, you could encapsulate it if you wanted to:

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.";
}

Just be careful of integer overflows on the 'count' variable if you get a absolute bucketload of increments.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜