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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
23 // Andreia Gaita (avidigal@novell.com)
31 using System
.Collections
.Generic
;
33 using System
.Collections
;
35 using System
.Reflection
;
38 using System
.Diagnostics
;
40 namespace System
.Windows
.Forms
42 internal class DebugHelper
44 static DebugHelper () {
45 Debug
.AutoFlush
= true;
49 public MethodBase method
;
51 public Data (MethodBase m
, object[] a
) {
57 static Stack
<Data
> methods
= new Stack
<Data
>();
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();
73 [Conditional("DEBUG")]
74 internal static void DumpCallers () {
75 StackTrace trace
= new StackTrace(true);
76 int count
= trace
.FrameCount
;
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()
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
);
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()
111 [Conditional("DEBUG")]
112 internal static void Enter ()
114 StackTrace trace
= new StackTrace();
115 methods
.Push (new Data (trace
.GetFrame(1).GetMethod(), null));
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
));
129 [Conditional("DEBUG")]
130 internal static void Leave ()
132 if (methods
.Count
> 0) {
138 [Conditional("DEBUG")]
139 internal static void Print ()
141 if (methods
.Count
== 0)
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)
155 Stack
<Data
> temp
= new Stack
<Data
>(index
-1);
157 DataStack temp
= new DataStack(index
-1);
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());
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)
177 Stack
<Data
> temp
= new Stack
<Data
>();
179 DataStack temp
= new DataStack();
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
)) {
190 temp
.Push (methods
.Pop ());
193 for (int i
= 0; i
< temp
.Count
; i
++)
194 methods
.Push (temp
.Pop());
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
) {
206 Debug
.Write (parameterName
+ "=");
207 if (pi
[i
].ParameterType
== typeof(IntPtr
))
208 Debug
.WriteLine (String
.Format ("0x{0:x}", ((IntPtr
)data
.args
[i
]).ToInt32()));
210 Debug
.WriteLine (data
.args
[i
]);
216 [Conditional("DEBUG")]
217 internal static void Print (string parameterName
)
219 if (methods
.Count
== 0)
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
) {
228 Debug
.Write (parameterName
+ "=");
229 if (pi
[i
].ParameterType
== typeof(IntPtr
))
230 Debug
.WriteLine (String
.Format ("0x{0:x}", data
.args
[i
]));
232 Debug
.WriteLine (data
.args
[i
]);
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 ()
262 [Conditional("DEBUG")]
263 internal static void 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
);