123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Reflection;
- namespace PaintDotNet.SystemLayer
- {
- /// <summary>
- /// Methods for manual profiling and tracing.
- /// </summary>
- /// <remarks>
- /// This class does not rely on any system-specific functionality, but is placed
- /// in the SystemLayer assembly (as opposed to Core) so that classes here may
- /// also used it.
- /// </remarks>
- public static class Tracing
- {
- private static void WriteLine(string msg)
- {
- Console.WriteLine(msg);
- }
- private static Set<string> features = new Set<string>();
- public static void LogFeature(string featureName)
- {
- if (string.IsNullOrEmpty(featureName))
- {
- return;
- }
- Ping("Logging feature: " + featureName);
- lock (features)
- {
- if (!features.Contains(featureName))
- {
- features.Add(featureName);
- }
- }
- }
- public static IEnumerable<string> GetLoggedFeatures()
- {
- lock (features)
- {
- return features.ToArray();
- }
- }
- #if DEBUG
- private static Timing timing = new Timing();
- private static Stack tracePoints = new Stack();
- private class TracePoint
- {
- private string message;
- private ulong timestamp;
- public string Message
- {
- get
- {
- return message;
- }
- }
- public ulong Timestamp
- {
- get
- {
- return timestamp;
- }
- }
- public TracePoint(string message, ulong timestamp)
- {
- this.message = message;
- this.timestamp = timestamp;
- }
- }
- #endif
- [Conditional("DEBUG")]
- public static void Enter()
- {
- #if DEBUG
- StackTrace trace = new StackTrace();
- StackFrame parentFrame = trace.GetFrame(1);
- MethodBase parentMethod = parentFrame.GetMethod();
- ulong now = timing.GetTickCount();
- string msg = new string(' ', 4 * tracePoints.Count) + parentMethod.DeclaringType.Name + "." + parentMethod.Name;
- WriteLine((now - timing.BirthTick).ToString() + ": " + msg);
- TracePoint tracePoint = new TracePoint(msg, now);
- tracePoints.Push(tracePoint);
- #endif
- }
- [Conditional("DEBUG")]
- public static void Enter(string message)
- {
- #if DEBUG
- StackTrace trace = new StackTrace();
- StackFrame parentFrame = trace.GetFrame(1);
- MethodBase parentMethod = parentFrame.GetMethod();
- ulong now = timing.GetTickCount();
- string msg = new string(' ', 4 * tracePoints.Count) + parentMethod.DeclaringType.Name + "." +
- parentMethod.Name + ": " + message;
- WriteLine((now - timing.BirthTick).ToString() + ": " + msg);
- TracePoint tracePoint = new TracePoint(msg, now);
- tracePoints.Push(tracePoint);
- #endif
- }
- [Conditional("DEBUG")]
- public static void Leave()
- {
- #if DEBUG
- TracePoint tracePoint = (TracePoint)tracePoints.Pop();
- ulong now = timing.GetTickCount();
- WriteLine((now - timing.BirthTick).ToString() + ": " + tracePoint.Message + " (" +
- (now - tracePoint.Timestamp).ToString() + "ms)");
- #endif
- }
- [Conditional("DEBUG")]
- public static void Ping(string message, int callerCount)
- {
- #if DEBUG
- StackTrace trace = new StackTrace();
- string callerString = "";
- for (int i = 0; i < Math.Min(trace.FrameCount - 1, callerCount); ++i)
- {
- StackFrame frame = trace.GetFrame(1 + i);
- MethodBase method = frame.GetMethod();
- callerString += method.DeclaringType.Name + "." + method.Name;
- if (i != callerCount - 1)
- {
- callerString += " <- ";
- }
- }
- ulong now = timing.GetTickCount();
- WriteLine((now - timing.BirthTick).ToString() + ": " + new string(' ', 4 * tracePoints.Count) +
- callerString + (message != null ? (": " + message) : ""));
- #endif
- }
- [Conditional("DEBUG")]
- public static void Ping(string message)
- {
- #if DEBUG
- StackTrace trace = new StackTrace();
- StackFrame parentFrame = trace.GetFrame(1);
- MethodBase parentMethod = parentFrame.GetMethod();
- ulong now = timing.GetTickCount();
- WriteLine((now - timing.BirthTick).ToString() + ": " + new string(' ', 4 * tracePoints.Count) +
- parentMethod.DeclaringType.Name + "." + parentMethod.Name + (message != null ? (": " + message) : ""));
- #endif
- }
- [Conditional("DEBUG")]
- public static void Ping()
- {
- #if DEBUG
- StackTrace trace = new StackTrace();
- StackFrame parentFrame = trace.GetFrame(1);
- MethodBase parentMethod = parentFrame.GetMethod();
- ulong now = timing.GetTickCount();
- WriteLine((now - timing.BirthTick).ToString() + ": " + new string(' ', 4 * tracePoints.Count) +
- parentMethod.DeclaringType.Name + "." + parentMethod.Name);
- #endif
- }
- }
- }
|