Skip to content

Commit

Permalink
Limit propagation of error to handlers to 6 consecutive errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnymind committed Oct 4, 2024
1 parent 41bcc22 commit cafb5e2
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions core/error/error_macros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@
#include "scene/main/node.h"
#endif

int repetitions_last_line = -1;
const char *repetitions_last_function = NULL;
// No need to initialize: by constructon, can never be checked if line/function don't match.
char repetitions_last_message[256];
int repetitions_last_message_count = 0;
const int MAX_LOG_MESSAGE_REPETITIONS = 6;
const char *REPETITIONS_WARNING_MESSAGE = "Last message repeated 6 times...";

static ErrorHandlerList *error_handler_list = nullptr;

void add_error_handler(ErrorHandlerList *p_handler) {
Expand Down Expand Up @@ -98,6 +106,25 @@ void _err_print_error(const char *p_function, const char *p_file, int p_line, co
}

_global_lock();

if (repetitions_last_line == p_line && repetitions_last_function == p_function && ::strcmp(p_message, repetitions_last_message) == 0) {
if (repetitions_last_message_count == MAX_LOG_MESSAGE_REPETITIONS) {
p_message = REPETITIONS_WARNING_MESSAGE;
repetitions_last_message_count += 1; // avoid rollover errors
} else if (repetitions_last_message_count < MAX_LOG_MESSAGE_REPETITIONS) {
repetitions_last_message_count += 1;
} else {
_global_unlock();
return;
}
} else {
repetitions_last_message_count = 0;
repetitions_last_line = p_line;
repetitions_last_function = p_function;
// The message memory lifetime may end after this call.
::strncpy(repetitions_last_message, p_message, sizeof(repetitions_last_message));
}

ErrorHandlerList *l = error_handler_list;
while (l) {
l->errfunc(l->userdata, p_function, p_file, p_line, p_error, p_message, p_editor_notify, p_type);
Expand Down

0 comments on commit cafb5e2

Please sign in to comment.