Merge pull request #799 from kebby/master
[mono.git] / mcs / class / System / System.Diagnostics / TraceSource.cs
1 //
2 // TraceSource.cs
3 //
4 // Authors:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //      Marek Safar (marek.safar@gmail.com)
7 //
8 // Copyright (C) 2007 Novell, Inc.
9 // Copyright 2013 Xamarin Inc
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Configuration;
35
36 namespace System.Diagnostics
37 {
38         public class TraceSource
39         {
40                 SourceSwitch source_switch;
41                 TraceListenerCollection listeners;
42
43                 public TraceSource (string name)
44                         : this (name, SourceLevels.Off)
45                 {
46                 }
47
48                 public TraceSource (string name, SourceLevels defaultLevel)
49                 {
50                         if (name == null)
51                                 throw new ArgumentNullException ("name");
52                         if (name.Length == 0)
53                                 throw new ArgumentException ("name");
54                         
55                         Hashtable sources = DiagnosticsConfiguration.Settings ["sources"] as Hashtable;
56                         TraceSourceInfo info = sources != null ? sources [name] as TraceSourceInfo : null;
57                         source_switch = new SourceSwitch (name);
58
59                         if (info == null) {
60                                 listeners = new TraceListenerCollection ();
61                                 source_switch.Level = defaultLevel;
62                         } else {
63                                 source_switch.Level = info.Levels;
64                                 listeners = info.Listeners;
65                         }
66                 }
67
68                 public StringDictionary Attributes {
69                         get { return source_switch.Attributes; }
70                 }
71
72                 public TraceListenerCollection Listeners {
73                         get { return listeners; }
74                 }
75
76                 public string Name {
77                         get { return source_switch.DisplayName; }
78                 }
79
80                 public SourceSwitch Switch {
81                         get { return source_switch; }
82                         set {
83                                 if (value == null)
84                                         throw new ArgumentNullException ("value");
85                                 source_switch = value;
86                         }
87                 }
88
89                 public void Close ()
90                 {
91                         lock (((ICollection) listeners).SyncRoot) {
92                                 foreach (TraceListener tl in listeners)
93                                         tl.Close ();
94                         }
95                 }
96
97                 public void Flush ()
98                 {
99                         lock (((ICollection) listeners).SyncRoot) {
100                                 foreach (TraceListener tl in listeners)
101                                         tl.Flush ();
102                         }
103                 }
104
105                 [Conditional ("TRACE")]
106                 public void TraceData (
107                         TraceEventType eventType, int id, object data)
108                 {
109                         if (!source_switch.ShouldTrace (eventType))
110                                 return;
111                         lock (((ICollection) listeners).SyncRoot) {
112                                 foreach (TraceListener tl in listeners)
113                                         tl.TraceData (new TraceEventCache(), Name, eventType, id, data);
114                         }
115                 }
116
117                 [Conditional ("TRACE")]
118                 public void TraceData (
119                         TraceEventType eventType, int id, params object [] data)
120                 {
121                         if (!source_switch.ShouldTrace (eventType))
122                                 return;
123                         lock (((ICollection) listeners).SyncRoot) {
124                                 foreach (TraceListener tl in listeners)
125                                         tl.TraceData (new TraceEventCache(), Name, eventType, id, data);
126                         }
127                 }
128
129                 [Conditional ("TRACE")]
130                 public void TraceEvent (TraceEventType eventType, int id)
131                 {
132                         if (!source_switch.ShouldTrace (eventType))
133                                 return;
134                         lock (((ICollection) listeners).SyncRoot) {
135                                 foreach (TraceListener tl in listeners)
136                                         tl.TraceEvent (new TraceEventCache(), Name, eventType, id);
137                         }
138                 }
139
140                 [Conditional ("TRACE")]
141                 public void TraceEvent (TraceEventType eventType,
142                         int id, string message)
143                 {
144                         if (!source_switch.ShouldTrace (eventType))
145                                 return;
146                         lock (((ICollection) listeners).SyncRoot) {
147                                 foreach (TraceListener tl in listeners)
148                                         tl.TraceEvent (new TraceEventCache(), Name, eventType, id, message);
149                         }
150                 }
151
152                 [Conditional ("TRACE")]
153                 public void TraceEvent (TraceEventType eventType,
154                         int id, string format, params object [] args)
155                 {
156                         if (!source_switch.ShouldTrace (eventType))
157                                 return;
158                         lock (((ICollection) listeners).SyncRoot) {
159                                 foreach (TraceListener tl in listeners)
160                                         tl.TraceEvent (new TraceEventCache(), Name, eventType, id, format, args);
161                         }
162                 }
163
164                 [Conditional ("TRACE")]
165                 public void TraceInformation (string format)
166                 {
167                         TraceEvent (TraceEventType.Information, 0, format);
168                 }
169
170                 [Conditional ("TRACE")]
171                 public void TraceInformation (
172                         string format, params object [] args)
173                 {
174                         TraceEvent (TraceEventType.Information, 0, format, args);
175                 }
176
177                 [Conditional ("TRACE")]
178                 public void TraceTransfer (int id, string message, Guid relatedActivityId)
179                 {
180                         if (!source_switch.ShouldTrace (TraceEventType.Transfer ))
181                                 return;
182                         lock (((ICollection) listeners).SyncRoot) {
183                                 foreach (TraceListener tl in listeners)
184                                         tl.TraceTransfer (new TraceEventCache(), Name, id, message, relatedActivityId);
185                         }
186                 }
187
188                 protected virtual string [] GetSupportedAttributes ()
189                 {
190                         return null;
191                 }
192         }
193 }
194