From c5218b500543b60893edb1efb5984f0cb6f68a88 Mon Sep 17 00:00:00 2001 From: kdysput Date: Fri, 26 Apr 2024 19:50:43 +0200 Subject: [PATCH] Extend breadcrumbs interface with new method that allows to define breadcrumb level --- .../Model/Breadcrumbs/BacktraceBreadcrumbs.cs | 28 ++++++++++------- .../BacktraceBreadcrumbsEventHandler.cs | 2 +- .../Breadcrumbs/IBacktraceBreadcrumbs.cs | 1 + .../BacktraceBreadcrumbsTypeTests.cs | 30 +++++++++++++++++++ 4 files changed, 50 insertions(+), 11 deletions(-) diff --git a/Runtime/Model/Breadcrumbs/BacktraceBreadcrumbs.cs b/Runtime/Model/Breadcrumbs/BacktraceBreadcrumbs.cs index 26cdabaa..6193abee 100644 --- a/Runtime/Model/Breadcrumbs/BacktraceBreadcrumbs.cs +++ b/Runtime/Model/Breadcrumbs/BacktraceBreadcrumbs.cs @@ -69,14 +69,15 @@ public bool EnableBreadcrumbs() public bool FromBacktrace(BacktraceReport report) { + const BreadcrumbLevel level = BreadcrumbLevel.System; var type = report.ExceptionTypeReport ? UnityEngineLogLevel.Error : UnityEngineLogLevel.Info; - if (!ShouldLog(type)) + if (!ShouldLog(level, type)) { return false; } return AddBreadcrumbs( report.Message, - BreadcrumbLevel.System, + level, type, null); } @@ -136,25 +137,32 @@ public bool Log(string message, LogType type) return Log(message, type, null); } public bool Log(string message, LogType logType, IDictionary attributes) + { + return Log(message, BreadcrumbLevel.Manual, logType, attributes); + } + + public bool Log(string message, BreadcrumbLevel level, LogType logType, IDictionary attributes) { var type = ConvertLogTypeToLogLevel(logType); - if (!ShouldLog(type)) - { - return false; - } - return AddBreadcrumbs(message, BreadcrumbLevel.Manual, type, attributes); + return AddBreadcrumbs(message, level, type, attributes); } + internal bool AddBreadcrumbs(string message, BreadcrumbLevel level, UnityEngineLogLevel type, IDictionary attributes = null) { - if (!BreadcrumbsLevel.HasFlag((BacktraceBreadcrumbType)level)) + if (!ShouldLog(level, type)) { return false; } return LogManager.Add(message, level, type, attributes); } - internal bool ShouldLog(UnityEngineLogLevel type) + + internal bool ShouldLog(BreadcrumbLevel level, UnityEngineLogLevel type) + { + return ShouldLog((BacktraceBreadcrumbType)level, type); + } + internal bool ShouldLog(BacktraceBreadcrumbType level, UnityEngineLogLevel type) { - if (!BreadcrumbsLevel.HasFlag(BacktraceBreadcrumbType.Manual)) + if (!BreadcrumbsLevel.HasFlag(level)) { return false; } diff --git a/Runtime/Model/Breadcrumbs/BacktraceBreadcrumbsEventHandler.cs b/Runtime/Model/Breadcrumbs/BacktraceBreadcrumbsEventHandler.cs index 7e74c057..99230b11 100644 --- a/Runtime/Model/Breadcrumbs/BacktraceBreadcrumbsEventHandler.cs +++ b/Runtime/Model/Breadcrumbs/BacktraceBreadcrumbsEventHandler.cs @@ -139,7 +139,7 @@ private void Application_focusChanged(bool hasFocus) private void Log(string message, LogType level, BreadcrumbLevel breadcrumbLevel, IDictionary attributes = null) { var type = BacktraceBreadcrumbs.ConvertLogTypeToLogLevel(level); - if (!_breadcrumbs.ShouldLog(type)) + if (!_breadcrumbs.ShouldLog(breadcrumbLevel, type)) { return; } diff --git a/Runtime/Model/Breadcrumbs/IBacktraceBreadcrumbs.cs b/Runtime/Model/Breadcrumbs/IBacktraceBreadcrumbs.cs index 3c559f0f..5689bbff 100644 --- a/Runtime/Model/Breadcrumbs/IBacktraceBreadcrumbs.cs +++ b/Runtime/Model/Breadcrumbs/IBacktraceBreadcrumbs.cs @@ -11,6 +11,7 @@ public interface IBacktraceBreadcrumbs [Obsolete("Please use EnableBreadcrumbs instead. This function will be removed in the future updates")] bool EnableBreadcrumbs(BacktraceBreadcrumbType level, UnityEngineLogLevel unityLogLevel); bool ClearBreadcrumbs(); + bool Log(string message, BreadcrumbLevel level, LogType logType, IDictionary attributes); bool Log(string message, LogType type, IDictionary attributes); bool Log(string message, LogType type); bool Debug(string message); diff --git a/Tests/Runtime/Breadcrumbs/BacktraceBreadcrumbsTypeTests.cs b/Tests/Runtime/Breadcrumbs/BacktraceBreadcrumbsTypeTests.cs index 3d1cd4e3..5efc01e7 100644 --- a/Tests/Runtime/Breadcrumbs/BacktraceBreadcrumbsTypeTests.cs +++ b/Tests/Runtime/Breadcrumbs/BacktraceBreadcrumbsTypeTests.cs @@ -7,6 +7,36 @@ namespace Backtrace.Unity.Tests.Runtime.Breadcrumbs { public class BacktraceBreadcrumbsTypeTests { + + [Test] + public void TestManualLogWithLogLevel_ShouldSuccessfullyAddLog_LogIsStored() + { + const string message = "message"; + var inMemoryBreadcrumbStorage = new BacktraceInMemoryLogManager(); + //anything else than Manual + BreadcrumbLevel breadcrumbLevel = BreadcrumbLevel.System; + UnityEngineLogLevel level = UnityEngineLogLevel.Debug | UnityEngineLogLevel.Error | UnityEngineLogLevel.Fatal | UnityEngineLogLevel.Info | UnityEngineLogLevel.Warning; + var breadcrumbsManager = new BacktraceBreadcrumbs(inMemoryBreadcrumbStorage, BacktraceBreadcrumbType.System, level); + breadcrumbsManager.EnableBreadcrumbs(); + var result = breadcrumbsManager.Log(message, breadcrumbLevel, LogType.Log, null); + Assert.IsTrue(result); + } + + + [Test] + public void TestManualLogWithLogLevel_ShouldDropUnwantedBreadcrumbLevel_ReturnFalse() + { + const string message = "message"; + var inMemoryBreadcrumbStorage = new BacktraceInMemoryLogManager(); + //anything else than Manual + BreadcrumbLevel breadcrumbLevel = BreadcrumbLevel.Configuration; + UnityEngineLogLevel level = UnityEngineLogLevel.Debug | UnityEngineLogLevel.Error | UnityEngineLogLevel.Fatal | UnityEngineLogLevel.Info | UnityEngineLogLevel.Warning; + var breadcrumbsManager = new BacktraceBreadcrumbs(inMemoryBreadcrumbStorage, BacktraceBreadcrumbType.User, level); + breadcrumbsManager.EnableBreadcrumbs(); + var result = breadcrumbsManager.Log(message, breadcrumbLevel, LogType.Log, null); + Assert.IsFalse(result); + + } [TestCase(LogType.Log)] [TestCase(LogType.Warning)] [TestCase(LogType.Assert)]