minor fix for bug 9520:
[mono.git] / mcs / class / System / System.Diagnostics / TraceSource.cs
1 //
2 // TraceSource.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // Copyright (C) 2007 Novell, Inc.
8 //
9
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 sourceLevels)
49                 {
50                         if (name == null)
51                                 throw new ArgumentNullException ("name");
52                         Hashtable sources = DiagnosticsConfiguration.Settings ["sources"] as Hashtable;
53                         TraceSourceInfo info = sources != null ? sources [name] as TraceSourceInfo : null;
54                         source_switch = new SourceSwitch (name);
55
56                         if (info == null)
57                                 listeners = new TraceListenerCollection ();
58                         else {
59                                 source_switch.Level = info.Levels;
60                                 listeners = info.Listeners;
61                         }
62                 }
63
64                 public StringDictionary Attributes {
65                         get { return source_switch.Attributes; }
66                 }
67
68                 public TraceListenerCollection Listeners {
69                         get { return listeners; }
70                 }
71
72                 public string Name {
73                         get { return source_switch.DisplayName; }
74                 }
75
76                 public SourceSwitch Switch {
77                         get { return source_switch; }
78                         set {
79                                 if (value == null)
80                                         throw new ArgumentNullException ("value");
81                                 source_switch = value;
82                         }
83                 }
84
85                 public void Close ()
86                 {
87                         lock (((ICollection) listeners).SyncRoot) {
88                                 foreach (TraceListener tl in listeners)
89                                         tl.Close ();
90                         }
91                 }
92
93                 public void Flush ()
94                 {
95                         lock (((ICollection) listeners).SyncRoot) {
96                                 foreach (TraceListener tl in listeners)
97                                         tl.Flush ();
98                         }
99                 }
100
101                 [Conditional ("TRACE")]
102                 public void TraceData (
103                         TraceEventType eventType, int id, object data)
104                 {
105                         if (!source_switch.ShouldTrace (eventType))
106                                 return;
107                         lock (((ICollection) listeners).SyncRoot) {
108                                 foreach (TraceListener tl in listeners)
109                                         tl.TraceData (new TraceEventCache(), Name, eventType, id, data);
110                         }
111                 }
112
113                 [Conditional ("TRACE")]
114                 public void TraceData (
115                         TraceEventType eventType, int id, params object [] data)
116                 {
117                         if (!source_switch.ShouldTrace (eventType))
118                                 return;
119                         lock (((ICollection) listeners).SyncRoot) {
120                                 foreach (TraceListener tl in listeners)
121                                         tl.TraceData (new TraceEventCache(), Name, eventType, id, data);
122                         }
123                 }
124
125                 [Conditional ("TRACE")]
126                 public void TraceEvent (TraceEventType eventType, int id)
127                 {
128                         if (!source_switch.ShouldTrace (eventType))
129                                 return;
130                         lock (((ICollection) listeners).SyncRoot) {
131                                 foreach (TraceListener tl in listeners)
132                                         tl.TraceEvent (new TraceEventCache(), Name, eventType, id);
133                         }
134                 }
135
136                 [Conditional ("TRACE")]
137                 public void TraceEvent (TraceEventType eventType,
138                         int id, string message)
139                 {
140                         if (!source_switch.ShouldTrace (eventType))
141                                 return;
142                         lock (((ICollection) listeners).SyncRoot) {
143                                 foreach (TraceListener tl in listeners)
144                                         tl.TraceEvent (new TraceEventCache(), Name, eventType, id, message);
145                         }
146                 }
147
148                 [Conditional ("TRACE")]
149                 public void TraceEvent (TraceEventType eventType,
150                         int id, string format, params object [] args)
151                 {
152                         if (!source_switch.ShouldTrace (eventType))
153                                 return;
154                         lock (((ICollection) listeners).SyncRoot) {
155                                 foreach (TraceListener tl in listeners)
156                                         tl.TraceEvent (new TraceEventCache(), Name, eventType, id, format, args);
157                         }
158                 }
159
160                 [Conditional ("TRACE")]
161                 public void TraceInformation (string format)
162                 {
163                         TraceEvent (TraceEventType.Information, 0, format);
164                 }
165
166                 [Conditional ("TRACE")]
167                 public void TraceInformation (
168                         string format, params object [] args)
169                 {
170                         TraceEvent (TraceEventType.Information, 0, format, args);
171                 }
172
173                 [Conditional ("TRACE")]
174                 public void TraceTransfer (int id, string message, Guid relatedActivityId)
175                 {
176                         if (!source_switch.ShouldTrace (TraceEventType.Transfer ))
177                                 return;
178                         lock (((ICollection) listeners).SyncRoot) {
179                                 foreach (TraceListener tl in listeners)
180                                         tl.TraceTransfer (new TraceEventCache(), Name, id, message, relatedActivityId);
181                         }
182                 }
183
184                 protected virtual string [] GetSupportedAttributes ()
185                 {
186                         return null;
187                 }
188         }
189 }
190