Skip to content

Commit

Permalink
rework logging so exiting the app doesnt freeze
Browse files Browse the repository at this point in the history
  • Loading branch information
rednir committed Mar 16, 2023
1 parent 8a9632c commit daf4882
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/Utils/SkinMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace OsuSkinMixer.Utils;
/// <summary>Base for classes that peform tasks based on a list of <see cref="SkinOption"/>. Provides abstract methods for populating tasks to be peformed on the relevant skin folders.</summary>
public abstract class SkinMachine : IDisposable
{
private const int LOG_SPLIT_CHAR_SIZE = 100000;

protected static byte[] TransparentPngFile => new byte[] {
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x04, 0x00, 0x00, 0x00, 0xB5, 0x1C, 0x0C,
Expand Down Expand Up @@ -57,9 +59,11 @@ protected set

protected IEnumerable<SkinOption> FlattenedBottomLevelOptions => SkinOption.Flatten(SkinOptions).Where(o => o is not ParentSkinOption);

private readonly List<Action> _tasks = new();
private readonly List<StringBuilder> _logBuilders = new();

private readonly StringBuilder _logBuilder = new();
private StringBuilder _currentLogBuilder;

private readonly List<Action> _tasks = new();

private readonly Stopwatch _stopwatch = new();

Expand All @@ -72,7 +76,7 @@ public void Run(CancellationToken cancellationToken)
CancellationToken = cancellationToken;
OriginalElementsCache.Clear();
_tasks.Clear();
_logBuilder.Clear();
_logBuilders.Clear();

Settings.Log("Started skin machine.");
_stopwatch.Reset();
Expand All @@ -99,7 +103,11 @@ public void Run(CancellationToken cancellationToken)
finally
{
Progress = null;
Settings.Log("Logs for skin machine:" + _logBuilder.ToString());
Settings.Log("Logs for skin machine follows:");

_logBuilders.Add(_currentLogBuilder);
foreach (var builder in _logBuilders)
Settings.Log(builder.ToString());
}
}

Expand Down Expand Up @@ -306,10 +314,22 @@ protected void AddFileToOriginalElementsCache(string fullFilePath)

protected void Log(string message)
{
_logBuilder
.AppendLine(_stopwatch.ElapsedMilliseconds.ToString())
_currentLogBuilder ??= new StringBuilder() { Capacity = LOG_SPLIT_CHAR_SIZE };

_currentLogBuilder
.AppendLine()
.Append(_stopwatch.ElapsedMilliseconds)
.Append("ms: ")
.Append(message);

// Funky stuff happens if we print a massive string, so split it.
// We don't print the logs as soon as they arrive as we print a lot of logs at once,
// and Godot flushes the output buffer on program exit, causing a freeze.
if (_currentLogBuilder.Length > LOG_SPLIT_CHAR_SIZE)
{
_logBuilders.Add(_currentLogBuilder);
_currentLogBuilder = null;
}
}

protected static bool CheckIfFileAndOptionMatch(FileInfo file, SkinFileOption fileOption)
Expand Down Expand Up @@ -353,7 +373,7 @@ protected virtual void Dispose(bool disposing)
}

_tasks.Clear();
_logBuilder.Clear();
_logBuilders.Clear();
_disposedValue = true;
}
}
Expand Down

0 comments on commit daf4882

Please sign in to comment.