From d4808ba53b6bf7260e4e13efdd8071eb401a3801 Mon Sep 17 00:00:00 2001 From: past-due <30942300+past-due@users.noreply.github.com> Date: Tue, 28 Nov 2023 19:30:16 -0500 Subject: [PATCH] debugLogFromGfxCallback: Add repeated message handling --- lib/framework/debug.cpp | 53 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/lib/framework/debug.cpp b/lib/framework/debug.cpp index 240d57c20cd..2aae380d53f 100644 --- a/lib/framework/debug.cpp +++ b/lib/framework/debug.cpp @@ -523,6 +523,10 @@ static void debugDisplayFatalErrorMsgBox(const char* outputLogLine) // Special version of _debug that doesn't use any global debug-logging state (or special sauce) *except* the callbackRegistry (via printToDebugCallbacks) void _debugFromGfxCallback(int line, code_part part, const char *function, const char *str, ...) { + thread_local char lastGfxCallbackMsg[MAX_LEN_LOG_LINE] = {'\0'}; + thread_local unsigned int repeated = 0; /* times last message repeated */ + thread_local unsigned int prev = 0; /* total on last update */ + char outputBuffer[MAX_LEN_LOG_LINE]; time_t rawtime; @@ -549,11 +553,54 @@ void _debugFromGfxCallback(int line, code_part part, const char *function, const vsnprintf(pStartOutputLineContents, remainingLength, str, ap); va_end(ap); - printToDebugCallbacks(outputBuffer, part); + if (strncmp(lastGfxCallbackMsg, pStartOutputLineContents, MAX_LEN_LOG_LINE) == 0) + { + // last message, duplicated + ++repeated; + bool isRepeatedPowOf2 = repeated >= 2 && ((repeated & (repeated - 1)) == 0); + if (isRepeatedPowOf2) + { + if (repeated > 2) + { + snprintf(pStartOutputLineContents, remainingLength, "last message repeated %u times (total %u repeats)", repeated - prev, repeated); + } + else + { + snprintf(pStartOutputLineContents, remainingLength, "last message repeated %u times", repeated - prev); + } + printToDebugCallbacks(outputBuffer, part); + prev = repeated; + } + } + else + { + // Received another line, cleanup the old + if (repeated > 0 && repeated != prev && repeated != 1) + { + /* just repeat the previous message when only one repeat occurred */ + if (repeated > 2) + { + ssprintf(lastGfxCallbackMsg, "last message repeated %u times (total %u repeats)", repeated - prev, repeated); + } + else + { + ssprintf(lastGfxCallbackMsg, "last message repeated %u times", repeated - prev); + } + printToDebugCallbacks(lastGfxCallbackMsg, part); + } + repeated = 0; + prev = 0; + sstrcpy(lastGfxCallbackMsg, pStartOutputLineContents); + } - if (part == LOG_FATAL) + if (!repeated) { - debugDisplayFatalErrorMsgBox(pStartOutputLineContents); + printToDebugCallbacks(outputBuffer, part); + + if (part == LOG_FATAL) + { + debugDisplayFatalErrorMsgBox(pStartOutputLineContents); + } } }