Bläddra i källkod

DiagnosticsConfigurationHandler.cs:
Don't assume that optional attributes are always present
<assert/> can't have any child nodes
Change in semantics: if the attribute isn't present, GetAttribute()
returns null, not "". This allows us to differentiate between an attribute
not being present and an attribute with an empty value.
Translate exceptions if a TraceListener type is invalid

ChangeLog: Update for the previous changes I forgot to record

svn path=/trunk/mcs/; revision=9806

Jonathan Pryor 23 år sedan
förälder
incheckning
f69bd81f97

+ 28 - 0
mcs/class/System/System.Diagnostics/ChangeLog

@@ -1,3 +1,31 @@
+2002-12-20  Jonathan Pryor <[email protected]>
+	* DiagnosticsConfigurationHandler.cs: 
+	  - Don't assume that optional attributes are always present
+		- <assert/> can't have any child nodes
+		- Change in semantics: if the attribute isn't present, GetAttribute()
+		  returns null, not "".  This allows us to differentiate between an
+		  attribute not being present and an attribute with an empty value.
+		- Translate exceptions if a TraceListener type is invalid
+
+2002-12-19  Jonathan Pryor <[email protected]>
+	* TraceListenerCollection.cs: IndentLevel and IndentSize shouldn't be 
+	  hardcoded; they should be set to whatever TraceImpl is using (which in 
+	  turn may have been set by the .config file, so we should get the 
+	  user-specified values in added listeners).
+	* TraceListener.cs: Make sure that indents are initially written.  This 
+	  allows code that uses Trace.Indent() before a Trace.WriteLine() to be 
+	  indented properly.
+	* TraceImpl.cs: provide a static constructor to explicitly specify the
+	  ordering of initialization, in particular the ordering of 
+	  TraceImpl.Listeners and the reading of the .config file (by accessing
+	  DiagnosticsConfiguration.Settings).  This (hopefully) ensures that the
+	  Listeners collection is initialized before the .config file is read in, as
+	  the DiagnosticsConfigurationHandler will directly modify the listeners
+	  collection.
+	  The DiagnosticsConfigurationHandler assumes this so that it can <add/> and
+	  <remove/> trace listeners and set the logfile for the DefaultTraceListener.
+
+
 2002-12-18  Jonathan Pryor <[email protected]>
 	* BooleanSwitch.cs: Complete re-write.  It works now.
 	* DefaultTraceListener.cs:

+ 61 - 32
mcs/class/System/System.Diagnostics/DiagnosticsConfigurationHandler.cs

@@ -89,20 +89,29 @@ namespace System.Diagnostics
 			string assertuienabled = GetAttribute (c, "assertuienabled", false, node);
 			string logfilename = GetAttribute (c, "logfilename", false, node);
 			ValidateInvalidAttributes (c, node);
-			try {
-				d ["assertuienabled"] = bool.Parse (assertuienabled);
-			}
-			catch (Exception e) {
-				throw new ConfigurationException ("The `assertuienabled' attribute must be `true' or `false'",
-						e, 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);
+				}
 			}
-			d ["logfilename"] = logfilename;
+
+			if (logfilename != null)
+				d ["logfilename"] = logfilename;
 
 			DefaultTraceListener dtl = (DefaultTraceListener) TraceImpl.Listeners["Default"];
 			if (dtl != null) {
-				dtl.AssertUiEnabled = (bool) d ["assertuienabled"];
-				dtl.LogFileName = logfilename;
+				if (assertuienabled != null)
+					dtl.AssertUiEnabled = (bool) d ["assertuienabled"];
+				if (logfilename != null)
+					dtl.LogFileName = logfilename;
 			}
+
+			if (node.ChildNodes.Count > 0)
+				ThrowUnrecognizedElement (node.ChildNodes[0]);
 		}
 
 		// name attribute is required, value is optional
@@ -127,7 +136,7 @@ namespace System.Diagnostics
 						case "add":
 							name = GetAttribute (attributes, "name", true, child);
 							value = GetAttribute (attributes, "value", false, child);
-							newNodes[name] = value;
+							newNodes[name] = AsString (value);
 							break;
 						case "remove":
 							name = GetAttribute (attributes, "name", true, child);
@@ -176,23 +185,27 @@ namespace System.Diagnostics
 			string autoflush = GetAttribute (c, "autoflush", false, node);
 			string indentsize = GetAttribute (c, "indentsize", false, node);
 			ValidateInvalidAttributes (c, node);
-			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);
-			}
-			try {
-				int n = int.Parse (indentsize);
-				d ["indentsize"] = n;
-				TraceImpl.IndentSize = n;
+			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);
+				}
 			}
-			catch (Exception e) {
-				throw new ConfigurationException ("The `indentsize' attribute must be an integral value.",
-						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);
+				}
 			}
 		}
 
@@ -241,12 +254,19 @@ namespace System.Diagnostics
 		{
 			Type t = Type.GetType (type);
 			object[] args = null;
-			if (initializeData == string.Empty)
+			if (initializeData == null)
 				args = new object[]{name};
 			else
 				args = new object[]{initializeData, name};
-			TraceListener l = (TraceListener) Activator.CreateInstance (t, args);
-			TraceImpl.Listeners.Add (l);
+			try {
+				TraceListener l = (TraceListener) Activator.CreateInstance (t, args);
+				TraceImpl.Listeners.Add (l);
+			}
+			catch (Exception e) {
+				throw new ConfigurationException (
+						string.Format ("Invalid Type Specified: {0}", type),
+						e);
+			}
 		}
 
 		private void RemoveTraceListener (string name)
@@ -258,13 +278,18 @@ namespace System.Diagnostics
 				// 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 = string.Empty;
+			string r = null;
 
 			if (a != null) {
 				r = a.Value;
@@ -277,11 +302,15 @@ namespace System.Diagnostics
 
 			return r;
 		}
+		
+		private string AsString (string s)
+		{
+			return s == null ? string.Empty : s;
+		}
 
 		private void ValidateAttribute (string attribute, string value, XmlNode node)
 		{
-			// Don't need to check for null; handled in GetAttribute
-			if (value.Length == 0)
+			if (value == null || value.Length == 0)
 				throw new ConfigurationException (string.Format ("Required attribute `{0}' cannot be empty.", attribute), node);
 		}