use the LogHandler in Interface

This commit is contained in:
Stephen Birarda 2014-10-28 14:23:34 -07:00
parent d3bbd251db
commit 8e3102266a
4 changed files with 61 additions and 28 deletions

View file

@ -116,12 +116,10 @@ const QString SKIP_FILENAME = QStandardPaths::writableLocation(QStandardPaths::D
const QString DEFAULT_SCRIPTS_JS_URL = "http://public.highfidelity.io/scripts/defaultScripts.js";
void messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) {
if (message.size() > 0) {
QString dateString = QDateTime::currentDateTime().toTimeSpec(Qt::LocalTime).toString(Qt::ISODate);
QString formattedMessage = QString("[%1] %2\n").arg(dateString).arg(message);
fprintf(stdout, "%s", qPrintable(formattedMessage));
Application::getInstance()->getLogger()->addMessage(qPrintable(formattedMessage));
QString logMessage = LogHandler::getInstance().printMessage(type, context, message);
if (!logMessage.isEmpty()) {
Application::getInstance()->getLogger()->addMessage(qPrintable(logMessage));
}
}

View file

@ -212,6 +212,9 @@ bool LimitedNodeList::packetVersionAndHashMatch(const QByteArray& packet) {
<< uuidFromPacketHeader(packet);
}
} else {
static QString repeatedMessage
= LogHandler::getInstance().addRepeatedMessageRegex("Packet of type \\d+ received from unknown node with UUID");
qDebug() << "Packet of type" << checkType << "received from unknown node with UUID"
<< uuidStringWithoutCurlyBraces(uuidFromPacketHeader(packet));
}

View file

@ -17,6 +17,7 @@
#define pid_t int // hack to build
#endif
#include <qdebug.h>
#include <qtimer.h>
#include "LogHandler.h"
@ -26,7 +27,9 @@ LogHandler& LogHandler::getInstance() {
return staticInstance;
}
LogHandler::LogHandler() {
LogHandler::LogHandler() :
_shouldOutputPID(false)
{
// setup our timer to flush the verbose logs every 5 seconds
QTimer* logFlushTimer = new QTimer(this);
connect(logFlushTimer, &QTimer::timeout, this, &LogHandler::flushRepeatedMessages);
@ -51,9 +54,33 @@ const char* stringForLogType(QtMsgType msgType) {
// the following will produce 2000-10-02 13:55:36 -0700
const char DATE_STRING_FORMAT[] = "%Y-%m-%d %H:%M:%S %z";
void LogHandler::verboseMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) {
void LogHandler::flushRepeatedMessages() {
QHash<QString, int>::iterator message = _repeatMessageCountHash.begin();
while (message != _repeatMessageCountHash.end()) {
message = _repeatMessageCountHash.erase(message);
}
}
QString LogHandler::printMessage(QtMsgType type, const QMessageLogContext& context, const QString& message) {
if (message.isEmpty()) {
return;
return QString();
}
if (type == QtDebugMsg) {
// for debug messages, check if this matches any of our regexes for repeated log messages
foreach(const QString& regexString, getInstance()._repeatedMessageRegexes) {
QRegExp repeatRegex(regexString);
if (repeatRegex.indexIn(message) != -1) {
// we have a match - add 1 to the count of repeats for this message and set this as the last repeated message
_repeatMessageCountHash[regexString] += 1;
_lastRepeatedMessage[regexString] = message;
// return out, we're not printing this one
return QString();
}
}
}
// log prefix is in the following format
@ -70,27 +97,26 @@ void LogHandler::verboseMessageHandler(QtMsgType type, const QMessageLogContext&
prefixString.append(QString(" [%1]").arg(dateString));
prefixString.append(QString(" [%1").arg(getpid()));
pid_t parentProcessID = getppid();
if (parentProcessID != 0) {
prefixString.append(QString(":%1]").arg(parentProcessID));
} else {
prefixString.append("]");
if (_shouldOutputPID) {
prefixString.append(QString(" [%1").arg(getpid()));
pid_t parentProcessID = getppid();
if (parentProcessID != 0) {
prefixString.append(QString(":%1]").arg(parentProcessID));
} else {
prefixString.append("]");
}
}
if (!getInstance()._targetName.isEmpty()) {
prefixString.append(QString(" [%1]").arg(getInstance()._targetName));
if (!_targetName.isEmpty()) {
prefixString.append(QString(" [%1]").arg(_targetName));
}
fprintf(stdout, "%s %s\n", prefixString.toLocal8Bit().constData(), message.toLocal8Bit().constData());
QString logMessage = QString("%1 %2").arg(prefixString, message);
fprintf(stdout, "%s\n", qPrintable(logMessage));
return logMessage;
}
void LogHandler::flushRepeatedMessages() {
QHash<QString, int>::iterator message = _repeatMessageCountHash.begin();
while (message != _repeatMessageCountHash.end()) {
message = _repeatMessageCountHash.erase(message);
}
void LogHandler::verboseMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) {
getInstance().printMessage(type, context, message);
}

View file

@ -31,19 +31,25 @@ public:
/// \param targetName the desired target name to output in logs
void setTargetName(const QString& targetName) { _targetName = targetName; }
void setShouldOutputPID(bool shouldOutputPID) { _shouldOutputPID = shouldOutputPID; }
QString printMessage(QtMsgType type, const QMessageLogContext& context, const QString &message);
/// a qtMessageHandler that can be hooked up to a target that links to Qt
/// prints various process, message type, and time information
static void verboseMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString &message);
void addRepeatedMessageRegex(const QRegExp& regex) { _repeatedMessageRegexes.append(regex); }
const QString& addRepeatedMessageRegex(const QString& regexString) { return *_repeatedMessageRegexes.insert(regexString); }
private:
LogHandler();
void flushRepeatedMessages();
QString _targetName;
QList<QRegExp> _repeatedMessageRegexes;
bool _shouldOutputPID;
QSet<QString> _repeatedMessageRegexes;
QHash<QString, int> _repeatMessageCountHash;
QHash<QString, QString> _lastRepeatedMessage;
};
#endif // hifi_LogHandler_h