Don't dispose the handler
[mono.git] / mcs / class / System / System.Diagnostics / TraceImpl.cs
1 //
2 // System.Diagnostics.TraceImpl.cs
3 //
4 // Authors:
5 //   Jonathan Pryor (jonpryor@vt.edu)
6 //
7 // (C) 2002 Jonathan Pryor
8 //
9
10
11 using System;
12 using System.Diagnostics;
13 using System.Configuration;
14
15 namespace System.Diagnostics {
16
17         internal class TraceImpl {
18
19                 private static object lock_ = new object ();
20
21                 private static bool autoFlush;
22
23                 [ThreadStatic]
24                 private static int indentLevel = 0;
25
26                 [ThreadStatic]
27                 private static int indentSize;
28
29                 // Grab the .config file stuff.
30                 //
31                 // There are some ordering issues with the .config file.
32                 //
33                 // The DiagnosticsConfigurationHandler assumes that the TraceImpl.Listeners
34                 // collection exists (so it can initialize the DefaultTraceListener and
35                 // add/remove existing listeners).
36                 //
37                 // When is the .config file read?  That's somewhat undefined.  The .config
38                 // file will be read the first time someone calls
39                 // ConfigurationSettings.GetConfig(), but when that occurs is
40                 // indeterminate.
41                 //
42                 // Since it's probable that the Trace/Debug classes will be used by the
43                 // application, the .config file should be read in before they're used.
44                 //
45                 // Thus, place the initialization here.  We can ensure that everything is
46                 // initialized before reading in the .config file, which should ensure
47                 // that everything is sane.
48                 static TraceImpl ()
49                 {
50                         // defaults
51                         autoFlush = false;
52                         indentLevel = 0;
53                         indentSize = 4;
54
55                         listeners = new TraceListenerCollection ();
56
57                         // Initialize the world
58                         System.Collections.IDictionary d = DiagnosticsConfiguration.Settings;
59
60                         // remove warning about d being unused
61                         d = d;
62                 }
63
64                 private TraceImpl ()
65                 {
66                 }
67
68                 public static bool AutoFlush {
69                         get {return autoFlush;}
70                         set {autoFlush = value;}
71                 }
72
73                 public static int IndentLevel {
74                         get {return indentLevel;}
75                         set {
76                                 indentLevel = value;
77
78                                 // Don't need to lock for threadsafety as 
79                                 // TraceListener.IndentLevel is [ThreadStatic]
80                                 foreach (TraceListener t in Listeners) {
81                                         t.IndentLevel = indentLevel;
82                                 }
83                         }
84                 }
85
86                 public static int IndentSize {
87                         get {return indentSize;}
88                         set {
89                                 indentSize = value;
90
91                                 // Don't need to lock for threadsafety as 
92                                 // TraceListener.IndentSize is [ThreadStatic]
93                                 foreach (TraceListener t in Listeners) {
94                                         t.IndentSize = indentSize;
95                                 }
96                         }
97                 }
98
99                 private static TraceListenerCollection listeners;
100
101                 public static TraceListenerCollection Listeners {
102                         get {return listeners;}
103                 }
104
105                 // FIXME: According to MSDN, this method should display a dialog box
106                 [MonoTODO]
107                 public static void Assert (bool condition)
108                 {
109                         if (!condition)
110                                 Fail (new StackTrace().ToString());
111                 }
112
113                 // FIXME: According to MSDN, this method should display a dialog box
114                 [MonoTODO]
115                 public static void Assert (bool condition, string message)
116                 {
117                         if (!condition)
118                                 Fail (message);
119                 }
120
121                 // FIXME: According to MSDN, this method should display a dialog box
122                 [MonoTODO]
123                 public static void Assert (bool condition, string message, 
124                         string detailMessage)
125                 {
126                         if (!condition)
127                                 Fail (message, detailMessage);
128                 }
129
130                 public static void Close ()
131                 {
132                         lock (lock_) {
133                                 foreach (TraceListener listener in Listeners) {
134                                         listener.Close ();
135                                 }
136                         }
137                 }
138
139                 // FIXME: From testing .NET, this method should display a dialog
140                 [MonoTODO]
141                 public static void Fail (string message)
142                 {
143                         lock (lock_) {
144                                 foreach (TraceListener listener in Listeners) {
145                                         listener.Fail (message);
146                                 }
147                         }
148                 }
149
150                 // FIXME: From testing .NET, this method should display a dialog
151                 [MonoTODO]
152                 public static void Fail (string message, string detailMessage)
153                 {
154                         lock (lock_) {
155                                 foreach (TraceListener listener in Listeners) {
156                                         listener.Fail (message, detailMessage);
157                                 }
158                         }
159                 }
160
161                 public static void Flush ()
162                 {
163                         lock (lock_) {
164                                 foreach (TraceListener listener in Listeners){
165                                         listener.Flush ();
166                                 }
167                         }
168                 }
169
170                 public static void Indent ()
171                 {
172                         lock (lock_) {
173                                 foreach (TraceListener listener in Listeners) {
174                                         listener.IndentLevel++;
175                                 }
176                         }
177                 }
178
179                 public static void Unindent ()
180                 {
181                         lock (lock_) {
182                                 foreach (TraceListener listener in Listeners) {
183                                         listener.IndentLevel--;
184                                 }
185                         }
186                 }
187
188                 public static void Write (object value)
189                 {
190                         lock (lock_) {
191                                 foreach (TraceListener listener in Listeners) {
192                                         listener.Write (value);
193
194                                         if (AutoFlush)
195                                                 listener.Flush ();
196                                 }
197                         }
198                 }
199
200                 public static void Write (string message)
201                 {
202                         lock (lock_) {
203                                 foreach (TraceListener listener in Listeners) {
204                                         listener.Write (message);
205
206                                         if (AutoFlush)
207                                                 listener.Flush ();
208                                 }
209                         }
210                 }
211
212                 public static void Write (object value, string category)
213                 {
214                         lock (lock_) {
215                                 foreach (TraceListener listener in Listeners) {
216                                         listener.Write (value, category);
217
218                                         if (AutoFlush)
219                                                 listener.Flush ();
220                                 }
221                         }
222                 }
223
224                 public static void Write (string message, string category)
225                 {
226                         lock (lock_) {
227                                 foreach (TraceListener listener in Listeners) {
228                                         listener.Write (message, category);
229
230                                         if (AutoFlush)
231                                                 listener.Flush ();
232                                 }
233                         }
234                 }
235
236                 public static void WriteIf (bool condition, object value)
237                 {
238                         if (condition)
239                                 Write (value);
240                 }
241
242                 public static void WriteIf (bool condition, string message)
243                 {
244                         if (condition)
245                                 Write (message);
246                 }
247
248                 public static void WriteIf (bool condition, object value, 
249                         string category)
250                 {
251                         if (condition)
252                                 Write (value, category);
253                 }
254
255                 public static void WriteIf (bool condition, string message, 
256                         string category)
257                 {
258                         if (condition)
259                                 Write (message, category);
260                 }
261
262                 public static void WriteLine (object value)
263                 {
264                         lock (lock_) {
265                                 foreach (TraceListener listener in Listeners) {
266                                         listener.WriteLine (value);
267
268                                         if (AutoFlush)
269                                                 listener.Flush ();
270                                 }
271                         }
272                 }
273
274                 public static void WriteLine (string message)
275                 {
276                         lock (lock_) {
277                                 foreach (TraceListener listener in Listeners) {
278                                         listener.WriteLine (message);
279
280                                         if (AutoFlush)
281                                                 listener.Flush ();
282                                 }
283                         }
284                 }
285
286                 public static void WriteLine (object value, string category)
287                 {
288                         lock (lock_) {
289                                 foreach (TraceListener listener in Listeners) {
290                                         listener.WriteLine (value, category);
291
292                                         if (AutoFlush)
293                                                 listener.Flush ();
294                                 }
295                         }
296                 }
297
298                 public static void WriteLine (string message, string category)
299                 {
300                         lock (lock_) {
301                                 foreach (TraceListener listener in Listeners) {
302                                         listener.WriteLine (message, category);
303
304                                         if (AutoFlush)
305                                                 listener.Flush ();
306                                 }
307                         }
308                 }
309
310                 public static void WriteLineIf (bool condition, object value)
311                 {
312                         if (condition)
313                                 WriteLine (value);
314                 }
315
316                 public static void WriteLineIf (bool condition, string message)
317                 {
318                         if (condition)
319                                 WriteLine (message);
320                 }
321
322                 public static void WriteLineIf (bool condition, object value, 
323                         string category)
324                 {
325                         if (condition)
326                                 WriteLine (value, category);
327                 }
328
329                 public static void WriteLineIf (bool condition, string message, 
330                         string category)
331                 {
332                         if (condition)
333                                 WriteLine (message, category);
334                 }
335         }
336 }
337