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