Skip to content

Commit

Permalink
Extend breadcrumbs interface with new method that allows to define br…
Browse files Browse the repository at this point in the history
…eadcrumb level
  • Loading branch information
konraddysput committed Apr 26, 2024
1 parent f57bae4 commit c5218b5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
28 changes: 18 additions & 10 deletions Runtime/Model/Breadcrumbs/BacktraceBreadcrumbs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -136,25 +137,32 @@ public bool Log(string message, LogType type)
return Log(message, type, null);
}
public bool Log(string message, LogType logType, IDictionary<string, string> attributes)
{
return Log(message, BreadcrumbLevel.Manual, logType, attributes);
}

public bool Log(string message, BreadcrumbLevel level, LogType logType, IDictionary<string, string> 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<string, string> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private void Application_focusChanged(bool hasFocus)
private void Log(string message, LogType level, BreadcrumbLevel breadcrumbLevel, IDictionary<string, string> attributes = null)
{
var type = BacktraceBreadcrumbs.ConvertLogTypeToLogLevel(level);
if (!_breadcrumbs.ShouldLog(type))
if (!_breadcrumbs.ShouldLog(breadcrumbLevel, type))
{
return;
}
Expand Down
1 change: 1 addition & 0 deletions Runtime/Model/Breadcrumbs/IBacktraceBreadcrumbs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> attributes);
bool Log(string message, LogType type, IDictionary<string, string> attributes);
bool Log(string message, LogType type);
bool Debug(string message);
Expand Down
30 changes: 30 additions & 0 deletions Tests/Runtime/Breadcrumbs/BacktraceBreadcrumbsTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit c5218b5

Please sign in to comment.