add the VerboseLoggingHelper class for repeated messages

This commit is contained in:
Stephen Birarda 2014-10-28 12:05:56 -07:00
parent 4e7e9641f0
commit 9c30903eb6
2 changed files with 70 additions and 0 deletions

View file

@ -0,0 +1,34 @@
//
// VerboseLoggingHelper.cpp
// libraries/shared/src
//
// Created by Stephen Birarda on 2014-10-28.
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include <qdebug.h>
#include "VerboseLoggingHelper.h"
VerboseLoggingHelper& VerboseLoggingHelper::getInstance() {
static VerboseLoggingHelper sharedInstance;
return sharedInstance;
}
VerboseLoggingHelper::VerboseLoggingHelper() {
// setup our timer to flush the verbose logs every 5 seconds
_timer = new QTimer(this);
connect(_timer, &QTimer::timeout, this, &VerboseLoggingHelper::flushMessages);
_timer->start(VERBOSE_LOG_INTERVAL_SECONDS * 1000);
}
void VerboseLoggingHelper::flushMessages() {
QHash<QString, int>::iterator message = _messageCountHash.begin();
while (message != _messageCountHash.end()) {
qDebug() << message.key().arg(message.value());
message = _messageCountHash.erase(message);
}
}

View file

@ -0,0 +1,36 @@
//
// VerboseLoggingHelper.h
// libraries/shared/src
//
// Created by Stephen Birarda on 2014-10-28.
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_VerboseLoggingHelper_h
#define hifi_VerboseLoggingHelper_h
#include <qhash.h>
#include <qobject.h>
#include <qtimer.h>
const int VERBOSE_LOG_INTERVAL_SECONDS = 5;
class VerboseLoggingHelper : public QObject {
Q_OBJECT
public:
static VerboseLoggingHelper& getInstance();
void addMessage(const QString& message) { _messageCountHash[message] += 1; }
private:
VerboseLoggingHelper();
void flushMessages();
QTimer* _timer;
QHash<QString, int> _messageCountHash;
};
#endif // hifi_VerboseLoggingHelper_h