mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-11-04 12:51:48 +00:00
Fixed exception logging and entity id of messages
This commit is contained in:
+86
-22
@@ -1,7 +1,11 @@
|
||||
using GlitchyEngine.Core;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using GlitchyEngine.Core;
|
||||
using System.Runtime.InteropServices;
|
||||
using static GlitchyEngine.Log;
|
||||
|
||||
namespace GlitchyEngine;
|
||||
|
||||
@@ -31,9 +35,10 @@ public class Log
|
||||
/// <param name="message">The message to log.</param>
|
||||
/// <param name="callerFilePath">File path of the caller.</param>
|
||||
/// <param name="callerLineNumber">Line number of the caller.</param>
|
||||
public static void Trace(string message, [CallerFilePath]string callerFilePath = "", [CallerLineNumber]int callerLineNumber = 0)
|
||||
/// <param name="callerMemberName">Name of the calling member.</param>
|
||||
public static void Trace(string message, [CallerFilePath]string callerFilePath = "", [CallerLineNumber]int callerLineNumber = 0, [CallerMemberName]string callerMemberName = "")
|
||||
{
|
||||
ScriptGlue.Log_LogMessage(LogLevel.Trace, message, callerFilePath, callerLineNumber);
|
||||
ScriptGlue.Log_LogMessage(LogLevel.Trace, ScriptGlue.CurrentEntityId, message, callerFilePath, callerMemberName, callerLineNumber, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -42,9 +47,10 @@ public class Log
|
||||
/// <param name="message">The message to log.</param>
|
||||
/// <param name="callerFilePath">File path of the caller.</param>
|
||||
/// <param name="callerLineNumber">Line number of the caller.</param>
|
||||
public static void Info(string message, [CallerFilePath]string callerFilePath = "", [CallerLineNumber]int callerLineNumber = 0)
|
||||
/// <param name="callerMemberName">Name of the calling member.</param>
|
||||
public static void Info(string message, [CallerFilePath]string callerFilePath = "", [CallerLineNumber]int callerLineNumber = 0, [CallerMemberName]string callerMemberName = "")
|
||||
{
|
||||
ScriptGlue.Log_LogMessage(LogLevel.Info, message, callerFilePath, callerLineNumber);
|
||||
ScriptGlue.Log_LogMessage(LogLevel.Info, ScriptGlue.CurrentEntityId, message, callerFilePath, callerMemberName, callerLineNumber, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -53,9 +59,10 @@ public class Log
|
||||
/// <param name="message">The message to log.</param>
|
||||
/// <param name="callerFilePath">File path of the caller.</param>
|
||||
/// <param name="callerLineNumber">Line number of the caller.</param>
|
||||
public static void Warning(string message, [CallerFilePath]string callerFilePath = "", [CallerLineNumber]int callerLineNumber = 0)
|
||||
/// <param name="callerMemberName">Name of the calling member.</param>
|
||||
public static void Warning(string message, [CallerFilePath]string callerFilePath = "", [CallerLineNumber]int callerLineNumber = 0, [CallerMemberName]string callerMemberName = "")
|
||||
{
|
||||
ScriptGlue.Log_LogMessage(LogLevel.Warning, message, callerFilePath, callerLineNumber);
|
||||
ScriptGlue.Log_LogMessage(LogLevel.Warning, ScriptGlue.CurrentEntityId, message, callerFilePath, callerMemberName, callerLineNumber, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -64,9 +71,10 @@ public class Log
|
||||
/// <param name="message">The message to log.</param>
|
||||
/// <param name="callerFilePath">File path of the caller.</param>
|
||||
/// <param name="callerLineNumber">Line number of the caller.</param>
|
||||
public static void Error(string message, [CallerFilePath]string callerFilePath = "", [CallerLineNumber]int callerLineNumber = 0)
|
||||
/// <param name="callerMemberName">Name of the calling member.</param>
|
||||
public static void Error(string message, [CallerFilePath]string callerFilePath = "", [CallerLineNumber]int callerLineNumber = 0, [CallerMemberName]string callerMemberName = "")
|
||||
{
|
||||
ScriptGlue.Log_LogMessage(LogLevel.Error, message, callerFilePath, callerLineNumber);
|
||||
ScriptGlue.Log_LogMessage(LogLevel.Error, ScriptGlue.CurrentEntityId, message, callerFilePath, callerMemberName, callerLineNumber, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -75,9 +83,10 @@ public class Log
|
||||
/// <param name="message">The message to log.</param>
|
||||
/// <param name="callerFilePath">File path of the caller.</param>
|
||||
/// <param name="callerLineNumber">Line number of the caller.</param>
|
||||
public static void Critical(string message, [CallerFilePath]string callerFilePath = "", [CallerLineNumber]int callerLineNumber = 0)
|
||||
/// <param name="callerMemberName">Name of the calling member.</param>
|
||||
public static void Critical(string message, [CallerFilePath]string callerFilePath = "", [CallerLineNumber]int callerLineNumber = 0, [CallerMemberName]string callerMemberName = "")
|
||||
{
|
||||
ScriptGlue.Log_LogMessage(LogLevel.Critical, message, callerFilePath, callerLineNumber);
|
||||
ScriptGlue.Log_LogMessage(LogLevel.Critical, ScriptGlue.CurrentEntityId, message, callerFilePath, callerMemberName, callerLineNumber, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -86,9 +95,10 @@ public class Log
|
||||
/// <param name="obj">The object to serialize.</param>
|
||||
/// <param name="callerFilePath">File path of the caller.</param>
|
||||
/// <param name="callerLineNumber">Line number of the caller.</param>
|
||||
public static void Trace(object obj, [CallerFilePath]string callerFilePath = "", [CallerLineNumber]int callerLineNumber = 0)
|
||||
/// <param name="callerMemberName">Name of the calling member.</param>
|
||||
public static void Trace(object obj, [CallerFilePath]string callerFilePath = "", [CallerLineNumber]int callerLineNumber = 0, [CallerMemberName]string callerMemberName = "")
|
||||
{
|
||||
ScriptGlue.Log_LogMessage(LogLevel.Trace, obj.ToString(), callerFilePath, callerLineNumber);
|
||||
ScriptGlue.Log_LogMessage(LogLevel.Trace, ScriptGlue.CurrentEntityId, obj.ToString(), callerFilePath, callerMemberName, callerLineNumber, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -97,9 +107,10 @@ public class Log
|
||||
/// <param name="obj">The object to serialize.</param>
|
||||
/// <param name="callerFilePath">File path of the caller.</param>
|
||||
/// <param name="callerLineNumber">Line number of the caller.</param>
|
||||
public static void Info(object obj, [CallerFilePath]string callerFilePath = "", [CallerLineNumber]int callerLineNumber = 0)
|
||||
/// <param name="callerMemberName">Name of the calling member.</param>
|
||||
public static void Info(object obj, [CallerFilePath]string callerFilePath = "", [CallerLineNumber]int callerLineNumber = 0, [CallerMemberName]string callerMemberName = "")
|
||||
{
|
||||
ScriptGlue.Log_LogMessage(LogLevel.Info, obj.ToString(), callerFilePath, callerLineNumber);
|
||||
ScriptGlue.Log_LogMessage(LogLevel.Info, ScriptGlue.CurrentEntityId, obj.ToString(), callerFilePath, callerMemberName, callerLineNumber, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -108,9 +119,10 @@ public class Log
|
||||
/// <param name="obj">The object to serialize.</param>
|
||||
/// <param name="callerFilePath">File path of the caller.</param>
|
||||
/// <param name="callerLineNumber">Line number of the caller.</param>
|
||||
public static void Warning(object obj, [CallerFilePath]string callerFilePath = "", [CallerLineNumber]int callerLineNumber = 0)
|
||||
/// <param name="callerMemberName">Name of the calling member.</param>
|
||||
public static void Warning(object obj, [CallerFilePath]string callerFilePath = "", [CallerLineNumber]int callerLineNumber = 0, [CallerMemberName]string callerMemberName = "")
|
||||
{
|
||||
ScriptGlue.Log_LogMessage(LogLevel.Warning, obj.ToString(), callerFilePath, callerLineNumber);
|
||||
ScriptGlue.Log_LogMessage(LogLevel.Warning, ScriptGlue.CurrentEntityId, obj.ToString(), callerFilePath, callerMemberName, callerLineNumber, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -119,9 +131,10 @@ public class Log
|
||||
/// <param name="obj">The object to serialize.</param>
|
||||
/// <param name="callerFilePath">File path of the caller.</param>
|
||||
/// <param name="callerLineNumber">Line number of the caller.</param>
|
||||
public static void Error(object obj, [CallerFilePath]string callerFilePath = "", [CallerLineNumber]int callerLineNumber = 0)
|
||||
/// <param name="callerMemberName">Name of the calling member.</param>
|
||||
public static void Error(object obj, [CallerFilePath]string callerFilePath = "", [CallerLineNumber]int callerLineNumber = 0, [CallerMemberName]string callerMemberName = "")
|
||||
{
|
||||
ScriptGlue.Log_LogMessage(LogLevel.Error, obj.ToString(), callerFilePath, callerLineNumber);
|
||||
ScriptGlue.Log_LogMessage(LogLevel.Error, ScriptGlue.CurrentEntityId, obj.ToString(), callerFilePath, callerMemberName, callerLineNumber, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -130,9 +143,30 @@ public class Log
|
||||
/// <param name="obj">The object to serialize.</param>
|
||||
/// <param name="callerFilePath">File path of the caller.</param>
|
||||
/// <param name="callerLineNumber">Line number of the caller.</param>
|
||||
public static void Critical(object obj, [CallerFilePath]string callerFilePath = "", [CallerLineNumber]int callerLineNumber = 0)
|
||||
/// <param name="callerMemberName">Name of the calling member.</param>
|
||||
public static void Critical(object obj, [CallerFilePath]string callerFilePath = "", [CallerLineNumber]int callerLineNumber = 0, [CallerMemberName]string callerMemberName = "")
|
||||
{
|
||||
ScriptGlue.Log_LogMessage(LogLevel.Critical, obj.ToString(), callerFilePath, callerLineNumber);
|
||||
ScriptGlue.Log_LogMessage(LogLevel.Critical, ScriptGlue.CurrentEntityId, obj.ToString(), callerFilePath, callerMemberName, callerLineNumber, 0);
|
||||
}
|
||||
|
||||
[DebuggerDisplay("{ToString(),raw}")]
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0)]
|
||||
[EngineClass("GlitchyEngine.Scripting.ScriptGlue.GlueStackFrameInfo")]
|
||||
internal struct StackFrameInfo
|
||||
{
|
||||
public Native.StringView FileName;
|
||||
public Native.StringView MethodSignature;
|
||||
public int LineNumber;
|
||||
public int ColumnNumber;
|
||||
}
|
||||
|
||||
static string GetFullSignature(MethodBase method)
|
||||
{
|
||||
var typeName = method.DeclaringType?.FullName ?? "<unknown>";
|
||||
var parameters = string.Join(", ",
|
||||
method.GetParameters().Select(p => $"{p.ParameterType.Name}"));
|
||||
|
||||
return $"{typeName}.{method.Name}({parameters})";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -141,6 +175,36 @@ public class Log
|
||||
/// <param name="exception">The exception to log.</param>
|
||||
public static void Exception(Exception exception)
|
||||
{
|
||||
ScriptGlue.Log_LogException(UUID.Zero, exception.GetType().FullName, exception.Message, exception.StackTrace);
|
||||
StackTrace st = new StackTrace(exception, true);
|
||||
StackFrame[] frames = st.GetFrames();
|
||||
|
||||
StackFrameInfo[] stackFrameInfos = new StackFrameInfo[frames.Length];
|
||||
|
||||
// We only need StackFrameInfo for exception logging, so it's okay to manually handle the marshalling here.
|
||||
foreach (StackFrame stackFrame in frames)
|
||||
{
|
||||
MethodBase? method = stackFrame.GetMethod();
|
||||
string methodSignature = method != null ? GetFullSignature(method) : "<unknown>";
|
||||
stackFrameInfos[frames.IndexOf(stackFrame)] = new StackFrameInfo
|
||||
{
|
||||
FileName = Native.StringView.FromManagedString(stackFrame.GetFileName()),
|
||||
MethodSignature = Native.StringView.FromManagedString(methodSignature),
|
||||
LineNumber = stackFrame.GetFileLineNumber(),
|
||||
ColumnNumber = stackFrame.GetFileColumnNumber()
|
||||
};
|
||||
}
|
||||
|
||||
unsafe
|
||||
{
|
||||
fixed (StackFrameInfo* stackFrames = stackFrameInfos)
|
||||
{
|
||||
ScriptGlue.Log_LogException(ScriptGlue.CurrentEntityId, exception.GetType().FullName, exception.Message, stackFrames, stackFrameInfos.Length);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (StackFrameInfo stackFrameInfo in stackFrameInfos)
|
||||
{
|
||||
Native.StringView.FreeNativeMemory(stackFrameInfo.FileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Loader;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace GlitchyEngine;
|
||||
|
||||
@@ -272,11 +273,16 @@ internal static unsafe partial class ScriptGlue
|
||||
_unsafeClasses = null;
|
||||
}
|
||||
|
||||
private static readonly AsyncLocal<UUID> _currentEntityId = new ();
|
||||
|
||||
internal static UUID CurrentEntityId => _currentEntityId.Value;
|
||||
|
||||
[UnmanagedCallersOnly]
|
||||
public static void ShowEntityEditor(UUID entityId)
|
||||
{
|
||||
try
|
||||
{
|
||||
_currentEntityId.Value = entityId;
|
||||
(Entity entity, Type type) = EntityScriptInstances[entityId];
|
||||
EntityEditor.ShowEntityEditor(entity);
|
||||
}
|
||||
@@ -284,6 +290,10 @@ internal static unsafe partial class ScriptGlue
|
||||
{
|
||||
Log.Exception(e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_currentEntityId.Value = UUID.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
[UnmanagedCallersOnly]
|
||||
@@ -291,6 +301,7 @@ internal static unsafe partial class ScriptGlue
|
||||
{
|
||||
try
|
||||
{
|
||||
_currentEntityId.Value = entityId;
|
||||
(Entity entity, Type type) = EntityScriptInstances[entityId];
|
||||
entity.OnCreate();
|
||||
}
|
||||
@@ -298,6 +309,10 @@ internal static unsafe partial class ScriptGlue
|
||||
{
|
||||
Log.Exception(e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_currentEntityId.Value = UUID.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
|
||||
@@ -305,6 +320,7 @@ internal static unsafe partial class ScriptGlue
|
||||
{
|
||||
try
|
||||
{
|
||||
_currentEntityId.Value = entityId;
|
||||
(Entity entity, Type type) = EntityScriptInstances[entityId];
|
||||
entity.OnUpdate(deltaTime);
|
||||
}
|
||||
@@ -312,6 +328,10 @@ internal static unsafe partial class ScriptGlue
|
||||
{
|
||||
Log.Exception(e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_currentEntityId.Value = UUID.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
|
||||
@@ -319,6 +339,7 @@ internal static unsafe partial class ScriptGlue
|
||||
{
|
||||
try
|
||||
{
|
||||
_currentEntityId.Value = entityId;
|
||||
(Entity entity, Type type) = EntityScriptInstances[entityId];
|
||||
entity.OnCollisionEnter2D(collision);
|
||||
}
|
||||
@@ -326,6 +347,10 @@ internal static unsafe partial class ScriptGlue
|
||||
{
|
||||
Log.Exception(e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_currentEntityId.Value = UUID.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
[UnmanagedCallersOnly]
|
||||
@@ -333,6 +358,7 @@ internal static unsafe partial class ScriptGlue
|
||||
{
|
||||
try
|
||||
{
|
||||
_currentEntityId.Value = entityId;
|
||||
if (EntityScriptInstances.Remove(entityId, out var match) && callDestroy > 0)
|
||||
{
|
||||
match.Entity.OnDestroy();
|
||||
@@ -342,6 +368,10 @@ internal static unsafe partial class ScriptGlue
|
||||
{
|
||||
Log.Exception(e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_currentEntityId.Value = UUID.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
internal static Type? GetTypeFromNativeString(byte* fullTypeName)
|
||||
@@ -365,6 +395,7 @@ internal static unsafe partial class ScriptGlue
|
||||
{
|
||||
try
|
||||
{
|
||||
_currentEntityId.Value = entityId;
|
||||
Type? scriptType = GetTypeFromNativeString(scriptClassName);
|
||||
|
||||
Debug.Assert(scriptType != null, "Script class Type not found.");
|
||||
@@ -379,6 +410,10 @@ internal static unsafe partial class ScriptGlue
|
||||
{
|
||||
Log.Exception(e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_currentEntityId.Value = UUID.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
struct ComponentFunctionPointers
|
||||
@@ -459,6 +494,7 @@ internal static unsafe partial class ScriptGlue
|
||||
{
|
||||
try
|
||||
{
|
||||
_currentEntityId.Value = entityId;
|
||||
if (!EntityScriptInstances.TryGetValue(entityId, out var match))
|
||||
return;
|
||||
|
||||
@@ -468,6 +504,10 @@ internal static unsafe partial class ScriptGlue
|
||||
{
|
||||
Log.Exception(e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_currentEntityId.Value = UUID.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
[UnmanagedCallersOnly]
|
||||
@@ -475,6 +515,7 @@ internal static unsafe partial class ScriptGlue
|
||||
{
|
||||
try
|
||||
{
|
||||
_currentEntityId.Value = entityId;
|
||||
if (!EntityScriptInstances.TryGetValue(entityId, out var match))
|
||||
return;
|
||||
|
||||
@@ -484,6 +525,10 @@ internal static unsafe partial class ScriptGlue
|
||||
{
|
||||
Log.Exception(e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_currentEntityId.Value = UUID.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
[UnmanagedCallersOnly]
|
||||
|
||||
Reference in New Issue
Block a user