import System.Diagnostics.Trace etc. from referencesource.
[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 #if !MOBILE
41         internal class TraceImplSettings {
42                 public const string Key = ".__TraceInfoSettingsKey__.";
43
44         // Disable warning that AutoFlush is not used
45 #pragma warning disable 649
46                 public bool AutoFlush;
47 #pragma warning restore
48                 //public int IndentLevel;
49                 public int IndentSize = 4;
50                 public TraceListenerCollection Listeners = new TraceListenerCollection ();
51
52                 public TraceImplSettings ()
53                 {
54                         Listeners.Add (new DefaultTraceListener () { IndentSize = this.IndentSize });
55                 }
56         }
57 #endif
58
59 /*
60         static class TraceImpl {
61
62 #if !MOBILE
63                 private static object initLock = new object ();
64 #endif
65
66                 private static bool autoFlush;
67
68                 [ThreadStatic]
69                 private static int indentLevel;
70
71                 [ThreadStatic]
72                 private static int indentSize;
73
74 #if MOBILE
75                 static TraceListenerCollection listeners = new TraceListenerCollection (true);
76 #else
77                 static TraceListenerCollection listeners;
78 #endif
79
80                 static bool use_global_lock;
81                 static CorrelationManager correlation_manager = new CorrelationManager ();
82
83                 public static bool AutoFlush {
84                         get {
85                                 InitOnce ();
86                                 return autoFlush;
87                         }
88                         set {
89                                 InitOnce ();
90                                 autoFlush = value;
91                         }
92                 }
93
94                 public static int IndentLevel {
95                         get {
96                                 InitOnce ();
97                                 return indentLevel;
98                         }
99                         set {
100                                 lock (ListenersSyncRoot) {
101                                         indentLevel = value;
102
103                                         foreach (TraceListener t in Listeners) {
104                                                 t.IndentLevel = indentLevel;
105                                         }
106                                 }
107                         }
108                 }
109
110                 public static int IndentSize {
111                         get {
112                                 InitOnce ();
113                                 return indentSize;
114                         }
115                         set {
116                                 lock (ListenersSyncRoot) {
117                                         indentSize = value;
118
119                                         foreach (TraceListener t in Listeners) {
120                                                 t.IndentSize = indentSize;
121                                         }
122                                 }
123                         }
124                 }
125
126                 public static TraceListenerCollection Listeners {
127                         get {
128                                 InitOnce ();
129
130                                 return listeners;
131                         }
132                 }
133
134                 private static object ListenersSyncRoot {
135                         get {
136                                 return ((ICollection) Listeners).SyncRoot;
137                         }
138                 }
139
140                 public static CorrelationManager CorrelationManager {
141                         get {
142                                 InitOnce ();
143                                 return correlation_manager;
144                         }
145                 }
146
147                 [MonoLimitation ("the property exists but it does nothing.")]
148                 public static bool UseGlobalLock {
149                         get {
150                                 InitOnce ();
151                                 return use_global_lock;
152                         }
153                         set {
154                                 InitOnce ();
155                                 use_global_lock = value;
156                         }
157                 }
158
159                 // Initialize the world.
160                 //
161                 // This logically belongs in the static constructor (as it only needs
162                 // to be done once), except for one thing: if the .config file has a
163                 // syntax error, .NET throws a ConfigurationException.  If we read the
164                 // .config file in the static ctor, we throw a ConfigurationException
165                 // from the static ctor, which results in a TypeLoadException.  Oops.
166                 // Reading the .config file here will allow the static ctor to
167                 // complete successfully, allowing us to throw a normal
168                 // ConfigurationException should the .config file contain an error.
169                 //
170                 // There are also some ordering issues.
171                 //
172                 // DiagnosticsConfigurationHandler doesn't store values within TraceImpl,
173                 // but instead stores values it reads from the .config file within a
174                 // TraceImplSettings object (accessible via the TraceImplSettings.Key key
175                 // in the IDictionary returned).
176                 private static void InitOnce ()
177                 {
178 #if !MOBILE
179                         if (initLock != null) {
180                                 lock (initLock) {
181                                         if (listeners == null) {
182                                                 IDictionary       d = DiagnosticsConfiguration.Settings;
183                                                 TraceImplSettings s = (TraceImplSettings) d [TraceImplSettings.Key];
184
185                                                 d.Remove (TraceImplSettings.Key);
186
187                                                 autoFlush   = s.AutoFlush;
188 //                                              indentLevel = s.IndentLevel;
189                                                 indentSize  = s.IndentSize;
190                                                 listeners   = s.Listeners;
191                                         }
192                                 }
193                                 initLock = null;
194                         }
195 #endif
196                 }
197
198                 public static void Assert (bool condition)
199                 {
200                         if (!condition)
201                                 Fail ("");
202                 }
203
204                 public static void Assert (bool condition, string message)
205                 {
206                         if (!condition)
207                                 Fail (message);
208                 }
209
210                 public static void Assert (bool condition, string message, 
211                         string detailMessage)
212                 {
213                         if (!condition)
214                                 Fail (message, detailMessage);
215                 }
216
217                 public static void Close ()
218                 {
219                         lock (ListenersSyncRoot) {
220                                 foreach (TraceListener listener in Listeners) {
221                                         listener.Close ();
222                                 }
223                         }
224                 }
225
226                 // FIXME: From testing .NET, this method should display a dialog
227                 //(it probably depends on the listener)p
228                 [MonoTODO]
229                 public static void Fail (string message)
230                 {
231                         lock (ListenersSyncRoot) {
232                                 foreach (TraceListener listener in Listeners) {
233                                         listener.Fail (message);
234                                 }
235                         }
236                 }
237
238                 // FIXME: From testing .NET, this method should display a dialog
239                 // (it probably depends on the listener)p
240                 [MonoTODO]
241                 public static void Fail (string message, string detailMessage)
242                 {
243                         lock (ListenersSyncRoot) {
244                                 foreach (TraceListener listener in Listeners) {
245                                         listener.Fail (message, detailMessage);
246                                 }
247                         }
248                 }
249
250                 public static void Flush ()
251                 {
252                         lock (ListenersSyncRoot) {
253                                 foreach (TraceListener listener in Listeners){
254                                         listener.Flush ();
255                                 }
256                         }
257                 }
258
259                 public static void Indent ()
260                 {
261                         IndentLevel ++;
262                 }
263
264                 public static void Unindent ()
265                 {
266                         IndentLevel --;
267                 }
268
269                 public static void Write (object value)
270                 {
271                         lock (ListenersSyncRoot) {
272                                 foreach (TraceListener listener in Listeners) {
273                                         listener.Write (value);
274
275                                         if (AutoFlush)
276                                                 listener.Flush ();
277                                 }
278                         }
279                 }
280
281                 public static void Write (string message)
282                 {
283                         lock (ListenersSyncRoot) {
284                                 foreach (TraceListener listener in Listeners) {
285                                         listener.Write (message);
286
287                                         if (AutoFlush)
288                                                 listener.Flush ();
289                                 }
290                         }
291                 }
292
293                 public static void Write (object value, string category)
294                 {
295                         lock (ListenersSyncRoot) {
296                                 foreach (TraceListener listener in Listeners) {
297                                         listener.Write (value, category);
298
299                                         if (AutoFlush)
300                                                 listener.Flush ();
301                                 }
302                         }
303                 }
304
305                 public static void Write (string message, string category)
306                 {
307                         lock (ListenersSyncRoot) {
308                                 foreach (TraceListener listener in Listeners) {
309                                         listener.Write (message, category);
310
311                                         if (AutoFlush)
312                                                 listener.Flush ();
313                                 }
314                         }
315                 }
316
317                 public static void WriteIf (bool condition, object value)
318                 {
319                         if (condition)
320                                 Write (value);
321                 }
322
323                 public static void WriteIf (bool condition, string message)
324                 {
325                         if (condition)
326                                 Write (message);
327                 }
328
329                 public static void WriteIf (bool condition, object value, 
330                         string category)
331                 {
332                         if (condition)
333                                 Write (value, category);
334                 }
335
336                 public static void WriteIf (bool condition, string message, 
337                         string category)
338                 {
339                         if (condition)
340                                 Write (message, category);
341                 }
342
343                 public static void WriteLine (object value)
344                 {
345                         lock (ListenersSyncRoot) {
346                                 foreach (TraceListener listener in Listeners) {
347                                         listener.WriteLine (value);
348
349                                         if (AutoFlush)
350                                                 listener.Flush ();
351                                 }
352                         }
353                 }
354
355                 public static void WriteLine (string message)
356                 {
357                         lock (ListenersSyncRoot) {
358                                 foreach (TraceListener listener in Listeners) {
359                                         listener.WriteLine (message);
360
361                                         if (AutoFlush)
362                                                 listener.Flush ();
363                                 }
364                         }
365                 }
366
367                 public static void WriteLine (object value, string category)
368                 {
369                         lock (ListenersSyncRoot) {
370                                 foreach (TraceListener listener in Listeners) {
371                                         listener.WriteLine (value, category);
372
373                                         if (AutoFlush)
374                                                 listener.Flush ();
375                                 }
376                         }
377                 }
378
379                 public static void WriteLine (string message, string category)
380                 {
381                         lock (ListenersSyncRoot) {
382                                 foreach (TraceListener listener in Listeners) {
383                                         listener.WriteLine (message, category);
384
385                                         if (AutoFlush)
386                                                 listener.Flush ();
387                                 }
388                         }
389                 }
390
391                 public static void WriteLineIf (bool condition, object value)
392                 {
393                         if (condition)
394                                 WriteLine (value);
395                 }
396
397                 public static void WriteLineIf (bool condition, string message)
398                 {
399                         if (condition)
400                                 WriteLine (message);
401                 }
402
403                 public static void WriteLineIf (bool condition, object value, 
404                         string category)
405                 {
406                         if (condition)
407                                 WriteLine (value, category);
408                 }
409
410                 public static void WriteLineIf (bool condition, string message, 
411                         string category)
412                 {
413                         if (condition)
414                                 WriteLine (message, category);
415                 }
416         }
417 */
418 }
419