Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TraceListener.cs « System.Diagnostics « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 873a62343c63dd7e6c17524706b06f0ee1166874 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//
// System.Diagnostics.TraceListener.cs
//
// Authors:
//   Jonathan Pryor (jonpryor@vt.edu)
//   Atsushi Enomoto (atsushi@ximian.com)
//
// Comments from John R. Hicks <angryjohn69@nc.rr.com> original implementation 
// can be found at: /mcs/docs/apidocs/xml/en/System.Diagnostics
//
// (C) 2002 Jonathan Pryor
// (C) 2007 Novell, Inc.
//

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace System.Diagnostics {

	public abstract class TraceListener : MarshalByRefObject, IDisposable {

#if TARGET_JVM
		readonly LocalDataStoreSlot _indentLevelStore = System.Threading.Thread.AllocateDataSlot ();
		readonly LocalDataStoreSlot _indentSizeStore = System.Threading.Thread.AllocateDataSlot ();
		readonly LocalDataStoreSlot _attributesStore = System.Threading.Thread.AllocateDataSlot ();
		readonly LocalDataStoreSlot _filterStore = System.Threading.Thread.AllocateDataSlot ();
		readonly LocalDataStoreSlot _optionsStore = System.Threading.Thread.AllocateDataSlot ();
		
		private int indentLevel {
			get {
				object o = System.Threading.Thread.GetData (_indentLevelStore);
				if (o == null)
					return 0;
				return (int) o;
			}
			set { System.Threading.Thread.SetData (_indentLevelStore, value); }
		}
		
		private int indentSize {
			get {
				object o = System.Threading.Thread.GetData (_indentSizeStore);
				if (o == null)
					return 4;
				return (int) o;
			}
			set { System.Threading.Thread.SetData (_indentSizeStore, value); }
		}

#if NET_2_0
		private StringDictionary attributes {
			get { return (StringDictionary) System.Threading.Thread.GetData (_attributesStore); }
			set { System.Threading.Thread.SetData (_attributesStore, value); }
		}

		private TraceFilter filter {
			get { return (TraceFilter) System.Threading.Thread.GetData (_filterStore); }
			set { System.Threading.Thread.SetData (_filterStore, value); }
		}

		private TraceOptions options {
			get {
				object o = System.Threading.Thread.GetData (_optionsStore);
				if (o == null)
					return TraceOptions.None;
				return (TraceOptions) o;
			}
			set { System.Threading.Thread.SetData (_optionsStore, value); }
		}
#endif
#else
		[ThreadStatic]
		private int indentLevel = 0;

		[ThreadStatic]
		private int indentSize = 4;

#if NET_2_0
		[ThreadStatic]
		private StringDictionary attributes = new StringDictionary ();
		[ThreadStatic]
		private TraceFilter filter;
		[ThreadStatic]
		private TraceOptions options;
#endif
#endif

		private string name;
		private bool needIndent = true;

		protected TraceListener () : this ("")
		{
		}

		protected TraceListener (string name)
		{
			Name = name;
		}

		public int IndentLevel {
			get {return indentLevel;}
			set {indentLevel = value;}
		}

		public int IndentSize {
			get {return indentSize;}
			set {indentSize = value;}
		}

		public virtual string Name {
			get {return name;}
			set {name = value;}
		}

		protected bool NeedIndent {
			get {return needIndent;}
			set {needIndent = value;}
		}

#if NET_2_0
		[MonoLimitation ("This property exists but is never considered.")]
		public virtual bool IsThreadSafe {
			get { return false; }
		}
#endif

		public virtual void Close ()
		{
			Dispose ();
		}

		public void Dispose ()
		{
			Dispose (true);
			GC.SuppressFinalize (this);
		}

		protected virtual void Dispose (bool disposing)
		{
		}

		public virtual void Fail (string message)
		{
			Fail (message, "");
		}

		public virtual void Fail (string message, string detailMessage)
		{
			WriteLine ("---- DEBUG ASSERTION FAILED ----");
			WriteLine ("---- Assert Short Message ----");
			WriteLine (message);
			WriteLine ("---- Assert Long Message ----");
			WriteLine (detailMessage);
			WriteLine ("");
		}

		public virtual void Flush ()
		{
		}

		public virtual void Write (object o)
		{
			Write (o.ToString());
		}

		public abstract void Write (string message);

		public virtual void Write (object o, string category)
		{
			Write (o.ToString(), category);
		}

		public virtual void Write (string message, string category)
		{
			Write (category + ": " + message);
		}

		protected virtual void WriteIndent ()
		{
			// Must set NeedIndent to false before Write; otherwise, we get endless
			// recursion with Write->WriteIndent->Write->WriteIndent...*boom*
			NeedIndent = false;
			String indent = new String (' ', IndentLevel*IndentSize);
			Write (indent);
		}

		public virtual void WriteLine (object o)
		{
			WriteLine (o.ToString());
		}

		public abstract void WriteLine (string message);

		public virtual void WriteLine (object o, string category)
		{
			WriteLine (o.ToString(), category);
		}

		public virtual void WriteLine (string message, string category)
		{
			WriteLine (category + ": " + message);
		}

#if NET_2_0
		internal static string FormatArray (ICollection list, string joiner)
		{
			string [] arr = new string [list.Count];
			int i = 0;
			foreach (object o in list)
				arr [i++] = o != null ? o.ToString () : String.Empty;
			return String.Join (joiner, arr);
		}

		[ComVisible (false)]
		public virtual void TraceData (TraceEventCache eventCache, string source,
			TraceEventType eventType, int id, object data)
		{
			if (Filter != null &&
			    !Filter.ShouldTrace (eventCache, source, eventType,
						 id, null, null, data, null))
				return;

			WriteLine (String.Format ("{0} {1}: {2} : {3}", source, eventType, id, data));

			if (eventCache == null)
				return;

			if ((TraceOutputOptions & TraceOptions.ProcessId) != 0)
				WriteLine ("    ProcessId=" + eventCache.ProcessId);
			if ((TraceOutputOptions & TraceOptions.LogicalOperationStack) != 0)
				WriteLine ("    LogicalOperationStack=" + FormatArray (eventCache.LogicalOperationStack, ", "));
			if ((TraceOutputOptions & TraceOptions.ThreadId) != 0)
				WriteLine ("    ThreadId=" + eventCache.ThreadId);
			if ((TraceOutputOptions & TraceOptions.DateTime) != 0)
				WriteLine ("    DateTime=" + eventCache.DateTime.ToString ("o"));
			if ((TraceOutputOptions & TraceOptions.Timestamp) != 0)
				WriteLine ("    Timestamp=" + eventCache.Timestamp);
			if ((TraceOutputOptions & TraceOptions.Callstack) != 0)
				WriteLine ("    Callstack=" + eventCache.Callstack);
		}

		[ComVisible (false)]
		public virtual void TraceData (TraceEventCache eventCache, string source,
			TraceEventType eventType, int id, params object [] data)
		{
			if (Filter != null &&
			    !Filter.ShouldTrace (eventCache, source, eventType,
						 id, null, null, null, data))
				return;

			TraceData (eventCache, source, eventType, id, FormatArray (data, " "));
		}

		[ComVisible (false)]
		public virtual void TraceEvent (TraceEventCache eventCache, string source, TraceEventType eventType, int id)
		{
			TraceEvent (eventCache, source, eventType, id, null);
		}

		[ComVisible (false)]
		public virtual void TraceEvent (TraceEventCache eventCache, string source, TraceEventType eventType,
			int id, string message)
		{
			TraceData (eventCache, source, eventType, id, message);
		}

		[ComVisible (false)]
		public virtual void TraceEvent (TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object [] args)
		{
			TraceEvent (eventCache, source, eventType, id, String.Format (format, args));
		}

		[ComVisible (false)]
		public virtual void TraceTransfer (TraceEventCache eventCache, string source, int id, string message, Guid relatedActivityId)
		{
			TraceEvent (eventCache, source, TraceEventType.Transfer, id, String.Format ("{0}, relatedActivityId={1}", message, relatedActivityId));
		}

		protected internal virtual string [] GetSupportedAttributes ()
		{
			return null;
		}

		public StringDictionary Attributes {
			get { return attributes; }
		}

		[ComVisibleAttribute (false)]
		public TraceFilter Filter {
			get { return filter; }
			set { filter = value; }
		}

		[ComVisibleAttribute (false)]
		public TraceOptions TraceOutputOptions {
			get { return options; }
			set { options = value; }
		}
#endif
	}
}