DiagnosticsConfigurationHandler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. //
  2. // System.Diagnostics.DiagnosticsConfigurationHandler.cs
  3. //
  4. // Comments from John R. Hicks <[email protected]> original implementation
  5. // can be found at: /mcs/docs/apidocs/xml/en/System.Diagnostics
  6. //
  7. // Authors:
  8. // John R. Hicks <[email protected]>
  9. // Jonathan Pryor <[email protected]>
  10. //
  11. // (C) 2002
  12. //
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System;
  34. using System.Collections;
  35. using System.Configuration;
  36. #if (XML_DEP)
  37. using System.Xml;
  38. #endif
  39. namespace System.Diagnostics
  40. {
  41. internal sealed class DiagnosticsConfiguration
  42. {
  43. private static IDictionary settings = null;
  44. public static IDictionary Settings {
  45. get {
  46. // TODO: Does anybody know if this is actually thread-safe under .NET?
  47. // I've heard that this construct isn't safe under Java, but it's used
  48. // reasonably often under C++, so I'm not sure about .NET.
  49. if (settings == null) {
  50. lock (typeof(DiagnosticsConfiguration)) {
  51. if (settings == null)
  52. settings = (IDictionary) ConfigurationSettings.GetConfig ("system.diagnostics");
  53. }
  54. }
  55. return settings;
  56. }
  57. }
  58. }
  59. #if (XML_DEP)
  60. public class DiagnosticsConfigurationHandler : IConfigurationSectionHandler
  61. {
  62. delegate void ElementHandler (IDictionary d, XmlNode node);
  63. IDictionary elementHandlers = new Hashtable ();
  64. public DiagnosticsConfigurationHandler ()
  65. {
  66. elementHandlers ["assert"] = new ElementHandler (AddAssertNode);
  67. elementHandlers ["switches"] = new ElementHandler (AddSwitchesNode);
  68. elementHandlers ["trace"] = new ElementHandler (AddTraceNode);
  69. }
  70. public virtual object Create (object parent, object configContext, XmlNode section)
  71. {
  72. IDictionary d;
  73. if (parent == null)
  74. d = new Hashtable (CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);
  75. else
  76. d = (IDictionary) ((ICloneable)parent).Clone();
  77. foreach (XmlNode child in section.ChildNodes) {
  78. XmlNodeType type = child.NodeType;
  79. switch (type) {
  80. /* ignore */
  81. case XmlNodeType.Whitespace:
  82. case XmlNodeType.Comment:
  83. continue;
  84. case XmlNodeType.Element:
  85. ElementHandler eh = (ElementHandler) elementHandlers [child.Name];
  86. if (eh != null)
  87. eh (d, child);
  88. else
  89. ThrowUnrecognizedElement (child);
  90. break;
  91. default:
  92. ThrowUnrecognizedElement (child);
  93. break;
  94. }
  95. }
  96. return d;
  97. }
  98. // Remarks: Both attribute are optional
  99. private void AddAssertNode (IDictionary d, XmlNode node)
  100. {
  101. XmlAttributeCollection c = node.Attributes;
  102. string assertuienabled = GetAttribute (c, "assertuienabled", false, node);
  103. string logfilename = GetAttribute (c, "logfilename", false, node);
  104. ValidateInvalidAttributes (c, node);
  105. if (assertuienabled != null) {
  106. try {
  107. d ["assertuienabled"] = bool.Parse (assertuienabled);
  108. }
  109. catch (Exception e) {
  110. throw new ConfigurationException ("The `assertuienabled' attribute must be `true' or `false'",
  111. e, node);
  112. }
  113. }
  114. if (logfilename != null)
  115. d ["logfilename"] = logfilename;
  116. DefaultTraceListener dtl = (DefaultTraceListener) TraceImpl.Listeners["Default"];
  117. if (dtl != null) {
  118. if (assertuienabled != null)
  119. dtl.AssertUiEnabled = (bool) d ["assertuienabled"];
  120. if (logfilename != null)
  121. dtl.LogFileName = logfilename;
  122. }
  123. if (node.ChildNodes.Count > 0)
  124. ThrowUnrecognizedElement (node.ChildNodes[0]);
  125. }
  126. // name attribute is required, value is optional
  127. // Docs do not define "remove" or "clear" elements, but .NET recognizes
  128. // them
  129. private void AddSwitchesNode (IDictionary d, XmlNode node)
  130. {
  131. // There are no attributes on <switch/>
  132. ValidateInvalidAttributes (node.Attributes, node);
  133. IDictionary newNodes = new Hashtable ();
  134. foreach (XmlNode child in node.ChildNodes) {
  135. XmlNodeType t = child.NodeType;
  136. if (t == XmlNodeType.Whitespace || t == XmlNodeType.Comment)
  137. continue;
  138. if (t == XmlNodeType.Element) {
  139. XmlAttributeCollection attributes = child.Attributes;
  140. string name = null;
  141. string value = null;
  142. switch (child.Name) {
  143. case "add":
  144. name = GetAttribute (attributes, "name", true, child);
  145. value = GetAttribute (attributes, "value", false, child);
  146. newNodes[name] = AsString (value);
  147. break;
  148. case "remove":
  149. name = GetAttribute (attributes, "name", true, child);
  150. newNodes.Remove (name);
  151. break;
  152. case "clear":
  153. newNodes.Clear ();
  154. break;
  155. default:
  156. ThrowUnrecognizedElement (child);
  157. break;
  158. }
  159. ValidateInvalidAttributes (attributes, child);
  160. }
  161. else
  162. ThrowUnrecognizedNode (child);
  163. }
  164. d [node.Name] = newNodes;
  165. }
  166. private void AddTraceNode (IDictionary d, XmlNode node)
  167. {
  168. AddTraceAttributes (d, node);
  169. foreach (XmlNode child in node.ChildNodes) {
  170. XmlNodeType t = child.NodeType;
  171. if (t == XmlNodeType.Whitespace || t == XmlNodeType.Comment)
  172. continue;
  173. if (t == XmlNodeType.Element) {
  174. if (child.Name == "listeners")
  175. AddTraceListeners (child);
  176. else
  177. ThrowUnrecognizedElement (child);
  178. ValidateInvalidAttributes (child.Attributes, child);
  179. }
  180. else
  181. ThrowUnrecognizedNode (child);
  182. }
  183. }
  184. // all attributes are optional
  185. private void AddTraceAttributes (IDictionary d, XmlNode node)
  186. {
  187. XmlAttributeCollection c = node.Attributes;
  188. string autoflush = GetAttribute (c, "autoflush", false, node);
  189. string indentsize = GetAttribute (c, "indentsize", false, node);
  190. ValidateInvalidAttributes (c, node);
  191. if (autoflush != null) {
  192. try {
  193. bool b = bool.Parse (autoflush);
  194. d ["autoflush"] = b;
  195. TraceImpl.AutoFlush = b;
  196. }
  197. catch (Exception e) {
  198. throw new ConfigurationException ("The `autoflush' attribute must be `true' or `false'",
  199. e, node);
  200. }
  201. }
  202. if (indentsize != null) {
  203. try {
  204. int n = int.Parse (indentsize);
  205. d ["indentsize"] = n;
  206. TraceImpl.IndentSize = n;
  207. }
  208. catch (Exception e) {
  209. throw new ConfigurationException ("The `indentsize' attribute must be an integral value.",
  210. e, node);
  211. }
  212. }
  213. }
  214. // only defines "add" and "remove", but "clear" also works
  215. // for add, "name" and "type" are required; initializeData is optional
  216. private void AddTraceListeners (XmlNode listenersNode)
  217. {
  218. // There are no attributes on <listeners/>
  219. ValidateInvalidAttributes (listenersNode.Attributes, listenersNode);
  220. foreach (XmlNode child in listenersNode.ChildNodes) {
  221. XmlNodeType t = child.NodeType;
  222. if (t == XmlNodeType.Whitespace || t == XmlNodeType.Comment)
  223. continue;
  224. if (t == XmlNodeType.Element) {
  225. XmlAttributeCollection attributes = child.Attributes;
  226. string name = null;
  227. string type = null;
  228. string id = null;
  229. switch (child.Name) {
  230. case "add":
  231. name = GetAttribute (attributes, "name", true, child);
  232. type = GetAttribute (attributes, "type", true, child);
  233. id = GetAttribute (attributes, "initializeData", false, child);
  234. AddTraceListener (name, type, id);
  235. break;
  236. case "remove":
  237. name = GetAttribute (attributes, "name", true, child);
  238. RemoveTraceListener (name);
  239. break;
  240. case "clear":
  241. TraceImpl.Listeners.Clear ();
  242. break;
  243. default:
  244. ThrowUnrecognizedElement (child);
  245. break;
  246. }
  247. ValidateInvalidAttributes (attributes, child);
  248. }
  249. else
  250. ThrowUnrecognizedNode (child);
  251. }
  252. }
  253. private void AddTraceListener (string name, string type, string initializeData)
  254. {
  255. Type t = Type.GetType (type);
  256. if (t == null)
  257. throw new ConfigurationException (string.Format ("Invalid Type Specified: {0}", type));
  258. object[] args;
  259. Type[] types;
  260. if (initializeData != null) {
  261. args = new object[] { initializeData };
  262. types = new Type[] { typeof(string) };
  263. }
  264. else {
  265. args = null;
  266. types = new Type[0];
  267. }
  268. System.Reflection.ConstructorInfo ctor = t.GetConstructor (types);
  269. if (ctor == null)
  270. throw new ConfigurationException ("Couldn't find constructor for class " + type);
  271. TraceListener l = (TraceListener) ctor.Invoke (args);
  272. l.Name = name;
  273. TraceImpl.Listeners.Add (l);
  274. }
  275. private void RemoveTraceListener (string name)
  276. {
  277. try {
  278. TraceImpl.Listeners.Remove (name);
  279. }
  280. catch (ArgumentException) {
  281. // The specified listener wasn't in the collection
  282. // Ignore this; .NET does.
  283. }
  284. catch (Exception e) {
  285. throw new ConfigurationException (
  286. string.Format ("Unknown error removing listener: {0}", name),
  287. e);
  288. }
  289. }
  290. private string GetAttribute (XmlAttributeCollection attrs, string attr, bool required, XmlNode node)
  291. {
  292. XmlAttribute a = attrs[attr];
  293. string r = null;
  294. if (a != null) {
  295. r = a.Value;
  296. if (required)
  297. ValidateAttribute (attr, r, node);
  298. attrs.Remove (a);
  299. }
  300. else if (required)
  301. ThrowMissingAttribute (attr, node);
  302. return r;
  303. }
  304. private string AsString (string s)
  305. {
  306. return s == null ? string.Empty : s;
  307. }
  308. private void ValidateAttribute (string attribute, string value, XmlNode node)
  309. {
  310. if (value == null || value.Length == 0)
  311. throw new ConfigurationException (string.Format ("Required attribute `{0}' cannot be empty.", attribute), node);
  312. }
  313. private void ValidateInvalidAttributes (XmlAttributeCollection c, XmlNode node)
  314. {
  315. if (c.Count != 0)
  316. ThrowUnrecognizedAttribute (c[0].Name, node);
  317. }
  318. private void ThrowMissingAttribute (string attribute, XmlNode node)
  319. {
  320. throw new ConfigurationException (string.Format ("Missing required attribute `{0}'.", attribute), node);
  321. }
  322. private void ThrowUnrecognizedNode (XmlNode node)
  323. {
  324. throw new ConfigurationException (
  325. string.Format ("Unrecognized node `{0}'; nodeType={1}", node.Name, node.NodeType),
  326. node);
  327. }
  328. private void ThrowUnrecognizedElement (XmlNode node)
  329. {
  330. throw new ConfigurationException (
  331. string.Format ("Unrecognized element <{0}/>", node.Name),
  332. node);
  333. }
  334. private void ThrowUnrecognizedAttribute (string attribute, XmlNode node)
  335. {
  336. throw new ConfigurationException (
  337. string.Format ("Unrecognized attribute `{0}' on element <{1}/>.", attribute, node.Name),
  338. node);
  339. }
  340. }
  341. #endif
  342. }