Debug.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Reflection;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. namespace BansheeEngine
  9. {
  10. /// <summary>
  11. /// Possible types of debug messages.
  12. /// </summary>
  13. public enum DebugMessageType // Note: Must match C++ enum DebugChannel
  14. {
  15. Info, Warning, Error
  16. }
  17. /// <summary>
  18. /// Contains data for a single entry in a call stack associated with a log entry.
  19. /// </summary>
  20. public class CallStackEntry
  21. {
  22. public string method;
  23. public string file;
  24. public int line;
  25. }
  26. /// <summary>
  27. /// Contains data for a single log entry.
  28. /// </summary>
  29. public class LogEntryData
  30. {
  31. public string message;
  32. public CallStackEntry[] callstack;
  33. }
  34. /// <summary>
  35. /// Utility class providing various debug functionality.
  36. /// </summary>
  37. public sealed class Debug
  38. {
  39. /// <summary>
  40. /// Triggered when a new message is added to the debug log.
  41. /// </summary>
  42. public static Action<DebugMessageType, string> OnAdded;
  43. /// <summary>
  44. /// Logs a new informative message to the global debug log.
  45. /// </summary>
  46. /// <param name="message">Message to log.</param>
  47. public static void Log(object message)
  48. {
  49. StringBuilder sb = new StringBuilder();
  50. sb.AppendLine(message.ToString());
  51. sb.Append(GetStackTrace(1));
  52. Internal_Log(sb.ToString());
  53. }
  54. /// <summary>
  55. /// Logs a new warning message to the global debug log.
  56. /// </summary>
  57. /// <param name="message">Message to log.</param>
  58. public static void LogWarning(object message)
  59. {
  60. StringBuilder sb = new StringBuilder();
  61. sb.AppendLine(message.ToString());
  62. sb.Append(GetStackTrace(1));
  63. Internal_LogWarning(sb.ToString());
  64. }
  65. /// <summary>
  66. /// Logs a new error message to the global debug log.
  67. /// </summary>
  68. /// <param name="message">Message to log.</param>
  69. public static void LogError(object message)
  70. {
  71. StringBuilder sb = new StringBuilder();
  72. sb.AppendLine(message.ToString());
  73. sb.Append(GetStackTrace(1));
  74. Internal_LogError(sb.ToString());
  75. }
  76. /// <summary>
  77. /// Returns the stack trace of the current point in code.
  78. /// </summary>
  79. /// <param name="ignoreFirstN">Determines how many of the top-level entries should be ignored.</param>
  80. /// <returns>String containing the stack trace.</returns>
  81. public static string GetStackTrace(int ignoreFirstN = 0)
  82. {
  83. StackTrace stackTrace = new StackTrace(1, true);
  84. StackFrame[] frames = stackTrace.GetFrames();
  85. if (frames == null)
  86. return "";
  87. StringBuilder sb = new StringBuilder();
  88. for(int i = 0; i < frames.Length; i++)
  89. {
  90. if (i < ignoreFirstN)
  91. continue;
  92. StackFrame frame = frames[i];
  93. MethodBase method = frame.GetMethod();
  94. if (method == null)
  95. continue;
  96. Type parentType = method.DeclaringType;
  97. if (parentType == null)
  98. continue;
  99. sb.Append("\tat " + parentType.Name + "." + method.Name + "(");
  100. ParameterInfo[] methodParams = method.GetParameters();
  101. for(int j = 0; j < methodParams.Length; j++)
  102. {
  103. if (j > 0)
  104. sb.Append(", ");
  105. sb.Append(methodParams[j].ParameterType.Name);
  106. }
  107. sb.Append(")");
  108. string ns = parentType.Namespace;
  109. string fileName = frame.GetFileName();
  110. if (!string.IsNullOrEmpty(fileName))
  111. {
  112. int line = frame.GetFileLineNumber();
  113. int column = frame.GetFileColumnNumber();
  114. sb.Append(" in " + fileName + ", line " + line + ", column " + column + ", namespace " + ns);
  115. }
  116. else
  117. {
  118. if (!string.IsNullOrEmpty(ns))
  119. sb.Append(" in namespace " + ns);
  120. }
  121. sb.AppendLine();
  122. }
  123. return sb.ToString();
  124. }
  125. /// <summary>
  126. /// Parses a log message and outputs a data object with a separate message and callstack entries.
  127. /// </summary>
  128. /// <param name="message">Message to parse.</param>
  129. /// <returns>Parsed log message.</returns>
  130. public static LogEntryData ParseLogMessage(string message)
  131. {
  132. // Note: If you are modifying GetStackTrace method make sure to also update this one to match the formattting
  133. int firstMatchIdx = -1;
  134. Regex regex = new Regex(@"\tat (.*) in (.*), line (\d*), column .*, namespace .*");
  135. var matches = regex.Matches(message);
  136. LogEntryData newEntry = new LogEntryData();
  137. newEntry.callstack = new CallStackEntry[matches.Count];
  138. for (int i = 0; i < matches.Count; i++)
  139. {
  140. CallStackEntry callstackEntry = new CallStackEntry();
  141. callstackEntry.method = matches[i].Groups[1].Value;
  142. callstackEntry.file = matches[i].Groups[2].Value;
  143. int.TryParse(matches[i].Groups[3].Value, out callstackEntry.line);
  144. newEntry.callstack[i] = callstackEntry;
  145. if (firstMatchIdx == -1)
  146. firstMatchIdx = matches[i].Index;
  147. }
  148. if (firstMatchIdx != -1)
  149. newEntry.message = message.Substring(0, firstMatchIdx);
  150. else
  151. newEntry.message = message;
  152. return newEntry;
  153. }
  154. /// <summary>
  155. /// Triggered by the runtime when a new message is added to the debug log.
  156. /// </summary>
  157. /// <param name="type">Type of the message that was added.</param>
  158. /// <param name="message">Text of the message.</param>
  159. private static void Internal_OnAdded(DebugMessageType type, string message)
  160. {
  161. if (OnAdded != null)
  162. OnAdded(type, message);
  163. }
  164. [MethodImpl(MethodImplOptions.InternalCall)]
  165. internal static extern void Internal_Log(string message);
  166. [MethodImpl(MethodImplOptions.InternalCall)]
  167. internal static extern void Internal_LogWarning(string message);
  168. [MethodImpl(MethodImplOptions.InternalCall)]
  169. internal static extern void Internal_LogError(string message);
  170. }
  171. }