diff --git a/isometric-park-fna/Engines/Spawners/ContractSpawner.cs b/isometric-park-fna/Engines/Spawners/ContractSpawner.cs --- a/isometric-park-fna/Engines/Spawners/ContractSpawner.cs +++ b/isometric-park-fna/Engines/Spawners/ContractSpawner.cs @@ -206,7 +206,8 @@ }; AddComponent(contract, new AreaComponent { squares = squares }); - AddComponent(contract, new NameAndDescriptionComponent { DisplayName = this.grammar.Flatten(message.name) }); + var nameAndDescription = new NameAndDescriptionComponent { DisplayName = this.grammar.Flatten(message.name) }; + AddComponent(contract, nameAndDescription); AddComponent(contract, new SelectedComponent { selected = false }); AddComponent(contract, new ContractStatusComponent { status = ContractStatus.Proposed, date = this.simulation.DateTime }); AddComponent(contract, new TreeDeltaComponent { deltaTrees = deltaTrees }); @@ -219,8 +220,8 @@ AddComponent(contract, new ImageComponent {ImageIndex = image_index}); AddComponent(contract, new WindowTypeComponent { type = Window.Contract}); AddComponent(contract, new VisibilityComponent { visible = false}); + Logging.Spy(nameAndDescription); return; - } else { } diff --git a/isometric-park-fna/Logging.cs b/isometric-park-fna/Logging.cs --- a/isometric-park-fna/Logging.cs +++ b/isometric-park-fna/Logging.cs @@ -34,11 +34,10 @@ public LogLevel level; public ITuple data; } + public class Logging { - // private - // #if DEBUG public static LogLevel minimumConsoleLevel = LogLevel.Debug; @@ -65,7 +64,7 @@ private static void Log_(LogLevel level, string message, - int lineNumber, string caller, string path) + int lineNumber, string caller, string path, ConsoleColor message_foreground = ConsoleColor.White) { var timestamp = DateTime.Now; @@ -82,21 +81,22 @@ //29/Apr/2021 22:43:30 Console.Out.Write(string.Format("[{0}] {1}", timestamp.ToString("s"), level.ToString())); + Console.BackgroundColor = start_background; + Console.ForegroundColor = message_foreground; + + Console.Out.Write(" " + message); + Console.ForegroundColor = start_foreground; - Console.BackgroundColor = start_background; - Console.Out.WriteLine(string.Format(" {0} [{1}:{2}]", message, path, lineNumber)); + Console.Out.WriteLine(string.Format(" [{1}:{2}]", message, path, lineNumber)); } logFile.WriteLine(string.Format("[{0}] {1} {2} [{3}:{4}]", timestamp.ToString("s"), level.ToString(), message, path, lineNumber)); - Logging.entries.Add(new LogEntry - { - timestamp = timestamp, - level = level, - message = message - }); + Logging.entries.Add(new LogEntry { timestamp = timestamp, + level = level, + message = message}); } private static void Log_(LogLevel level, string message, T data, @@ -250,7 +250,6 @@ [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null, [CallerFilePath] string path = "" - ) where T : class { // var properties = typeof(T).GetProperties(); @@ -270,5 +269,68 @@ Logging.Log_(LogLevel.Spy, message, lineNumber, caller, path); } + + + public static void Spy(T? value, + [CallerLineNumber] int lineNumber = 0, + [CallerMemberName] string caller = null, + [CallerFilePath] string path = "" + // , T _ = default + ) where T : struct + { + + if (value == null) + { + Logging.Log_(LogLevel.Spy, "Value is null!", lineNumber, caller, path, ConsoleColor.Red); + + return; + + } + + var properties = new List(); + + foreach (var field in typeof(T).GetFields()) + { + try { + properties.Add(field.ToString() + "=" + + field.GetValue(value).ToString()); + } + catch (NullReferenceException e) + { + properties.Add(field.ToString() + "= " ); + } + } + + var message = "{" + String.Join(", ", properties) + "}"; + + Logging.Log_(LogLevel.Spy, message, lineNumber, caller, path); + } + public static void Spy(T value, + [CallerLineNumber] int lineNumber = 0, + [CallerMemberName] string caller = null, + [CallerFilePath] string path = "", + T _ = default) where T : struct + { + //C/o Jannes on StackOverflow for the extra parameter with a default trick + //https://stackoverflow.com/questions/2974519/generic-constraints-where-t-struct-and-where-t-class#comment111131939_36775837 + + var properties = new List(); + + foreach (var field in typeof(T).GetFields()) + { + try { + properties.Add(field.ToString() + "=" + + field.GetValue(value).ToString()); + } + catch (NullReferenceException e) + { + properties.Add(field.ToString() + "= " ); + } + } + + var message = "{" + String.Join(", ", properties) + "}"; + + Logging.Log_(LogLevel.Spy, message, lineNumber, caller, path); + } } }