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 { "None", 0 }, { "Default", 1 },
14 };
15
19 public static readonly BadLogMask None = new BadLogMask(0);
20
24 public static readonly BadLogMask All = new BadLogMask(~0UL);
25
29 public static readonly BadLogMask Default = new BadLogMask(1);
30
31
35 private readonly ulong m_Mask;
36
41 private BadLogMask(ulong mask)
42 {
43 m_Mask = mask;
44 }
45
46
52 public static BadLogMask Register(string name)
53 {
54 ulong value = (ulong)1 << (s_Masks.Count - 1);
55 s_Masks.Add(name, value);
56
57 return new BadLogMask(value);
58 }
59
65 public static implicit operator BadLogMask(ulong mask)
66 {
67 return new BadLogMask(mask);
68 }
69
75 public static implicit operator BadLogMask(string name)
76 {
77 switch (name)
78 {
79 case "None":
80 return None;
81 case "All":
82 return All;
83 }
84
85 return !s_Masks.ContainsKey(name) ? Register(name) : new BadLogMask(s_Masks[name]);
86 }
87
93 private static bool IsPowerOfTwo(ulong number)
94 {
95 if (number == 0)
96 {
97 return false;
98 }
99
100 for (ulong power = 1; power > 0; power <<= 1)
101 {
102 // This for loop used shifting for powers of 2, meaning
103 // that the value will become 0 after the last shift
104 // (from binary 1000...0000 to 0000...0000) then, the 'for'
105 // loop will break out.
106
107 if (power == number)
108 {
109 return true;
110 }
111
112 if (power > number)
113 {
114 return false;
115 }
116 }
117
118 return false;
119 }
120
121
127 public static BadLogMask GetMask(params BadLogMask[] masks)
128 {
129 return masks.Aggregate<BadLogMask, ulong>(0, (current, name) => current | name.m_Mask);
130 }
131
137 public static implicit operator ulong(BadLogMask mask)
138 {
139 return mask.m_Mask;
140 }
141
146 public string[] GetNames()
147 {
148 if (m_Mask == None.m_Mask)
149 {
150 return new[] { "None" };
151 }
152
153 if (m_Mask == All.m_Mask)
154 {
155 return new[] { "All" };
156 }
157
158 if (IsPowerOfTwo(m_Mask))
159 {
160 if (s_Masks.Any(x => x.Value == m_Mask))
161 {
162 return new[]
163 {
164 s_Masks.First(x => x.Value == m_Mask)
165 .Key,
166 };
167 }
168
169 return Array.Empty<string>();
170 }
171
172 List<string> names =
173 (from kvp in s_Masks where kvp.Value != 0 && (kvp.Value & m_Mask) == kvp.Value select kvp.Key).ToList();
174
175 return names.Count == 0 ? Array.Empty<string>() : names.ToArray();
176 }
177
183 public static implicit operator string(BadLogMask mask)
184 {
185 return string.Join(" ", mask.GetNames());
186 }
187
193 public bool IsContainedIn(BadLogMask other)
194 {
195 return (m_Mask & other.m_Mask) == m_Mask;
196 }
197
203 public bool IsExactly(BadLogMask other)
204 {
205 return m_Mask == other.m_Mask;
206 }
207
213 public bool Contains(BadLogMask mask)
214 {
215 return mask.IsContainedIn(this);
216 }
217
222 public override string ToString()
223 {
224 return this;
225 }
226}
Implements a Mask for Log Messages.
Definition BadLogMask.cs:7
static readonly BadLogMask None
Static Helper for the "None" Mask.
Definition BadLogMask.cs:19
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:29
string[] GetNames()
Returns a list of masks that are contained in this mask.
BadLogMask(ulong mask)
Creates a new Log Mask.
Definition BadLogMask.cs:41
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:52
static bool IsPowerOfTwo(ulong number)
Returns true if the specified number is a power of two.
Definition BadLogMask.cs:93
readonly ulong m_Mask
The Mask Flags.
Definition BadLogMask.cs:35
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:24
Contains Logging system for the BadScript Runtime.
Definition BadLog.cs:6