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); }
73 private StringDictionary attributes {
74 get { return (StringDictionary) System.Threading.Thread.GetData (_attributesStore); }
75 set { System.Threading.Thread.SetData (_attributesStore, value); }
79 private TraceFilter filter {
80 get { return (TraceFilter) System.Threading.Thread.GetData (_filterStore); }
81 set { System.Threading.Thread.SetData (_filterStore, value); }
85 private TraceOptions options {
87 object o = System.Threading.Thread.GetData (_optionsStore);
89 return TraceOptions.None;
90 return (TraceOptions) o;
92 set { System.Threading.Thread.SetData (_optionsStore, value); }
96 private int indentLevel = 0;
99 private int indentSize = 4;
102 private StringDictionary attributes = new StringDictionary ();
105 private TraceFilter filter;
108 private TraceOptions options;
112 private bool needIndent = true;
114 protected TraceListener () : this ("")
118 protected TraceListener (string name)
123 public int IndentLevel {
124 get {return indentLevel;}
125 set {indentLevel = value;}
128 public int IndentSize {
129 get {return indentSize;}
130 set {indentSize = value;}
133 public virtual string Name {
138 protected bool NeedIndent {
139 get {return needIndent;}
140 set {needIndent = value;}
143 [MonoLimitation ("This property exists but is never considered.")]
144 public virtual bool IsThreadSafe {
145 get { return false; }
148 public virtual void Close ()
153 public void Dispose ()
156 GC.SuppressFinalize (this);
159 protected virtual void Dispose (bool disposing)
163 public virtual void Fail (string message)
165 Fail (message, null);
168 public virtual void Fail (string message, string detailMessage)
170 WriteLine ("---- DEBUG ASSERTION FAILED ----");
171 WriteLine ("---- Assert Short Message ----");
173 if (detailMessage != null) {
174 WriteLine ("---- Assert Long Message ----");
175 WriteLine (detailMessage);
181 public virtual void Flush ()
185 public virtual void Write (object o)
187 Write (o.ToString());
190 public abstract void Write (string message);
192 public virtual void Write (object o, string category)
194 Write (o.ToString(), category);
197 public virtual void Write (string message, string category)
199 Write (category + ": " + message);
202 protected virtual void WriteIndent ()
204 // Must set NeedIndent to false before Write; otherwise, we get endless
205 // recursion with Write->WriteIndent->Write->WriteIndent...*boom*
207 String indent = new String (' ', IndentLevel*IndentSize);
211 public virtual void WriteLine (object o)
213 WriteLine (o.ToString());
216 public abstract void WriteLine (string message);
218 public virtual void WriteLine (object o, string category)
220 WriteLine (o.ToString(), category);
223 public virtual void WriteLine (string message, string category)
225 WriteLine (category + ": " + message);
228 internal static string FormatArray (ICollection list, string joiner)
230 string [] arr = new string [list.Count];
232 foreach (object o in list)
233 arr [i++] = o != null ? o.ToString () : String.Empty;
234 return String.Join (joiner, arr);
239 public virtual void TraceData (TraceEventCache eventCache, string source,
240 TraceEventType eventType, int id, object data)
242 if (Filter != null &&
243 !Filter.ShouldTrace (eventCache, source, eventType,
244 id, null, null, data, null))
247 WriteLine (String.Format ("{0} {1}: {2} : {3}", source, eventType, id, data));
249 if (eventCache == null)
252 if ((TraceOutputOptions & TraceOptions.ProcessId) != 0)
253 WriteLine (" ProcessId=" + eventCache.ProcessId);
254 if ((TraceOutputOptions & TraceOptions.LogicalOperationStack) != 0)
255 WriteLine (" LogicalOperationStack=" + FormatArray (eventCache.LogicalOperationStack, ", "));
256 if ((TraceOutputOptions & TraceOptions.ThreadId) != 0)
257 WriteLine (" ThreadId=" + eventCache.ThreadId);
258 if ((TraceOutputOptions & TraceOptions.DateTime) != 0)
259 WriteLine (" DateTime=" + eventCache.DateTime.ToString ("o"));
260 if ((TraceOutputOptions & TraceOptions.Timestamp) != 0)
261 WriteLine (" Timestamp=" + eventCache.Timestamp);
262 if ((TraceOutputOptions & TraceOptions.Callstack) != 0)
263 WriteLine (" Callstack=" + eventCache.Callstack);
267 public virtual void TraceData (TraceEventCache eventCache, string source,
268 TraceEventType eventType, int id, params object [] data)
270 if (Filter != null &&
271 !Filter.ShouldTrace (eventCache, source, eventType,
272 id, null, null, null, data))
275 TraceData (eventCache, source, eventType, id, FormatArray (data, " "));
279 public virtual void TraceEvent (TraceEventCache eventCache, string source, TraceEventType eventType, int id)
281 TraceEvent (eventCache, source, eventType, id, null);
285 public virtual void TraceEvent (TraceEventCache eventCache, string source, TraceEventType eventType,
286 int id, string message)
288 TraceData (eventCache, source, eventType, id, message);
292 public virtual void TraceEvent (TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object [] args)
294 TraceEvent (eventCache, source, eventType, id, String.Format (format, args));
298 public virtual void TraceTransfer (TraceEventCache eventCache, string source, int id, string message, Guid relatedActivityId)
300 TraceEvent (eventCache, source, TraceEventType.Transfer, id, String.Format ("{0}, relatedActivityId={1}", message, relatedActivityId));
304 protected internal virtual string [] GetSupportedAttributes ()
309 public StringDictionary Attributes {
310 get { return attributes; }
314 [ComVisibleAttribute (false)]
315 public TraceFilter Filter {
316 get { return filter; }
317 set { filter = value; }
321 [ComVisibleAttribute (false)]
322 public TraceOptions TraceOutputOptions {
323 get { return options; }
324 set { options = value; }