mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 13:28:09 +02:00
use the LogHandler in Interface
This commit is contained in:
parent
d3bbd251db
commit
8e3102266a
4 changed files with 61 additions and 28 deletions
|
@ -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";
|
const QString DEFAULT_SCRIPTS_JS_URL = "http://public.highfidelity.io/scripts/defaultScripts.js";
|
||||||
|
|
||||||
void messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) {
|
void messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) {
|
||||||
if (message.size() > 0) {
|
QString logMessage = LogHandler::getInstance().printMessage(type, context, message);
|
||||||
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));
|
if (!logMessage.isEmpty()) {
|
||||||
Application::getInstance()->getLogger()->addMessage(qPrintable(formattedMessage));
|
Application::getInstance()->getLogger()->addMessage(qPrintable(logMessage));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -212,6 +212,9 @@ bool LimitedNodeList::packetVersionAndHashMatch(const QByteArray& packet) {
|
||||||
<< uuidFromPacketHeader(packet);
|
<< uuidFromPacketHeader(packet);
|
||||||
}
|
}
|
||||||
} else {
|
} 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"
|
qDebug() << "Packet of type" << checkType << "received from unknown node with UUID"
|
||||||
<< uuidStringWithoutCurlyBraces(uuidFromPacketHeader(packet));
|
<< uuidStringWithoutCurlyBraces(uuidFromPacketHeader(packet));
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
#define pid_t int // hack to build
|
#define pid_t int // hack to build
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <qdebug.h>
|
||||||
#include <qtimer.h>
|
#include <qtimer.h>
|
||||||
|
|
||||||
#include "LogHandler.h"
|
#include "LogHandler.h"
|
||||||
|
@ -26,7 +27,9 @@ LogHandler& LogHandler::getInstance() {
|
||||||
return staticInstance;
|
return staticInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
LogHandler::LogHandler() {
|
LogHandler::LogHandler() :
|
||||||
|
_shouldOutputPID(false)
|
||||||
|
{
|
||||||
// setup our timer to flush the verbose logs every 5 seconds
|
// setup our timer to flush the verbose logs every 5 seconds
|
||||||
QTimer* logFlushTimer = new QTimer(this);
|
QTimer* logFlushTimer = new QTimer(this);
|
||||||
connect(logFlushTimer, &QTimer::timeout, this, &LogHandler::flushRepeatedMessages);
|
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
|
// the following will produce 2000-10-02 13:55:36 -0700
|
||||||
const char DATE_STRING_FORMAT[] = "%Y-%m-%d %H:%M:%S %z";
|
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()) {
|
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
|
// 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(dateString));
|
||||||
|
|
||||||
prefixString.append(QString(" [%1").arg(getpid()));
|
if (_shouldOutputPID) {
|
||||||
|
prefixString.append(QString(" [%1").arg(getpid()));
|
||||||
|
|
||||||
pid_t parentProcessID = getppid();
|
pid_t parentProcessID = getppid();
|
||||||
if (parentProcessID != 0) {
|
if (parentProcessID != 0) {
|
||||||
prefixString.append(QString(":%1]").arg(parentProcessID));
|
prefixString.append(QString(":%1]").arg(parentProcessID));
|
||||||
} else {
|
} else {
|
||||||
prefixString.append("]");
|
prefixString.append("]");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!getInstance()._targetName.isEmpty()) {
|
if (!_targetName.isEmpty()) {
|
||||||
prefixString.append(QString(" [%1]").arg(getInstance()._targetName));
|
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() {
|
void LogHandler::verboseMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) {
|
||||||
QHash<QString, int>::iterator message = _repeatMessageCountHash.begin();
|
getInstance().printMessage(type, context, message);
|
||||||
while (message != _repeatMessageCountHash.end()) {
|
|
||||||
message = _repeatMessageCountHash.erase(message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -31,19 +31,25 @@ public:
|
||||||
/// \param targetName the desired target name to output in logs
|
/// \param targetName the desired target name to output in logs
|
||||||
void setTargetName(const QString& targetName) { _targetName = targetName; }
|
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
|
/// a qtMessageHandler that can be hooked up to a target that links to Qt
|
||||||
/// prints various process, message type, and time information
|
/// prints various process, message type, and time information
|
||||||
static void verboseMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString &message);
|
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:
|
private:
|
||||||
LogHandler();
|
LogHandler();
|
||||||
|
|
||||||
void flushRepeatedMessages();
|
void flushRepeatedMessages();
|
||||||
|
|
||||||
QString _targetName;
|
QString _targetName;
|
||||||
QList<QRegExp> _repeatedMessageRegexes;
|
bool _shouldOutputPID;
|
||||||
|
QSet<QString> _repeatedMessageRegexes;
|
||||||
QHash<QString, int> _repeatMessageCountHash;
|
QHash<QString, int> _repeatMessageCountHash;
|
||||||
|
QHash<QString, QString> _lastRepeatedMessage;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // hifi_LogHandler_h
|
#endif // hifi_LogHandler_h
|
Loading…
Reference in a new issue