Skip to content

Commit

Permalink
Merge pull request #67 from nowsprinting/feature/regex_ignore_message
Browse files Browse the repository at this point in the history
Make ignore-messages can be specified by regex
  • Loading branch information
asurato authored Sep 20, 2024
2 parents f1d7941 + 4ee60f3 commit d12d2ef
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Set up a filter to catch abnormal log messages and notify using Reporter.
<dt>Handle Error</dt><dd>Report when error message is detected in log</dd>
<dt>Handle Assert</dt><dd>Report when assert message is detected in log</dd>
<dt>Handle Warning</dt><dd>Report when warning message is detected in log</dd>
<dt>Ignore Messages</dt><dd>Log messages containing this string will not be report</dd>
<dt>Ignore Messages</dt><dd>Log messages containing this string will not be reported. Regex is also available.</dd>
</dl>


Expand Down Expand Up @@ -368,7 +368,7 @@ This Agent instance (.asset file) can contain the following.

<dl>
<dt>Agent</dt><dd>Working Agent. If this Agent exits first, the TimeBombAgent will fail.</dd>
<dt>Defuse Message</dt><dd>Defuse the time bomb when this message comes to the log first. Can specify regex.</dd>
<dt>Defuse Message</dt><dd>Defuse the time bomb when this message comes to the log first. Regex is also available.</dd>
</dl>


Expand Down
2 changes: 1 addition & 1 deletion README_ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ v1.0.0時点では `EmergencyExitAgent` の使用を想定しています。
<dt>handle Error</dt><dd>エラーを検知したらレポータで通知します</dd>
<dt>handle Assert</dt><dd>アサート違反を検知したらレポータで通知します</dd>
<dt>handle Warning</dt><dd>警告を検知したらレポータで通知します</dd>
<dt>Ignore Messages</dt><dd>ここに設定した文字列を含むメッセージはレポータで通知しません</dd>
<dt>Ignore Messages</dt><dd>ここに設定した文字列を含むメッセージはレポータで通知しません。正規表現でも指定できます</dd>
</dl>


Expand Down
40 changes: 34 additions & 6 deletions Runtime/Utilities/LogMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,6 +20,7 @@ public class LogMessageHandler : IDisposable
{
private readonly AutopilotSettings _settings;
private readonly AbstractReporter _reporter;
private List<Regex> _ignoreMessagesRegexes;

/// <summary>
/// Constructor
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)))
Expand All @@ -107,5 +113,27 @@ private static bool IsIgnoreMessage(string logString, string stackTrace, LogType

return false;
}

private static List<Regex> CreateIgnoreMessageRegexes(AutopilotSettings settings)
{
List<Regex> 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;
}
}
}
48 changes: 48 additions & 0 deletions Tests/Runtime/Utilities/LogMessageHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using DeNA.Anjin.TestDoubles;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;

namespace DeNA.Anjin.Utilities
{
Expand Down Expand Up @@ -152,6 +153,53 @@ 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<SpyReporter>();
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<SpyReporter>();
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_InvalidIgnoreMessagePattern_ThrowArgumentExceptionAndReported()
{
var settings = CreateEmptyAutopilotSettings();
var spyReporter = ScriptableObject.CreateInstance<SpyReporter>();
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()
{
Expand Down

0 comments on commit d12d2ef

Please sign in to comment.