Skip to content

Commit a3f3a2e

Browse files
authored
feat: Added BeforeCapture callbacks for screenshots and view hierarchy (#2023)
1 parent 3d626fd commit a3f3a2e

6 files changed

+89
-10
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
### Features
1414

15+
- Added `SetBeforeCaptureScreenshot` and `SetBeforeCaptureViewHierarchy` to the options. Users can now choose whether to capture those as attachment on an individual event basis. ([#2023](https://github.com/getsentry/sentry-unity/pull/2023))
1516
- When capturing events via `Debug.LogError`, the SDK now provides stacktraces. Note, that the SDK is currently not able to provide line numbers for these events. ([#1965](https://github.com/getsentry/sentry-unity/pull/1965))
1617
- Added option to enable/disable automatic capture of `Debug.LogError` as event. ([#2009](https://github.com/getsentry/sentry-unity/pull/2009))
1718
- The `Ignore CLI Errors` checkbox in the Debug Symbols tab now applies to all supported platforms. ([#2008](https://github.com/getsentry/sentry-unity/pull/2008))

src/Sentry.Unity/ScreenshotEventProcessor.cs

+12-3
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,24 @@ public ScreenshotEventProcessor(SentryUnityOptions sentryOptions)
2424
return @event;
2525
}
2626

27-
if (Screen.width == 0 || Screen.height == 0)
27+
if (_options.BeforeCaptureScreenshotInternal?.Invoke() is not false)
2828
{
29-
_options.DiagnosticLogger?.LogWarning("Can't capture screenshots on a screen with a resolution of '{0}x{1}'.", Screen.width, Screen.height);
29+
if (Screen.width == 0 || Screen.height == 0)
30+
{
31+
_options.DiagnosticLogger?.LogWarning("Can't capture screenshots on a screen with a resolution of '{0}x{1}'.", Screen.width, Screen.height);
32+
}
33+
else
34+
{
35+
hint.AddAttachment(CaptureScreenshot(Screen.width, Screen.height), "screenshot.jpg", contentType: "image/jpeg");
36+
}
3037
}
3138
else
3239
{
33-
hint.AddAttachment(CaptureScreenshot(Screen.width, Screen.height), "screenshot.jpg", contentType: "image/jpeg");
40+
_options.DiagnosticLogger?.LogInfo("Screenshot attachment skipped by BeforeAttachScreenshot callback.");
3441
}
3542

43+
44+
3645
return @event;
3746
}
3847

src/Sentry.Unity/SentryUnityOptions.cs

+32
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,38 @@ public sealed class SentryUnityOptions : SentryOptions
228228
/// </summary>
229229
public new StackTraceMode StackTraceMode { get; private set; }
230230

231+
private Func<bool>? _beforeCaptureScreenshot;
232+
233+
internal Func<bool>? BeforeCaptureScreenshotInternal => _beforeCaptureScreenshot;
234+
235+
/// <summary>
236+
/// Configures a callback function to be invoked before capturing and attaching a screenshot to an event.
237+
/// </summary>
238+
/// <remarks>
239+
/// This callback will get invoked right before a screenshot gets taken. If the screenshot should not
240+
/// be taken return `false`.
241+
/// </remarks>
242+
public void SetBeforeCaptureScreenshot(Func<bool> beforeAttachScreenshot)
243+
{
244+
_beforeCaptureScreenshot = beforeAttachScreenshot;
245+
}
246+
247+
private Func<bool>? _beforeCaptureViewHierarchy;
248+
249+
internal Func<bool>? BeforeCaptureViewHierarchyInternal => _beforeCaptureViewHierarchy;
250+
251+
/// <summary>
252+
/// Configures a callback function to be invoked before capturing and attaching the view hierarchy to an event.
253+
/// </summary>
254+
/// <remarks>
255+
/// This callback will get invoked right before the view hierarchy gets taken. If the view hierarchy should not
256+
/// be taken return `false`.
257+
/// </remarks>
258+
public void SetBeforeCaptureViewHierarchy(Func<bool> beforeAttachViewHierarchy)
259+
{
260+
_beforeCaptureViewHierarchy = beforeAttachViewHierarchy;
261+
}
262+
231263
// Initialized by native SDK binding code to set the User.ID in .NET (UnityEventProcessor).
232264
internal string? _defaultUserId;
233265
internal string? DefaultUserId

src/Sentry.Unity/ViewHierarchyEventProcessor.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,18 @@ public ViewHierarchyEventProcessor(SentryUnityOptions sentryOptions)
2626
{
2727
if (!MainThreadData.IsMainThread())
2828
{
29-
_options.DiagnosticLogger?.LogDebug("Can't capture screenshots on other than main (UI) thread.");
29+
_options.DiagnosticLogger?.LogDebug("Can't capture view hierarchy on other than main (UI) thread.");
3030
return @event;
3131
}
3232

33-
hint.AddAttachment(CaptureViewHierarchy(), "view-hierarchy.json", contentType: "application/json");
33+
if (_options.BeforeCaptureViewHierarchyInternal?.Invoke() is not false)
34+
{
35+
hint.AddAttachment(CaptureViewHierarchy(), "view-hierarchy.json", contentType: "application/json");
36+
}
37+
else
38+
{
39+
_options.DiagnosticLogger?.LogInfo("View hierarchy attachment skipped by BeforeAttachViewHierarchy callback.");
40+
}
3441

3542
return @event;
3643
}

test/Sentry.Unity.Tests/ScreenshotEventProcessorTests.cs

+17-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void GetTargetResolution_ReturnsTargetMaxSize(ScreenshotQuality quality,
4040
}
4141

4242
[Test]
43-
public void GetStream_IsMainThread_AddsScreenshotToHint()
43+
public void Process_IsMainThread_AddsScreenshotToHint()
4444
{
4545
var sut = _fixture.GetSut();
4646
var sentryEvent = new SentryEvent();
@@ -52,7 +52,7 @@ public void GetStream_IsMainThread_AddsScreenshotToHint()
5252
}
5353

5454
[Test]
55-
public void GetStream_IsNonMainThread_DoesNotAddScreenshotToHint()
55+
public void Process_IsNonMainThread_DoesNotAddScreenshotToHint()
5656
{
5757
var sut = _fixture.GetSut();
5858
var sentryEvent = new SentryEvent();
@@ -67,6 +67,21 @@ public void GetStream_IsNonMainThread_DoesNotAddScreenshotToHint()
6767
}).Start();
6868
}
6969

70+
[Test]
71+
[TestCase(true)]
72+
[TestCase(false)]
73+
public void Process_BeforeCaptureScreenshotCallbackProvided_RespectsScreenshotCaptureDecision(bool captureScreenshot)
74+
{
75+
_fixture.Options.SetBeforeCaptureScreenshot(() => captureScreenshot);
76+
var sut = _fixture.GetSut();
77+
var sentryEvent = new SentryEvent();
78+
var hint = new SentryHint();
79+
80+
sut.Process(sentryEvent, hint);
81+
82+
Assert.AreEqual(captureScreenshot ? 1 : 0, hint.Attachments.Count);
83+
}
84+
7085
[Test]
7186
[TestCase(ScreenshotQuality.High, 1920)]
7287
[TestCase(ScreenshotQuality.Medium, 1280)]

test/Sentry.Unity.Tests/UnityViewHierarchyEventProcessorTests.cs test/Sentry.Unity.Tests/ViewHierarchyEventProcessorTests.cs

+18-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Sentry.Unity.Tests;
88

9-
public class UnityViewHierarchyEventProcessorTests
9+
public class ViewHierarchyEventProcessorTests
1010
{
1111
private class Fixture
1212
{
@@ -30,7 +30,7 @@ public void TearDown()
3030
}
3131

3232
[Test]
33-
public void GetStream_IsMainThread_AddsViewHierarchyToHint()
33+
public void Process_IsMainThread_AddsViewHierarchyToHint()
3434
{
3535
var sut = _fixture.GetSut();
3636
var sentryEvent = new SentryEvent();
@@ -42,7 +42,7 @@ public void GetStream_IsMainThread_AddsViewHierarchyToHint()
4242
}
4343

4444
[Test]
45-
public void GetStream_IsNonMainThread_DoesNotAddViewHierarchyToHint()
45+
public void Process_IsNonMainThread_DoesNotAddViewHierarchyToHint()
4646
{
4747
var sut = _fixture.GetSut();
4848
var sentryEvent = new SentryEvent();
@@ -58,6 +58,21 @@ public void GetStream_IsNonMainThread_DoesNotAddViewHierarchyToHint()
5858
}).Start();
5959
}
6060

61+
[Test]
62+
[TestCase(true)]
63+
[TestCase(false)]
64+
public void Process_BeforeCaptureViewHierarchyCallbackProvided_RespectViewHierarchyCaptureDecision(bool captureViewHierarchy)
65+
{
66+
_fixture.Options.SetBeforeCaptureViewHierarchy(() => captureViewHierarchy);
67+
var sut = _fixture.GetSut();
68+
var sentryEvent = new SentryEvent();
69+
var hint = new SentryHint();
70+
71+
sut.Process(sentryEvent, hint);
72+
73+
Assert.AreEqual(captureViewHierarchy ? 1 : 0, hint.Attachments.Count);
74+
}
75+
6176
[Test]
6277
public void CaptureViewHierarchy_ReturnsNonNullOrEmptyByteArray()
6378
{

0 commit comments

Comments
 (0)