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;
40 namespace System.Diagnostics
42 internal sealed class DiagnosticsConfiguration
44 private static object lock_ = new object();
45 private static IDictionary settings;
47 public static IDictionary Settings {
51 settings = (IDictionary) ConfigurationSettings.GetConfig ("system.diagnostics");
58 public class DiagnosticsConfigurationHandler : IConfigurationSectionHandler
60 delegate void ElementHandler (IDictionary d, XmlNode node);
62 IDictionary elementHandlers = new Hashtable ();
64 public DiagnosticsConfigurationHandler ()
66 elementHandlers ["assert"] = new ElementHandler (AddAssertNode);
67 elementHandlers ["switches"] = new ElementHandler (AddSwitchesNode);
68 elementHandlers ["trace"] = new ElementHandler (AddTraceNode);
71 public virtual object Create (object parent, object configContext, XmlNode section)
75 d = new Hashtable (CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);
77 d = (IDictionary) ((ICloneable)parent).Clone();
79 foreach (XmlNode child in section.ChildNodes) {
80 XmlNodeType type = child.NodeType;
84 case XmlNodeType.Whitespace:
85 case XmlNodeType.Comment:
87 case XmlNodeType.Element:
88 ElementHandler eh = (ElementHandler) elementHandlers [child.Name];
92 ThrowUnrecognizedElement (child);
95 ThrowUnrecognizedElement (child);
103 // Remarks: Both attribute are optional
104 private void AddAssertNode (IDictionary d, XmlNode node)
106 XmlAttributeCollection c = node.Attributes;
107 string assertuienabled = GetAttribute (c, "assertuienabled", false, node);
108 string logfilename = GetAttribute (c, "logfilename", false, node);
109 ValidateInvalidAttributes (c, node);
110 if (assertuienabled != null) {
112 d ["assertuienabled"] = bool.Parse (assertuienabled);
114 catch (Exception e) {
115 throw new ConfigurationException ("The `assertuienabled' attribute must be `true' or `false'",
120 if (logfilename != null)
121 d ["logfilename"] = logfilename;
123 DefaultTraceListener dtl = (DefaultTraceListener) TraceImpl.Listeners["Default"];
125 if (assertuienabled != null)
126 dtl.AssertUiEnabled = (bool) d ["assertuienabled"];
127 if (logfilename != null)
128 dtl.LogFileName = logfilename;
131 if (node.ChildNodes.Count > 0)
132 ThrowUnrecognizedElement (node.ChildNodes[0]);
135 // name and value attributes are required
136 // Docs do not define "remove" or "clear" elements, but .NET recognizes
138 private void AddSwitchesNode (IDictionary d, XmlNode node)
140 // There are no attributes on <switch/>
141 ValidateInvalidAttributes (node.Attributes, node);
143 IDictionary newNodes = new Hashtable ();
145 foreach (XmlNode child in node.ChildNodes) {
146 XmlNodeType t = child.NodeType;
147 if (t == XmlNodeType.Whitespace || t == XmlNodeType.Comment)
149 if (t == XmlNodeType.Element) {
150 XmlAttributeCollection attributes = child.Attributes;
153 switch (child.Name) {
155 name = GetAttribute (attributes, "name", true, child);
156 value = GetAttribute (attributes, "value", true, child);
157 value = AsString (value);
158 ValidateIntegralValue (name, value);
159 newNodes [name] = value;
162 name = GetAttribute (attributes, "name", true, child);
163 newNodes.Remove (name);
169 ThrowUnrecognizedElement (child);
172 ValidateInvalidAttributes (attributes, child);
175 ThrowUnrecognizedNode (child);
178 d [node.Name] = newNodes;
181 private static void ValidateIntegralValue (string name, string value)
184 int n = int.Parse (value);
185 // remove warning about unused variable.
189 throw new ConfigurationException (string.Format (
191 "The value of a switch must be integral", name));
195 private void AddTraceNode (IDictionary d, XmlNode node)
197 AddTraceAttributes (d, node);
199 foreach (XmlNode child in node.ChildNodes) {
200 XmlNodeType t = child.NodeType;
201 if (t == XmlNodeType.Whitespace || t == XmlNodeType.Comment)
203 if (t == XmlNodeType.Element) {
204 if (child.Name == "listeners")
205 AddTraceListeners (child);
207 ThrowUnrecognizedElement (child);
208 ValidateInvalidAttributes (child.Attributes, child);
211 ThrowUnrecognizedNode (child);
215 // all attributes are optional
216 private void AddTraceAttributes (IDictionary d, XmlNode node)
218 XmlAttributeCollection c = node.Attributes;
219 string autoflush = GetAttribute (c, "autoflush", false, node);
220 string indentsize = GetAttribute (c, "indentsize", false, node);
221 ValidateInvalidAttributes (c, node);
222 if (autoflush != null) {
224 bool b = bool.Parse (autoflush);
226 TraceImpl.AutoFlush = b;
228 catch (Exception e) {
229 throw new ConfigurationException ("The `autoflush' attribute must be `true' or `false'",
233 if (indentsize != null) {
235 int n = int.Parse (indentsize);
236 d ["indentsize"] = n;
237 TraceImpl.IndentSize = n;
239 catch (Exception e) {
240 throw new ConfigurationException ("The `indentsize' attribute must be an integral value.",
246 // only defines "add" and "remove", but "clear" also works
247 // for add, "name" and "type" are required; initializeData is optional
248 private void AddTraceListeners (XmlNode listenersNode)
250 // There are no attributes on <listeners/>
251 ValidateInvalidAttributes (listenersNode.Attributes, listenersNode);
253 foreach (XmlNode child in listenersNode.ChildNodes) {
254 XmlNodeType t = child.NodeType;
255 if (t == XmlNodeType.Whitespace || t == XmlNodeType.Comment)
257 if (t == XmlNodeType.Element) {
258 XmlAttributeCollection attributes = child.Attributes;
262 switch (child.Name) {
264 name = GetAttribute (attributes, "name", true, child);
265 type = GetAttribute (attributes, "type", true, child);
266 id = GetAttribute (attributes, "initializeData", false, child);
267 AddTraceListener (name, type, id);
270 name = GetAttribute (attributes, "name", true, child);
271 RemoveTraceListener (name);
274 TraceImpl.Listeners.Clear ();
277 ThrowUnrecognizedElement (child);
280 ValidateInvalidAttributes (attributes, child);
283 ThrowUnrecognizedNode (child);
287 private void AddTraceListener (string name, string type, string initializeData)
289 Type t = Type.GetType (type);
291 throw new ConfigurationException (string.Format ("Invalid Type Specified: {0}", type));
296 if (initializeData != null) {
297 args = new object[] { initializeData };
298 types = new Type[] { typeof(string) };
305 System.Reflection.ConstructorInfo ctor = t.GetConstructor (types);
307 throw new ConfigurationException ("Couldn't find constructor for class " + type);
309 TraceListener l = (TraceListener) ctor.Invoke (args);
311 TraceImpl.Listeners.Add (l);
314 private void RemoveTraceListener (string name)
317 TraceImpl.Listeners.Remove (name);
319 catch (ArgumentException) {
320 // The specified listener wasn't in the collection
321 // Ignore this; .NET does.
323 catch (Exception e) {
324 throw new ConfigurationException (
325 string.Format ("Unknown error removing listener: {0}", name),
330 private string GetAttribute (XmlAttributeCollection attrs, string attr, bool required, XmlNode node)
332 XmlAttribute a = attrs[attr];
339 ValidateAttribute (attr, r, node);
343 ThrowMissingAttribute (attr, node);
348 private string AsString (string s)
350 return s == null ? string.Empty : s;
353 private void ValidateAttribute (string attribute, string value, XmlNode node)
355 if (value == null || value.Length == 0)
356 throw new ConfigurationException (string.Format ("Required attribute `{0}' cannot be empty.", attribute), node);
359 private void ValidateInvalidAttributes (XmlAttributeCollection c, XmlNode node)
362 ThrowUnrecognizedAttribute (c[0].Name, node);
365 private void ThrowMissingAttribute (string attribute, XmlNode node)
367 throw new ConfigurationException (string.Format ("Missing required attribute `{0}'.", attribute), node);
370 private void ThrowUnrecognizedNode (XmlNode node)
372 throw new ConfigurationException (
373 string.Format ("Unrecognized node `{0}'; nodeType={1}", node.Name, node.NodeType),
377 private void ThrowUnrecognizedElement (XmlNode node)
379 throw new ConfigurationException (
380 string.Format ("Unrecognized element <{0}/>", node.Name),
384 private void ThrowUnrecognizedAttribute (string attribute, XmlNode node)
386 throw new ConfigurationException (
387 string.Format ("Unrecognized attribute `{0}' on element <{1}/>.", attribute, node.Name),