From 15cfb167d2c321127b9489f22d9b0d2371d37e64 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Tue, 17 Sep 2024 04:40:41 +0900 Subject: [PATCH 1/3] Make ignore-messages can be specified by regex --- README.md | 2 +- README_ja.md | 2 +- Runtime/Utilities/LogMessageHandler.cs | 23 ++++++++++---- .../Utilities/LogMessageHandlerTest.cs | 30 +++++++++++++++++++ 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 388ad65..d0ac860 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ Set up a filter to catch abnormal log messages and notify using Reporter.
Handle Error
Report when error message is detected in log
Handle Assert
Report when assert message is detected in log
Handle Warning
Report when warning message is detected in log
-
Ignore Messages
Log messages containing this string will not be report
+
Ignore Messages
Log messages containing this string will not be report. Can specify regex.
diff --git a/README_ja.md b/README_ja.md index 2d1bee8..91a117b 100644 --- a/README_ja.md +++ b/README_ja.md @@ -131,7 +131,7 @@ v1.0.0時点では `EmergencyExitAgent` の使用を想定しています。
handle Error
エラーを検知したらレポータで通知します
handle Assert
アサート違反を検知したらレポータで通知します
handle Warning
警告を検知したらレポータで通知します
-
Ignore Messages
ここに設定した文字列を含むメッセージはレポータで通知しません
+
Ignore Messages
ここに設定した文字列を含むメッセージはレポータで通知しません。正規表現でも指定できます
diff --git a/Runtime/Utilities/LogMessageHandler.cs b/Runtime/Utilities/LogMessageHandler.cs index c68d292..3d158a1 100644 --- a/Runtime/Utilities/LogMessageHandler.cs +++ b/Runtime/Utilities/LogMessageHandler.cs @@ -2,6 +2,9 @@ // This software is released under the MIT License. using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; using Cysharp.Threading.Tasks; using DeNA.Anjin.Reporters; using DeNA.Anjin.Settings; @@ -17,6 +20,7 @@ public class LogMessageHandler : IDisposable { private readonly AutopilotSettings _settings; private readonly AbstractReporter _reporter; + private List _ignoreMessagesRegexes; /// /// Constructor @@ -63,7 +67,7 @@ public async void HandleLog(string logString, string stackTrace, LogType type) } } - private static bool IsIgnoreMessage(string logString, string stackTrace, LogType type, + private bool IsIgnoreMessage(string logString, string stackTrace, LogType type, AutopilotSettings settings) { if (type == LogType.Log) @@ -91,12 +95,14 @@ private static bool IsIgnoreMessage(string logString, string stackTrace, LogType return true; } - foreach (var ignoreMessage in settings.ignoreMessages) + if (_ignoreMessagesRegexes == null) { - if (logString.Contains(ignoreMessage)) - { - return true; - } + _ignoreMessagesRegexes = CreateIgnoreMessageRegexes(_settings); + } + + if (_ignoreMessagesRegexes.Exists(regex => regex.IsMatch(logString))) + { + return true; } if (stackTrace.Contains(nameof(LogMessageHandler)) || stackTrace.Contains(nameof(SlackAPI))) @@ -107,5 +113,10 @@ private static bool IsIgnoreMessage(string logString, string stackTrace, LogType return false; } + + private static List CreateIgnoreMessageRegexes(AutopilotSettings settings) + { + return settings.ignoreMessages.Select(ignoreMessage => new Regex(ignoreMessage)).ToList(); + } } } diff --git a/Tests/Runtime/Utilities/LogMessageHandlerTest.cs b/Tests/Runtime/Utilities/LogMessageHandlerTest.cs index af45411..c9a49ec 100644 --- a/Tests/Runtime/Utilities/LogMessageHandlerTest.cs +++ b/Tests/Runtime/Utilities/LogMessageHandlerTest.cs @@ -152,6 +152,36 @@ public async Task HandleLog_ContainsIgnoreMessage_notReported() Assert.That(spyReporter.Arguments, Is.Empty); } + [Test] + public async Task HandleLog_MatchIgnoreMessagePattern_notReported() + { + var settings = CreateEmptyAutopilotSettings(); + var spyReporter = ScriptableObject.CreateInstance(); + var sut = new LogMessageHandler(settings, spyReporter); + + settings.ignoreMessages = new[] { "ignore.+ignore" }; + settings.handleException = true; + sut.HandleLog("ignore_xxx_ignore", string.Empty, LogType.Exception); + await UniTask.NextFrame(); + + Assert.That(spyReporter.Arguments, Is.Empty); + } + + [Test] + public async Task HandleLog_NotMatchIgnoreMessagePattern_Reported() + { + var settings = CreateEmptyAutopilotSettings(); + var spyReporter = ScriptableObject.CreateInstance(); + var sut = new LogMessageHandler(settings, spyReporter); + + settings.ignoreMessages = new[] { "ignore.+ignore" }; + settings.handleException = true; + sut.HandleLog("ignore", string.Empty, LogType.Exception); + await UniTask.NextFrame(); + + Assert.That(spyReporter.Arguments, Is.Not.Empty); + } + [Test] public async Task HandleLog_StacktraceByLogMessageHandler_notReported() { From 4877ff22bd7514d13cd79d7fa62b2aee48a57292 Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Thu, 19 Sep 2024 08:52:03 +0900 Subject: [PATCH 2/3] Fix wording --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d0ac860..6f06dee 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ Set up a filter to catch abnormal log messages and notify using Reporter.
Handle Error
Report when error message is detected in log
Handle Assert
Report when assert message is detected in log
Handle Warning
Report when warning message is detected in log
-
Ignore Messages
Log messages containing this string will not be report. Can specify regex.
+
Ignore Messages
Log messages containing this string will not be reported. Regex is also available.
@@ -368,7 +368,7 @@ This Agent instance (.asset file) can contain the following.
Agent
Working Agent. If this Agent exits first, the TimeBombAgent will fail.
-
Defuse Message
Defuse the time bomb when this message comes to the log first. Can specify regex.
+
Defuse Message
Defuse the time bomb when this message comes to the log first. Regex is also available.
From 4ee60f362c20ab7e7740b93ede8a1ddbe9d6daef Mon Sep 17 00:00:00 2001 From: Koji Hasegawa Date: Thu, 19 Sep 2024 09:27:50 +0900 Subject: [PATCH 3/3] Fix invalid regex pattern specified case --- Runtime/Utilities/LogMessageHandler.cs | 19 ++++++++++++++++++- .../Utilities/LogMessageHandlerTest.cs | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/Runtime/Utilities/LogMessageHandler.cs b/Runtime/Utilities/LogMessageHandler.cs index 3d158a1..06f7a68 100644 --- a/Runtime/Utilities/LogMessageHandler.cs +++ b/Runtime/Utilities/LogMessageHandler.cs @@ -116,7 +116,24 @@ private bool IsIgnoreMessage(string logString, string stackTrace, LogType type, private static List CreateIgnoreMessageRegexes(AutopilotSettings settings) { - return settings.ignoreMessages.Select(ignoreMessage => new Regex(ignoreMessage)).ToList(); + List regexs = new(); + foreach (var message in settings.ignoreMessages) + { + Regex pattern; + try + { + pattern = new Regex(message); + } + catch (ArgumentException e) + { + Debug.LogException(e); + continue; + } + + regexs.Add(pattern); + } + + return regexs; } } } diff --git a/Tests/Runtime/Utilities/LogMessageHandlerTest.cs b/Tests/Runtime/Utilities/LogMessageHandlerTest.cs index c9a49ec..71a8342 100644 --- a/Tests/Runtime/Utilities/LogMessageHandlerTest.cs +++ b/Tests/Runtime/Utilities/LogMessageHandlerTest.cs @@ -7,6 +7,7 @@ using DeNA.Anjin.TestDoubles; using NUnit.Framework; using UnityEngine; +using UnityEngine.TestTools; namespace DeNA.Anjin.Utilities { @@ -182,6 +183,23 @@ public async Task HandleLog_NotMatchIgnoreMessagePattern_Reported() Assert.That(spyReporter.Arguments, Is.Not.Empty); } + [Test] + public async Task HandleLog_InvalidIgnoreMessagePattern_ThrowArgumentExceptionAndReported() + { + var settings = CreateEmptyAutopilotSettings(); + var spyReporter = ScriptableObject.CreateInstance(); + var sut = new LogMessageHandler(settings, spyReporter); + + settings.ignoreMessages = new[] { "[a" }; // invalid pattern + settings.handleException = true; + + sut.HandleLog("ignore", string.Empty, LogType.Exception); + await UniTask.NextFrame(); + + LogAssert.Expect(LogType.Exception, "ArgumentException: parsing \"[a\" - Unterminated [] set."); + Assert.That(spyReporter.Arguments, Is.Not.Empty); // Report is executed + } + [Test] public async Task HandleLog_StacktraceByLogMessageHandler_notReported() {