Added Debug.cs to the build.
[mono.git] / mcs / class / System / System.Diagnostics / Debug.cs
1 //\r
2 // System.Diagnostics.Debug.cs\r
3 //\r
4 // Author: John R. Hicks <angryjohn69@nc.rr.com>\r
5 //\r
6 // (C) 2002\r
7 //\r
8 using System;\r
9 \r
10 namespace System.Diagnostics\r
11 {\r
12         \r
13         /// <summary>\r
14         /// Provides a set of methods to help debug code\r
15         /// </summary>\r
16         public sealed class Debug\r
17         {\r
18                 private static bool autoFlush;\r
19                 private static int indentLevel;\r
20                 private static int indentSize;\r
21                 private static TraceListenerCollection listeners;\r
22                 \r
23                 static Debug()\r
24                 {\r
25                         autoFlush = false;\r
26                         indentLevel = 0;\r
27                         indentSize = 4;\r
28                         listeners = new TraceListenerCollection();\r
29                 }\r
30                 \r
31                 /// <summary>\r
32                 /// Gets or sets value indicating whether Flush should\r
33                 /// be called on the listeners.\r
34                 /// </summary>\r
35                 public static bool AutoFlush\r
36                 {\r
37                         get\r
38                         {\r
39                                 return autoFlush;\r
40                         }\r
41                         set\r
42                         {\r
43                                 autoFlush = value;\r
44                         }\r
45                 }\r
46                 \r
47                 /// <summary>\r
48                 /// Gets or sets indent level\r
49                 /// </summary>\r
50                 public static int IndentLevel\r
51                 {\r
52                         get\r
53                         {\r
54                                 return indentLevel;\r
55                         }\r
56                         set\r
57                         {\r
58                                 indentLevel = value;\r
59                         }\r
60                 }\r
61                 \r
62                 /// <summary>\r
63                 /// The number of spaces in an indent.\r
64                 /// </summary>\r
65                 public static int IndentSize\r
66                 {\r
67                         get\r
68                         {\r
69                                 return indentSize;\r
70                         }\r
71                         set\r
72                         {\r
73                                 indentSize = value;\r
74                         }\r
75                 }\r
76                 \r
77                 /// <summary>\r
78                 /// Returns the listeners collection\r
79                 /// </summary>\r
80                 public static TraceListenerCollection Listeners\r
81                 {\r
82                         get\r
83                         {\r
84                                 return listeners;\r
85                         }\r
86                 }\r
87                 \r
88                 /// <summary>\r
89                 /// Checks for a condition, and prints a stack trace\r
90                 /// if the condition is false.\r
91                 /// </summary>\r
92                 public static void Assert(bool condition)\r
93                 {\r
94                         if(!condition) {\r
95                                 WriteLine(new StackTrace().ToString());         \r
96                         }\r
97                         \r
98                 }\r
99                 \r
100                 /// <summary>\r
101                 /// Checks for a condition, and displays a message if the condition\r
102                 /// is false.\r
103                 /// </summary>\r
104                 public static void Assert(bool condition, string message)\r
105                 {\r
106                         if(!condition) {\r
107                                 WriteLine(message);             \r
108                                 \r
109                         }\r
110                         \r
111                 }\r
112                 \r
113                 /// <summary>\r
114                 /// Checks for a condtion, and displays a message and a detailed message\r
115                 /// string if the condition is false.\r
116                 /// </summary>\r
117                 public static void Assert(bool condition, string message, string detailMessage)\r
118                 {\r
119                         if(!condition) {\r
120                                 WriteLine(message);\r
121                                 Indent();\r
122                                 WriteLine(detailMessage);\r
123                                 Unindent();\r
124                                 \r
125                         }\r
126                 }\r
127                 \r
128                 /// <summary>\r
129                 /// Closes the Debug buffer\r
130                 /// </summary>\r
131                 public static void Close()\r
132                 {\r
133                         foreach(TraceListener l in listeners)\r
134                         {\r
135                                 l.Close();\r
136                         }\r
137                 }\r
138                 \r
139                 /// <summary>\r
140                 /// Emits the specified error message.\r
141                 /// </summary>\r
142                 public static void Fail(string message)\r
143                 {\r
144                         WriteLine(message);\r
145                         \r
146                 }\r
147                 \r
148                 /// <summary>\r
149                 /// Emits the specified error message and detailed error message.\r
150                 /// </summary>\r
151                 public static void Fail(string message, string detailMessage)\r
152                 {\r
153                         WriteLine(message);\r
154                         Indent();\r
155                         WriteLine(detailMessage);\r
156                         Unindent();\r
157                         \r
158                 }\r
159                 \r
160                 /// <summary>\r
161                 /// Flushes the listeners\r
162                 /// </summary>\r
163                 public static void Flush()\r
164                 {\r
165                         foreach(TraceListener l in listeners)\r
166                         {\r
167                                 l.Flush();\r
168                         }\r
169                 }\r
170                 \r
171                 /// <summary>\r
172                 /// Increments the indent level\r
173                 /// </summary>\r
174                 public static void Indent()\r
175                 {\r
176                         indentLevel++;  \r
177                 }\r
178                 \r
179                 /// <summary>\r
180                 /// Decrements the indent level\r
181                 /// </summary>\r
182                 public static void Unindent()\r
183                 {\r
184                         if(indentLevel == 0)\r
185                                 return;\r
186                         else\r
187                                 indentLevel--;\r
188                 }\r
189                 \r
190                 /// <summary>\r
191                 /// Writes the value of the specified object's ToString method\r
192                 /// to the listeners.\r
193                 /// </summary>\r
194                 public static void Write(object value)\r
195                 {\r
196                         foreach(TraceListener l in listeners)\r
197                         {\r
198                                 l.Write(value.ToString());\r
199                         }\r
200                 }\r
201                 \r
202                 /// <summary>\r
203                 /// Writes the specified message to each listener in the Listeners collection.\r
204                 /// </summary>\r
205                 public static void Write(string message)\r
206                 {\r
207                         foreach(TraceListener l in listeners)\r
208                         {\r
209                                 l.Write(message);\r
210                         }\r
211                 }\r
212                 \r
213                 /// <summary>\r
214                 /// Writes the category name and value of the specified object's\r
215                 /// ToString method to each listener in the Listeners collection.\r
216                 /// </summary>\r
217                 public static void Write(object value, string category)\r
218                 {\r
219                         foreach(TraceListener l in listeners)\r
220                         {\r
221                                 l.Write("[" + category + "] " + value.ToString());\r
222                         }\r
223                 }\r
224                 \r
225                 /// <summary>\r
226                 /// Writes the category name and the specified message\r
227                 /// to each listener in the Listeners collection.\r
228                 /// </summary>\r
229                 public static void Write(string message, string category)\r
230                 {\r
231                         foreach(TraceListener l in listeners)\r
232                         {\r
233                                 l.Write("[" + category + "] " + message);\r
234                         }\r
235                 }\r
236                 \r
237                 /// <summary>\r
238                 /// Writes the value of the specified object's ToString method\r
239                 /// to each of the listeners if the condition is true.\r
240                 /// </summary>\r
241                 public static void WriteIf(bool condition, object value)\r
242                 {\r
243                         if(condition)\r
244                         {\r
245                                 foreach(TraceListener l in listeners)\r
246                                 {\r
247                                         l.Write(value.ToString());\r
248                                 }\r
249                         }\r
250                 }\r
251                 \r
252                 /// <summary>\r
253                 /// Writes the specified message to each of the listeners\r
254                 /// if the specified condition is true.\r
255                 /// </summary>\r
256                 public static void WriteIf(bool condition, string message)\r
257                 {\r
258                         if(condition)\r
259                         {\r
260                                 foreach(TraceListener l in listeners)\r
261                                 {\r
262                                         l.Write(message);\r
263                                 }\r
264                         }\r
265                 }\r
266                 \r
267                 /// <summary>\r
268                 /// Writes the value of the specified object's ToString message\r
269                 /// and category to each of the listeners if the condition is true.\r
270                 /// </summary>\r
271                 public static void WriteIf(bool condition, object value, string category)\r
272                 {\r
273                         if(condition)\r
274                         {\r
275                                 foreach(TraceListener l in listeners)\r
276                                 {\r
277                                         l.Write("[" + category + "] " + value.ToString());\r
278                                 }\r
279                         }\r
280                 }\r
281                 \r
282                 /// <summary>\r
283                 /// Writes the category and specified message to each listener\r
284                 /// if the specified condition is true.\r
285                 /// </summary>\r
286                 public static void WriteIf(bool condition, string message, string category)\r
287                 {\r
288                         if(condition)\r
289                         {\r
290                                 foreach(TraceListener l in listeners)\r
291                                 {\r
292                                         l.Write("[" + category + "] " + message);\r
293                                 }\r
294                         }\r
295                         \r
296                 }\r
297                 \r
298                 /// <summary>\r
299                 /// Writes the value of the object's ToString method,\r
300                 /// followed by a line terminator, to each listener.\r
301                 /// </summary>\r
302                 public static void WriteLine(object value)\r
303                 {\r
304                         foreach(TraceListener l in listeners)\r
305                         {\r
306                                 l.WriteLine(value.ToString());\r
307                         }\r
308                 }\r
309                 \r
310                 /// <summary>\r
311                 /// Writes the specified message, followed by a line terminator,\r
312                 /// to each listener.\r
313                 /// </summary>\r
314                 public static void WriteLine(string message)\r
315                 {\r
316                         foreach(TraceListener l in listeners)\r
317                         {\r
318                                 l.WriteLine(message);\r
319                         }\r
320                 }\r
321                 \r
322                 /// <summary>\r
323                 /// Writes the value of the specified object's ToString method,\r
324                 /// along with a category, followed by a line terminator, to each listener.\r
325                 /// </summary>\r
326                 public static void WriteLine(object value, string category)\r
327                 {\r
328                         foreach(TraceListener l in listeners)\r
329                         {\r
330                                 l.WriteLine("[" + category + "] " + value.ToString());\r
331                         }\r
332                 }\r
333                 \r
334                 /// <summary>\r
335                 /// Writes the specified category and message, followed by a line terminator,\r
336                 /// to each listener.\r
337                 /// </summary>\r
338                 public static void WriteLine(string message, string category)\r
339                 {\r
340                         foreach(TraceListener l in listeners)\r
341                         {\r
342                                 l.WriteLine("[" + category + "] " + message);\r
343                         }\r
344                 }\r
345                 \r
346                 /// <summary>\r
347                 /// Writes the value of the object's ToString method\r
348                 /// to each listener if the specified condition is true.\r
349                 /// </summary>\r
350                 public static void WriteLineIf(bool condition, object value)\r
351                 {\r
352                         if(condition)\r
353                         {\r
354                                 foreach(TraceListener l in listeners)\r
355                                 {\r
356                                         l.WriteLine(value.ToString());\r
357                                 }\r
358                         }\r
359                 }\r
360                 \r
361                 /// <summary>\r
362                 /// Writes the specified message to each listener\r
363                 /// if the specified condition is true.\r
364                 /// </summary>\r
365                 public static void WriteLineIf(bool condition, string message)\r
366                 {\r
367                         if(condition)\r
368                         {\r
369                                 foreach(TraceListener l in listeners)\r
370                                 {\r
371                                         l.WriteLine(message);\r
372                                 }\r
373                         }\r
374                 }\r
375                 \r
376                 /// <summary>\r
377                 /// Writes the value of the object's ToString method, and a category\r
378                 /// to each listener if the specified condition is true.\r
379                 /// </summary>\r
380                 public static void WriteLineIf(bool condition, object value, string category)\r
381                 {\r
382                         if(condition)\r
383                         {\r
384                                 foreach(TraceListener l in listeners)\r
385                                 {\r
386                                         l.WriteLine("[" + category + "] " + value.ToString());\r
387                                 }\r
388                         }\r
389                 }\r
390                 \r
391                 /// <summary>\r
392                 /// Writes the specified category and message to each listener, followed by a line\r
393                 /// terminator, if the specified condition is true.\r
394                 /// </summary>\r
395                 public static void WriteLineIf(bool condition, string message, string category)\r
396                 {\r
397                         if(condition)\r
398                         {\r
399                                 foreach(TraceListener l in listeners)\r
400                                 {\r
401                                         l.WriteLine("[" + category + "] " + message);\r
402                                 }\r
403                         }\r
404                         \r
405                 }\r
406         }\r
407 }\r