Merge pull request #1304 from slluis/mac-proxy-autoconfig
[mono.git] / mcs / class / System / System.Diagnostics / TraceListener.cs
1 //
2 // System.Diagnostics.TraceListener.cs
3 //
4 // Authors:
5 //   Jonathan Pryor (jonpryor@vt.edu)
6 //   Atsushi Enomoto (atsushi@ximian.com)
7 //
8 // Comments from John R. Hicks <angryjohn69@nc.rr.com> original implementation 
9 // can be found at: /mcs/docs/apidocs/xml/en/System.Diagnostics
10 //
11 // (C) 2002 Jonathan Pryor
12 // (C) 2007 Novell, Inc.
13 //
14
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35
36 using System;
37 using System.Collections;
38 using System.Collections.Specialized;
39 using System.Runtime.InteropServices;
40 using System.Diagnostics;
41
42 namespace System.Diagnostics {
43
44         public abstract class TraceListener : MarshalByRefObject, IDisposable {
45
46                 [ThreadStatic]
47                 private int indentLevel = 0;
48
49                 [ThreadStatic]
50                 private int indentSize = 4;
51
52                 [ThreadStatic]
53                 private StringDictionary attributes = new StringDictionary ();
54 #if !MOBILE
55                 [ThreadStatic]
56                 private TraceFilter filter;
57 #endif
58                 [ThreadStatic]
59                 private TraceOptions options;
60
61                 private string name;
62                 private bool needIndent = true;
63
64                 protected TraceListener () : this ("")
65                 {
66                 }
67
68                 protected TraceListener (string name)
69                 {
70                         Name = name;
71                 }
72
73                 public int IndentLevel {
74                         get {return indentLevel;}
75                         set {indentLevel = value;}
76                 }
77
78                 public int IndentSize {
79                         get {return indentSize;}
80                         set {indentSize = value;}
81                 }
82
83                 public virtual string Name {
84                         get {return name;}
85                         set {name = value;}
86                 }
87
88                 protected bool NeedIndent {
89                         get {return needIndent;}
90                         set {needIndent = value;}
91                 }
92
93                 [MonoLimitation ("This property exists but is never considered.")]
94                 public virtual bool IsThreadSafe {
95                         get { return false; }
96                 }
97
98                 public virtual void Close ()
99                 {
100                         Dispose ();
101                 }
102
103                 public void Dispose ()
104                 {
105                         Dispose (true);
106                         GC.SuppressFinalize (this);
107                 }
108
109                 protected virtual void Dispose (bool disposing)
110                 {
111                 }
112
113                 public virtual void Fail (string message)
114                 {
115                         Fail (message, null);
116                 }
117
118                 public virtual void Fail (string message, string detailMessage)
119                 {
120                         WriteLine ("---- DEBUG ASSERTION FAILED ----");
121                         WriteLine ("---- Assert Short Message ----");
122                         WriteLine (message);
123                         if (detailMessage != null) {
124                                 WriteLine ("---- Assert Long Message ----");
125                                 WriteLine (detailMessage);
126                         }
127
128                         WriteLine ("");
129                 }
130
131                 public virtual void Flush ()
132                 {
133                 }
134
135                 public virtual void Write (object o)
136                 {
137                         Write (o.ToString());
138                 }
139
140                 public abstract void Write (string message);
141
142                 public virtual void Write (object o, string category)
143                 {
144                         Write (o.ToString(), category);
145                 }
146
147                 public virtual void Write (string message, string category)
148                 {
149                         Write (category + ": " + message);
150                 }
151
152                 protected virtual void WriteIndent ()
153                 {
154                         // Must set NeedIndent to false before Write; otherwise, we get endless
155                         // recursion with Write->WriteIndent->Write->WriteIndent...*boom*
156                         NeedIndent = false;
157                         String indent = new String (' ', IndentLevel*IndentSize);
158                         Write (indent);
159                 }
160
161                 public virtual void WriteLine (object o)
162                 {
163                         WriteLine (o.ToString());
164                 }
165
166                 public abstract void WriteLine (string message);
167
168                 public virtual void WriteLine (object o, string category)
169                 {
170                         WriteLine (o.ToString(), category);
171                 }
172
173                 public virtual void WriteLine (string message, string category)
174                 {
175                         WriteLine (category + ": " + message);
176                 }
177
178                 internal static string FormatArray (ICollection list, string joiner)
179                 {
180                         string [] arr = new string [list.Count];
181                         int i = 0;
182                         foreach (object o in list)
183                                 arr [i++] = o != null ? o.ToString () : String.Empty;
184                         return String.Join (joiner, arr);
185                 }
186
187 #if !MOBILE
188                 [ComVisible (false)]
189                 public virtual void TraceData (TraceEventCache eventCache, string source,
190                         TraceEventType eventType, int id, object data)
191                 {
192                         if (Filter != null &&
193                             !Filter.ShouldTrace (eventCache, source, eventType,
194                                                  id, null, null, data, null))
195                                 return;
196
197                         WriteLine (String.Format ("{0} {1}: {2} : {3}", source, eventType, id, data));
198
199                         if (eventCache == null)
200                                 return;
201
202                         if ((TraceOutputOptions & TraceOptions.ProcessId) != 0)
203                                 WriteLine ("    ProcessId=" + eventCache.ProcessId);
204                         if ((TraceOutputOptions & TraceOptions.LogicalOperationStack) != 0)
205                                 WriteLine ("    LogicalOperationStack=" + FormatArray (eventCache.LogicalOperationStack, ", "));
206                         if ((TraceOutputOptions & TraceOptions.ThreadId) != 0)
207                                 WriteLine ("    ThreadId=" + eventCache.ThreadId);
208                         if ((TraceOutputOptions & TraceOptions.DateTime) != 0)
209                                 WriteLine ("    DateTime=" + eventCache.DateTime.ToString ("o"));
210                         if ((TraceOutputOptions & TraceOptions.Timestamp) != 0)
211                                 WriteLine ("    Timestamp=" + eventCache.Timestamp);
212                         if ((TraceOutputOptions & TraceOptions.Callstack) != 0)
213                                 WriteLine ("    Callstack=" + eventCache.Callstack);
214                 }
215
216                 [ComVisible (false)]
217                 public virtual void TraceData (TraceEventCache eventCache, string source,
218                         TraceEventType eventType, int id, params object [] data)
219                 {
220                         if (Filter != null &&
221                             !Filter.ShouldTrace (eventCache, source, eventType,
222                                                  id, null, null, null, data))
223                                 return;
224
225                         TraceData (eventCache, source, eventType, id, FormatArray (data, " "));
226                 }
227
228                 [ComVisible (false)]
229                 public virtual void TraceEvent (TraceEventCache eventCache, string source, TraceEventType eventType, int id)
230                 {
231                         TraceEvent (eventCache, source, eventType, id, null);
232                 }
233
234                 [ComVisible (false)]
235                 public virtual void TraceEvent (TraceEventCache eventCache, string source, TraceEventType eventType,
236                         int id, string message)
237                 {
238                         TraceData (eventCache, source, eventType, id, message);
239                 }
240
241                 [ComVisible (false)]
242                 public virtual void TraceEvent (TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object [] args)
243                 {
244                         TraceEvent (eventCache, source, eventType, id, String.Format (format, args));
245                 }
246
247                 [ComVisible (false)]
248                 public virtual void TraceTransfer (TraceEventCache eventCache, string source, int id, string message, Guid relatedActivityId)
249                 {
250                         TraceEvent (eventCache, source, TraceEventType.Transfer, id, String.Format ("{0}, relatedActivityId={1}", message, relatedActivityId));
251                 }
252 #endif
253
254                 protected internal virtual string [] GetSupportedAttributes ()
255                 {
256                         return null;
257                 }
258
259                 public StringDictionary Attributes {
260                         get { return attributes; }
261                 }
262
263 #if !MOBILE
264                 [ComVisibleAttribute (false)]
265                 public TraceFilter Filter {
266                         get { return filter; }
267                         set { filter = value; }
268                 }
269 #endif
270
271                 [ComVisibleAttribute (false)]
272                 public TraceOptions TraceOutputOptions {
273                         get { return options; }
274                         set { options = value; }
275                 }
276         }
277 }
278