1 // ==++==
2 //
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 //
5 // ==--==
6 namespace System.Diagnostics {
7     using System;
8     using System.IO;
9     using System.Collections;
10     using System.Runtime.Versioning;
11     using System.Diagnostics.Contracts;
12     using System.Diagnostics.CodeAnalysis;
13 
14     [Serializable]
15     internal class LogSwitch
16     {
17         // ! WARNING !
18         // If any fields are added/deleted/modified, perform the
19         // same in the EE code (debugdebugger.cpp/debugdebugger.h)
20         internal String strName;
21         internal String strDescription;
22         private LogSwitch ParentSwitch;
23         internal volatile LoggingLevels iLevel;
24         internal volatile LoggingLevels iOldLevel;
25 
26         // ! END WARNING !
27 
28 
LogSwitch()29         private LogSwitch ()
30         {
31         }
32 
33         // Constructs a LogSwitch.  A LogSwitch is used to categorize log messages.
34         //
35         // All switches (except for the global LogSwitch) have a parent LogSwitch.
36         //
37         [System.Security.SecuritySafeCritical]  // auto-generated
LogSwitch(String name, String description, LogSwitch parent)38         public LogSwitch(String name, String description, LogSwitch parent)
39         {
40             if (name != null && name.Length == 0)
41                 throw new ArgumentOutOfRangeException("Name", Environment.GetResourceString("Argument_StringZeroLength"));
42             Contract.EndContractBlock();
43 
44             if ((name != null) && (parent != null))
45             {
46                 strName = name;
47                 strDescription = description;
48                 iLevel = LoggingLevels.ErrorLevel;
49                 iOldLevel = iLevel;
50                 ParentSwitch = parent;
51 
52                 Log.m_Hashtable.Add (strName, this);
53 
54                 // Call into the EE to let it know about the creation of
55                 // this switch
56                 Log.AddLogSwitch (this);
57             }
58             else
59                 throw new ArgumentNullException ((name==null ? "name" : "parent"));
60         }
61 
62         [System.Security.SecuritySafeCritical]  // auto-generated
LogSwitch(String name, String description)63         internal LogSwitch(String name, String description)
64         {
65             strName = name;
66             strDescription = description;
67             iLevel = LoggingLevels.ErrorLevel;
68             iOldLevel = iLevel;
69             ParentSwitch = null;
70 
71             Log.m_Hashtable.Add (strName, this);
72 
73             // Call into the EE to let it know about the creation of
74             // this switch
75             Log.AddLogSwitch (this);
76         }
77 
78 
79         // Get property returns the name of the switch
80         public virtual String Name
81         {
82             get { return strName;}
83         }
84 
85         // Get property returns the description of the switch
86         public virtual String Description
87         {
88             get {return strDescription;}
89         }
90 
91 
92         // Get property returns the parent of the switch
93         public virtual LogSwitch Parent
94         {
95             get { return ParentSwitch; }
96         }
97 
98 
99         // Property to Get/Set the level of log messages which are "on" for the switch.
100         //
101         public  virtual LoggingLevels  MinimumLevel
102         {
103             get { return iLevel; }
104             [System.Security.SecuritySafeCritical]  // auto-generated
105             [ResourceExposure(ResourceScope.None)]
106             [ResourceConsumption(ResourceScope.Process, ResourceScope.Process)]
107             set
108             {
109                 iLevel = value;
110                 iOldLevel = value;
111                 String strParentName = ParentSwitch!=null ? ParentSwitch.Name : "";
112                 if (Debugger.IsAttached)
113                     Log.ModifyLogSwitch ((int)iLevel, strName, strParentName);
114 
115                 Log.InvokeLogSwitchLevelHandlers (this, iLevel);
116             }
117         }
118 
119 
120         // Checks if the given level is "on" for this switch or one of its parents.
121         //
CheckLevel(LoggingLevels level)122         public virtual bool CheckLevel(LoggingLevels level)
123         {
124             if (iLevel > level)
125             {
126                 // recurse through the list till parent is hit.
127                 if (this.ParentSwitch == null)
128                     return false;
129                 else
130                     return this.ParentSwitch.CheckLevel (level);
131             }
132             else
133                 return true;
134         }
135 
136 
137         // Returns a switch with the particular name, if any.  Returns null if no
138         // such switch exists.
GetSwitch(String name)139         public static LogSwitch GetSwitch(String name)
140         {
141             return (LogSwitch)Log.m_Hashtable[name];
142         }
143 
144     }
145 }
146