2 // System.Diagnostics.TraceImpl.cs
5 // Jonathan Pryor (jonpryor@vt.edu)
7 // (C) 2002, 2005 Jonathan Pryor
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
33 using System.Collections;
34 using System.Diagnostics;
35 using System.Configuration;
36 using System.Threading;
38 namespace System.Diagnostics {
40 internal class TraceImpl {
43 private static object lock_ = new object ();
46 private static bool autoFlush;
49 private static int indentLevel = 0;
52 private static int indentSize;
66 public static bool AutoFlush {
67 get {return autoFlush;}
68 set {autoFlush = value;}
71 public static int IndentLevel {
72 get {return indentLevel;}
74 lock (ListenersSyncRoot) {
77 foreach (TraceListener t in Listeners) {
78 t.IndentLevel = indentLevel;
84 public static int IndentSize {
85 get {return indentSize;}
87 lock (ListenersSyncRoot) {
90 foreach (TraceListener t in Listeners) {
91 t.IndentSize = indentSize;
97 private static object listeners;
99 public static TraceListenerCollection Listeners {
103 return (TraceListenerCollection) listeners;
107 private static object ListenersSyncRoot {
109 return ((ICollection) Listeners).SyncRoot;
113 // Initialize the world.
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.
124 // There are also some ordering issues.
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 ()
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;
141 Thread.MemoryBarrier ();
144 // The lock version (saved for posterity and potential debugging)
146 if (listeners == null) {
147 listeners = new TraceListenerCollection ();
148 // Read in the .config file and get the ball rolling...
149 d = DiagnosticsConfiguration.Settings;
156 // FIXME: According to MSDN, this method should display a dialog box
158 public static void Assert (bool condition)
161 Fail (new StackTrace().ToString());
164 // FIXME: According to MSDN, this method should display a dialog box
166 public static void Assert (bool condition, string message)
172 // FIXME: According to MSDN, this method should display a dialog box
174 public static void Assert (bool condition, string message,
175 string detailMessage)
178 Fail (message, detailMessage);
181 public static void Close ()
183 lock (ListenersSyncRoot) {
184 foreach (TraceListener listener in Listeners) {
190 // FIXME: From testing .NET, this method should display a dialog
192 public static void Fail (string message)
194 lock (ListenersSyncRoot) {
195 foreach (TraceListener listener in Listeners) {
196 listener.Fail (message);
201 // FIXME: From testing .NET, this method should display a dialog
203 public static void Fail (string message, string detailMessage)
205 lock (ListenersSyncRoot) {
206 foreach (TraceListener listener in Listeners) {
207 listener.Fail (message, detailMessage);
212 public static void Flush ()
214 lock (ListenersSyncRoot) {
215 foreach (TraceListener listener in Listeners){
221 public static void Indent ()
223 lock (ListenersSyncRoot) {
224 foreach (TraceListener listener in Listeners) {
225 listener.IndentLevel++;
230 public static void Unindent ()
232 lock (ListenersSyncRoot) {
233 foreach (TraceListener listener in Listeners) {
234 listener.IndentLevel--;
239 public static void Write (object value)
241 lock (ListenersSyncRoot) {
242 foreach (TraceListener listener in Listeners) {
243 listener.Write (value);
251 public static void Write (string message)
253 lock (ListenersSyncRoot) {
254 foreach (TraceListener listener in Listeners) {
255 listener.Write (message);
263 public static void Write (object value, string category)
265 lock (ListenersSyncRoot) {
266 foreach (TraceListener listener in Listeners) {
267 listener.Write (value, category);
275 public static void Write (string message, string category)
277 lock (ListenersSyncRoot) {
278 foreach (TraceListener listener in Listeners) {
279 listener.Write (message, category);
287 public static void WriteIf (bool condition, object value)
293 public static void WriteIf (bool condition, string message)
299 public static void WriteIf (bool condition, object value,
303 Write (value, category);
306 public static void WriteIf (bool condition, string message,
310 Write (message, category);
313 public static void WriteLine (object value)
315 lock (ListenersSyncRoot) {
316 foreach (TraceListener listener in Listeners) {
317 listener.WriteLine (value);
325 public static void WriteLine (string message)
327 lock (ListenersSyncRoot) {
328 foreach (TraceListener listener in Listeners) {
329 listener.WriteLine (message);
337 public static void WriteLine (object value, string category)
339 lock (ListenersSyncRoot) {
340 foreach (TraceListener listener in Listeners) {
341 listener.WriteLine (value, category);
349 public static void WriteLine (string message, string category)
351 lock (ListenersSyncRoot) {
352 foreach (TraceListener listener in Listeners) {
353 listener.WriteLine (message, category);
361 public static void WriteLineIf (bool condition, object value)
367 public static void WriteLineIf (bool condition, string message)
373 public static void WriteLineIf (bool condition, object value,
377 WriteLine (value, category);
380 public static void WriteLineIf (bool condition, string message,
384 WriteLine (message, category);