From aab5b22e25cb186c53e5d33078fcbe8a48bcc304 Mon Sep 17 00:00:00 2001 From: Dale Glass Date: Sun, 27 Dec 2020 22:35:30 +0100 Subject: [PATCH] Skip duplicated log entries This helps with log flooding. Successive repeated log messages will be skipped and counted, the count will be output when a different message is logged. A new option of 'keep_repeats' has been added to VIRCADIA_LOG_OPTIONS to disable this. --- libraries/shared/src/LogHandler.cpp | 15 ++++++++++++++- libraries/shared/src/LogHandler.h | 5 +++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/libraries/shared/src/LogHandler.cpp b/libraries/shared/src/LogHandler.cpp index 72d53f0bcb..098e5e9a80 100644 --- a/libraries/shared/src/LogHandler.cpp +++ b/libraries/shared/src/LogHandler.cpp @@ -64,6 +64,8 @@ LogHandler::LogHandler() { _shouldOutputThreadID = true; } else if (option == "milliseconds") { _shouldDisplayMilliseconds = true; + } else if (option == "keep_repeats") { + _keepRepeats = true; } else if (option != "") { fprintf(stdout, "Unrecognized option in VIRCADIA_LOG_OPTIONS: '%s'\n", option.toUtf8().constData()); } @@ -202,7 +204,18 @@ QString LogHandler::printMessage(LogMsgType type, const QMessageLogContext& cont resetColor = colorReset(); } - fprintf(stdout, "%s%s%s", color, qPrintable(logMessage), resetColor); + if (_keepRepeats || _previousMessage != message) { + if (_repeatCount > 0) { + fprintf(stdout, "[Previous message was repeated %i times]\n", _repeatCount); + } + + fprintf(stdout, "%s%s%s", color, qPrintable(logMessage), resetColor); + _repeatCount = 0; + } else { + _repeatCount++; + } + + _previousMessage = message; #ifdef Q_OS_WIN // On windows, this will output log lines into the Visual Studio "output" tab OutputDebugStringA(qPrintable(logMessage)); diff --git a/libraries/shared/src/LogHandler.h b/libraries/shared/src/LogHandler.h index 7bd5c69c63..71df2a4189 100644 --- a/libraries/shared/src/LogHandler.h +++ b/libraries/shared/src/LogHandler.h @@ -67,6 +67,11 @@ private: bool _shouldOutputThreadID { false }; bool _shouldDisplayMilliseconds { false }; bool _useColor { false }; + bool _keepRepeats { false }; + + QString _previousMessage; + int _repeatCount { 0 }; + int _currentMessageID { 0 }; struct RepeatedMessageRecord {