Switch to compiler-tester
[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, 2005 Jonathan Pryor
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31
32 using System;
33 using System.Collections;
34 using System.Diagnostics;
35 using System.Configuration;
36 using System.Threading;
37
38 namespace System.Diagnostics {
39
40         internal class TraceImpl {
41
42 #if NO_LOCK_FREE
43                 private static object lock_ = new object ();
44 #endif
45
46                 private static bool autoFlush;
47
48                 [ThreadStatic]
49                 private static int indentLevel = 0;
50
51                 [ThreadStatic]
52                 private static int indentSize;
53
54                 static TraceImpl ()
55                 {
56                         // defaults
57                         autoFlush = false;
58                         indentLevel = 0;
59                         indentSize = 4;
60                 }
61
62                 private TraceImpl ()
63                 {
64                 }
65
66                 public static bool AutoFlush {
67                         get {return autoFlush;}
68                         set {autoFlush = value;}
69                 }
70
71                 public static int IndentLevel {
72                         get {return indentLevel;}
73                         set {
74                                 lock (ListenersSyncRoot) {
75                                         indentLevel = value;
76
77                                         foreach (TraceListener t in Listeners) {
78                                                 t.IndentLevel = indentLevel;
79                                         }
80                                 }
81                         }
82                 }
83
84                 public static int IndentSize {
85                         get {return indentSize;}
86                         set {
87                                 lock (ListenersSyncRoot) {
88                                         indentSize = value;
89
90                                         foreach (TraceListener t in Listeners) {
91                                                 t.IndentSize = indentSize;
92                                         }
93                                 }
94                         }
95                 }
96
97                 private static object listeners;
98
99                 public static TraceListenerCollection Listeners {
100                         get {
101                                 InitOnce ();
102
103                                 return (TraceListenerCollection) listeners;
104                         }
105                 }
106
107                 private static object ListenersSyncRoot {
108                         get {
109                                 return ((ICollection) Listeners).SyncRoot;
110                         }
111                 }
112
113                 // Initialize the world.
114                 //
115                 // This logically belongs in the static constructor (as it only needs
116                 // to be done once), except for one thing: if the .config file has a
117                 // syntax error, .NET throws a ConfigurationException.  If we read the
118                 // .config file in the static ctor, we throw a ConfigurationException
119                 // from the static ctor, which results in a TypeLoadException.  Oops.
120                 // Reading the .config file here will allow the static ctor to
121                 // complete successfully, allowing us to throw a normal
122                 // ConfigurationException should the .config file contain an error.
123                 //
124                 // There are also some ordering issues.
125                 //
126                 // The DiagnosticsConfigurationHandler assumes that the TraceImpl.Listeners
127                 // collection exists (so it can initialize the DefaultTraceListener and
128                 // add/remove existing listeners).
129                 private static object InitOnce ()
130                 {
131                         object d = null;
132 #if !NO_LOCK_FREE
133                         // The lock-free version
134                         if (listeners == null) {
135                                 TraceListenerCollection c = new TraceListenerCollection ();
136                                 Thread.MemoryBarrier ();
137                                 while (Interlocked.CompareExchange (ref listeners, c, null) == null) {
138                                         // Read in the .config file and get the ball rolling...
139                                         d = DiagnosticsConfiguration.Settings;
140                                 }
141                                 Thread.MemoryBarrier ();
142                         }
143 #else
144                         // The lock version (saved for posterity and potential debugging)
145                         lock (lock_) {
146                                 if (listeners == null) {
147                                         listeners = new TraceListenerCollection ();
148                                         // Read in the .config file and get the ball rolling...
149                                         d = DiagnosticsConfiguration.Settings;
150                                 }
151                         }
152 #endif
153                         return d;
154                 }
155
156                 // FIXME: According to MSDN, this method should display a dialog box
157                 [MonoTODO]
158                 public static void Assert (bool condition)
159                 {
160                         if (!condition)
161                                 Fail (new StackTrace().ToString());
162                 }
163
164                 // FIXME: According to MSDN, this method should display a dialog box
165                 [MonoTODO]
166                 public static void Assert (bool condition, string message)
167                 {
168                         if (!condition)
169                                 Fail (message);
170                 }
171
172                 // FIXME: According to MSDN, this method should display a dialog box
173                 [MonoTODO]
174                 public static void Assert (bool condition, string message, 
175                         string detailMessage)
176                 {
177                         if (!condition)
178                                 Fail (message, detailMessage);
179                 }
180
181                 public static void Close ()
182                 {
183                         lock (ListenersSyncRoot) {
184                                 foreach (TraceListener listener in Listeners) {
185                                         listener.Close ();
186                                 }
187                         }
188                 }
189
190                 // FIXME: From testing .NET, this method should display a dialog
191                 [MonoTODO]
192                 public static void Fail (string message)
193                 {
194                         lock (ListenersSyncRoot) {
195                                 foreach (TraceListener listener in Listeners) {
196                                         listener.Fail (message);
197                                 }
198                         }
199                 }
200
201                 // FIXME: From testing .NET, this method should display a dialog
202                 [MonoTODO]
203                 public static void Fail (string message, string detailMessage)
204                 {
205                         lock (ListenersSyncRoot) {
206                                 foreach (TraceListener listener in Listeners) {
207                                         listener.Fail (message, detailMessage);
208                                 }
209                         }
210                 }
211
212                 public static void Flush ()
213                 {
214                         lock (ListenersSyncRoot) {
215                                 foreach (TraceListener listener in Listeners){
216                                         listener.Flush ();
217                                 }
218                         }
219                 }
220
221                 public static void Indent ()
222                 {
223                         lock (ListenersSyncRoot) {
224                                 foreach (TraceListener listener in Listeners) {
225                                         listener.IndentLevel++;
226                                 }
227                         }
228                 }
229
230                 public static void Unindent ()
231                 {
232                         lock (ListenersSyncRoot) {
233                                 foreach (TraceListener listener in Listeners) {
234                                         listener.IndentLevel--;
235                                 }
236                         }
237                 }
238
239                 public static void Write (object value)
240                 {
241                         lock (ListenersSyncRoot) {
242                                 foreach (TraceListener listener in Listeners) {
243                                         listener.Write (value);
244
245                                         if (AutoFlush)
246                                                 listener.Flush ();
247                                 }
248                         }
249                 }
250
251                 public static void Write (string message)
252                 {
253                         lock (ListenersSyncRoot) {
254                                 foreach (TraceListener listener in Listeners) {
255                                         listener.Write (message);
256
257                                         if (AutoFlush)
258                                                 listener.Flush ();
259                                 }
260                         }
261                 }
262
263                 public static void Write (object value, string category)
264                 {
265                         lock (ListenersSyncRoot) {
266                                 foreach (TraceListener listener in Listeners) {
267                                         listener.Write (value, category);
268
269                                         if (AutoFlush)
270                                                 listener.Flush ();
271                                 }
272                         }
273                 }
274
275                 public static void Write (string message, string category)
276                 {
277                         lock (ListenersSyncRoot) {
278                                 foreach (TraceListener listener in Listeners) {
279                                         listener.Write (message, category);
280
281                                         if (AutoFlush)
282                                                 listener.Flush ();
283                                 }
284                         }
285                 }
286
287                 public static void WriteIf (bool condition, object value)
288                 {
289                         if (condition)
290                                 Write (value);
291                 }
292
293                 public static void WriteIf (bool condition, string message)
294                 {
295                         if (condition)
296                                 Write (message);
297                 }
298
299                 public static void WriteIf (bool condition, object value, 
300                         string category)
301                 {
302                         if (condition)
303                                 Write (value, category);
304                 }
305
306                 public static void WriteIf (bool condition, string message, 
307                         string category)
308                 {
309                         if (condition)
310                                 Write (message, category);
311                 }
312
313                 public static void WriteLine (object value)
314                 {
315                         lock (ListenersSyncRoot) {
316                                 foreach (TraceListener listener in Listeners) {
317                                         listener.WriteLine (value);
318
319                                         if (AutoFlush)
320                                                 listener.Flush ();
321                                 }
322                         }
323                 }
324
325                 public static void WriteLine (string message)
326                 {
327                         lock (ListenersSyncRoot) {
328                                 foreach (TraceListener listener in Listeners) {
329                                         listener.WriteLine (message);
330
331                                         if (AutoFlush)
332                                                 listener.Flush ();
333                                 }
334                         }
335                 }
336
337                 public static void WriteLine (object value, string category)
338                 {
339                         lock (ListenersSyncRoot) {
340                                 foreach (TraceListener listener in Listeners) {
341                                         listener.WriteLine (value, category);
342
343                                         if (AutoFlush)
344                                                 listener.Flush ();
345                                 }
346                         }
347                 }
348
349                 public static void WriteLine (string message, string category)
350                 {
351                         lock (ListenersSyncRoot) {
352                                 foreach (TraceListener listener in Listeners) {
353                                         listener.WriteLine (message, category);
354
355                                         if (AutoFlush)
356                                                 listener.Flush ();
357                                 }
358                         }
359                 }
360
361                 public static void WriteLineIf (bool condition, object value)
362                 {
363                         if (condition)
364                                 WriteLine (value);
365                 }
366
367                 public static void WriteLineIf (bool condition, string message)
368                 {
369                         if (condition)
370                                 WriteLine (message);
371                 }
372
373                 public static void WriteLineIf (bool condition, object value, 
374                         string category)
375                 {
376                         if (condition)
377                                 WriteLine (value, category);
378                 }
379
380                 public static void WriteLineIf (bool condition, string message, 
381                         string category)
382                 {
383                         if (condition)
384                                 WriteLine (message, category);
385                 }
386         }
387 }
388