2
0

Debug.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. namespace BansheeEngine
  8. {
  9. /// <summary>
  10. /// Utility class providing various debug functionality.
  11. /// </summary>
  12. public sealed class Debug
  13. {
  14. /// <summary>
  15. /// Logs a new informative message to the global debug log.
  16. /// </summary>
  17. /// <param name="message">Message to log.</param>
  18. public static void Log(object message)
  19. {
  20. StringBuilder sb = new StringBuilder();
  21. sb.AppendLine(message.ToString());
  22. sb.Append(GetStackTrace());
  23. Internal_Log(sb.ToString());
  24. }
  25. /// <summary>
  26. /// Logs a new warning message to the global debug log.
  27. /// </summary>
  28. /// <param name="message">Message to log.</param>
  29. public static void LogWarning(object message)
  30. {
  31. StringBuilder sb = new StringBuilder();
  32. sb.AppendLine(message.ToString());
  33. sb.Append(GetStackTrace());
  34. Internal_LogWarning(sb.ToString());
  35. }
  36. /// <summary>
  37. /// Logs a new error message to the global debug log.
  38. /// </summary>
  39. /// <param name="message">Message to log.</param>
  40. public static void LogError(object message)
  41. {
  42. StringBuilder sb = new StringBuilder();
  43. sb.AppendLine(message.ToString());
  44. sb.Append(GetStackTrace());
  45. Internal_LogError(sb.ToString());
  46. }
  47. /// <summary>
  48. /// Returns the stack trace of the current point in code.
  49. /// </summary>
  50. /// <returns>String containing the stack trace.</returns>
  51. public static string GetStackTrace()
  52. {
  53. StackTrace stackTrace = new StackTrace(1, true);
  54. StackFrame[] frames = stackTrace.GetFrames();
  55. if (frames == null)
  56. return "";
  57. StringBuilder sb = new StringBuilder();
  58. foreach (var frame in frames)
  59. {
  60. MethodBase method = frame.GetMethod();
  61. if (method == null)
  62. continue;
  63. Type parentType = method.DeclaringType;
  64. if (parentType == null)
  65. continue;
  66. sb.Append("\tat " + parentType.Name + "." + method.Name + "(");
  67. ParameterInfo[] methodParams = method.GetParameters();
  68. for(int i = 0; i < methodParams.Length; i++)
  69. {
  70. if (i > 0)
  71. sb.Append(", ");
  72. sb.Append(methodParams[i].ParameterType.Name);
  73. }
  74. sb.Append(")");
  75. string ns = parentType.Namespace;
  76. string fileName = frame.GetFileName();
  77. if (!string.IsNullOrEmpty(fileName))
  78. {
  79. int line = frame.GetFileLineNumber();
  80. int column = frame.GetFileColumnNumber();
  81. sb.Append(" in " + fileName + ", line " + line + ", column " + column + ", namespace " + ns);
  82. }
  83. else
  84. {
  85. if (!string.IsNullOrEmpty(ns))
  86. sb.Append(" in namespace " + ns);
  87. }
  88. sb.AppendLine();
  89. }
  90. return sb.ToString();
  91. }
  92. [MethodImpl(MethodImplOptions.InternalCall)]
  93. internal static extern Component Internal_Log(string message);
  94. [MethodImpl(MethodImplOptions.InternalCall)]
  95. internal static extern Component Internal_LogWarning(string message);
  96. [MethodImpl(MethodImplOptions.InternalCall)]
  97. internal static extern Component Internal_LogError(string message);
  98. }
  99. }