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