BadScript 2
Loading...
Searching...
No Matches
BadLogMask.cs
Go to the documentation of this file.
2
6public class BadLogMask
7{
11 private static readonly Dictionary<string, ulong> s_Masks = new Dictionary<string, ulong>
12 {
13 {
14 "None", 0
15 },
16 {
17 "Default", 1
18 },
19 };
20
24 public static readonly BadLogMask None = new BadLogMask(0);
25
29 public static readonly BadLogMask All = new BadLogMask(~0UL);
30
34 public static readonly BadLogMask Default = new BadLogMask(1);
35
36
40 private readonly ulong m_Mask;
41
46 private BadLogMask(ulong mask)
47 {
48 m_Mask = mask;
49 }
50
51
57 public static BadLogMask Register(string name)
58 {
59 ulong value = (ulong)1 << s_Masks.Count - 1;
60 s_Masks.Add(name, value);
61
62 return new BadLogMask(value);
63 }
64
70 public static implicit operator BadLogMask(ulong mask)
71 {
72 return new BadLogMask(mask);
73 }
74
80 public static implicit operator BadLogMask(string name)
81 {
82 switch (name)
83 {
84 case "None":
85 return None;
86 case "All":
87 return All;
88 }
89
90 return !s_Masks.ContainsKey(name) ? Register(name) : new BadLogMask(s_Masks[name]);
91 }
92
98 private static bool IsPowerOfTwo(ulong number)
99 {
100 if (number == 0)
101 {
102 return false;
103 }
104
105 for (ulong power = 1; power > 0; power <<= 1)
106 {
107 // This for loop used shifting for powers of 2, meaning
108 // that the value will become 0 after the last shift
109 // (from binary 1000...0000 to 0000...0000) then, the 'for'
110 // loop will break out.
111
112 if (power == number)
113 {
114 return true;
115 }
116
117 if (power > number)
118 {
119 return false;
120 }
121 }
122
123 return false;
124 }
125
126
132 public static BadLogMask GetMask(params BadLogMask[] masks)
133 {
134 return masks.Aggregate<BadLogMask, ulong>(0, (current, name) => current | name.m_Mask);
135 }
136
142 public static implicit operator ulong(BadLogMask mask)
143 {
144 return mask.m_Mask;
145 }
146
151 public string[] GetNames()
152 {
153 if (m_Mask == None.m_Mask)
154 {
155 return new[]
156 {
157 "None",
158 };
159 }
160
161 if (m_Mask == All.m_Mask)
162 {
163 return new[]
164 {
165 "All",
166 };
167 }
168
169 if (IsPowerOfTwo(m_Mask))
170 {
171 if (s_Masks.Any(x => x.Value == m_Mask))
172 {
173 return new[]
174 {
175 s_Masks.First(x => x.Value == m_Mask).Key,
176 };
177 }
178
179 return Array.Empty<string>();
180 }
181
182 List<string> names = (from kvp in s_Masks where kvp.Value != 0 && (kvp.Value & m_Mask) == kvp.Value select kvp.Key).ToList();
183
184 return names.Count == 0 ? Array.Empty<string>() : names.ToArray();
185 }
186
192 public static implicit operator string(BadLogMask mask)
193 {
194 return string.Join(" ", mask.GetNames());
195 }
196
202 public bool IsContainedIn(BadLogMask other)
203 {
204 return (m_Mask & other.m_Mask) == m_Mask;
205 }
206
212 public bool IsExactly(BadLogMask other)
213 {
214 return m_Mask == other.m_Mask;
215 }
216
222 public bool Contains(BadLogMask mask)
223 {
224 return mask.IsContainedIn(this);
225 }
226
231 public override string ToString()
232 {
233 return this;
234 }
235}
Implements a Mask for Log Messages.
Definition BadLogMask.cs:7
static readonly BadLogMask None
Static Helper for the "None" Mask.
Definition BadLogMask.cs:24
static BadLogMask GetMask(params BadLogMask[] masks)
Returns a combined mask of all masks provided.
static readonly Dictionary< string, ulong > s_Masks
The Masks that are "known".
Definition BadLogMask.cs:11
override string ToString()
Returns string representation of the mask.
static readonly BadLogMask Default
Static Helper for the "Default" Mask.
Definition BadLogMask.cs:34
string[] GetNames()
Returns a list of masks that are contained in this mask.
BadLogMask(ulong mask)
Creates a new Log Mask.
Definition BadLogMask.cs:46
bool Contains(BadLogMask mask)
Returns true if the other mask is contained in this mask.
bool IsExactly(BadLogMask other)
Returns true if this mask is identical to the other mask.
static BadLogMask Register(string name)
Registers a new Mask Name.
Definition BadLogMask.cs:57
static bool IsPowerOfTwo(ulong number)
Returns true if the specified number is a power of two.
Definition BadLogMask.cs:98
readonly ulong m_Mask
The Mask Flags.
Definition BadLogMask.cs:40
bool IsContainedIn(BadLogMask other)
Returns true if this mask is contained in the other mask.
static readonly BadLogMask All
Static Helper for the "All" Mask.
Definition BadLogMask.cs:29
Contains Logging system for the BadScript Runtime.
Definition BadLog.cs:6