Skip to content

Commit

Permalink
Merge pull request #120 from nowsprinting/fix/logger
Browse files Browse the repository at this point in the history
Fix FileLogger doesn't work when Autopilot is stopped and re-run
  • Loading branch information
bo40 authored Nov 27, 2024
2 parents faff502 + 6f004a2 commit 792ef5d
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 177 deletions.
1 change: 0 additions & 1 deletion Runtime/Autopilot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ private void OnDestroy()

_logger?.Log("Destroy Autopilot object");
_dispatcher?.Dispose();
_settings.LoggerAsset?.Dispose();
}

/// <inheritdoc/>
Expand Down
9 changes: 3 additions & 6 deletions Runtime/Loggers/AbstractLoggerAsset.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
// Copyright (c) 2023-2024 DeNA Co., Ltd.
// This software is released under the MIT License.

using System;
using UnityEngine;

namespace DeNA.Anjin.Loggers
{
/// <summary>
/// Abstract logger settings used for autopilot.
/// </summary>
public abstract class AbstractLoggerAsset : ScriptableObject, IDisposable
public abstract class AbstractLoggerAsset : ScriptableObject
{
#if UNITY_EDITOR
/// <summary>
/// Description about this agent instance.
/// </summary>
[Multiline] public string description;
[Multiline]
public string description;
#endif

/// <summary>
/// Logger implementation used for autopilot.
/// </summary>
public abstract ILogger Logger { get; }

/// <inheritdoc />
public abstract void Dispose();
}
}
17 changes: 1 addition & 16 deletions Runtime/Loggers/CompositeLoggerAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ public override ILogger Logger
}
}

/// <inheritdoc />
public override void Dispose()
{
_handler?.Dispose();
}

private class CompositeLogHandler : ILogHandler, IDisposable
private class CompositeLogHandler : ILogHandler
{
private readonly List<AbstractLoggerAsset> _loggerAssets;
private readonly AbstractLoggerAsset _owner;
Expand Down Expand Up @@ -73,15 +67,6 @@ public void LogException(Exception exception, Object context)
loggerAsset.Logger.LogException(exception, context);
}
}

/// <inheritdoc />
public void Dispose()
{
foreach (var loggerAsset in _loggerAssets.Where(x => x != null && x != _owner))
{
loggerAsset.Dispose();
}
}
}
}
}
18 changes: 0 additions & 18 deletions Runtime/Loggers/ConsoleLoggerAsset.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) 2023-2024 DeNA Co., Ltd.
// This software is released under the MIT License.

using DeNA.Anjin.Attributes;
using UnityEngine;

namespace DeNA.Anjin.Loggers
Expand Down Expand Up @@ -34,22 +33,5 @@ public override ILogger Logger
return _logger;
}
}

/// <inheritdoc />
public override void Dispose()
{
// Nothing to dispose.
}

[InitializeOnLaunchAutopilot(InitializeOnLaunchAutopilotAttribute.InitializeLoggerOrder)]
public static void ResetLoggers()
{
// Reset runtime instances
var loggerAssets = FindObjectsOfType<ConsoleLoggerAsset>();
foreach (var current in loggerAssets)
{
current._logger = null;
}
}
}
}
12 changes: 4 additions & 8 deletions Runtime/Loggers/FileLoggerAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public class FileLoggerAsset : AbstractLoggerAsset
/// </summary>
public bool timestamp = true;

private FileLogHandler _handler;
#pragma warning disable IDISP006
private FileLogHandler _handler; // Note: dispose in ResetLoggers
#pragma warning restore IDISP006
private ILogger _logger;

/// <inheritdoc />
Expand All @@ -66,12 +68,6 @@ public override ILogger Logger
}
}

/// <inheritdoc />
public override void Dispose()
{
_handler?.Dispose();
}

