From a3f7aaeeb7085493680f8a1f40cfc0b8d7d1bdb4 Mon Sep 17 00:00:00 2001 From: Ewy Date: Sat, 4 Mar 2023 23:06:57 +0100 Subject: [PATCH] Basic implementation of a log janitor so we don't run out of memory --- SpaceWarp/SpaceWarpPlugin.cs | 3 +++ SpaceWarp/UI/SpaceWarpConsole.cs | 3 +-- SpaceWarp/UI/SpaceWarpConsoleLogListener.cs | 10 ++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/SpaceWarp/SpaceWarpPlugin.cs b/SpaceWarp/SpaceWarpPlugin.cs index 6434e772..874b48d5 100644 --- a/SpaceWarp/SpaceWarpPlugin.cs +++ b/SpaceWarp/SpaceWarpPlugin.cs @@ -30,6 +30,7 @@ public sealed class SpaceWarpPlugin : BaseSpaceWarpPlugin internal ConfigEntry configShowConsoleButton; internal ConfigEntry configShowTimeStamps; internal ConfigEntry configTimeStampFormat; + internal ConfigEntry configDebugMessageLimit; internal new ManualLogSource Logger => base.Logger; @@ -53,6 +54,8 @@ public void Awake() "Show time stamps in debug console"); configTimeStampFormat = Config.Bind("Debug Console", "Timestamp Format", "HH:mm:ss.fff", "The format for the timestamps in the debug console."); + configDebugMessageLimit = Config.Bind("Debug Console", "Message Limit", 1000, + "The maximum number of messages to keep in the debug console."); BepInEx.Logging.Logger.Listeners.Add(new SpaceWarpConsoleLogListener(this)); diff --git a/SpaceWarp/UI/SpaceWarpConsole.cs b/SpaceWarp/UI/SpaceWarpConsole.cs index ea7dee8b..8e5c4bfc 100644 --- a/SpaceWarp/UI/SpaceWarpConsole.cs +++ b/SpaceWarp/UI/SpaceWarpConsole.cs @@ -1,5 +1,4 @@ -using System; -using BepInEx.Bootstrap; +using BepInEx.Bootstrap; using BepInEx.Logging; using KSP.Game; using KSP.UI.Binding; diff --git a/SpaceWarp/UI/SpaceWarpConsoleLogListener.cs b/SpaceWarp/UI/SpaceWarpConsoleLogListener.cs index be6d50cf..44cbfbb3 100644 --- a/SpaceWarp/UI/SpaceWarpConsoleLogListener.cs +++ b/SpaceWarp/UI/SpaceWarpConsoleLogListener.cs @@ -18,6 +18,16 @@ public SpaceWarpConsoleLogListener(SpaceWarpPlugin spaceWarpPluginInstance) public void LogEvent(object sender, LogEventArgs eventArgs) { DebugMessages.Add(BuildMessage(TimestampMessage(), eventArgs.Level, eventArgs.Data, eventArgs.Source)); + LogMessageJanitor(); + } + + private void LogMessageJanitor() + { + var configDebugMessageLimit = _spaceWarpPluginInstance.configDebugMessageLimit.Value; + if (DebugMessages.Count > configDebugMessageLimit) + { + DebugMessages.RemoveRange(0, DebugMessages.Count - configDebugMessageLimit); + } } private string TimestampMessage()