Restructured logging in tools.

This commit is contained in:
Harrison Deng 2020-07-04 18:57:59 -05:00
parent b744ae653c
commit 7d418a75ff
6 changed files with 50 additions and 24 deletions

View File

@ -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];

View File

@ -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 + "> ");
}
}
}
}

View File

@ -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; }
}
}

View File

@ -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();

View File

@ -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;

View File

@ -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 + "> ");
}
}
}
}