private class TimestampCache
{
private string _timestampCache;
Expand Down Expand Up @@ -148,7 +144,7 @@ public void Dispose()
public static void ResetLoggers()
{
// Reset runtime instances
var loggerAssets = FindObjectsOfType<FileLoggerAsset>();
var loggerAssets = Resources.FindObjectsOfTypeAll<FileLoggerAsset>();
foreach (var current in loggerAssets)
{
current._handler?.Dispose();
Expand Down
47 changes: 0 additions & 47 deletions Tests/Runtime/Loggers/CompositeLoggerAssetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,52 +113,5 @@ public void LogException_NestingLogger_IgnoreNested()

Assert.That(logger2.Logs, Does.Contain((LogType.Exception, exception.ToString())));
}

[Test]
public void Dispose_CompositeMultipleLoggers_DisposeAllLoggers()
{
var logger1 = ScriptableObject.CreateInstance<SpyLoggerAsset>();
var logger2 = ScriptableObject.CreateInstance<SpyLoggerAsset>();
var sut = ScriptableObject.CreateInstance<CompositeLoggerAsset>();
sut.loggerAssets.Add(logger1);
sut.loggerAssets.Add(logger2);

var message = TestContext.CurrentContext.Test.Name;
sut.Logger.Log(message);
sut.Dispose();

Assert.That(logger1.Disposed, Is.True);
Assert.That(logger2.Disposed, Is.True);
}

[Test]
public void Dispose_LoggersIncludesNull_IgnoreNull()
{
var logger2 = ScriptableObject.CreateInstance<SpyLoggerAsset>();
var sut = ScriptableObject.CreateInstance<CompositeLoggerAsset>();
sut.loggerAssets.Add(null);
sut.loggerAssets.Add(logger2);

var message = TestContext.CurrentContext.Test.Name;
sut.Logger.Log(message);
sut.Dispose();

Assert.That(logger2.Disposed, Is.True);
}

[Test]
public void Dispose_NestingLogger_IgnoreNested()
{
var logger2 = ScriptableObject.CreateInstance<SpyLoggerAsset>();
var sut = ScriptableObject.CreateInstance<CompositeLoggerAsset>();
sut.loggerAssets.Add(sut); // nesting
sut.loggerAssets.Add(logger2);

var message = TestContext.CurrentContext.Test.Name;
sut.Logger.Log(message);
sut.Dispose();

Assert.That(logger2.Disposed, Is.True);
}
}
}
20 changes: 0 additions & 20 deletions Tests/Runtime/Loggers/ConsoleLoggerAssetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace DeNA.Anjin.Loggers
{
Expand Down Expand Up @@ -40,22 +37,5 @@ public void FilterLogTypeSpecified(
LogAssert.Expect(logType, message);
LogAssert.NoUnexpectedReceived();
}

[Test]
public void ResetLoggers_ResetLoggerAsset()
{
var sut = ScriptableObject.CreateInstance<ConsoleLoggerAsset>();
sut.filterLogType = LogType.Warning;
sut.Logger.Log("Before reset");
sut.Dispose();
Assume.That(sut.Logger.filterLogType, Is.EqualTo(LogType.Warning));

ConsoleLoggerAsset.ResetLoggers(); // Called when on launch autopilot

sut.filterLogType = LogType.Error;
sut.Logger.Log("After reset");
sut.Dispose();
Assert.That(sut.Logger.filterLogType, Is.EqualTo(LogType.Error));
}
}
}
59 changes: 5 additions & 54 deletions Tests/Runtime/Loggers/FileLoggerAssetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public async Task GetLogger_DirectoryDoesNotExist_CreateDirectoryAndWriteToFile(
sut.timestamp = false;

sut.Logger.Log(message);
sut.Dispose();
await Task.Yield();

var actual = File.ReadAllText(path);
Expand All @@ -85,7 +84,6 @@ public async Task GetLogger_FileExists_OverwrittenToFile()
sut.timestamp = false;

sut.Logger.Log(message);
sut.Dispose();
await Task.Yield();

var actual = File.ReadAllText(path);
Expand Down Expand Up @@ -114,7 +112,6 @@ public async Task GetLogger_OutputPathIsAbsolutePath_WriteToFile()
sut.timestamp = false;

sut.Logger.Log(message);
sut.Dispose();
await Task.Yield();

var actual = File.ReadAllText(path);
Expand All @@ -135,28 +132,6 @@ public async Task LogFormat_WriteToFile()

sut.Logger.Log(message);
sut.Logger.Log(message);
sut.Dispose();
await Task.Yield();

var actual = File.ReadAllText(path);
Assert.That(actual, Is.EqualTo(message + Environment.NewLine + message + Environment.NewLine));
}

