9d9a20aac1cbcc292856348d74e8cd868e59f233
[mono.git] / mcs / class / referencesource / System / compmod / system / diagnostics / Trace.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="Trace.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6
7 /*
8  */
9 #define TRACE
10 namespace System.Diagnostics {
11     using System;
12     using System.Collections;
13     using System.Security.Permissions;
14     using System.Threading;
15
16     /// <devdoc>
17     ///    <para>Provides a set of properties and methods to trace the execution of your code.</para>
18     /// </devdoc>
19     public sealed class Trace {
20         private static volatile CorrelationManager correlationManager = null;
21         
22         // not creatble...
23         //
24         private Trace() {
25         }
26
27         /// <devdoc>
28         ///    <para>Gets the collection of listeners that is monitoring the trace output.</para>
29         /// </devdoc>
30         public static TraceListenerCollection Listeners {
31             [HostProtection(SharedState=true)]
32             get {
33                 // Do a full damand
34                 new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
35                 
36                 return TraceInternal.Listeners;
37             }
38         }
39
40         /// <devdoc>
41         ///    <para>
42         ///       Gets or sets whether <see cref='System.Diagnostics.Trace.Flush'/> should be called on the <see cref='System.Diagnostics.Trace.Listeners'/> after every write.
43         ///    </para>
44         /// </devdoc>
45         public static bool AutoFlush {
46             [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
47             get {
48                 return TraceInternal.AutoFlush;
49             }
50
51             [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
52             set {
53                 TraceInternal.AutoFlush = value;
54             }
55         }
56
57         public static bool UseGlobalLock {
58             [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
59             get {
60                 return TraceInternal.UseGlobalLock;
61             }
62
63             [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
64             set {
65                 TraceInternal.UseGlobalLock = value;
66             }
67         }
68         
69         public static CorrelationManager CorrelationManager {
70             [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
71             get { 
72                 if (correlationManager == null)
73                     correlationManager = new CorrelationManager();
74                 
75                 return correlationManager;
76             }
77         }
78
79         /// <devdoc>
80         ///    <para>Gets or sets the indent level.</para>
81         /// </devdoc>
82         public static int IndentLevel {
83             get { return TraceInternal.IndentLevel; }
84
85             set { TraceInternal.IndentLevel = value; }
86         }
87
88
89         /// <devdoc>
90         ///    <para>
91         ///       Gets or sets the number of spaces in an indent.
92         ///    </para>
93         /// </devdoc>
94         public static int IndentSize {
95             get { return TraceInternal.IndentSize; }
96
97             set { TraceInternal.IndentSize = value; }
98         }
99
100         /// <devdoc>
101         ///    <para>Clears the output buffer, and causes buffered data to
102         ///       be written to the <see cref='System.Diagnostics.Trace.Listeners'/>.</para>
103         /// </devdoc>
104         [System.Diagnostics.Conditional("TRACE")]
105         public static void Flush() {
106             TraceInternal.Flush();
107         }
108
109         /// <devdoc>
110         /// <para>Clears the output buffer, and then closes the <see cref='System.Diagnostics.Trace.Listeners'/> so that they no
111         ///    longer receive debugging output.</para>
112         /// </devdoc>
113         [System.Diagnostics.Conditional("TRACE")]
114         public static void Close() {
115             // Do a full damand
116             new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
117             
118             TraceInternal.Close();
119         }
120
121         /// <devdoc>
122         ///    <para>Checks for a condition, and outputs the callstack if the
123         ///       condition
124         ///       is <see langword='false'/>.</para>
125         /// </devdoc>
126         [System.Diagnostics.Conditional("TRACE")]  
127         public static void Assert(bool condition) {
128             TraceInternal.Assert(condition);
129         }
130
131         /// <devdoc>
132         ///    <para>Checks for a condition, and displays a message if the condition is
133         ///    <see langword='false'/>. </para>
134         /// </devdoc>
135         [System.Diagnostics.Conditional("TRACE")]  
136         public static void Assert(bool condition, string message) {
137             TraceInternal.Assert(condition, message);
138         }
139
140         /// <devdoc>
141         ///    <para>Checks for a condition, and displays both messages if the condition
142         ///       is <see langword='false'/>. </para>
143         /// </devdoc>
144         [System.Diagnostics.Conditional("TRACE")]  
145         public static void Assert(bool condition, string message, string detailMessage) {
146             TraceInternal.Assert(condition, message, detailMessage);
147         }
148
149         /// <devdoc>
150         ///    <para>Emits or displays a message for an assertion that always fails.</para>
151         /// </devdoc>
152         [System.Diagnostics.Conditional("TRACE")]
153         public static void Fail(string message) {
154             TraceInternal.Fail(message);
155         }
156
157         /// <devdoc>
158         ///    <para>Emits or displays both messages for an assertion that always fails.</para>
159         /// </devdoc>
160         [System.Diagnostics.Conditional("TRACE")]
161         public static void Fail(string message, string detailMessage) {
162             TraceInternal.Fail(message, detailMessage);
163         }
164
165         public static void Refresh() {
166 #if CONFIGURATION_DEP
167             DiagnosticsConfiguration.Refresh();
168 #endif
169             Switch.RefreshAll();
170             TraceSource.RefreshAll();
171             TraceInternal.Refresh();
172         }
173         
174         [System.Diagnostics.Conditional("TRACE")]
175         public static void TraceInformation(string message) {
176             TraceInternal.TraceEvent(TraceEventType.Information, 0, message, null);
177         }
178
179         [System.Diagnostics.Conditional("TRACE")]
180         public static void TraceInformation(string format, params object[] args) {
181             TraceInternal.TraceEvent(TraceEventType.Information, 0, format, args);
182         }
183
184         [System.Diagnostics.Conditional("TRACE")]
185         public static void TraceWarning(string message) {
186             TraceInternal.TraceEvent(TraceEventType.Warning, 0, message, null);
187         }
188
189         [System.Diagnostics.Conditional("TRACE")]
190         public static void TraceWarning(string format, params object[] args) {
191             TraceInternal.TraceEvent(TraceEventType.Warning, 0, format, args);
192         }
193
194         [System.Diagnostics.Conditional("TRACE")]
195         public static void TraceError(string message) {
196             TraceInternal.TraceEvent(TraceEventType.Error, 0, message, null);
197         }
198
199         [System.Diagnostics.Conditional("TRACE")]
200         public static void TraceError(string format, params object[] args) {
201             TraceInternal.TraceEvent(TraceEventType.Error, 0, format, args);
202         }
203
204         /// <devdoc>
205         /// <para>Writes a message to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/>
206         /// collection.</para>
207         /// </devdoc>
208         [System.Diagnostics.Conditional("TRACE")]
209         public static void Write(string message) {
210             TraceInternal.Write(message);
211         }
212
213         /// <devdoc>
214         /// <para>Writes the name of the <paramref name="value "/>
215         /// parameter to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection.</para>
216         /// </devdoc>
217         [System.Diagnostics.Conditional("TRACE")]
218         public static void Write(object value) {
219             TraceInternal.Write(value);
220         }
221
222         /// <devdoc>
223         ///    <para>Writes a category name and message to the trace listeners
224         ///       in the <see cref='System.Diagnostics.Trace.Listeners'/> collection.</para>
225         /// </devdoc>
226         [System.Diagnostics.Conditional("TRACE")]
227         public static void Write(string message, string category) {
228             TraceInternal.Write(message, category);
229         }
230
231         /// <devdoc>
232         ///    <para>Writes a category name and the name of the value parameter to the trace listeners
233         ///       in the <see cref='System.Diagnostics.Trace.Listeners'/> collection.</para>
234         /// </devdoc>
235         [System.Diagnostics.Conditional("TRACE")]
236         public static void Write(object value, string category) {
237             TraceInternal.Write(value, category);
238         }
239
240         /// <devdoc>
241         ///    <para>Writes a message followed by a line terminator to the
242         ///       trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection.
243         ///       The default line terminator is a carriage return followed by a line feed (\r\n).</para>
244         /// </devdoc>
245         [System.Diagnostics.Conditional("TRACE")]
246         public static void WriteLine(string message) {
247             TraceInternal.WriteLine(message);
248         }
249
250         /// <devdoc>
251         /// <para>Writes the name of the <paramref name="value "/> parameter followed by a line terminator to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection. The default line
252         ///    terminator is a carriage return followed by a line feed (\r\n).</para>
253         /// </devdoc>
254         [System.Diagnostics.Conditional("TRACE")]
255         public static void WriteLine(object value) {
256             TraceInternal.WriteLine(value);
257         }
258
259         /// <devdoc>
260         ///    <para>Writes a category name and message followed by a line terminator to the trace
261         ///       listeners in the <see cref='System.Diagnostics.Trace.Listeners'/>
262         ///       collection. The default line terminator is a carriage return followed by a line
263         ///       feed (\r\n).</para>
264         /// </devdoc>
265         [System.Diagnostics.Conditional("TRACE")]
266         public static void WriteLine(string message, string category) {
267             TraceInternal.WriteLine(message, category);
268         }
269
270         /// <devdoc>
271         /// <para>Writes a <paramref name="category "/>name and the name of the <paramref name="value "/> parameter followed by a line
272         ///    terminator to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection. The default line
273         ///    terminator is a carriage return followed by a line feed (\r\n).</para>
274         /// </devdoc>
275         [System.Diagnostics.Conditional("TRACE")]
276         public static void WriteLine(object value, string category) {
277             TraceInternal.WriteLine(value, category);
278         }
279
280         /// <devdoc>
281         /// <para>Writes a message to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection
282         ///    if a condition is <see langword='true'/>.</para>
283         /// </devdoc>
284         [System.Diagnostics.Conditional("TRACE")]
285         public static void WriteIf(bool condition, string message) {
286             TraceInternal.WriteIf(condition, message);
287         }
288
289         /// <devdoc>
290         /// <para>Writes the name of the <paramref name="value "/>
291         /// parameter to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection if a condition is
292         /// <see langword='true'/>. </para>
293         /// </devdoc>
294         [System.Diagnostics.Conditional("TRACE")]
295         public static void WriteIf(bool condition, object value) {
296             TraceInternal.WriteIf(condition, value);
297         }
298
299         /// <devdoc>
300         /// <para>Writes a category name and message to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/>
301         /// collection if a condition is <see langword='true'/>. </para>
302         /// </devdoc>
303         [System.Diagnostics.Conditional("TRACE")]
304         public static void WriteIf(bool condition, string message, string category) {
305             TraceInternal.WriteIf(condition, message, category);
306         }
307
308         /// <devdoc>
309         /// <para>Writes a category name and the name of the <paramref name="value"/> parameter to the trace
310         ///    listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection
311         ///    if a condition is <see langword='true'/>. </para>
312         /// </devdoc>
313         [System.Diagnostics.Conditional("TRACE")]
314         public static void WriteIf(bool condition, object value, string category) {
315             TraceInternal.WriteIf(condition, value, category);
316         }
317
318         /// <devdoc>
319         ///    <para>Writes a message followed by a line terminator to the trace listeners in the
320         ///    <see cref='System.Diagnostics.Trace.Listeners'/> collection if a condition is
321         ///    <see langword='true'/>. The default line terminator is a carriage return followed
322         ///       by a line feed (\r\n).</para>
323         /// </devdoc>
324         [System.Diagnostics.Conditional("TRACE")]
325         public static void WriteLineIf(bool condition, string message) {
326             TraceInternal.WriteLineIf(condition, message);
327         }
328
329         /// <devdoc>
330         /// <para>Writes the name of the <paramref name="value"/> parameter followed by a line terminator to the
331         ///    trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection
332         ///    if a condition is
333         /// <see langword='true'/>. The default line
334         ///    terminator is a carriage return followed by a line feed (\r\n).</para>
335         /// </devdoc>
336         [System.Diagnostics.Conditional("TRACE")]
337         public static void WriteLineIf(bool condition, object value) {
338             TraceInternal.WriteLineIf(condition, value);
339         }
340
341         /// <devdoc>
342         ///    <para>Writes a category name and message followed by a line terminator to the trace
343         ///       listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection if a condition is
344         ///    <see langword='true'/>. The default line terminator is a carriage return followed by a line feed (\r\n).</para>
345         /// </devdoc>
346         [System.Diagnostics.Conditional("TRACE")]
347         public static void WriteLineIf(bool condition, string message, string category) {
348             TraceInternal.WriteLineIf(condition, message, category);
349         }
350
351         /// <devdoc>
352         /// <para>Writes a category name and the name of the <paramref name="value "/> parameter followed by a line
353         ///    terminator to the trace listeners in the <see cref='System.Diagnostics.Trace.Listeners'/> collection
354         ///    if a <paramref name="condition"/> is <see langword='true'/>. The
355         ///    default line terminator is a carriage return followed by a line feed (\r\n).</para>
356         /// </devdoc>
357         [System.Diagnostics.Conditional("TRACE")]
358         public static void WriteLineIf(bool condition, object value, string category) {
359             TraceInternal.WriteLineIf(condition, value, category);
360         }
361
362         /// <devdoc>
363         ///    <para>[To be supplied.]</para>
364         /// </devdoc>
365         [System.Diagnostics.Conditional("TRACE")]
366         public static void Indent() {
367             TraceInternal.Indent();
368         }
369
370         /// <devdoc>
371         ///    <para>[To be supplied.]</para>
372         /// </devdoc>
373         [System.Diagnostics.Conditional("TRACE")]
374         public static void Unindent() {
375             TraceInternal.Unindent();
376         }
377     }
378 }