Merge pull request #95 from daleglass-overte/log-breakpoint

Add log breakpoint system
This commit is contained in:
Dale Glass 2022-06-26 17:56:20 +02:00 committed by GitHub
commit 2c40d872d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 0 deletions

View file

@ -0,0 +1,28 @@
//
// Breakpoint.h
//
//
// Created by Dale Glass on 5/6/2022
// Copyright 2022 Dale Glass
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
// Software defined breakpoints, for aiding in debugging
#pragma once
#if defined(__GNUC__)
#include <csignal>
#define BREAKPOINT raise(SIGINT);
#elif defined(__clang__)
#define BREAKPOINT __builtin_trap();
#elif _MSC_VER && !__INTEL_COMPILER
#include <intrin.h>
#define BREAKPOINT __debugbreak();
#else
#include "CrashHelpers.h"
#define BREAKPOINT crash::nullDeref();
#endif

View file

@ -11,6 +11,7 @@
//
#include "LogHandler.h"
#include "Breakpoint.h"
#include <mutex>
@ -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));
}

View file

@ -56,6 +56,27 @@ 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.
*
* @note Support for creating a breakpoint in software is compiler and OS specific. If there's no support for
* creating a breakpoint on the current compiler/OS, then an abort will be triggered instead.
*
* @param str Text to match
*/
static void breakOnMessage(const char *str);
private:
LogHandler();
~LogHandler() = default;
@ -79,6 +100,8 @@ private:
QString repeatString;
};
std::vector<RepeatedMessageRecord> _repeatedMessageRecords;
QStringList _breakMessages;
static QRecursiveMutex _mutex;
};