[Test]
public async Task LogFormat_Disposed_NotWriteToFile()
{
var message = TestContext.CurrentContext.Test.Name;
var path = LogFilePath;
var sut = ScriptableObject.CreateInstance<FileLoggerAsset>();
sut.outputPath = path;
sut.timestamp = false;

sut.Logger.Log(message);
sut.Logger.Log(message);
sut.Dispose();
await Task.Yield();

sut.Logger.Log(message); // write after disposed
await Task.Yield();

var actual = File.ReadAllText(path);
Expand All @@ -176,7 +151,6 @@ public async Task LogFormat_WithTimestamp_WriteTimestamp()
await UniTask.NextFrame();
sut.Logger.Log(message);
sut.Logger.Log(message); // using cache
sut.Dispose();
await Task.Yield();

var actual = File.ReadAllText(path);
Expand All @@ -199,28 +173,6 @@ public async Task LogException_WriteToFile()

var exception = CreateExceptionWithStacktrace(message);
sut.Logger.LogException(exception);
sut.Dispose();
await Task.Yield();

var actual = File.ReadAllText(path);
Assert.That(actual, Is.EqualTo(exception + Environment.NewLine));
}

[Test]
public async Task LogException_Disposed_NotWriteToFile()
{
var message = TestContext.CurrentContext.Test.Name;
var path = LogFilePath;
var sut = ScriptableObject.CreateInstance<FileLoggerAsset>();
sut.outputPath = path;
sut.timestamp = false;

var exception = CreateExceptionWithStacktrace(message);
sut.Logger.LogException(exception);
sut.Dispose();
await Task.Yield();

sut.Logger.LogException(exception); // write after disposed
await Task.Yield();

var actual = File.ReadAllText(path);
Expand All @@ -238,31 +190,30 @@ public async Task LogException_WithTimestamp_WriteTimestamp()

var exception = CreateExceptionWithStacktrace(message);
sut.Logger.LogException(exception);
sut.Dispose();
await Task.Yield();

var actual = File.ReadAllText(path);
Assert.That(actual, Does.Match("^" + TimestampRegex + ".*"));
}

[Test]
public async Task ResetLoggers_ResetLoggerAsset()
public async Task ResetLoggers_ResetLogHandler()
{
var path = LogFilePath;
var sut = ScriptableObject.CreateInstance<FileLoggerAsset>();
sut.outputPath = path;
sut.timestamp = false;
sut.Logger.Log("Before reset");
sut.Dispose();
await Task.Yield();
Assume.That(path, Does.Exist);

File.Delete(path);
FileLoggerAsset.ResetLoggers(); // Called when on launch autopilot

sut.Logger.Log("After reset");
sut.Dispose();
await Task.Yield();
Assert.That(path, Does.Exist);

var actual = File.ReadAllText(path);
Assert.That(actual, Is.EqualTo("After reset" + Environment.NewLine));
}
}
}
7 changes: 0 additions & 7 deletions Tests/Runtime/TestDoubles/SpyLoggerAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace DeNA.Anjin.TestDoubles
{
public class SpyLoggerAsset : AbstractLoggerAsset
{
public bool Disposed { get; private set; }

private SpyLogHandler _handler;
private ILogger _logger;

Expand All @@ -33,11 +31,6 @@ public override ILogger Logger

public List<(LogType logType, string message)> Logs => _handler.Logs;

public override void Dispose()
{
Disposed = true;
}

private class SpyLogHandler : ILogHandler
{
public readonly List<(LogType logType, string message)> Logs = new List<(LogType, string)>();
Expand Down

0 comments on commit 792ef5d

Please sign in to comment.