-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathSentryNativeCocoa.cs
110 lines (96 loc) · 4.4 KB
/
SentryNativeCocoa.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using Sentry.Extensibility;
using Sentry.Unity.Integrations;
using UnityEngine;
using UnityEngine.Analytics;
namespace Sentry.Unity.iOS;
/// <summary>
/// Access to the Sentry native support on iOS/macOS.
/// </summary>
public static class SentryNativeCocoa
{
/// <summary>
/// Configures the native support.
/// </summary>
/// <param name="options">The Sentry Unity options to use.</param>
/// <param name="sentryUnityInfo">Infos about the current Unity environment</param>
public static void Configure(SentryUnityOptions options, ISentryUnityInfo sentryUnityInfo) =>
Configure(options, sentryUnityInfo, ApplicationAdapter.Instance.Platform);
internal static void Configure(SentryUnityOptions options, ISentryUnityInfo sentryUnityInfo, RuntimePlatform platform)
{
options.DiagnosticLogger?.LogInfo("Attempting to configure native support via the Cocoa SDK");
if (!sentryUnityInfo.IsNativeSupportEnabled(options, platform))
{
options.DiagnosticLogger?.LogDebug("Native support is disabled for: '{0}'", platform);
return;
}
if (platform == RuntimePlatform.IPhonePlayer)
{
if (SentryCocoaBridgeProxy.IsEnabled())
{
options.DiagnosticLogger?.LogDebug("The native SDK is already initialized");
}
else if (!SentryCocoaBridgeProxy.Init(options))
{
options.DiagnosticLogger?.LogWarning("Failed to initialize the native SDK");
return;
}
options.ScopeObserver = new NativeScopeObserver("iOS", options);
}
else
{
if (!SentryCocoaBridgeProxy.Init(options))
{
options.DiagnosticLogger?.LogWarning("Failed to initialize the native SDK");
return;
}
options.ScopeObserver = new NativeScopeObserver("macOS", options);
}
options.NativeContextWriter = new NativeContextWriter();
options.EnableScopeSync = true;
options.CrashedLastRun = () =>
{
var crashedLastRun = SentryCocoaBridgeProxy.CrashedLastRun() == 1;
options.DiagnosticLogger?
.LogDebug("Native SDK reported: 'crashedLastRun': '{0}'", crashedLastRun);
return crashedLastRun;
};
options.NativeSupportCloseCallback += () => Close(options, sentryUnityInfo, platform);
if (sentryUnityInfo.IL2CPP)
{
options.DefaultUserId = SentryCocoaBridgeProxy.GetInstallationId();
if (string.IsNullOrEmpty(options.DefaultUserId))
{
// In case we can't get an installation ID we create one and sync that down to the native layer
options.DiagnosticLogger?.LogDebug("Failed to fetch 'Installation ID' from the native SDK. Creating new 'Default User ID'.");
// We fall back to Unity's Analytics Session Info: https://docs.unity3d.com/ScriptReference/Analytics.AnalyticsSessionInfo-userId.html
// It's a randomly generated GUID that gets created immediately after installation helping
// to identify the same instance of the game
options.DefaultUserId = AnalyticsSessionInfo.userId;
if (options.DefaultUserId is not null)
{
options.ScopeObserver.SetUser(new SentryUser { Id = options.DefaultUserId });
}
else
{
options.DiagnosticLogger?.LogDebug("Failed to create new 'Default User ID'.");
}
}
}
options.DiagnosticLogger?.LogInfo("Successfully configured the native SDK");
}
/// <summary>
/// Closes the native Cocoa support.
/// </summary>
public static void Close(SentryUnityOptions options, ISentryUnityInfo sentryUnityInfo) =>
Close(options, sentryUnityInfo, ApplicationAdapter.Instance.Platform);
internal static void Close(SentryUnityOptions options, ISentryUnityInfo sentryUnityInfo, RuntimePlatform platform)
{
if (!sentryUnityInfo.IsNativeSupportEnabled(options, platform))
{
options.DiagnosticLogger?.LogDebug("Native Support is not enable. Skipping closing the native SDK");
return;
}
options.DiagnosticLogger?.LogDebug("Closing the sentry-cocoa SDK");
SentryCocoaBridgeProxy.Close();
}
}