From 7d418a75fff7d795265307aa7f384c1d7a68d9d6 Mon Sep 17 00:00:00 2001 From: Harrison Date: Sat, 4 Jul 2020 18:57:59 -0500 Subject: [PATCH] Restructured logging in tools. --- .../CommandSystem/CommandProcessor.cs | 2 +- .../Interaction/ConsoleInteraction.cs | 14 ++-------- .../Interaction/IInteractable.cs | 5 ++-- .../Interaction/SingleConsoleInteraction.cs | 9 +----- .../Commands/GraphicalPlaygroundCommand.cs | 16 ++++++++++- .../Playground/ConsoleLogListener.cs | 28 +++++++++++++++++++ 6 files changed, 50 insertions(+), 24 deletions(-) create mode 100644 src/SlatedGameToolkit.Tools/Utilities/Playground/ConsoleLogListener.cs diff --git a/src/SlatedGameToolkit.Tools/CommandSystem/CommandProcessor.cs b/src/SlatedGameToolkit.Tools/CommandSystem/CommandProcessor.cs index 8785405..aac218c 100644 --- a/src/SlatedGameToolkit.Tools/CommandSystem/CommandProcessor.cs +++ b/src/SlatedGameToolkit.Tools/CommandSystem/CommandProcessor.cs @@ -16,7 +16,7 @@ namespace SlatedGameToolkit.Tools.CommandSystem this.commandMap = commands; } public void Process(IInteractable interactable) { - string message = interactable.Listen(); + string message = interactable.Listen().ToLower(); string[] splitMessage = message.Split(' '); string invocation = splitMessage[0]; diff --git a/src/SlatedGameToolkit.Tools/CommandSystem/Interaction/ConsoleInteraction.cs b/src/SlatedGameToolkit.Tools/CommandSystem/Interaction/ConsoleInteraction.cs index 58beec5..2f46e7a 100644 --- a/src/SlatedGameToolkit.Tools/CommandSystem/Interaction/ConsoleInteraction.cs +++ b/src/SlatedGameToolkit.Tools/CommandSystem/Interaction/ConsoleInteraction.cs @@ -3,10 +3,12 @@ using SlatedGameToolkit.Framework.Logging; namespace SlatedGameToolkit.Tools.CommandSystem.Interaction { - public class ConsoleInteraction : IInteractable, ILogListener + public class ConsoleInteraction : IInteractable { private volatile bool listening; + public bool Listening { get {return listening;}} string prefix; + public string Prefix {get {return prefix;}} public bool Debug { get; set; } public LogLevel Level => Debug ? LogLevel.DEBUG : LogLevel.INFO; @@ -33,15 +35,5 @@ namespace SlatedGameToolkit.Tools.CommandSystem.Interaction Console.SetCursorPosition(0, Console.CursorTop); Console.WriteLine(message); } - - public void LogMesesage(string message, DateTime time, LogLevel level) - { - Console.SetCursorPosition(0, Console.CursorTop); - Console.WriteLine(string.Format("Playground [{0}] [{1}]: {2}", level, time.ToString("H:mm:ss"), message)); - if (listening) { - Console.SetCursorPosition(0, Console.CursorTop); - Console.Write(prefix + "> "); - } - } } } \ No newline at end of file diff --git a/src/SlatedGameToolkit.Tools/CommandSystem/Interaction/IInteractable.cs b/src/SlatedGameToolkit.Tools/CommandSystem/Interaction/IInteractable.cs index 3c8a21d..e7b2fe0 100644 --- a/src/SlatedGameToolkit.Tools/CommandSystem/Interaction/IInteractable.cs +++ b/src/SlatedGameToolkit.Tools/CommandSystem/Interaction/IInteractable.cs @@ -1,11 +1,10 @@ -using SlatedGameToolkit.Framework.Logging; - namespace SlatedGameToolkit.Tools.CommandSystem.Interaction { - public interface IInteractable : ILogListener + public interface IInteractable { void Tell(string message); void Separate(); string Listen(); + public bool Listening { get; } } } \ No newline at end of file diff --git a/src/SlatedGameToolkit.Tools/CommandSystem/Interaction/SingleConsoleInteraction.cs b/src/SlatedGameToolkit.Tools/CommandSystem/Interaction/SingleConsoleInteraction.cs index fdc848f..55bcef7 100644 --- a/src/SlatedGameToolkit.Tools/CommandSystem/Interaction/SingleConsoleInteraction.cs +++ b/src/SlatedGameToolkit.Tools/CommandSystem/Interaction/SingleConsoleInteraction.cs @@ -1,5 +1,4 @@ using System; -using SlatedGameToolkit.Framework.Logging; using SlatedGameToolkit.Tools.CommandSystem.Exceptions; namespace SlatedGameToolkit.Tools.CommandSystem.Interaction @@ -12,7 +11,7 @@ namespace SlatedGameToolkit.Tools.CommandSystem.Interaction this.oneTime = oneTime; } - public LogLevel Level => LogLevel.DEBUG; + public bool Listening => false; public string Listen() { @@ -21,12 +20,6 @@ namespace SlatedGameToolkit.Tools.CommandSystem.Interaction return oneTime; } - public void LogMesesage(string message, DateTime time, LogLevel level) - { - Console.SetCursorPosition(0, Console.CursorTop); - Console.WriteLine(string.Format("Game Engine [{0}] [{1}]: \n{2}", level, time.ToString(), message)); - } - public void Separate() { Console.WriteLine(); diff --git a/src/SlatedGameToolkit.Tools/Commands/GraphicalPlaygroundCommand.cs b/src/SlatedGameToolkit.Tools/Commands/GraphicalPlaygroundCommand.cs index 1832297..7d69131 100644 --- a/src/SlatedGameToolkit.Tools/Commands/GraphicalPlaygroundCommand.cs +++ b/src/SlatedGameToolkit.Tools/Commands/GraphicalPlaygroundCommand.cs @@ -10,6 +10,8 @@ namespace SlatedGameToolkit.Tools.Commands public class GraphicalPlaygroundCommand : IInvocable { private readonly string[] invokers = new string[] {"playground"}; + private ConsoleLogListener logListener; + private bool debugging; public bool Execute(IInteractable interactable, string[] args) { @@ -20,7 +22,8 @@ namespace SlatedGameToolkit.Tools.Commands interactable.Tell("Engine is already running!"); return true; } - Logger.AddLogListener(interactable); + Logger.AddLogListener((logListener = new ConsoleLogListener(interactable as ConsoleInteraction))); + logListener.Debug = debugging; GameEngine.Ignite(new MainState()); return true; } else if (args[0].Equals("stop")) { @@ -28,12 +31,23 @@ namespace SlatedGameToolkit.Tools.Commands interactable.Tell("Engine was never running!"); return true; } + Logger.RemoveLogListener(logListener); GameEngine.Stop(); return true; } else if (args[0].Equals("status")) { interactable.Tell("Running: " + GameEngine.IsRunning()); interactable.Tell("Target FPS: " + (GameEngine.targetFPS <= 0 ? "Not bounded." : GameEngine.targetFPS.ToString())); interactable.Tell("Update Step: " + (GameEngine.UpdatesPerSecond <= 0 ? "Not Locked." : GameEngine.UpdatesPerSecond.ToString())); + interactable.Tell("Debug logging: " + logListener.Debug); + return true; + } else if (args[0].Equals("debug")) { + debugging = !debugging; + if (logListener != null) { + logListener.Debug = debugging; + interactable.Tell("Debug logging: " + logListener.Debug); + } else { + interactable.Tell(string.Format("Debug will be turned {0} when playground is started.", debugging ? "on" : "off")); + } return true; } return false; diff --git a/src/SlatedGameToolkit.Tools/Utilities/Playground/ConsoleLogListener.cs b/src/SlatedGameToolkit.Tools/Utilities/Playground/ConsoleLogListener.cs new file mode 100644 index 0000000..d75fda1 --- /dev/null +++ b/src/SlatedGameToolkit.Tools/Utilities/Playground/ConsoleLogListener.cs @@ -0,0 +1,28 @@ +using System; +using SlatedGameToolkit.Framework.Logging; +using SlatedGameToolkit.Tools.CommandSystem.Interaction; + +namespace SlatedGameToolkit.Tools.Utilities.Playground +{ + public class ConsoleLogListener : ILogListener + { + + public bool Debug { get; set; } + public LogLevel Level => Debug ? LogLevel.DEBUG : LogLevel.INFO; + private ConsoleInteraction interaction; + + public ConsoleLogListener(ConsoleInteraction interaction = null) { + this.interaction = interaction; + } + + public void LogMesesage(string message, DateTime time, LogLevel level) + { + Console.SetCursorPosition(0, Console.CursorTop); + Console.WriteLine(string.Format("Playground [{0}] [{1}]: {2}", level, time.ToString("H:mm:ss"), message)); + if (interaction != null && interaction.Listening) { + Console.SetCursorPosition(0, Console.CursorTop); + Console.Write(interaction.Prefix + "> "); + } + } + } +} \ No newline at end of file