Skip to content

Commit

Permalink
Basic implementation of a log janitor so we don't run out of memory
Browse files Browse the repository at this point in the history
  • Loading branch information
EwyBoy committed Mar 4, 2023
1 parent 63d70f5 commit a3f7aae
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
3 changes: 3 additions & 0 deletions SpaceWarp/SpaceWarpPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public sealed class SpaceWarpPlugin : BaseSpaceWarpPlugin
internal ConfigEntry<bool> configShowConsoleButton;
internal ConfigEntry<bool> configShowTimeStamps;
internal ConfigEntry<string> configTimeStampFormat;
internal ConfigEntry<int> configDebugMessageLimit;

internal new ManualLogSource Logger => base.Logger;

Expand All @@ -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));

Expand Down
3 changes: 1 addition & 2 deletions SpaceWarp/UI/SpaceWarpConsole.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using BepInEx.Bootstrap;
using BepInEx.Bootstrap;
using BepInEx.Logging;
using KSP.Game;
using KSP.UI.Binding;
Expand Down
10 changes: 10 additions & 0 deletions SpaceWarp/UI/SpaceWarpConsoleLogListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit a3f7aae

Please sign in to comment.