Explorar el Código

Added stack trace to debug log

Marko Pintera hace 10 años
padre
commit
400e85d85b
Se han modificado 1 ficheros con 70 adiciones y 4 borrados
  1. 70 4
      MBansheeEngine/Debug.cs

+ 70 - 4
MBansheeEngine/Debug.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
-using System.Linq;
+using System.Diagnostics;
+using System.Reflection;
 using System.Runtime.CompilerServices;
 using System.Text;
 
@@ -10,17 +11,82 @@ namespace BansheeEngine
     {
         public static void Log(object message)
         {
-            Internal_Log(message.ToString());
+            StringBuilder sb = new StringBuilder();
+            sb.AppendLine(message.ToString());
+            sb.Append(GetStackTrace());
+
+            Internal_Log(sb.ToString());
         }
 
         public static void LogWarning(object message)
         {
-            Internal_LogWarning(message.ToString());
+            StringBuilder sb = new StringBuilder();
+            sb.AppendLine(message.ToString());
+            sb.Append(GetStackTrace());
+
+            Internal_LogWarning(sb.ToString());
         }
 
         public static void LogError(object message)
         {
-            Internal_LogError(message.ToString());
+            StringBuilder sb = new StringBuilder();
+            sb.AppendLine(message.ToString());
+            sb.Append(GetStackTrace());
+
+            Internal_LogError(sb.ToString());
+        }
+
+        public static string GetStackTrace()
+        {
+            StackTrace stackTrace = new StackTrace(1, true);
+
+            StackFrame[] frames = stackTrace.GetFrames();
+            if (frames == null)
+                return "";
+
+            StringBuilder sb = new StringBuilder();
+            foreach (var frame in frames)
+            {
+                MethodBase method = frame.GetMethod();
+                if (method == null)
+                    continue;
+
+                Type parentType = method.DeclaringType;
+                if (parentType == null)
+                    continue;
+
+                sb.Append("\tat " + parentType.Name + "." + method.Name + "(");
+
+                ParameterInfo[] methodParams = method.GetParameters();
+                for(int i = 0; i < methodParams.Length; i++)
+                {
+                    if (i > 0)
+                        sb.Append(", ");
+
+                    sb.Append(methodParams[i].ParameterType.Name);
+                }
+
+                sb.Append(")");
+
+                string ns = parentType.Namespace;
+                string fileName = frame.GetFileName();
+                if (!string.IsNullOrEmpty(fileName))
+                {
+                    int line = frame.GetFileLineNumber();
+                    int column = frame.GetFileColumnNumber();
+
+                    sb.Append(" in " + fileName + ", line " + line + ", column " + column + ", namespace " + ns);
+                }
+                else
+                {
+                    if (!string.IsNullOrEmpty(ns))
+                        sb.Append(" in namespace " + ns);
+                }
+
+                sb.AppendLine();
+            }
+
+            return sb.ToString();
         }
 
         [MethodImpl(MethodImplOptions.InternalCall)]