2010-06-21 Marek Habersack <mhabersack@novell.com>
[mcs.git] / class / System / System.Diagnostics / TraceImpl.cs
blob265cd6eb60d4c3b456999502657f930dbf367e11
1 //
2 // System.Diagnostics.TraceImpl.cs
3 //
4 // Authors:
5 // Jonathan Pryor (jonpryor@vt.edu)
6 //
7 // (C) 2002, 2005 Jonathan Pryor
8 //
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.
32 using System;
33 using System.Collections;
34 using System.Diagnostics;
35 using System.Configuration;
36 using System.Threading;
38 namespace System.Diagnostics {
40 internal class TraceImplSettings {
41 public const string Key = ".__TraceInfoSettingsKey__.";
43 public bool AutoFlush;
44 public int IndentLevel, IndentSize = 4;
45 public TraceListenerCollection Listeners = new TraceListenerCollection (false);
47 public TraceImplSettings ()
49 Listeners.Add (new DefaultTraceListener (), this);
53 internal class TraceImpl {
55 private static object initLock = new object ();
57 private static bool autoFlush;
59 #if TARGET_JVM
60 static readonly LocalDataStoreSlot _indentLevelStore = System.Threading.Thread.AllocateDataSlot ();
61 static readonly LocalDataStoreSlot _indentSizeStore = System.Threading.Thread.AllocateDataSlot ();
63 private static int indentLevel {
64 get {
65 object o = System.Threading.Thread.GetData (_indentLevelStore);
66 if (o == null)
67 return 0;
68 return (int) o;
70 set { System.Threading.Thread.SetData (_indentLevelStore, value); }
73 private static int indentSize {
74 get {
75 object o = System.Threading.Thread.GetData (_indentSizeStore);
76 if (o == null)
77 return 0;
78 return (int) o;
80 set { System.Threading.Thread.SetData (_indentSizeStore, value); }
82 #else
83 [ThreadStatic]
84 private static int indentLevel = 0;
86 [ThreadStatic]
87 private static int indentSize;
88 #endif
90 private TraceImpl ()
94 public static bool AutoFlush {
95 get {
96 InitOnce ();
97 return autoFlush;
99 set {
100 InitOnce ();
101 autoFlush = value;
105 public static int IndentLevel {
106 get {
107 InitOnce ();
108 return indentLevel;
110 set {
111 lock (ListenersSyncRoot) {
112 indentLevel = value;
114 foreach (TraceListener t in Listeners) {
115 t.IndentLevel = indentLevel;
121 public static int IndentSize {
122 get {
123 InitOnce ();
124 return indentSize;
126 set {
127 lock (ListenersSyncRoot) {
128 indentSize = value;
130 foreach (TraceListener t in Listeners) {
131 t.IndentSize = indentSize;
137 private static TraceListenerCollection listeners;
139 public static TraceListenerCollection Listeners {
140 get {
141 InitOnce ();
143 return listeners;
147 private static object ListenersSyncRoot {
148 get {
149 return ((ICollection) Listeners).SyncRoot;
153 static bool use_global_lock;
154 #if NET_2_0
155 static CorrelationManager correlation_manager = new CorrelationManager ();
157 public static CorrelationManager CorrelationManager {
158 get {
159 InitOnce ();
160 return correlation_manager;
163 #endif
165 [MonoLimitation ("the property exists but it does nothing.")]
166 public static bool UseGlobalLock {
167 get {
168 InitOnce ();
169 return use_global_lock;
171 set {
172 InitOnce ();
173 use_global_lock = value;
177 // Initialize the world.
179 // This logically belongs in the static constructor (as it only needs
180 // to be done once), except for one thing: if the .config file has a
181 // syntax error, .NET throws a ConfigurationException. If we read the
182 // .config file in the static ctor, we throw a ConfigurationException
183 // from the static ctor, which results in a TypeLoadException. Oops.
184 // Reading the .config file here will allow the static ctor to
185 // complete successfully, allowing us to throw a normal
186 // ConfigurationException should the .config file contain an error.
188 // There are also some ordering issues.
190 // DiagnosticsConfigurationHandler doesn't store values within TraceImpl,
191 // but instead stores values it reads from the .config file within a
192 // TraceImplSettings object (accessible via the TraceImplSettings.Key key
193 // in the IDictionary returned).
194 private static void InitOnce ()
196 if (initLock != null) {
197 lock (initLock) {
198 if (listeners == null) {
199 IDictionary d = DiagnosticsConfiguration.Settings;
200 TraceImplSettings s = (TraceImplSettings) d [TraceImplSettings.Key];
202 d.Remove (TraceImplSettings.Key);
204 autoFlush = s.AutoFlush;
205 indentLevel = s.IndentLevel;
206 indentSize = s.IndentSize;
207 listeners = s.Listeners;
210 initLock = null;
214 // FIXME: According to MSDN, this method should display a dialog box
215 [MonoTODO]
216 public static void Assert (bool condition)
218 if (!condition)
219 Fail (new StackTrace(true).ToString());
222 // FIXME: According to MSDN, this method should display a dialog box
223 [MonoTODO]
224 public static void Assert (bool condition, string message)
226 if (!condition)
227 Fail (message);
230 // FIXME: According to MSDN, this method should display a dialog box
231 [MonoTODO]
232 public static void Assert (bool condition, string message,
233 string detailMessage)
235 if (!condition)
236 Fail (message, detailMessage);
239 public static void Close ()
241 lock (ListenersSyncRoot) {
242 foreach (TraceListener listener in Listeners) {
243 listener.Close ();
248 // FIXME: From testing .NET, this method should display a dialog
249 //(it probably depends on the listener)p
250 [MonoTODO]
251 public static void Fail (string message)
253 lock (ListenersSyncRoot) {
254 foreach (TraceListener listener in Listeners) {
255 listener.Fail (message);
260 // FIXME: From testing .NET, this method should display a dialog
261 // (it probably depends on the listener)p
262 [MonoTODO]
263 public static void Fail (string message, string detailMessage)
265 lock (ListenersSyncRoot) {
266 foreach (TraceListener listener in Listeners) {
267 listener.Fail (message, detailMessage);
272 public static void Flush ()
274 lock (ListenersSyncRoot) {
275 foreach (TraceListener listener in Listeners){
276 listener.Flush ();
281 public static void Indent ()
283 IndentLevel ++;
286 public static void Unindent ()
288 IndentLevel --;
291 public static void Write (object value)
293 lock (ListenersSyncRoot) {
294 foreach (TraceListener listener in Listeners) {
295 listener.Write (value);
297 if (AutoFlush)
298 listener.Flush ();
303 public static void Write (string message)
305 lock (ListenersSyncRoot) {
306 foreach (TraceListener listener in Listeners) {
307 listener.Write (message);
309 if (AutoFlush)
310 listener.Flush ();
315 public static void Write (object value, string category)
317 lock (ListenersSyncRoot) {
318 foreach (TraceListener listener in Listeners) {
319 listener.Write (value, category);
321 if (AutoFlush)
322 listener.Flush ();
327 public static void Write (string message, string category)
329 lock (ListenersSyncRoot) {
330 foreach (TraceListener listener in Listeners) {
331 listener.Write (message, category);
333 if (AutoFlush)
334 listener.Flush ();
339 public static void WriteIf (bool condition, object value)
341 if (condition)
342 Write (value);
345 public static void WriteIf (bool condition, string message)
347 if (condition)
348 Write (message);
351 public static void WriteIf (bool condition, object value,
352 string category)
354 if (condition)
355 Write (value, category);
358 public static void WriteIf (bool condition, string message,
359 string category)
361 if (condition)
362 Write (message, category);
365 public static void WriteLine (object value)
367 lock (ListenersSyncRoot) {
368 foreach (TraceListener listener in Listeners) {
369 listener.WriteLine (value);
371 if (AutoFlush)
372 listener.Flush ();
377 public static void WriteLine (string message)
379 lock (ListenersSyncRoot) {
380 foreach (TraceListener listener in Listeners) {
381 listener.WriteLine (message);
383 if (AutoFlush)
384 listener.Flush ();
389 public static void WriteLine (object value, string category)
391 lock (ListenersSyncRoot) {
392 foreach (TraceListener listener in Listeners) {
393 listener.WriteLine (value, category);
395 if (AutoFlush)
396 listener.Flush ();
401 public static void WriteLine (string message, string category)
403 lock (ListenersSyncRoot) {
404 foreach (TraceListener listener in Listeners) {
405 listener.WriteLine (message, category);
407 if (AutoFlush)
408 listener.Flush ();
413 public static void WriteLineIf (bool condition, object value)
415 if (condition)
416 WriteLine (value);
419 public static void WriteLineIf (bool condition, string message)
421 if (condition)
422 WriteLine (message);
425 public static void WriteLineIf (bool condition, object value,
426 string category)
428 if (condition)
429 WriteLine (value, category);
432 public static void WriteLineIf (bool condition, string message,
433 string category)
435 if (condition)
436 WriteLine (message, category);