Added a Godot TraceListener, which is automatically installed on startup. Fixes that Debug/Trace Assertions are simply swallowed by Godot.

This commit is contained in:
Sebastian Hartte 2019-03-07 12:17:56 +01:00
parent 6976181638
commit 37d448fca7
4 changed files with 64 additions and 0 deletions

View file

@ -19,6 +19,12 @@ namespace Godot
sb.Append(" "); sb.Append(" ");
} }
public static void InstallTraceListener()
{
Trace.Listeners.Clear();
Trace.Listeners.Add(new GodotTraceListener());
}
public static void GetStackFrameInfo(StackFrame frame, out string fileName, out int fileLineNumber, out string methodDecl) public static void GetStackFrameInfo(StackFrame frame, out string fileName, out int fileLineNumber, out string methodDecl)
{ {
fileName = frame.GetFileName(); fileName = frame.GetFileName();

View file

@ -0,0 +1,37 @@
using System;
using System.Diagnostics;
namespace Godot
{
internal class GodotTraceListener : TraceListener
{
public override void Write(string message)
{
GD.PrintRaw(message);
}
public override void WriteLine(string message)
{
GD.Print(message);
}
public override void Fail(string message, string detailMessage)
{
GD.PrintErr("Assertion failed: ", message);
if (detailMessage != null)
{
GD.PrintErr(" Details: ", detailMessage);
}
try
{
var stackTrace = new StackTrace(true).ToString();
GD.PrintErr(stackTrace);
}
catch
{
// ignored
}
}
}
}

View file

@ -587,6 +587,8 @@ bool GDMono::_load_core_api_assembly() {
CS_GLUE_VERSION != api_assembly_ver.cs_glue_version; CS_GLUE_VERSION != api_assembly_ver.cs_glue_version;
if (!core_api_assembly_out_of_sync) { if (!core_api_assembly_out_of_sync) {
GDMonoUtils::update_godot_api_cache(); GDMonoUtils::update_godot_api_cache();
_install_trace_listener();
} }
#else #else
GDMonoUtils::update_godot_api_cache(); GDMonoUtils::update_godot_api_cache();
@ -690,6 +692,23 @@ bool GDMono::_load_api_assemblies() {
return true; return true;
} }
void GDMono::_install_trace_listener() {
#ifdef DEBUG_ENABLED
// Install the trace listener now before the project assembly is loaded
typedef void (*DebuggingUtils_InstallTraceListener)(MonoObject **);
MonoException *exc = NULL;
GDMonoClass *debug_utils = core_api_assembly->get_class(BINDINGS_NAMESPACE, "DebuggingUtils");
DebuggingUtils_InstallTraceListener install_func =
(DebuggingUtils_InstallTraceListener)debug_utils->get_method_thunk("InstallTraceListener");
install_func((MonoObject **)&exc);
if (exc) {
ERR_PRINT("Failed to install System.Diagnostics.Trace listener");
GDMonoUtils::debug_print_unhandled_exception(exc);
}
#endif
}
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
String GDMono::_get_api_assembly_metadata_path() { String GDMono::_get_api_assembly_metadata_path() {

View file

@ -125,6 +125,8 @@ class GDMono {
String _get_api_assembly_metadata_path(); String _get_api_assembly_metadata_path();
#endif #endif
void _install_trace_listener();
void _register_internal_calls(); void _register_internal_calls();
Error _load_scripts_domain(); Error _load_scripts_domain();