Tracing.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Reflection;
  6. namespace PaintDotNet.SystemLayer
  7. {
  8. /// <summary>
  9. /// Methods for manual profiling and tracing.
  10. /// </summary>
  11. /// <remarks>
  12. /// This class does not rely on any system-specific functionality, but is placed
  13. /// in the SystemLayer assembly (as opposed to Core) so that classes here may
  14. /// also used it.
  15. /// </remarks>
  16. public static class Tracing
  17. {
  18. private static void WriteLine(string msg)
  19. {
  20. Console.WriteLine(msg);
  21. }
  22. private static Set<string> features = new Set<string>();
  23. public static void LogFeature(string featureName)
  24. {
  25. if (string.IsNullOrEmpty(featureName))
  26. {
  27. return;
  28. }
  29. Ping("Logging feature: " + featureName);
  30. lock (features)
  31. {
  32. if (!features.Contains(featureName))
  33. {
  34. features.Add(featureName);
  35. }
  36. }
  37. }
  38. public static IEnumerable<string> GetLoggedFeatures()
  39. {
  40. lock (features)
  41. {
  42. return features.ToArray();
  43. }
  44. }
  45. #if DEBUG
  46. private static Timing timing = new Timing();
  47. private static Stack tracePoints = new Stack();
  48. private class TracePoint
  49. {
  50. private string message;
  51. private ulong timestamp;
  52. public string Message
  53. {
  54. get
  55. {
  56. return message;
  57. }
  58. }
  59. public ulong Timestamp
  60. {
  61. get
  62. {
  63. return timestamp;
  64. }
  65. }
  66. public TracePoint(string message, ulong timestamp)
  67. {
  68. this.message = message;
  69. this.timestamp = timestamp;
  70. }
  71. }
  72. #endif
  73. [Conditional("DEBUG")]
  74. public static void Enter()
  75. {
  76. #if DEBUG
  77. StackTrace trace = new StackTrace();
  78. StackFrame parentFrame = trace.GetFrame(1);
  79. MethodBase parentMethod = parentFrame.GetMethod();
  80. ulong now = timing.GetTickCount();
  81. string msg = new string(' ', 4 * tracePoints.Count) + parentMethod.DeclaringType.Name + "." + parentMethod.Name;
  82. WriteLine((now - timing.BirthTick).ToString() + ": " + msg);
  83. TracePoint tracePoint = new TracePoint(msg, now);
  84. tracePoints.Push(tracePoint);
  85. #endif
  86. }
  87. [Conditional("DEBUG")]
  88. public static void Enter(string message)
  89. {
  90. #if DEBUG
  91. StackTrace trace = new StackTrace();
  92. StackFrame parentFrame = trace.GetFrame(1);
  93. MethodBase parentMethod = parentFrame.GetMethod();
  94. ulong now = timing.GetTickCount();
  95. string msg = new string(' ', 4 * tracePoints.Count) + parentMethod.DeclaringType.Name + "." +
  96. parentMethod.Name + ": " + message;
  97. WriteLine((now - timing.BirthTick).ToString() + ": " + msg);
  98. TracePoint tracePoint = new TracePoint(msg, now);
  99. tracePoints.Push(tracePoint);
  100. #endif
  101. }
  102. [Conditional("DEBUG")]
  103. public static void Leave()
  104. {
  105. #if DEBUG
  106. TracePoint tracePoint = (TracePoint)tracePoints.Pop();
  107. ulong now = timing.GetTickCount();
  108. WriteLine((now - timing.BirthTick).ToString() + ": " + tracePoint.Message + " (" +
  109. (now - tracePoint.Timestamp).ToString() + "ms)");
  110. #endif
  111. }
  112. [Conditional("DEBUG")]
  113. public static void Ping(string message, int callerCount)
  114. {
  115. #if DEBUG
  116. StackTrace trace = new StackTrace();
  117. string callerString = "";
  118. for (int i = 0; i < Math.Min(trace.FrameCount - 1, callerCount); ++i)
  119. {
  120. StackFrame frame = trace.GetFrame(1 + i);
  121. MethodBase method = frame.GetMethod();
  122. callerString += method.DeclaringType.Name + "." + method.Name;
  123. if (i != callerCount - 1)
  124. {
  125. callerString += " <- ";
  126. }
  127. }
  128. ulong now = timing.GetTickCount();
  129. WriteLine((now - timing.BirthTick).ToString() + ": " + new string(' ', 4 * tracePoints.Count) +
  130. callerString + (message != null ? (": " + message) : ""));
  131. #endif
  132. }
  133. [Conditional("DEBUG")]
  134. public static void Ping(string message)
  135. {
  136. #if DEBUG
  137. StackTrace trace = new StackTrace();
  138. StackFrame parentFrame = trace.GetFrame(1);
  139. MethodBase parentMethod = parentFrame.GetMethod();
  140. ulong now = timing.GetTickCount();
  141. WriteLine((now - timing.BirthTick).ToString() + ": " + new string(' ', 4 * tracePoints.Count) +
  142. parentMethod.DeclaringType.Name + "." + parentMethod.Name + (message != null ? (": " + message) : ""));
  143. #endif
  144. }
  145. [Conditional("DEBUG")]
  146. public static void Ping()
  147. {
  148. #if DEBUG
  149. StackTrace trace = new StackTrace();
  150. StackFrame parentFrame = trace.GetFrame(1);
  151. MethodBase parentMethod = parentFrame.GetMethod();
  152. ulong now = timing.GetTickCount();
  153. WriteLine((now - timing.BirthTick).ToString() + ": " + new string(' ', 4 * tracePoints.Count) +
  154. parentMethod.DeclaringType.Name + "." + parentMethod.Name);
  155. #endif
  156. }
  157. }
  158. }