summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Diagnostics/LogSwitch.cs
blob: 29d6a1d4e6069ba2eb973a67120448d8175dc843 (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.IO;
using System.Collections;
using System.Runtime.Versioning;
using System.Diagnostics.Contracts;
using System.Diagnostics.CodeAnalysis;

namespace System.Diagnostics
{
    [Serializable]
    internal class LogSwitch
    {
        // ! WARNING ! 
        // If any fields are added/deleted/modified, perform the 
        // same in the EE code (debugdebugger.cpp/debugdebugger.h)
        internal String strName;
        internal String strDescription;
        private LogSwitch ParentSwitch;
        internal volatile LoggingLevels iLevel;
        internal volatile LoggingLevels iOldLevel;

        // ! END WARNING !


        private LogSwitch()
        {
        }

        // Constructs a LogSwitch.  A LogSwitch is used to categorize log messages.
        // 
        // All switches (except for the global LogSwitch) have a parent LogSwitch.
        //
        public LogSwitch(String name, String description, LogSwitch parent)
        {
            if (name != null && name.Length == 0)
                throw new ArgumentOutOfRangeException(nameof(Name), SR.Argument_StringZeroLength);
            Contract.EndContractBlock();

            if ((name != null) && (parent != null))
            {
                strName = name;
                strDescription = description;
                iLevel = LoggingLevels.ErrorLevel;
                iOldLevel = iLevel;
                ParentSwitch = parent;

                Log.m_Hashtable.Add(strName, this);

                // Call into the EE to let it know about the creation of
                // this switch
                Log.AddLogSwitch(this);
            }
            else
                throw new ArgumentNullException((name == null ? nameof(name) : nameof(parent)));
        }

        internal LogSwitch(String name, String description)
        {
            strName = name;
            strDescription = description;
            iLevel = LoggingLevels.ErrorLevel;
            iOldLevel = iLevel;
            ParentSwitch = null;

            Log.m_Hashtable.Add(strName, this);

            // Call into the EE to let it know about the creation of
            // this switch
            Log.AddLogSwitch(this);
        }


        // Get property returns the name of the switch
        public virtual String Name
        {
            get { return strName; }
        }


        // Property to Get/Set the level of log messages which are "on" for the switch.  
        // 
        public virtual LoggingLevels MinimumLevel
        {
            get { return iLevel; }
            set
            {
                iLevel = value;
                iOldLevel = value;
                String strParentName = ParentSwitch != null ? ParentSwitch.Name : "";
                if (Debugger.IsAttached)
                    Log.ModifyLogSwitch((int)iLevel, strName, strParentName);

                Log.InvokeLogSwitchLevelHandlers(this, iLevel);
            }
        }


        // Checks if the given level is "on" for this switch or one of its parents.
        //
        public virtual bool CheckLevel(LoggingLevels level)
        {
            if (iLevel > level)
            {
                // recurse through the list till parent is hit. 
                if (ParentSwitch == null)
                    return false;
                else
                    return ParentSwitch.CheckLevel(level);
            }
            else
                return true;
        }


        // Returns a switch with the particular name, if any.  Returns null if no
        // such switch exists.
        public static LogSwitch GetSwitch(String name)
        {
            return (LogSwitch)Log.m_Hashtable[name];
        }
    }
}