2 // System.Diagnostics.TraceListener.cs
5 // Jonathan Pryor (jonpryor@vt.edu)
6 // Atsushi Enomoto (atsushi@ximian.com)
8 // Comments from John R. Hicks <angryjohn69@nc.rr.com> original implementation
9 // can be found at: /mcs/docs/apidocs/xml/en/System.Diagnostics
11 // (C) 2002 Jonathan Pryor
12 // (C) 2007 Novell, Inc.
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:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
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.
37 using System.Collections;
38 using System.Collections.Specialized;
39 using System.Runtime.InteropServices;
40 using System.Diagnostics;
42 namespace System.Diagnostics {
44 public abstract class TraceListener : MarshalByRefObject, IDisposable {
47 readonly LocalDataStoreSlot _indentLevelStore = System.Threading.Thread.AllocateDataSlot ();
48 readonly LocalDataStoreSlot _indentSizeStore = System.Threading.Thread.AllocateDataSlot ();
49 readonly LocalDataStoreSlot _attributesStore = System.Threading.Thread.AllocateDataSlot ();
50 readonly LocalDataStoreSlot _filterStore = System.Threading.Thread.AllocateDataSlot ();
51 readonly LocalDataStoreSlot _optionsStore = System.Threading.Thread.AllocateDataSlot ();
53 private int indentLevel {
55 object o = System.Threading.Thread.GetData (_indentLevelStore);
60 set { System.Threading.Thread.SetData (_indentLevelStore, value); }
63 private int indentSize {
65 object o = System.Threading.Thread.GetData (_indentSizeStore);
70 set { System.Threading.Thread.SetData (_indentSizeStore, value); }
74 private StringDictionary attributes {
75 get { return (StringDictionary) System.Threading.Thread.GetData (_attributesStore); }
76 set { System.Threading.Thread.SetData (_attributesStore, value); }
80 private TraceFilter filter {
81 get { return (TraceFilter) System.Threading.Thread.GetData (_filterStore); }
82 set { System.Threading.Thread.SetData (_filterStore, value); }
86 private TraceOptions options {
88 object o = System.Threading.Thread.GetData (_optionsStore);
90 return TraceOptions.None;
91 return (TraceOptions) o;
93 set { System.Threading.Thread.SetData (_optionsStore, value); }
98 private int indentLevel = 0;
101 private int indentSize = 4;
105 private StringDictionary attributes = new StringDictionary ();
108 private TraceFilter filter;
111 private TraceOptions options;
116 private bool needIndent = true;
118 protected TraceListener () : this ("")
122 protected TraceListener (string name)
127 public int IndentLevel {
128 get {return indentLevel;}
129 set {indentLevel = value;}
132 public int IndentSize {
133 get {return indentSize;}
134 set {indentSize = value;}
137 public virtual string Name {
142 protected bool NeedIndent {
143 get {return needIndent;}
144 set {needIndent = value;}
148 [MonoLimitation ("This property exists but is never considered.")]
149 public virtual bool IsThreadSafe {
150 get { return false; }
154 public virtual void Close ()
159 public void Dispose ()
162 GC.SuppressFinalize (this);
165 protected virtual void Dispose (bool disposing)
169 public virtual void Fail (string message)
174 public virtual void Fail (string message, string detailMessage)
176 WriteLine ("---- DEBUG ASSERTION FAILED ----");
177 WriteLine ("---- Assert Short Message ----");
179 WriteLine ("---- Assert Long Message ----");
180 WriteLine (detailMessage);
184 public virtual void Flush ()
188 public virtual void Write (object o)
190 Write (o.ToString());
193 public abstract void Write (string message);
195 public virtual void Write (object o, string category)
197 Write (o.ToString(), category);
200 public virtual void Write (string message, string category)
202 Write (category + ": " + message);
205 protected virtual void WriteIndent ()
207 // Must set NeedIndent to false before Write; otherwise, we get endless
208 // recursion with Write->WriteIndent->Write->WriteIndent...*boom*
210 String indent = new String (' ', IndentLevel*IndentSize);
214 public virtual void WriteLine (object o)
216 WriteLine (o.ToString());
219 public abstract void WriteLine (string message);
221 public virtual void WriteLine (object o, string category)
223 WriteLine (o.ToString(), category);
226 public virtual void WriteLine (string message, string category)
228 WriteLine (category + ": " + message);
232 internal static string FormatArray (ICollection list, string joiner)
234 string [] arr = new string [list.Count];
236 foreach (object o in list)
237 arr [i++] = o != null ? o.ToString () : String.Empty;
238 return String.Join (joiner, arr);
243 public virtual void TraceData (TraceEventCache eventCache, string source,
244 TraceEventType eventType, int id, object data)
246 if (Filter != null &&
247 !Filter.ShouldTrace (eventCache, source, eventType,
248 id, null, null, data, null))
251 WriteLine (String.Format ("{0} {1}: {2} : {3}", source, eventType, id, data));
253 if (eventCache == null)
256 if ((TraceOutputOptions & TraceOptions.ProcessId) != 0)
257 WriteLine (" ProcessId=" + eventCache.ProcessId);
258 if ((TraceOutputOptions & TraceOptions.LogicalOperationStack) != 0)
259 WriteLine (" LogicalOperationStack=" + FormatArray (eventCache.LogicalOperationStack, ", "));
260 if ((TraceOutputOptions & TraceOptions.ThreadId) != 0)
261 WriteLine (" ThreadId=" + eventCache.ThreadId);
262 if ((TraceOutputOptions & TraceOptions.DateTime) != 0)
263 WriteLine (" DateTime=" + eventCache.DateTime.ToString ("o"));
264 if ((TraceOutputOptions & TraceOptions.Timestamp) != 0)
265 WriteLine (" Timestamp=" + eventCache.Timestamp);
266 if ((TraceOutputOptions & TraceOptions.Callstack) != 0)
267 WriteLine (" Callstack=" + eventCache.Callstack);
271 public virtual void TraceData (TraceEventCache eventCache, string source,
272 TraceEventType eventType, int id, params object [] data)
274 if (Filter != null &&
275 !Filter.ShouldTrace (eventCache, source, eventType,
276 id, null, null, null, data))
279 TraceData (eventCache, source, eventType, id, FormatArray (data, " "));
283 public virtual void TraceEvent (TraceEventCache eventCache, string source, TraceEventType eventType, int id)
285 TraceEvent (eventCache, source, eventType, id, null);
289 public virtual void TraceEvent (TraceEventCache eventCache, string source, TraceEventType eventType,
290 int id, string message)
292 TraceData (eventCache, source, eventType, id, message);
296 public virtual void TraceEvent (TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object [] args)
298 TraceEvent (eventCache, source, eventType, id, String.Format (format, args));
302 public virtual void TraceTransfer (TraceEventCache eventCache, string source, int id, string message, Guid relatedActivityId)
304 TraceEvent (eventCache, source, TraceEventType.Transfer, id, String.Format ("{0}, relatedActivityId={1}", message, relatedActivityId));
308 protected internal virtual string [] GetSupportedAttributes ()
313 public StringDictionary Attributes {
314 get { return attributes; }
318 [ComVisibleAttribute (false)]
319 public TraceFilter Filter {
320 get { return filter; }
321 set { filter = value; }
325 [ComVisibleAttribute (false)]
326 public TraceOptions TraceOutputOptions {
327 get { return options; }
328 set { options = value; }