2 // System.Diagnostics.DiagnosticsConfigurationHandler.cs
4 // Comments from John R. Hicks <angryjohn69@nc.rr.com> original implementation
5 // can be found at: /mcs/docs/apidocs/xml/en/System.Diagnostics
8 // John R. Hicks <angryjohn69@nc.rr.com>
9 // Jonathan Pryor <jonpryor@vt.edu>
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Collections;
36 using System.Configuration;
37 using System.Threading;
41 namespace System.Diagnostics
43 internal sealed class DiagnosticsConfiguration
46 private static object lock_ = new object();
48 private static object settings;
50 public static IDictionary Settings {
53 if (settings == null) {
54 object s = ConfigurationSettings.GetConfig ("system.diagnostics");
55 Thread.MemoryBarrier ();
56 while (Interlocked.CompareExchange (ref settings, s, null) == null) {
57 // do nothing; we're just setting settings.
59 Thread.MemoryBarrier ();
64 settings = ConfigurationSettings.GetConfig ("system.diagnostics");
67 return (IDictionary) settings;
72 public class DiagnosticsConfigurationHandler : IConfigurationSectionHandler
74 delegate void ElementHandler (IDictionary d, XmlNode node);
76 IDictionary elementHandlers = new Hashtable ();
78 public DiagnosticsConfigurationHandler ()
80 elementHandlers ["assert"] = new ElementHandler (AddAssertNode);
81 elementHandlers ["switches"] = new ElementHandler (AddSwitchesNode);
82 elementHandlers ["trace"] = new ElementHandler (AddTraceNode);
85 public virtual object Create (object parent, object configContext, XmlNode section)
89 d = new Hashtable (CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);
91 d = (IDictionary) ((ICloneable)parent).Clone();
93 foreach (XmlNode child in section.ChildNodes) {
94 XmlNodeType type = child.NodeType;
98 case XmlNodeType.Whitespace:
99 case XmlNodeType.Comment:
101 case XmlNodeType.Element:
102 ElementHandler eh = (ElementHandler) elementHandlers [child.Name];
106 ThrowUnrecognizedElement (child);
109 ThrowUnrecognizedElement (child);
117 // Remarks: Both attribute are optional
118 private void AddAssertNode (IDictionary d, XmlNode node)
120 XmlAttributeCollection c = node.Attributes;
121 string assertuienabled = GetAttribute (c, "assertuienabled", false, node);
122 string logfilename = GetAttribute (c, "logfilename", false, node);
123 ValidateInvalidAttributes (c, node);
124 if (assertuienabled != null) {
126 d ["assertuienabled"] = bool.Parse (assertuienabled);
128 catch (Exception e) {
129 throw new ConfigurationException ("The `assertuienabled' attribute must be `true' or `false'",
134 if (logfilename != null)
135 d ["logfilename"] = logfilename;
137 DefaultTraceListener dtl = (DefaultTraceListener) TraceImpl.Listeners["Default"];
139 if (assertuienabled != null)
140 dtl.AssertUiEnabled = (bool) d ["assertuienabled"];
141 if (logfilename != null)
142 dtl.LogFileName = logfilename;
145 if (node.ChildNodes.Count > 0)
146 ThrowUnrecognizedElement (node.ChildNodes[0]);
149 // name and value attributes are required
150 // Docs do not define "remove" or "clear" elements, but .NET recognizes
152 private void AddSwitchesNode (IDictionary d, XmlNode node)
154 // There are no attributes on <switch/>
155 ValidateInvalidAttributes (node.Attributes, node);
157 IDictionary newNodes = new Hashtable ();
159 foreach (XmlNode child in node.ChildNodes) {
160 XmlNodeType t = child.NodeType;
161 if (t == XmlNodeType.Whitespace || t == XmlNodeType.Comment)
163 if (t == XmlNodeType.Element) {
164 XmlAttributeCollection attributes = child.Attributes;
167 switch (child.Name) {
169 name = GetAttribute (attributes, "name", true, child);
170 value = GetAttribute (attributes, "value", true, child);
171 value = AsString (value); ValidateIntegralValue (name, value);
172 newNodes [name] = value;
175 name = GetAttribute (attributes, "name", true, child);
176 newNodes.Remove (name);
182 ThrowUnrecognizedElement (child);
185 ValidateInvalidAttributes (attributes, child);
188 ThrowUnrecognizedNode (child);
191 d [node.Name] = newNodes;
194 private static int ValidateIntegralValue (string name, string value)
197 return int.Parse (value);
199 throw new ConfigurationException (string.Format (
201 "The value of a switch must be integral", name));
205 private void AddTraceNode (IDictionary d, XmlNode node)
207 AddTraceAttributes (d, node);
209 foreach (XmlNode child in node.ChildNodes) {
210 XmlNodeType t = child.NodeType;
211 if (t == XmlNodeType.Whitespace || t == XmlNodeType.Comment)
213 if (t == XmlNodeType.Element) {
214 if (child.Name == "listeners")
215 AddTraceListeners (child);
217 ThrowUnrecognizedElement (child);
218 ValidateInvalidAttributes (child.Attributes, child);
221 ThrowUnrecognizedNode (child);
225 // all attributes are optional
226 private void AddTraceAttributes (IDictionary d, XmlNode node)
228 XmlAttributeCollection c = node.Attributes;
229 string autoflush = GetAttribute (c, "autoflush", false, node);
230 string indentsize = GetAttribute (c, "indentsize", false, node);
231 ValidateInvalidAttributes (c, node);
232 if (autoflush != null) {
234 bool b = bool.Parse (autoflush);
236 TraceImpl.AutoFlush = b;
238 catch (Exception e) {
239 throw new ConfigurationException ("The `autoflush' attribute must be `true' or `false'",
243 if (indentsize != null) {
245 int n = int.Parse (indentsize);
246 d ["indentsize"] = n;
247 TraceImpl.IndentSize = n;
249 catch (Exception e) {
250 throw new ConfigurationException ("The `indentsize' attribute must be an integral value.",
256 // only defines "add" and "remove", but "clear" also works
257 // for add, "name" and "type" are required; initializeData is optional
258 private void AddTraceListeners (XmlNode listenersNode)
260 // There are no attributes on <listeners/>
261 ValidateInvalidAttributes (listenersNode.Attributes, listenersNode);
263 foreach (XmlNode child in listenersNode.ChildNodes) {
264 XmlNodeType t = child.NodeType;
265 if (t == XmlNodeType.Whitespace || t == XmlNodeType.Comment)
267 if (t == XmlNodeType.Element) {
268 XmlAttributeCollection attributes = child.Attributes;
272 switch (child.Name) {
274 name = GetAttribute (attributes, "name", true, child);
275 type = GetAttribute (attributes, "type", true, child);
276 id = GetAttribute (attributes, "initializeData", false, child);
277 AddTraceListener (name, type, id);
280 name = GetAttribute (attributes, "name", true, child);
281 RemoveTraceListener (name);
284 TraceImpl.Listeners.Clear ();
287 ThrowUnrecognizedElement (child);
290 ValidateInvalidAttributes (attributes, child);
293 ThrowUnrecognizedNode (child);
297 private void AddTraceListener (string name, string type, string initializeData)
299 Type t = Type.GetType (type);
301 throw new ConfigurationException (string.Format ("Invalid Type Specified: {0}", type));
306 if (initializeData != null) {
307 args = new object[] { initializeData };
308 types = new Type[] { typeof(string) };
315 System.Reflection.ConstructorInfo ctor = t.GetConstructor (types);
317 throw new ConfigurationException ("Couldn't find constructor for class " + type);
319 TraceListener l = (TraceListener) ctor.Invoke (args);
321 TraceImpl.Listeners.Add (l);
324 private void RemoveTraceListener (string name)
327 TraceImpl.Listeners.Remove (name);
329 catch (ArgumentException) {
330 // The specified listener wasn't in the collection
331 // Ignore this; .NET does.
333 catch (Exception e) {
334 throw new ConfigurationException (
335 string.Format ("Unknown error removing listener: {0}", name),
340 private string GetAttribute (XmlAttributeCollection attrs, string attr, bool required, XmlNode node)
342 XmlAttribute a = attrs[attr];
349 ValidateAttribute (attr, r, node);
353 ThrowMissingAttribute (attr, node);
358 private string AsString (string s)
360 return s == null ? string.Empty : s;
363 private void ValidateAttribute (string attribute, string value, XmlNode node)
365 if (value == null || value.Length == 0)
366 throw new ConfigurationException (string.Format ("Required attribute `{0}' cannot be empty.", attribute), node);
369 private void ValidateInvalidAttributes (XmlAttributeCollection c, XmlNode node)
372 ThrowUnrecognizedAttribute (c[0].Name, node);
375 private void ThrowMissingAttribute (string attribute, XmlNode node)
377 throw new ConfigurationException (string.Format ("Missing required attribute `{0}'.", attribute), node);
380 private void ThrowUnrecognizedNode (XmlNode node)
382 throw new ConfigurationException (
383 string.Format ("Unrecognized node `{0}'; nodeType={1}", node.Name, node.NodeType),
387 private void ThrowUnrecognizedElement (XmlNode node)
389 throw new ConfigurationException (
390 string.Format ("Unrecognized element <{0}/>", node.Name),
394 private void ThrowUnrecognizedAttribute (string attribute, XmlNode node)
396 throw new ConfigurationException (
397 string.Format ("Unrecognized attribute `{0}' on element <{1}/>.", attribute, node.Name),