From d323be22df7e6c8988df0f81ab1ae1eb13fe8748 Mon Sep 17 00:00:00 2001 From: Dale Glass Date: Mon, 20 Jun 2022 16:39:22 +0200 Subject: [PATCH] Add log breakpoint system --- libraries/shared/src/LogHandler.cpp | 14 ++++++++++++++ libraries/shared/src/LogHandler.h | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/libraries/shared/src/LogHandler.cpp b/libraries/shared/src/LogHandler.cpp index 50d2d53640..f214613bfa 100644 --- a/libraries/shared/src/LogHandler.cpp +++ b/libraries/shared/src/LogHandler.cpp @@ -11,6 +11,7 @@ // #include "LogHandler.h" +#include "Breakpoint.h" #include @@ -216,6 +217,13 @@ QString LogHandler::printMessage(LogMsgType type, const QMessageLogContext& cont _repeatCount++; } + if ( !_breakMessages.empty() ) { + for(const auto &str : _breakMessages) { + if (logMessage.contains(str)) { + BREAKPOINT + } + } + } _previousMessage = message; #ifdef Q_OS_WIN // On windows, this will output log lines into the Visual Studio "output" tab @@ -262,3 +270,9 @@ void LogHandler::printRepeatedMessage(int messageID, LogMsgType type, const QMes ++_repeatedMessageRecords[messageID].repeatCount; } + + +void LogHandler::breakOnMessage(const char *message) { + QMutexLocker lock(&_mutex); + LogHandler::getInstance()._breakMessages.append(QString::fromUtf8(message)); +} \ No newline at end of file diff --git a/libraries/shared/src/LogHandler.h b/libraries/shared/src/LogHandler.h index d314a4f7c2..40eeb9431c 100644 --- a/libraries/shared/src/LogHandler.h +++ b/libraries/shared/src/LogHandler.h @@ -56,6 +56,24 @@ public: void setupRepeatedMessageFlusher(); + /** + * @brief Break when a message that contains the specified string is logged + * + * This is a function intended to be invoked from inside a debugger. It should be of help when it's hard to put a breakpoint + * on the generating line because it comes from inlined code, or the interesting text is generated at runtime. + * + * Example usage: + * + * @code {.cpp} + * LogHandler::breakOnMessage("No instance available for"); + * @endcode + * + * Then the debugger should be triggered as soon as a message containing that string is logged. Backtracking + * through the call stack should lead back to the source. + * + * @param str Text to match + */ + static void breakOnMessage(const char *str); private: LogHandler(); ~LogHandler() = default; @@ -79,6 +97,8 @@ private: QString repeatString; }; std::vector _repeatedMessageRecords; + + QStringList _breakMessages; static QRecursiveMutex _mutex; };