Skip to content

Commit

Permalink
Merge pull request #69 from nowsprinting/fix/initialize_logger
Browse files Browse the repository at this point in the history
Crean logger instances when launch autopilot
  • Loading branch information
asurato authored Sep 20, 2024
2 parents d12d2ef + ae1ef4d commit e3ef73f
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Runtime/Loggers/ConsoleLoggerAsset.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Copyright (c) 2023-2024 DeNA Co., Ltd.
// This software is released under the MIT License.

using DeNA.Anjin.Attributes;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace DeNA.Anjin.Loggers
{
Expand Down Expand Up @@ -39,5 +43,24 @@ public override void Dispose()
{
// Nothing to dispose.
}

[InitializeOnLaunchAutopilot]
public static void ResetLoggers()
{
// Reset runtime instances
var loggerAssets = FindObjectsOfType<ConsoleLoggerAsset>();
foreach (var current in loggerAssets)
{
current._logger = null;
}
#if UNITY_EDITOR
// Reset asset files (in Editor only)
foreach (var guid in AssetDatabase.FindAssets($"t:{nameof(ConsoleLoggerAsset)}"))
{
var so = AssetDatabase.LoadAssetAtPath<ConsoleLoggerAsset>(AssetDatabase.GUIDToAssetPath(guid));
so._logger = null;
}
#endif
}
}
}
27 changes: 27 additions & 0 deletions Runtime/Loggers/FileLoggerAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

using System;
using System.IO;
using DeNA.Anjin.Attributes;
using DeNA.Anjin.Settings;
using UnityEngine;
using Object = UnityEngine.Object;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace DeNA.Anjin.Loggers
{
Expand Down Expand Up @@ -145,5 +149,28 @@ public void Dispose()
_disposed = true;
}
}

[InitializeOnLaunchAutopilot]
public static void ResetLoggers()
{
// Reset runtime instances
var loggerAssets = FindObjectsOfType<FileLoggerAsset>();
foreach (var current in loggerAssets)
{
current._handler?.Dispose();
current._handler = null;
current._logger = null;
}
#if UNITY_EDITOR
// Reset asset files (in Editor only)
foreach (var guid in AssetDatabase.FindAssets($"t:{nameof(FileLoggerAsset)}"))
{
var so = AssetDatabase.LoadAssetAtPath<FileLoggerAsset>(AssetDatabase.GUIDToAssetPath(guid));
so._handler?.Dispose();
so._handler = null;
so._logger = null;
}
#endif
}
}
}
39 changes: 39 additions & 0 deletions Tests/Runtime/Loggers/ConsoleLoggerAssetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
#if UNITY_EDITOR
using UnityEditor;
#endif

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

[Test]
public void ResetLoggers_InstanceOnly_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));
}

[Test]
[UnityPlatform(RuntimePlatform.OSXEditor, RuntimePlatform.WindowsEditor, RuntimePlatform.LinuxEditor)]
public void ResetLoggers_FromAssetFile_ResetLoggerAsset()
{
#if UNITY_EDITOR
var sut = AssetDatabase.LoadAssetAtPath<ConsoleLoggerAsset>(
"Packages/com.dena.anjin/Tests/TestAssets/ConsoleLogger.asset");
sut.Logger.Log("Before reset");
sut.Dispose();
Assume.That(sut.Logger.filterLogType, Is.EqualTo(LogType.Warning));
#endif
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));
}
}
}
47 changes: 47 additions & 0 deletions Tests/Runtime/Loggers/FileLoggerAssetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
using Cysharp.Threading.Tasks;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
#if UNITY_EDITOR
using UnityEditor;
#endif

// ReSharper disable MethodHasAsyncOverload

Expand Down Expand Up @@ -229,5 +233,48 @@ public async Task LogException_WithTimestamp_WriteTimestamp()
var actual = File.ReadAllText(path);
Assert.That(actual, Does.Match("^" + TimestampRegex + ".*"));
}

[Test]
public async Task ResetLoggers_InstanceOnly_ResetLoggerAsset()
{
var path = GetOutputPath();
var sut = ScriptableObject.CreateInstance<FileLoggerAsset>();
sut.outputPath = path;
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);
}

[Test]
[UnityPlatform(RuntimePlatform.OSXEditor, RuntimePlatform.WindowsEditor, RuntimePlatform.LinuxEditor)]
public async Task ResetLoggers_FromAssetFile_ResetLoggerAsset()
{
var path = GetOutputPath();
#if UNITY_EDITOR
var sut = AssetDatabase.LoadAssetAtPath<FileLoggerAsset>(
"Packages/com.dena.anjin/Tests/TestAssets/FileLogger.asset");
sut.outputPath = path;
sut.Logger.Log("Before reset");
sut.Dispose();
await Task.Yield();
Assume.That(path, Does.Exist);
#endif
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);
}
}
}
16 changes: 16 additions & 0 deletions Tests/TestAssets/ConsoleLogger.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e5b0f8b0450fb478d95fe8ea52ea83ff, type: 3}
m_Name: ConsoleLogger
m_EditorClassIdentifier:
description:
filterLogType: 2
8 changes: 8 additions & 0 deletions Tests/TestAssets/ConsoleLogger.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Tests/TestAssets/FileLogger.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1efc191684b0450db1298fe7a4c4f1b2, type: 3}
m_Name: FileLogger
m_EditorClassIdentifier:
description:
outputPath: Logs/FileLoggerTest.log
filterLogType: 2
timestamp: 1
8 changes: 8 additions & 0 deletions Tests/TestAssets/FileLogger.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e3ef73f

Please sign in to comment.