Browse Source

Fix empty lines being added for errors with no script backtrace

Mikael Hermansson 3 months ago
parent
commit
31b90246e7

+ 3 - 1
core/io/logger.cpp

@@ -88,7 +88,9 @@ void Logger::log_error(const char *p_function, const char *p_file, int p_line, c
 	logf_error("   at: %s (%s:%i)\n", p_function, p_file, p_line);
 	logf_error("   at: %s (%s:%i)\n", p_function, p_file, p_line);
 
 
 	for (const Ref<ScriptBacktrace> &backtrace : p_script_backtraces) {
 	for (const Ref<ScriptBacktrace> &backtrace : p_script_backtraces) {
-		logf_error("%s\n", backtrace->format(3).utf8().get_data());
+		if (!backtrace->is_empty()) {
+			logf_error("%s\n", backtrace->format(3).utf8().get_data());
+		}
 	}
 	}
 }
 }
 
 

+ 2 - 1
core/object/script_backtrace.cpp

@@ -52,6 +52,7 @@ void ScriptBacktrace::_store_variables(const List<String> &p_names, const List<V
 void ScriptBacktrace::_bind_methods() {
 void ScriptBacktrace::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("get_language_name"), &ScriptBacktrace::get_language_name);
 	ClassDB::bind_method(D_METHOD("get_language_name"), &ScriptBacktrace::get_language_name);
 
 
+	ClassDB::bind_method(D_METHOD("is_empty"), &ScriptBacktrace::is_empty);
 	ClassDB::bind_method(D_METHOD("get_frame_count"), &ScriptBacktrace::get_frame_count);
 	ClassDB::bind_method(D_METHOD("get_frame_count"), &ScriptBacktrace::get_frame_count);
 	ClassDB::bind_method(D_METHOD("get_frame_function", "index"), &ScriptBacktrace::get_frame_function);
 	ClassDB::bind_method(D_METHOD("get_frame_function", "index"), &ScriptBacktrace::get_frame_function);
 	ClassDB::bind_method(D_METHOD("get_frame_file", "index"), &ScriptBacktrace::get_frame_file);
 	ClassDB::bind_method(D_METHOD("get_frame_file", "index"), &ScriptBacktrace::get_frame_file);
@@ -173,7 +174,7 @@ Variant ScriptBacktrace::get_member_variable_value(int p_frame_index, int p_vari
 }
 }
 
 
 String ScriptBacktrace::format(int p_indent_all, int p_indent_frames) const {
 String ScriptBacktrace::format(int p_indent_all, int p_indent_frames) const {
-	if (stack_frames.is_empty()) {
+	if (is_empty()) {
 		return String();
 		return String();
 	}
 	}
 
 

+ 1 - 0
core/object/script_backtrace.h

@@ -65,6 +65,7 @@ public:
 
 
 	String get_language_name() const { return language_name; }
 	String get_language_name() const { return language_name; }
 
 
+	bool is_empty() const { return stack_frames.is_empty(); }
 	int get_frame_count() const { return stack_frames.size(); }
 	int get_frame_count() const { return stack_frames.size(); }
 	String get_frame_function(int p_index) const;
 	String get_frame_function(int p_index) const;
 	String get_frame_file(int p_index) const;
 	String get_frame_file(int p_index) const;

+ 6 - 0
doc/classes/ScriptBacktrace.xml

@@ -124,5 +124,11 @@
 				[b]Warning:[/b] With GDScript backtraces, the returned [Variant] will be the variable's actual value, including any object references. This means that storing the returned [Variant] will prevent any such object from being deallocated, so it's generally recommended not to do so.
 				[b]Warning:[/b] With GDScript backtraces, the returned [Variant] will be the variable's actual value, including any object references. This means that storing the returned [Variant] will prevent any such object from being deallocated, so it's generally recommended not to do so.
 			</description>
 			</description>
 		</method>
 		</method>
+		<method name="is_empty" qualifiers="const">
+			<return type="bool" />
+			<description>
+				Returns [code]true[/code] if the backtrace has no stack frames.
+			</description>
+		</method>
 	</methods>
 	</methods>
 </class>
 </class>

+ 3 - 1
drivers/unix/os_unix.cpp

@@ -1228,7 +1228,9 @@ void UnixTerminalLogger::log_error(const char *p_function, const char *p_file, i
 	logf_error("%s%sat: %s (%s:%i)%s\n", gray, indent, p_function, p_file, p_line, reset);
 	logf_error("%s%sat: %s (%s:%i)%s\n", gray, indent, p_function, p_file, p_line, reset);
 
 
 	for (const Ref<ScriptBacktrace> &backtrace : p_script_backtraces) {
 	for (const Ref<ScriptBacktrace> &backtrace : p_script_backtraces) {
-		logf_error("%s%s%s\n", gray, backtrace->format(strlen(indent)).utf8().get_data(), reset);
+		if (!backtrace->is_empty()) {
+			logf_error("%s%s%s\n", gray, backtrace->format(strlen(indent)).utf8().get_data(), reset);
+		}
 	}
 	}
 }
 }
 
 

+ 3 - 1
platform/ios/ios_terminal_logger.mm

@@ -71,7 +71,9 @@ void IOSTerminalLogger::log_error(const char *p_function, const char *p_file, in
 	}
 	}
 
 
 	for (const Ref<ScriptBacktrace> &backtrace : p_script_backtraces) {
 	for (const Ref<ScriptBacktrace> &backtrace : p_script_backtraces) {
-		os_log_error(OS_LOG_DEFAULT, "%{public}s", backtrace->format().utf8().get_data());
+		if (!backtrace->is_empty()) {
+			os_log_error(OS_LOG_DEFAULT, "%{public}s", backtrace->format().utf8().get_data());
+		}
 	}
 	}
 }
 }
 
 

