Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend breadcrumbs interface with new method that allows to define breadcrumb level #212

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading