2010-04-07 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Managed.Windows.Forms / System.Windows.Forms.Internal / DebugHelper.cs
blobaa32089ed3511f895841020fc58eb94327aeed9c
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2008 Novell, Inc.
22 // Authors:
23 // Andreia Gaita (avidigal@novell.com)
26 //#define DEBUG
27 //#define TRACE
29 using System;
30 #if NET_2_0
31 using System.Collections.Generic;
32 #else
33 using System.Collections;
34 #endif
35 using System.Reflection;
36 using System.Text;
37 using System.IO;
38 using System.Diagnostics;
40 namespace System.Windows.Forms
42 internal class DebugHelper
44 static DebugHelper () {
45 Debug.AutoFlush = true;
48 struct Data {
49 public MethodBase method;
50 public object[] args;
51 public Data (MethodBase m, object[] a) {
52 this.method = m;
53 this.args = a;
56 #if NET_2_0
57 static Stack<Data> methods = new Stack<Data>();
58 #else
59 class DataStack : System.Collections.Stack {
60 public new Data Peek () {
61 return (Data) base.Peek ();
63 public new Data Pop () {
64 return (Data) base.Pop ();
66 public DataStack (int initialCapacity) : base (initialCapacity) {}
67 public DataStack () : base () {}
68 public DataStack (ICollection icol) : base (icol) {}
70 static DataStack methods = new DataStack();
71 #endif
73 [Conditional("DEBUG")]
74 internal static void DumpCallers () {
75 StackTrace trace = new StackTrace(true);
76 int count = trace.FrameCount;
77 Debug.Indent ();
78 for (int i = 1; i < count; i++) {
79 StackFrame parentFrame = trace.GetFrame(i);
80 MethodBase parentMethod = parentFrame.GetMethod();
81 string file = parentFrame.GetFileName();
82 if (file != null && file.Length > 1)
83 file = file.Substring (file.LastIndexOf (Path.DirectorySeparatorChar) + 1);
84 Debug.WriteLine(parentMethod.DeclaringType.Name + "." + parentMethod.Name +
85 " at " + file + ":" + parentFrame.GetFileLineNumber()
86 );
89 Debug.Unindent ();
92 [Conditional("DEBUG")]
93 internal static void DumpCallers (int count) {
94 StackTrace trace = new StackTrace(true);
95 int c = (count > trace.FrameCount ? trace.FrameCount : count);
96 Debug.Indent ();
97 for (int i = 1; i < c; i++) {
98 StackFrame parentFrame = trace.GetFrame(i);
99 MethodBase parentMethod = parentFrame.GetMethod();
100 string file = parentFrame.GetFileName();
101 if (file != null && file.Length > 1)
102 file = file.Substring (file.LastIndexOf (Path.DirectorySeparatorChar) + 1);
103 Debug.WriteLine(parentMethod.DeclaringType.Name + "." + parentMethod.Name +
104 " at " + file + ":" + parentFrame.GetFileLineNumber()
108 Debug.Unindent ();
111 [Conditional("DEBUG")]
112 internal static void Enter ()
114 StackTrace trace = new StackTrace();
115 methods.Push (new Data (trace.GetFrame(1).GetMethod(), null));
116 Print ();
117 Debug.Indent ();
120 [Conditional("DEBUG")]
121 internal static void Enter (object[] args)
123 StackTrace trace = new StackTrace();
124 methods.Push (new Data (trace.GetFrame(1).GetMethod(), args));
125 Print ();
126 Debug.Indent ();
129 [Conditional("DEBUG")]
130 internal static void Leave ()
132 if (methods.Count > 0) {
133 methods.Pop ();
134 Debug.Unindent ();
138 [Conditional("DEBUG")]
139 internal static void Print ()
141 if (methods.Count == 0)
142 return;
144 Data data = methods.Peek ();
145 Debug.WriteLine (data.method.DeclaringType.Name + "." + data.method.Name);
148 [Conditional("DEBUG")]
149 internal static void Print (int index)
151 if (methods.Count == 0 || methods.Count <= index || index < 0)
152 return;
154 #if NET_2_0
155 Stack<Data> temp = new Stack<Data>(index-1);
156 #else
157 DataStack temp = new DataStack(index-1);
158 #endif
159 for (int i = 0; i < index; i++)
160 temp.Push (methods.Pop ());
162 Data data = methods.Peek ();
163 for (int i = 0; i < temp.Count; i++)
164 methods.Push (temp.Pop());
165 temp = null;
167 Debug.WriteLine (data.method.DeclaringType.Name + "." + data.method.Name);
170 [Conditional("DEBUG")]
171 internal static void Print (string methodName, string parameterName)
173 if (methods.Count == 0)
174 return;
176 #if NET_2_0
177 Stack<Data> temp = new Stack<Data>();
178 #else
179 DataStack temp = new DataStack();
180 #endif
181 Data data = methods.Peek ();
182 bool foundit = false;
183 for (int i = 0; i < methods.Count; i++)
185 data = methods.Peek ();
186 if (data.method.Name.Equals (methodName)) {
187 foundit = true;
188 break;
190 temp.Push (methods.Pop ());
193 for (int i = 0; i < temp.Count; i++)
194 methods.Push (temp.Pop());
195 temp = null;
197 if (!foundit)
198 return;
200 Debug.WriteLine (data.method.DeclaringType.Name + "." + data.method.Name);
201 ParameterInfo[] pi = data.method.GetParameters ();
203 for (int i = 0; i < pi.Length; i++) {
204 if (pi[i].Name == parameterName) {
205 Debug.Indent ();
206 Debug.Write (parameterName + "=");
207 if (pi[i].ParameterType == typeof(IntPtr))
208 Debug.WriteLine (String.Format ("0x{0:x}", ((IntPtr)data.args[i]).ToInt32()));
209 else
210 Debug.WriteLine (data.args[i]);
211 Debug.Unindent ();
216 [Conditional("DEBUG")]
217 internal static void Print (string parameterName)
219 if (methods.Count == 0)
220 return;
221 Data data = methods.Peek ();
223 ParameterInfo[] pi = data.method.GetParameters ();
225 for (int i = 0; i < pi.Length; i++) {
226 if (pi[i].Name == parameterName) {
227 Debug.Indent ();
228 Debug.Write (parameterName + "=");
229 if (pi[i].ParameterType == typeof(IntPtr))
230 Debug.WriteLine (String.Format ("0x{0:x}", data.args[i]));
231 else
232 Debug.WriteLine (data.args[i]);
233 Debug.Unindent ();
238 [Conditional("DEBUG")]
239 internal static void WriteLine (object arg)
241 Debug.WriteLine (arg);
244 [Conditional("DEBUG")]
245 internal static void WriteLine (string format, params object[] arg)
247 Debug.WriteLine (String.Format (format, arg));
250 [Conditional("DEBUG")]
251 internal static void WriteLine (string message)
253 Debug.WriteLine (message);
256 [Conditional("DEBUG")]
257 internal static void Indent ()
259 Debug.Indent ();
262 [Conditional("DEBUG")]
263 internal static void Unindent ()
265 Debug.Unindent ();
268 [Conditional("TRACE")]
269 internal static void TraceWriteLine (string format, params object[] arg)
271 Debug.WriteLine (String.Format (format, arg));
274 [Conditional("TRACE")]
275 internal static void TraceWriteLine (string message)
277 Debug.WriteLine (message);