Switches (Boolean & Trace) should be working now. Behavior of user-provided
[mono.git] / mcs / class / System / System.Diagnostics / Switch.cs
1 //
2 // System.Diagnostics.Switch.cs
3 //
4 // Comments from John R. Hicks <angryjohn69@nc.rr.com> original implementation 
5 // can be found at: /mcs/docs/apidocs/xml/en/System.Diagnostics
6 //
7 // Author:
8 //      John R. Hicks  (angryjohn69@nc.rr.com)
9 //      Jonathan Pryor (jonpryor@vt.edu)
10 //
11 // (C) 2001-2002
12 //
13
14 using System.Collections;
15
16 namespace System.Diagnostics
17 {
18         public abstract class Switch
19         {
20                 private string name = "";
21                 private string description = "";
22                 private int switchSetting = 0;
23
24                 // MS Behavior is that (quoting from MSDN for OnSwitchSettingChanged()):
25                 //              "...It is invoked the first time a switch reads its value from the
26                 //              configuration file..."
27                 // The docs + testing implies two things:
28                 //      1. The value of the switch is not read in from the constructor
29                 //      2. The value is instead read in on the first time get_SwitchSetting is
30                 //              invoked
31                 // Assuming that OnSwitchSettingChanged() is invoked on a .config file
32                 // read and on all changes
33                 //
34                 // Thus, we need to keep track of whether or not switchSetting has been
35                 // initialized.  Using `switchSetting=-1' seems logical, but if someone
36                 // actually wants to use -1 as a switch value that would cause problems.
37                 private bool initialized = false;
38
39                 protected Switch(string displayName, string description)
40                 {
41                         this.name = displayName;
42                         this.description = description;
43                 }
44
45                 public string Description {
46                         get {return description;}
47                 }
48
49                 public string DisplayName {
50                         get {return name;}
51                 }
52
53                 protected int SwitchSetting {
54                         get {
55                                 if (!initialized) {
56                                         initialized = true;
57                                         GetConfigFileSetting ();
58                                         OnSwitchSettingChanged ();
59                                 }
60                                 return switchSetting;
61                         }
62                         set {
63                                 if(switchSetting != value) {
64                                         switchSetting = value;
65                                         OnSwitchSettingChanged();
66                                 }
67                                 initialized = true;
68                         }
69                 }
70
71                 private void GetConfigFileSetting ()
72                 {
73                         // Load up the specified switch
74                         IDictionary d = 
75                                 (IDictionary) DiagnosticsConfiguration.Settings ["switches"];
76                         if (d != null) {
77                                 object o = d [name];
78         try {
79           switchSetting = int.Parse (o.ToString());
80         }
81         catch {
82           switchSetting = 0;
83         }
84                         }
85                 }
86
87                 protected virtual void OnSwitchSettingChanged()
88                 {
89                         // Do nothing.  This is merely provided for derived classes to know when
90                         // the value of SwitchSetting has changed.
91                 }
92         }
93 }
94