+ 3 - 1
platform/linuxbsd/crash_handler_linuxbsd.cpp

@@ -153,7 +153,9 @@ static void handle_crash(int sig) {
 	}
 	}
 	if (!script_backtraces.is_empty()) {
 	if (!script_backtraces.is_empty()) {
 		for (const Ref<ScriptBacktrace> &backtrace : script_backtraces) {
 		for (const Ref<ScriptBacktrace> &backtrace : script_backtraces) {
-			print_error(backtrace->format());
+			if (!backtrace->is_empty()) {
+				print_error(backtrace->format());
+			}
 		}
 		}
 		print_error("-- END OF SCRIPT BACKTRACE --");
 		print_error("-- END OF SCRIPT BACKTRACE --");
 		print_error("================================================================");
 		print_error("================================================================");

+ 3 - 1
platform/macos/crash_handler_macos.mm

@@ -183,7 +183,9 @@ static void handle_crash(int sig) {
 	}
 	}
 	if (!script_backtraces.is_empty()) {
 	if (!script_backtraces.is_empty()) {
 		for (const Ref<ScriptBacktrace> &backtrace : script_backtraces) {
 		for (const Ref<ScriptBacktrace> &backtrace : script_backtraces) {
-			print_error(backtrace->format());
+			if (!backtrace->is_empty()) {
+				print_error(backtrace->format());
+			}
 		}
 		}
 		print_error("-- END OF SCRIPT BACKTRACE --");
 		print_error("-- END OF SCRIPT BACKTRACE --");
 		print_error("================================================================");
 		print_error("================================================================");

+ 4 - 2
platform/macos/macos_terminal_logger.mm

@@ -84,8 +84,10 @@ void MacOSTerminalLogger::log_error(const char *p_function, const char *p_file,
 	}
 	}
 
 
 	for (const Ref<ScriptBacktrace> &backtrace : p_script_backtraces) {
 	for (const Ref<ScriptBacktrace> &backtrace : p_script_backtraces) {
-		os_log_error(OS_LOG_DEFAULT, "%{public}s", backtrace->format().utf8().get_data());
-		logf_error("\E[0;90m%s\E[0m\n", backtrace->format(strlen(indent)).utf8().get_data());
+		if (!backtrace->is_empty()) {
+			os_log_error(OS_LOG_DEFAULT, "%{public}s", backtrace->format().utf8().get_data());
+			logf_error("\E[0;90m%s\E[0m\n", backtrace->format(strlen(indent)).utf8().get_data());
+		}
 	}
 	}
 }
 }
 
 

+ 3 - 1
platform/windows/crash_handler_windows_seh.cpp

@@ -237,7 +237,9 @@ DWORD CrashHandlerException(EXCEPTION_POINTERS *ep) {
 	}
 	}
 	if (!script_backtraces.is_empty()) {
 	if (!script_backtraces.is_empty()) {
 		for (const Ref<ScriptBacktrace> &backtrace : script_backtraces) {
 		for (const Ref<ScriptBacktrace> &backtrace : script_backtraces) {
-			print_error(backtrace->format());
+			if (!backtrace->is_empty()) {
+				print_error(backtrace->format());
+			}
 		}
 		}
 		print_error("-- END OF SCRIPT BACKTRACE --");
 		print_error("-- END OF SCRIPT BACKTRACE --");
 		print_error("================================================================");
 		print_error("================================================================");

+ 3 - 1
platform/windows/crash_handler_windows_signal.cpp

@@ -191,7 +191,9 @@ extern void CrashHandlerException(int signal) {
 	}
 	}
 	if (!script_backtraces.is_empty()) {
 	if (!script_backtraces.is_empty()) {
 		for (const Ref<ScriptBacktrace> &backtrace : script_backtraces) {
 		for (const Ref<ScriptBacktrace> &backtrace : script_backtraces) {
-			print_error(backtrace->format());
+			if (!backtrace->is_empty()) {
+				print_error(backtrace->format());
+			}
 		}
 		}
 		print_error("-- END OF SCRIPT BACKTRACE --");
 		print_error("-- END OF SCRIPT BACKTRACE --");
 		print_error("================================================================");
 		print_error("================================================================");

+ 3 - 1
platform/windows/windows_terminal_logger.cpp

@@ -143,7 +143,9 @@ void WindowsTerminalLogger::log_error(const char *p_function, const char *p_file
 		}
 		}
 
 
 		for (const Ref<ScriptBacktrace> &backtrace : p_script_backtraces) {
 		for (const Ref<ScriptBacktrace> &backtrace : p_script_backtraces) {
-			logf_error("%s\n", backtrace->format(strlen(indent)).utf8().get_data());
+			if (!backtrace->is_empty()) {
+				logf_error("%s\n", backtrace->format(strlen(indent)).utf8().get_data());
+			}
 		}
 		}
 
 
 		SetConsoleTextAttribute(hCon, sbi.wAttributes);
 		SetConsoleTextAttribute(hCon, sbi.wAttributes);