diff options
author | Jonathan Pryor <jpryor@novell.com> | 2002-12-18 19:01:09 +0300 |
---|---|---|
committer | Jonathan Pryor <jpryor@novell.com> | 2002-12-18 19:01:09 +0300 |
commit | 60d72a0f50aa2294110af41620ae4f6cee078fc6 (patch) | |
tree | 12c0d94fd93bc805798bad9919f393fe46182c2a /mcs/class/System/System.Diagnostics | |
parent | 920f482d9c17fb0be3660878e81f71dfadeed35b (diff) |
Switches (Boolean & Trace) should be working now. Behavior of user-provided
Switch derived classes should match that of .NET.
Go forth and use Swiches! ;-)
ChangeLog:
* BooleanSwitch.cs: Complete re-write. It works now.
* DefaultTraceListener.cs:
- Use `const' strings, so I don't worry about copy/paste errors
- Give `AssertUiEnabled' an actual backing member
* DiagnosticsConfigurationHandler.cs: To avoid race conditions, let the
configuration handler set .config-specified properties on
DefaultTraceListener (AssertUiEnabled, LogFileName) and TraceImpl
(AutoFlush, IndentSize).
* Switch.cs: Near complete re-write. Actually works, and is (should be)
comformant with .NET behavior. Changed member names because they were
confusing me. (Yes, that doesn't say much about my memory.)
* TextWriterTraceListener.cs: Append text to already existing files, don't
overwrite them.
* TraceImpl.cs:
- Added private destructor, to ensure no instances are created.
- Move members declarations to be closer to each other.
* TraceSwitch.cs: Complete re-write. It works now.
svn path=/trunk/mcs/; revision=9747
Diffstat (limited to 'mcs/class/System/System.Diagnostics')
8 files changed, 158 insertions, 122 deletions
diff --git a/mcs/class/System/System.Diagnostics/BooleanSwitch.cs b/mcs/class/System/System.Diagnostics/BooleanSwitch.cs index e4f83292d55..04785c98fa6 100755 --- a/mcs/class/System/System.Diagnostics/BooleanSwitch.cs +++ b/mcs/class/System/System.Diagnostics/BooleanSwitch.cs @@ -3,57 +3,37 @@ // // Author: // John R. Hicks (angryjohn69@nc.rr.com) +// Jonathan Pryor (jonpryor@vt.edu) // -// (C) 2001 +// (C) 2001-2002 // namespace System.Diagnostics { - /// <summary> - /// Provides a simple on/off switch that controls debuggina - /// and tracing output - /// </summary> - public class BooleanSwitch : Switch - { - /// <summary> - /// Initializes a new instance - /// </summary> - public BooleanSwitch(string displayName, string description) - : base(displayName, description) - { - SwitchSetting = (int)BooleanSwitchSetting.False; - } - - // =================== Properties =================== - - /// <summary> - /// Specifies whether the switch is enabled or disabled - /// </summary> - public bool Enabled - { - get - { - if((int)BooleanSwitchSetting.False == SwitchSetting) { - return false; - } - else { - return true; - } - } - set - { - if(value) { - SwitchSetting = (int)BooleanSwitchSetting.True; - } - else { - SwitchSetting = (int)BooleanSwitchSetting.False; - } - } - } - - private enum BooleanSwitchSetting : int { - True = 1, False = 0 + /// <summary> + /// Provides a simple on/off switch that controls debugging + /// and tracing output + /// </summary> + public class BooleanSwitch : Switch + { + /// <summary> + /// Initializes a new instance + /// </summary> + public BooleanSwitch(string displayName, string description) + : base(displayName, description) + { } - } + /// <summary> + /// Specifies whether the switch is enabled or disabled + /// </summary> + public bool Enabled { + // On .NET, any non-zero value is true. Only 0 is false. + get {return SwitchSetting != 0;} + set { + SwitchSetting = Convert.ToInt32(value); + } + } + } } + diff --git a/mcs/class/System/System.Diagnostics/ChangeLog b/mcs/class/System/System.Diagnostics/ChangeLog index 30487a82d0a..11677af764b 100644 --- a/mcs/class/System/System.Diagnostics/ChangeLog +++ b/mcs/class/System/System.Diagnostics/ChangeLog @@ -1,3 +1,22 @@ +2002-12-18 Jonathan Pryor <jonpryor@vt.edu> + * BooleanSwitch.cs: Complete re-write. It works now. + * DefaultTraceListener.cs: + - Use `const' strings, so I don't worry about copy/paste errors + - Give `AssertUiEnabled' an actual backing member + * DiagnosticsConfigurationHandler.cs: To avoid race conditions, let the + configuration handler set .config-specified properties on + DefaultTraceListener (AssertUiEnabled, LogFileName) and TraceImpl + (AutoFlush, IndentSize). + * Switch.cs: Near complete re-write. Actually works, and is (should be) + comformant with .NET behavior. Changed member names because they were + confusing me. (Yes, that doesn't say much about my memory.) + * TextWriterTraceListener.cs: Append text to already existing files, don't + overwrite them. + * TraceImpl.cs: + - Added private destructor, to ensure no instances are created. + - Move members declarations to be closer to each other. + * TraceSwitch.cs: Complete re-write. It works now. + 2002-12-17 Jonathan Pryor <jonpryor@vt.edu> * DiagnosticsConfigurationHandler.cs: Implement so that .config files support <system.diagnostics> sections. diff --git a/mcs/class/System/System.Diagnostics/DefaultTraceListener.cs b/mcs/class/System/System.Diagnostics/DefaultTraceListener.cs index 3a57c17d6e7..4201a82d04d 100644 --- a/mcs/class/System/System.Diagnostics/DefaultTraceListener.cs +++ b/mcs/class/System/System.Diagnostics/DefaultTraceListener.cs @@ -12,6 +12,7 @@ using System; using System.IO; +using System.Collections; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -23,8 +24,8 @@ namespace System.Diagnostics { private static readonly bool OnWin32; - private static readonly string ConsoleOutTrace = "Console.Out"; - private static readonly string ConsoleErrorTrace = "Console.Error"; + private const string ConsoleOutTrace = "Console.Out"; + private const string ConsoleErrorTrace = "Console.Error"; private static readonly string MonoTracePrefix; private static readonly string MonoTraceFile; @@ -105,6 +106,8 @@ namespace System.Diagnostics { private string logFileName = null; + private bool assertUiEnabled = false; + public DefaultTraceListener () : base ("Default") { } @@ -112,7 +115,7 @@ namespace System.Diagnostics { // It's hard to do anything with a UI when we don't have Windows.Forms... [MonoTODO] public bool AssertUiEnabled { - get {return false;} + get {return assertUiEnabled;} set {/* ignore */} } @@ -148,10 +151,10 @@ namespace System.Diagnostics { private void WriteMonoTrace (string message) { switch (MonoTraceFile) { - case "Console.Out": + case ConsoleOutTrace: Console.Out.Write (message); break; - case "Console.Error": + case ConsoleErrorTrace: Console.Error.Write (message); break; default: diff --git a/mcs/class/System/System.Diagnostics/DiagnosticsConfigurationHandler.cs b/mcs/class/System/System.Diagnostics/DiagnosticsConfigurationHandler.cs index 5f102e481de..bcfbefd3fdb 100644 --- a/mcs/class/System/System.Diagnostics/DiagnosticsConfigurationHandler.cs +++ b/mcs/class/System/System.Diagnostics/DiagnosticsConfigurationHandler.cs @@ -97,6 +97,12 @@ namespace System.Diagnostics e, node);
}
d ["logfilename"] = logfilename;
+
+ DefaultTraceListener dtl = (DefaultTraceListener) TraceImpl.Listeners["Default"];
+ if (dtl != null) {
+ dtl.AssertUiEnabled = (bool) d ["assertuienabled"];
+ dtl.LogFileName = logfilename;
+ }
}
// name attribute is required, value is optional
@@ -171,14 +177,18 @@ namespace System.Diagnostics string indentsize = GetAttribute (c, "indentsize", false, node);
ValidateInvalidAttributes (c, node);
try {
- d ["autoflush"] = bool.Parse (autoflush);
+ 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 {
- d ["indentsize"] = int.Parse (indentsize);
+ 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.",
diff --git a/mcs/class/System/System.Diagnostics/Switch.cs b/mcs/class/System/System.Diagnostics/Switch.cs index 7dcec8fd9f7..d154668710a 100755 --- a/mcs/class/System/System.Diagnostics/Switch.cs +++ b/mcs/class/System/System.Diagnostics/Switch.cs @@ -6,55 +6,88 @@ // // Author: // John R. Hicks (angryjohn69@nc.rr.com) +// Jonathan Pryor (jonpryor@vt.edu) // -// (C) 2001 +// (C) 2001-2002 // +using System.Collections; + namespace System.Diagnostics { public abstract class Switch { - private string desc = ""; - private string display_name = ""; - private int iSwitch; + private string name = ""; + private string description = ""; + private int switchSetting = 0; - // ================= Constructors =================== - protected Switch(string displayName, string description) - { - display_name = displayName; - desc = description; - } + // MS Behavior is that (quoting from MSDN for OnSwitchSettingChanged()): + // "...It is invoked the first time a switch reads its value from the + // configuration file..." + // The docs + testing implies two things: + // 1. The value of the switch is not read in from the constructor + // 2. The value is instead read in on the first time get_SwitchSetting is + // invoked + // Assuming that OnSwitchSettingChanged() is invoked on a .config file + // read and on all changes + // + // Thus, we need to keep track of whether or not switchSetting has been + // initialized. Using `switchSetting=-1' seems logical, but if someone + // actually wants to use -1 as a switch value that would cause problems. + private bool initialized = false; - ~Switch() + protected Switch(string displayName, string description) { + this.name = displayName; + this.description = description; } - // ================ Instance Methods ================ - - // ==================== Properties ================== - public string Description { - get {return desc;} + get {return description;} } public string DisplayName { - get {return display_name;} + get {return name;} } protected int SwitchSetting { - get {return iSwitch;} + get { + if (!initialized) { + initialized = true; + GetConfigFileSetting (); + OnSwitchSettingChanged (); + } + return switchSetting; + } set { - if(iSwitch != value) { - iSwitch = value; + if(switchSetting != value) { + switchSetting = value; OnSwitchSettingChanged(); } + initialized = true; + } + } + + private void GetConfigFileSetting () + { + // Load up the specified switch + IDictionary d = + (IDictionary) DiagnosticsConfiguration.Settings ["switches"]; + if (d != null) { + object o = d [name]; + try { + switchSetting = int.Parse (o.ToString()); + } + catch { + switchSetting = 0; + } } } - [MonoTODO] protected virtual void OnSwitchSettingChanged() { - // TODO: implement me + // Do nothing. This is merely provided for derived classes to know when + // the value of SwitchSetting has changed. } } } diff --git a/mcs/class/System/System.Diagnostics/TextWriterTraceListener.cs b/mcs/class/System/System.Diagnostics/TextWriterTraceListener.cs index 8823d8b46b1..45dff28e9c0 100644 --- a/mcs/class/System/System.Diagnostics/TextWriterTraceListener.cs +++ b/mcs/class/System/System.Diagnostics/TextWriterTraceListener.cs @@ -52,7 +52,7 @@ namespace System.Diagnostics { { if (fileName == null) throw new ArgumentNullException ("fileName"); - writer = new StreamWriter (File.OpenWrite (fileName)); + writer = File.AppendText (fileName); } public TextWriterTraceListener (TextWriter writer, string name) diff --git a/mcs/class/System/System.Diagnostics/TraceImpl.cs b/mcs/class/System/System.Diagnostics/TraceImpl.cs index f159f2518c4..623bf2a0c40 100644 --- a/mcs/class/System/System.Diagnostics/TraceImpl.cs +++ b/mcs/class/System/System.Diagnostics/TraceImpl.cs @@ -10,6 +10,7 @@ using System; using System.Diagnostics; +using System.Configuration; namespace System.Diagnostics { @@ -17,16 +18,23 @@ namespace System.Diagnostics { private static object lock_ = new object (); - private static bool autoFlush = false; + private static bool autoFlush; + + [ThreadStatic] + private static int indentLevel = 0; + + [ThreadStatic] + private static int indentSize; + + private TraceImpl () + { + } public static bool AutoFlush { get {return autoFlush;} set {autoFlush = value;} } - [ThreadStatic] - private static int indentLevel = 0; - public static int IndentLevel { get {return indentLevel;} set { @@ -40,9 +48,6 @@ namespace System.Diagnostics { } } - [ThreadStatic] - private static int indentSize = 4; - public static int IndentSize { get {return indentSize;} set { diff --git a/mcs/class/System/System.Diagnostics/TraceSwitch.cs b/mcs/class/System/System.Diagnostics/TraceSwitch.cs index e2404fe77cf..3ddc37141df 100755 --- a/mcs/class/System/System.Diagnostics/TraceSwitch.cs +++ b/mcs/class/System/System.Diagnostics/TraceSwitch.cs @@ -6,68 +6,54 @@ // // Author: // John R. Hicks (angryjohn69@nc.rr.com) +// Jonathan Pryor (jonpryor@vt.edu) // -// (C) 2001 +// (C) 2001-2002 // namespace System.Diagnostics { public class TraceSwitch : Switch { - private TraceLevel level; - private bool traceError = false; - private bool traceInfo = false; - private bool traceVerbose = false; - private bool traceWarning = false; - public TraceSwitch(string displayName, string description) : base(displayName, description) { } - public TraceLevel Level - { - get - { - return level; - } - set - { - level = value; + public TraceLevel Level { + get {return (TraceLevel) SwitchSetting;} + set { + if (!Enum.IsDefined (typeof(TraceLevel), value)) + throw new ArgumentException ("value"); + SwitchSetting = (int) value; } + } + public bool TraceError { + get {return SwitchSetting >= (int) TraceLevel.Error;} } - public bool TraceError - { - get - { - return traceError; - } + public bool TraceWarning { + get {return SwitchSetting >= (int) TraceLevel.Warning;} } - public bool TraceInfo - { - get - { - return traceInfo; - } + public bool TraceInfo { + get {return SwitchSetting >= (int) TraceLevel.Info;} } - public bool TraceVerbose - { - get - { - return traceVerbose; - } + public bool TraceVerbose { + get {return SwitchSetting >= (int) TraceLevel.Verbose;} } - public bool TraceWarning + // .NET accepts values over 4; they're equivalent to TraceLevel.Verbose + // For -1, .NET crashes. (Oops!) Other negative numbers work with an + // equivalent to setting SwitchSetting to TraceLevel.Off. + // The logic for the accessors will cope with values >= 4, so we'll just + // check for negative numbers. + protected override void OnSwitchSettingChanged() { - get - { - return traceWarning; - } + if (SwitchSetting < 0) + SwitchSetting = (int) TraceLevel.Off; } } } |