diff --git a/Runtime/Loggers/ConsoleLoggerAsset.cs b/Runtime/Loggers/ConsoleLoggerAsset.cs index 4d61dfc..f4a824e 100644 --- a/Runtime/Loggers/ConsoleLoggerAsset.cs +++ b/Runtime/Loggers/ConsoleLoggerAsset.cs @@ -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 { @@ -39,5 +43,24 @@ public override void Dispose() { // Nothing to dispose. } + + [InitializeOnLaunchAutopilot] + public static void ResetLoggers() + { + // Reset runtime instances + var loggerAssets = FindObjectsOfType(); + 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(AssetDatabase.GUIDToAssetPath(guid)); + so._logger = null; + } +#endif + } } } diff --git a/Runtime/Loggers/FileLoggerAsset.cs b/Runtime/Loggers/FileLoggerAsset.cs index 34030a4..e4a92e4 100644 --- a/Runtime/Loggers/FileLoggerAsset.cs +++ b/Runtime/Loggers/FileLoggerAsset.cs @@ -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 { @@ -145,5 +149,28 @@ public void Dispose() _disposed = true; } } + + [InitializeOnLaunchAutopilot] + public static void ResetLoggers() + { + // Reset runtime instances + var loggerAssets = FindObjectsOfType(); + 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(AssetDatabase.GUIDToAssetPath(guid)); + so._handler?.Dispose(); + so._handler = null; + so._logger = null; + } +#endif + } } } diff --git a/Tests/Runtime/Loggers/ConsoleLoggerAssetTest.cs b/Tests/Runtime/Loggers/ConsoleLoggerAssetTest.cs index f29687b..b0953a9 100644 --- a/Tests/Runtime/Loggers/ConsoleLoggerAssetTest.cs +++ b/Tests/Runtime/Loggers/ConsoleLoggerAssetTest.cs @@ -4,6 +4,9 @@ using NUnit.Framework; using UnityEngine; using UnityEngine.TestTools; +#if UNITY_EDITOR +using UnityEditor; +#endif namespace DeNA.Anjin.Loggers { @@ -37,5 +40,41 @@ public void FilterLogTypeSpecified( LogAssert.Expect(logType, message); LogAssert.NoUnexpectedReceived(); } + + [Test] + public void ResetLoggers_InstanceOnly_ResetLoggerAsset() + { + var sut = ScriptableObject.CreateInstance(); + 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( + "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)); + } } } diff --git a/Tests/Runtime/Loggers/FileLoggerAssetTest.cs b/Tests/Runtime/Loggers/FileLoggerAssetTest.cs index 883e2cf..9f21b2d 100644 --- a/Tests/Runtime/Loggers/FileLoggerAssetTest.cs +++ b/Tests/Runtime/Loggers/FileLoggerAssetTest.cs @@ -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 @@ -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(); + 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( + "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); + } } } diff --git a/Tests/TestAssets/ConsoleLogger.asset b/Tests/TestAssets/ConsoleLogger.asset new file mode 100644 index 0000000..4864fed --- /dev/null +++ b/Tests/TestAssets/ConsoleLogger.asset @@ -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 diff --git a/Tests/TestAssets/ConsoleLogger.asset.meta b/Tests/TestAssets/ConsoleLogger.asset.meta new file mode 100644 index 0000000..ac2799e --- /dev/null +++ b/Tests/TestAssets/ConsoleLogger.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e454b698282bc4833aa7dffd78ae22b4 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Tests/TestAssets/FileLogger.asset b/Tests/TestAssets/FileLogger.asset new file mode 100644 index 0000000..c37bf31 --- /dev/null +++ b/Tests/TestAssets/FileLogger.asset @@ -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 diff --git a/Tests/TestAssets/FileLogger.asset.meta b/Tests/TestAssets/FileLogger.asset.meta new file mode 100644 index 0000000..24e9798 --- /dev/null +++ b/Tests/TestAssets/FileLogger.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 4915b88d8f09041bba3ab87d99e0bc90 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: