From af5be8d1d91546a37aa4ef6358b4666adf56af4e Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Tue, 18 Oct 2016 15:59:00 -0700 Subject: [PATCH] reduce data-races in logging --- assignment-client/src/audio/AudioMixer.cpp | 4 +++ libraries/networking/src/LimitedNodeList.cpp | 2 +- libraries/shared/src/LogHandler.cpp | 34 +++++++++++++++++--- libraries/shared/src/LogHandler.h | 12 +++---- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/assignment-client/src/audio/AudioMixer.cpp b/assignment-client/src/audio/AudioMixer.cpp index 08e4ff8972..0252c037bf 100644 --- a/assignment-client/src/audio/AudioMixer.cpp +++ b/assignment-client/src/audio/AudioMixer.cpp @@ -638,6 +638,10 @@ QString AudioMixer::percentageForMixStats(int counter) { void AudioMixer::sendStatsPacket() { QJsonObject statsObject; + if (_numStatFrames == 0) { + return; + } + statsObject["useDynamicJitterBuffers"] = _numStaticJitterFrames == -1; statsObject["trailing_sleep_percentage"] = _trailingSleepRatio * 100.0f; statsObject["performance_throttling_ratio"] = _performanceThrottlingRatio; diff --git a/libraries/networking/src/LimitedNodeList.cpp b/libraries/networking/src/LimitedNodeList.cpp index e652c2deb3..fa404b39d8 100644 --- a/libraries/networking/src/LimitedNodeList.cpp +++ b/libraries/networking/src/LimitedNodeList.cpp @@ -300,7 +300,7 @@ bool LimitedNodeList::packetSourceAndHashMatchAndTrackBandwidth(const udt::Packe = LogHandler::getInstance().addRepeatedMessageRegex(UNKNOWN_REGEX); qCDebug(networking) << "Packet of type" << headerType - << "received from unknown node with UUID" << qPrintable(uuidStringWithoutCurlyBraces(sourceID)); + << "received from unknown node with UUID" << uuidStringWithoutCurlyBraces(sourceID); } } diff --git a/libraries/shared/src/LogHandler.cpp b/libraries/shared/src/LogHandler.cpp index f003506aa7..d142e3f839 100644 --- a/libraries/shared/src/LogHandler.cpp +++ b/libraries/shared/src/LogHandler.cpp @@ -21,6 +21,8 @@ #include "LogHandler.h" +QMutex LogHandler::_mutex; + LogHandler& LogHandler::getInstance() { static LogHandler staticInstance; return staticInstance; @@ -63,8 +65,29 @@ const QString DATE_STRING_FORMAT = "MM/dd hh:mm:ss"; // the following will produce 11/18 13:55:36.999 const QString DATE_STRING_FORMAT_WITH_MILLISECONDS = "MM/dd hh:mm:ss.zzz"; +void LogHandler::setTargetName(const QString& targetName) { + QMutexLocker lock(&_mutex); + _targetName = targetName; +} + +void LogHandler::setShouldOutputProcessID(bool shouldOutputProcessID) { + QMutexLocker lock(&_mutex); + _shouldOutputProcessID = shouldOutputProcessID; +} + +void LogHandler::setShouldOutputThreadID(bool shouldOutputThreadID) { + QMutexLocker lock(&_mutex); + _shouldOutputThreadID = shouldOutputThreadID; +} + +void LogHandler::setShouldDisplayMilliseconds(bool shouldDisplayMilliseconds) { + QMutexLocker lock(&_mutex); + _shouldDisplayMilliseconds = shouldDisplayMilliseconds; +} + + void LogHandler::flushRepeatedMessages() { - QMutexLocker locker(&_repeatedMessageLock); + QMutexLocker lock(&_mutex); QHash::iterator message = _repeatMessageCountHash.begin(); while (message != _repeatMessageCountHash.end()) { @@ -73,7 +96,9 @@ void LogHandler::flushRepeatedMessages() { .arg(message.value()).arg(message.key()).arg(_lastRepeatedMessage.value(message.key())); QMessageLogContext emptyContext; + lock.unlock(); printMessage(LogSuppressed, emptyContext, repeatMessage); + lock.relock(); } _lastRepeatedMessage.remove(message.key()); @@ -82,13 +107,13 @@ void LogHandler::flushRepeatedMessages() { } QString LogHandler::printMessage(LogMsgType type, const QMessageLogContext& context, const QString& message) { + QMutexLocker lock(&_mutex); if (message.isEmpty()) { return QString(); } if (type == LogDebug) { // for debug messages, check if this matches any of our regexes for repeated log messages - QMutexLocker locker(&_repeatedMessageLock); foreach(const QString& regexString, getInstance()._repeatedMessageRegexes) { QRegExp repeatRegex(regexString); if (repeatRegex.indexIn(message) != -1) { @@ -111,7 +136,6 @@ QString LogHandler::printMessage(LogMsgType type, const QMessageLogContext& cont } } if (type == LogDebug) { - QMutexLocker locker(&_onlyOnceMessageLock); // see if this message is one we should only print once foreach(const QString& regexString, getInstance()._onlyOnceMessageRegexes) { QRegExp onlyOnceRegex(regexString); @@ -163,11 +187,11 @@ void LogHandler::verboseMessageHandler(QtMsgType type, const QMessageLogContext& } const QString& LogHandler::addRepeatedMessageRegex(const QString& regexString) { - QMutexLocker locker(&_repeatedMessageLock); + QMutexLocker lock(&_mutex); return *_repeatedMessageRegexes.insert(regexString); } const QString& LogHandler::addOnlyOnceMessageRegex(const QString& regexString) { - QMutexLocker locker(&_onlyOnceMessageLock); + QMutexLocker lock(&_mutex); return *_onlyOnceMessageRegexes.insert(regexString); } diff --git a/libraries/shared/src/LogHandler.h b/libraries/shared/src/LogHandler.h index d346913dd3..890497b891 100644 --- a/libraries/shared/src/LogHandler.h +++ b/libraries/shared/src/LogHandler.h @@ -38,11 +38,11 @@ public: /// sets the target name to output via the verboseMessageHandler, called once before logging begins /// \param targetName the desired target name to output in logs - void setTargetName(const QString& targetName) { _targetName = targetName; } + void setTargetName(const QString& targetName); - void setShouldOutputProcessID(bool shouldOutputProcessID) { _shouldOutputProcessID = shouldOutputProcessID; } - void setShouldOutputThreadID(bool shouldOutputThreadID) { _shouldOutputThreadID = shouldOutputThreadID; } - void setShouldDisplayMilliseconds(bool shouldDisplayMilliseconds) { _shouldDisplayMilliseconds = shouldDisplayMilliseconds; } + void setShouldOutputProcessID(bool shouldOutputProcessID); + void setShouldOutputThreadID(bool shouldOutputThreadID); + void setShouldDisplayMilliseconds(bool shouldDisplayMilliseconds); QString printMessage(LogMsgType type, const QMessageLogContext& context, const QString &message); @@ -64,11 +64,11 @@ private: QSet _repeatedMessageRegexes; QHash _repeatMessageCountHash; QHash _lastRepeatedMessage; - QMutex _repeatedMessageLock; QSet _onlyOnceMessageRegexes; QHash _onlyOnceMessageCountHash; - QMutex _onlyOnceMessageLock; + + static QMutex _mutex; }; #endif // hifi_LogHandler_h