Skip to content

Commit f7207a7

Browse files
authored
Extend breadcrumbs interface with new method that allows to define breadcrumb level (#212)
1 parent afa3e05 commit f7207a7

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed

Runtime/Model/Breadcrumbs/BacktraceBreadcrumbs.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,15 @@ public bool EnableBreadcrumbs()
6969

7070
public bool FromBacktrace(BacktraceReport report)
7171
{
72+
const BreadcrumbLevel level = BreadcrumbLevel.System;
7273
var type = report.ExceptionTypeReport ? UnityEngineLogLevel.Error : UnityEngineLogLevel.Info;
73-
if (!ShouldLog(type))
74+
if (!ShouldLog(level, type))
7475
{
7576
return false;
7677
}
7778
return AddBreadcrumbs(
7879
report.Message,
79-
BreadcrumbLevel.System,
80+
level,
8081
type,
8182
null);
8283
}
@@ -136,25 +137,32 @@ public bool Log(string message, LogType type)
136137
return Log(message, type, null);
137138
}
138139
public bool Log(string message, LogType logType, IDictionary<string, string> attributes)
140+
{
141+
return Log(message, BreadcrumbLevel.Manual, logType, attributes);
142+
}
143+
144+
public bool Log(string message, BreadcrumbLevel level, LogType logType, IDictionary<string, string> attributes)
139145
{
140146
var type = ConvertLogTypeToLogLevel(logType);
141-
if (!ShouldLog(type))
142-
{
143-
return false;
144-
}
145-
return AddBreadcrumbs(message, BreadcrumbLevel.Manual, type, attributes);
147+
return AddBreadcrumbs(message, level, type, attributes);
146148
}
149+
147150
internal bool AddBreadcrumbs(string message, BreadcrumbLevel level, UnityEngineLogLevel type, IDictionary<string, string> attributes = null)
148151
{
149-
if (!BreadcrumbsLevel.HasFlag((BacktraceBreadcrumbType)level))
152+
if (!ShouldLog(level, type))
150153
{
151154
return false;
152155
}
153156
return LogManager.Add(message, level, type, attributes);
154157
}
155-
internal bool ShouldLog(UnityEngineLogLevel type)
158+
159+
internal bool ShouldLog(BreadcrumbLevel level, UnityEngineLogLevel type)
160+
{
161+
return ShouldLog((BacktraceBreadcrumbType)level, type);
162+
}
163+
internal bool ShouldLog(BacktraceBreadcrumbType level, UnityEngineLogLevel type)
156164
{
157-
if (!BreadcrumbsLevel.HasFlag(BacktraceBreadcrumbType.Manual))
165+
if (!BreadcrumbsLevel.HasFlag(level))
158166
{
159167
return false;
160168
}

Runtime/Model/Breadcrumbs/BacktraceBreadcrumbsEventHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ private void Application_focusChanged(bool hasFocus)
139139
private void Log(string message, LogType level, BreadcrumbLevel breadcrumbLevel, IDictionary<string, string> attributes = null)
140140
{
141141
var type = BacktraceBreadcrumbs.ConvertLogTypeToLogLevel(level);
142-
if (!_breadcrumbs.ShouldLog(type))
142+
if (!_breadcrumbs.ShouldLog(breadcrumbLevel, type))
143143
{
144144
return;
145145
}

Runtime/Model/Breadcrumbs/IBacktraceBreadcrumbs.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public interface IBacktraceBreadcrumbs
1111
[Obsolete("Please use EnableBreadcrumbs instead. This function will be removed in the future updates")]
1212
bool EnableBreadcrumbs(BacktraceBreadcrumbType level, UnityEngineLogLevel unityLogLevel);
1313
bool ClearBreadcrumbs();
14+
bool Log(string message, BreadcrumbLevel level, LogType logType, IDictionary<string, string> attributes);
1415
bool Log(string message, LogType type, IDictionary<string, string> attributes);
1516
bool Log(string message, LogType type);
1617
bool Debug(string message);

Tests/Runtime/Breadcrumbs/BacktraceBreadcrumbsTypeTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,36 @@ namespace Backtrace.Unity.Tests.Runtime.Breadcrumbs
77
{
88
public class BacktraceBreadcrumbsTypeTests
99
{
10+
11+
[Test]
12+
public void TestManualLogWithLogLevel_ShouldSuccessfullyAddLog_LogIsStored()
13+
{
14+
const string message = "message";
15+
var inMemoryBreadcrumbStorage = new BacktraceInMemoryLogManager();
16+
//anything else than Manual
17+
BreadcrumbLevel breadcrumbLevel = BreadcrumbLevel.System;
18+
UnityEngineLogLevel level = UnityEngineLogLevel.Debug | UnityEngineLogLevel.Error | UnityEngineLogLevel.Fatal | UnityEngineLogLevel.Info | UnityEngineLogLevel.Warning;
19+
var breadcrumbsManager = new BacktraceBreadcrumbs(inMemoryBreadcrumbStorage, BacktraceBreadcrumbType.System, level);
20+
breadcrumbsManager.EnableBreadcrumbs();
21+
var result = breadcrumbsManager.Log(message, breadcrumbLevel, LogType.Log, null);
22+
Assert.IsTrue(result);
23+
}
24+
25+
26+
[Test]
27+
public void TestManualLogWithLogLevel_ShouldDropUnwantedBreadcrumbLevel_ReturnFalse()
28+
{
29+
const string message = "message";
30+
var inMemoryBreadcrumbStorage = new BacktraceInMemoryLogManager();
31+
//anything else than Manual
32+
BreadcrumbLevel breadcrumbLevel = BreadcrumbLevel.Configuration;
33+
UnityEngineLogLevel level = UnityEngineLogLevel.Debug | UnityEngineLogLevel.Error | UnityEngineLogLevel.Fatal | UnityEngineLogLevel.Info | UnityEngineLogLevel.Warning;
34+
var breadcrumbsManager = new BacktraceBreadcrumbs(inMemoryBreadcrumbStorage, BacktraceBreadcrumbType.User, level);
35+
breadcrumbsManager.EnableBreadcrumbs();
36+
var result = breadcrumbsManager.Log(message, breadcrumbLevel, LogType.Log, null);
37+
Assert.IsFalse(result);
38+
39+
}
1040
[TestCase(LogType.Log)]
1141
[TestCase(LogType.Warning)]
1242
[TestCase(LogType.Assert)]

0 commit comments

Comments
 (0)