Debug.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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());
  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());
  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());
  74. Internal_LogError(sb.ToString());
  75. }
  76. /// <summary>
  77. /// Returns the stack trace of the current point in code.
  78. /// </summary>
  79. /// <returns>String containing the stack trace.</returns>
  80. public static string GetStackTrace()
  81. {
  82. StackTrace stackTrace = new StackTrace(1, true);
  83. StackFrame[] frames = stackTrace.GetFrames();
  84. if (frames == null)
  85. return "";
  86. StringBuilder sb = new StringBuilder();
  87. foreach (var frame in frames)
  88. {
  89. MethodBase method = frame.GetMethod();
  90. if (method == null)
  91. continue;
  92. Type parentType = method.DeclaringType;
  93. if (parentType == null)
  94. continue;
  95. sb.Append("\tat " + parentType.Name + "." + method.Name + "(");
  96. ParameterInfo[] methodParams = method.GetParameters();
  97. for(int i = 0; i < methodParams.Length; i++)
  98. {
  99. if (i > 0)
  100. sb.Append(", ");
  101. sb.Append(methodParams[i].ParameterType.Name);
  102. }
  103. sb.Append(")");
  104. string ns = parentType.Namespace;
  105. string fileName = frame.GetFileName();
  106. if (!string.IsNullOrEmpty(fileName))
  107. {
  108. int line = frame.GetFileLineNumber();
  109. int column = frame.GetFileColumnNumber();
  110. sb.Append(" in " + fileName + ", line " + line + ", column " + column + ", namespace " + ns);
  111. }
  112. else
  113. {
  114. if (!string.IsNullOrEmpty(ns))
  115. sb.Append(" in namespace " + ns);
  116. }
  117. sb.AppendLine();
  118. }
  119. return sb.ToString();
  120. }
  121. /// <summary>
  122. /// Parses a log message and outputs a data object with a separate message and callstack entries.
  123. /// </summary>
  124. /// <param name="message">Message to parse.</param>
  125. /// <returns>Parsed log message.</returns>
  126. public static LogEntryData ParseLogMessage(string message)
  127. {
  128. // Note: If you are modifying GetStackTrace method make sure to also update this one to match the formattting
  129. int firstMatchIdx = -1;
  130. Regex regex = new Regex(@"\tat (.*) in (.*), line (\d*), column .*, namespace .*");
  131. var matches = regex.Matches(message);
  132. LogEntryData newEntry = new LogEntryData();
  133. newEntry.callstack = new CallStackEntry[matches.Count];
  134. for (int i = 0; i < matches.Count; i++)
  135. {
  136. CallStackEntry callstackEntry = new CallStackEntry();
  137. callstackEntry.method = matches[i].Groups[1].Value;
  138. callstackEntry.file = matches[i].Groups[2].Value;
  139. int.TryParse(matches[i].Groups[3].Value, out callstackEntry.line);
  140. newEntry.callstack[i] = callstackEntry;
  141. if (firstMatchIdx == -1)
  142. firstMatchIdx = matches[i].Index;
  143. }
  144. if (firstMatchIdx != -1)
  145. newEntry.message = message.Substring(0, firstMatchIdx);
  146. else
  147. newEntry.message = message;
  148. return newEntry;
  149. }
  150. /// <summary>
  151. /// Triggered by the runtime when a new message is added to the debug log.
  152. /// </summary>
  153. /// <param name="type">Type of the message that was added.</param>
  154. /// <param name="message">Text of the message.</param>
  155. private static void Internal_OnAdded(DebugMessageType type, string message)
  156. {
  157. if (OnAdded != null)
  158. OnAdded(type, message);
  159. }
  160. [MethodImpl(MethodImplOptions.InternalCall)]
  161. internal static extern void Internal_Log(string message);
  162. [MethodImpl(MethodImplOptions.InternalCall)]
  163. internal static extern void Internal_LogWarning(string message);
  164. [MethodImpl(MethodImplOptions.InternalCall)]
  165. internal static extern void Internal_LogError(string message);
  166. }
  167. }