Description:
More flexible logging, whew.
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
@@ -206,7 +206,8 | |||
|
206 | 206 | }; |
|
207 | 207 | |
|
208 | 208 | AddComponent(contract, new AreaComponent { squares = squares }); |
|
209 |
|
|
|
209 | var nameAndDescription = new NameAndDescriptionComponent { DisplayName = this.grammar.Flatten(message.name) }; | |
|
210 | AddComponent(contract, nameAndDescription); | |
|
210 | 211 | AddComponent(contract, new SelectedComponent { selected = false }); |
|
211 | 212 | AddComponent(contract, new ContractStatusComponent { status = ContractStatus.Proposed, date = this.simulation.DateTime }); |
|
212 | 213 | AddComponent(contract, new TreeDeltaComponent { deltaTrees = deltaTrees }); |
@@ -219,8 +220,8 | |||
|
219 | 220 | AddComponent(contract, new ImageComponent {ImageIndex = image_index}); |
|
220 | 221 | AddComponent(contract, new WindowTypeComponent { type = Window.Contract}); |
|
221 | 222 | AddComponent(contract, new VisibilityComponent { visible = false}); |
|
223 | Logging.Spy(nameAndDescription); | |
|
222 | 224 | return; |
|
223 | ||
|
224 | 225 | } |
|
225 | 226 | else { |
|
226 | 227 | } |
@@ -34,11 +34,10 | |||
|
34 | 34 | public LogLevel level; |
|
35 | 35 | public ITuple data; |
|
36 | 36 | } |
|
37 | ||
|
37 | 38 | public class Logging |
|
38 | 39 | { |
|
39 | 40 | |
|
40 | // private | |
|
41 | // | |
|
42 | 41 | |
|
43 | 42 | #if DEBUG |
|
44 | 43 | public static LogLevel minimumConsoleLevel = LogLevel.Debug; |
@@ -65,7 +64,7 | |||
|
65 | 64 | |
|
66 | 65 | |
|
67 | 66 | private static void Log_(LogLevel level, string message, |
|
68 | int lineNumber, string caller, string path) | |
|
67 | int lineNumber, string caller, string path, ConsoleColor message_foreground = ConsoleColor.White) | |
|
69 | 68 | { |
|
70 | 69 | var timestamp = DateTime.Now; |
|
71 | 70 | |
@@ -82,21 +81,22 | |||
|
82 | 81 | //29/Apr/2021 22:43:30 |
|
83 | 82 | Console.Out.Write(string.Format("[{0}] {1}", timestamp.ToString("s"), level.ToString())); |
|
84 | 83 | |
|
84 | Console.BackgroundColor = start_background; | |
|
85 | Console.ForegroundColor = message_foreground; | |
|
86 | ||
|
87 | Console.Out.Write(" " + message); | |
|
88 | ||
|
85 | 89 | Console.ForegroundColor = start_foreground; |
|
86 | Console.BackgroundColor = start_background; | |
|
87 | 90 | |
|
88 |
Console.Out.WriteLine(string.Format(" |
|
|
91 | Console.Out.WriteLine(string.Format(" [{1}:{2}]", message, path, lineNumber)); | |
|
89 | 92 | } |
|
90 | 93 | |
|
91 | 94 | logFile.WriteLine(string.Format("[{0}] {1} {2} [{3}:{4}]", timestamp.ToString("s"), level.ToString(), message, path, lineNumber)); |
|
92 | 95 | |
|
93 | 96 | |
|
94 | Logging.entries.Add(new LogEntry | |
|
95 | { | |
|
96 | timestamp = timestamp, | |
|
97 | level = level, | |
|
98 | message = message | |
|
99 | }); | |
|
97 | Logging.entries.Add(new LogEntry { timestamp = timestamp, | |
|
98 | level = level, | |
|
99 | message = message}); | |
|
100 | 100 | } |
|
101 | 101 | |
|
102 | 102 | private static void Log_<T>(LogLevel level, string message, T data, |
@@ -250,7 +250,6 | |||
|
250 | 250 | [CallerLineNumber] int lineNumber = 0, |
|
251 | 251 | [CallerMemberName] string caller = null, |
|
252 | 252 | [CallerFilePath] string path = "" |
|
253 | ||
|
254 | 253 | ) where T : class |
|
255 | 254 | { |
|
256 | 255 | // var properties = typeof(T).GetProperties(); |
@@ -270,5 +269,68 | |||
|
270 | 269 | |
|
271 | 270 | Logging.Log_(LogLevel.Spy, message, lineNumber, caller, path); |
|
272 | 271 | } |
|
272 | ||
|
273 | ||
|
274 | public static void Spy<T>(T? value, | |
|
275 | [CallerLineNumber] int lineNumber = 0, | |
|
276 | [CallerMemberName] string caller = null, | |
|
277 | [CallerFilePath] string path = "" | |
|
278 | // , T _ = default | |
|
279 | ) where T : struct | |
|
280 | { | |
|
281 | ||
|
282 | if (value == null) | |
|
283 | { | |
|
284 | Logging.Log_(LogLevel.Spy, "Value is null!", lineNumber, caller, path, ConsoleColor.Red); | |
|
285 | ||
|
286 | return; | |
|
287 | ||
|
288 | } | |
|
289 | ||
|
290 | var properties = new List<string>(); | |
|
291 | ||
|
292 | foreach (var field in typeof(T).GetFields()) | |
|
293 | { | |
|
294 | try { | |
|
295 | properties.Add(field.ToString() + "=" | |
|
296 | + field.GetValue(value).ToString()); | |
|
297 | } | |
|
298 | catch (NullReferenceException e) | |
|
299 | { | |
|
300 | properties.Add(field.ToString() + "= <null>" ); | |
|
301 | } | |
|
302 | } | |
|
303 | ||
|
304 | var message = "{" + String.Join(", ", properties) + "}"; | |
|
305 | ||
|
306 | Logging.Log_(LogLevel.Spy, message, lineNumber, caller, path); | |
|
307 | } | |
|
308 | public static void Spy<T>(T value, | |
|
309 | [CallerLineNumber] int lineNumber = 0, | |
|
310 | [CallerMemberName] string caller = null, | |
|
311 | [CallerFilePath] string path = "", | |
|
312 | T _ = default) where T : struct | |
|
313 | { | |
|
314 | //C/o Jannes on StackOverflow for the extra parameter with a default trick | |
|
315 | //https://stackoverflow.com/questions/2974519/generic-constraints-where-t-struct-and-where-t-class#comment111131939_36775837 | |
|
316 | ||
|
317 | var properties = new List<string>(); | |
|
318 | ||
|
319 | foreach (var field in typeof(T).GetFields()) | |
|
320 | { | |
|
321 | try { | |
|
322 | properties.Add(field.ToString() + "=" | |
|
323 | + field.GetValue(value).ToString()); | |
|
324 | } | |
|
325 | catch (NullReferenceException e) | |
|
326 | { | |
|
327 | properties.Add(field.ToString() + "= <null>" ); | |
|
328 | } | |
|
329 | } | |
|
330 | ||
|
331 | var message = "{" + String.Join(", ", properties) + "}"; | |
|
332 | ||
|
333 | Logging.Log_(LogLevel.Spy, message, lineNumber, caller, path); | |
|
334 | } | |
|
273 | 335 | } |
|
274 | 336 | } |
You need to be logged in to leave comments.
Login now