-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathSentryNativeBridge.m
261 lines (223 loc) · 8.74 KB
/
SentryNativeBridge.m
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#import <Sentry/PrivateSentrySDKOnly.h>
#import <Sentry/SentryOptions+HybridSDKs.h>
#import <Sentry/Sentry.h>
NS_ASSUME_NONNULL_BEGIN
// macOS only
// On iOS, the SDK is linked statically so we don't need to dlopen() it.
int SentryNativeBridgeLoadLibrary() { return 1; }
int SentryNativeBridgeIsEnabled() { return [SentrySDK isEnabled] ? 1 : 0; }
const void *SentryNativeBridgeOptionsNew()
{
NSMutableDictionary *dictOptions = [[NSMutableDictionary alloc] init];
dictOptions[@"sdk"] = @ { @"name" : @"sentry.cocoa.unity" };
dictOptions[@"enableAutoSessionTracking"] = @NO;
dictOptions[@"enableAppHangTracking"] = @NO;
return CFBridgingRetain(dictOptions);
}
void SentryNativeBridgeOptionsSetString(const void *options, const char *name, const char *value)
{
NSMutableDictionary *dictOptions = (__bridge NSMutableDictionary *)options;
dictOptions[[NSString stringWithUTF8String:name]] = [NSString stringWithUTF8String:value];
}
void SentryNativeBridgeOptionsSetInt(const void *options, const char *name, int32_t value)
{
NSMutableDictionary *dictOptions = (__bridge NSMutableDictionary *)options;
dictOptions[[NSString stringWithUTF8String:name]] = [NSNumber numberWithInt:value];
}
int SentryNativeBridgeStartWithOptions(const void *options)
{
NSMutableDictionary *dictOptions = (__bridge_transfer NSMutableDictionary *)options;
NSError *error = nil;
SentryOptions *sentryOptions = [[SentryOptions alloc] initWithDict:dictOptions didFailWithError:&error];
if (error != nil)
{
return 0;
}
[SentrySDK startWithOptions:sentryOptions];
return 1;
}
int SentryNativeBridgeCrashedLastRun() { return [SentrySDK crashedLastRun] ? 1 : 0; }
void SentryNativeBridgeClose() { [SentrySDK close]; }
void SentryNativeBridgeAddBreadcrumb(
const char *timestamp, const char *message, const char *type, const char *category, int level)
{
if (timestamp == NULL && message == NULL && type == NULL && category == NULL) {
return;
}
[SentrySDK configureScope:^(SentryScope *scope) {
SentryBreadcrumb *breadcrumb = [[SentryBreadcrumb alloc]
initWithLevel:level
category:(category ? [NSString stringWithUTF8String:category] : nil)];
if (timestamp != NULL) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:NSCalendarIdentifierISO8601];
breadcrumb.timestamp =
[dateFormatter dateFromString:[NSString stringWithUTF8String:timestamp]];
}
if (message != NULL) {
breadcrumb.message = [NSString stringWithUTF8String:message];
}
if (type != NULL) {
breadcrumb.type = [NSString stringWithUTF8String:type];
}
[scope addBreadcrumb:breadcrumb];
}];
}
void SentryNativeBridgeSetExtra(const char *key, const char *value)
{
if (key == NULL) {
return;
}
[SentrySDK configureScope:^(SentryScope *scope) {
if (value != NULL) {
[scope setExtraValue:[NSString stringWithUTF8String:value]
forKey:[NSString stringWithUTF8String:key]];
} else {
[scope removeExtraForKey:[NSString stringWithUTF8String:key]];
}
}];
}
void SentryNativeBridgeSetTag(const char *key, const char *value)
{
if (key == NULL) {
return;
}
[SentrySDK configureScope:^(SentryScope *scope) {
if (value != NULL) {
[scope setTagValue:[NSString stringWithUTF8String:value]
forKey:[NSString stringWithUTF8String:key]];
} else {
[scope removeTagForKey:[NSString stringWithUTF8String:key]];
}
}];
}
void SentryNativeBridgeUnsetTag(const char *key)
{
if (key == NULL) {
return;
}
[SentrySDK configureScope:^(
SentryScope *scope) { [scope removeTagForKey:[NSString stringWithUTF8String:key]]; }];
}
void SentryNativeBridgeSetUser(
const char *email, const char *userId, const char *ipAddress, const char *username)
{
if (email == NULL && userId == NULL && ipAddress == NULL && username == NULL) {
return;
}
[SentrySDK configureScope:^(SentryScope *scope) {
SentryUser *user = [[SentryUser alloc] init];
if (email != NULL) {
user.email = [NSString stringWithUTF8String:email];
}
if (userId != NULL) {
user.userId = [NSString stringWithUTF8String:userId];
}
if (ipAddress != NULL) {
user.ipAddress = [NSString stringWithUTF8String:ipAddress];
}
if (username != NULL) {
user.username = [NSString stringWithUTF8String:username];
}
[scope setUser:user];
}];
}
void SentryNativeBridgeUnsetUser()
{
[SentrySDK configureScope:^(SentryScope *scope) { [scope setUser:nil]; }];
}
char *SentryNativeBridgeGetInstallationId()
{
// Create a null terminated C string on the heap as expected by marshalling.
// See Tips for iOS in https://docs.unity3d.com/Manual/PluginsForIOS.html
const char *nsStringUtf8 = [[PrivateSentrySDKOnly installationID] UTF8String];
size_t len = strlen(nsStringUtf8) + 1;
char *cString = (char *)malloc(len);
memcpy(cString, nsStringUtf8, len);
return cString;
}
static inline NSString *_NSStringOrNil(const char *value)
{
return value ? [NSString stringWithUTF8String:value] : nil;
}
static inline NSNumber *_NSNumberOrNil(int32_t value)
{
return value == 0 ? nil : @(value);
}
static inline NSNumber *_NSBoolOrNil(int8_t value)
{
if (value == 0) {
return @NO;
}
if (value == 1) {
return @YES;
}
return nil;
}
void SentryNativeBridgeWriteScope( // clang-format off
// // const char *AppStartTime,
// const char *AppBuildType,
// // const char *OperatingSystemRawDescription,
// int DeviceProcessorCount,
// const char *DeviceCpuDescription,
// const char *DeviceTimezone,
// int8_t DeviceSupportsVibration,
// const char *DeviceName,
// int8_t DeviceSimulator,
// const char *DeviceDeviceUniqueIdentifier,
// const char *DeviceDeviceType,
// // const char *DeviceModel,
// // long DeviceMemorySize,
int32_t GpuId,
const char *GpuName,
const char *GpuVendorName,
int32_t GpuMemorySize,
const char *GpuNpotSupport,
const char *GpuVersion,
const char *GpuApiType,
int32_t GpuMaxTextureSize,
int8_t GpuSupportsDrawCallInstancing,
int8_t GpuSupportsRayTracing,
int8_t GpuSupportsComputeShaders,
int8_t GpuSupportsGeometryShaders,
const char *GpuVendorId,
int8_t GpuMultiThreadedRendering,
const char *GpuGraphicsShaderLevel,
const char *EditorVersion,
const char *UnityInstallMode,
const char *UnityTargetFrameRate,
const char *UnityCopyTextureSupport,
const char *UnityRenderingThreadingMode
) // clang-format on
{
// Note: we're using a NSMutableDictionary because it will skip fields with nil values.
[SentrySDK configureScope:^(SentryScope *scope) {
NSMutableDictionary *gpu = [[NSMutableDictionary alloc] init];
gpu[@"id"] = _NSNumberOrNil(GpuId);
gpu[@"name"] = _NSStringOrNil(GpuName);
gpu[@"vendor_name"] = _NSStringOrNil(GpuVendorName);
gpu[@"memory_size"] = _NSNumberOrNil(GpuMemorySize);
gpu[@"npot_support"] = _NSStringOrNil(GpuNpotSupport);
gpu[@"version"] = _NSStringOrNil(GpuVersion);
gpu[@"api_type"] = _NSStringOrNil(GpuApiType);
gpu[@"max_texture_size"] = _NSNumberOrNil(GpuMaxTextureSize);
gpu[@"supports_draw_call_instancing"] = _NSBoolOrNil(GpuSupportsDrawCallInstancing);
gpu[@"supports_ray_tracing"] = _NSBoolOrNil(GpuSupportsRayTracing);
gpu[@"supports_compute_shaders"] = _NSBoolOrNil(GpuSupportsComputeShaders);
gpu[@"supports_geometry_shaders"] = _NSBoolOrNil(GpuSupportsGeometryShaders);
gpu[@"vendor_id"] = _NSStringOrNil(GpuVendorId);
gpu[@"multi_threaded_rendering"] = _NSBoolOrNil(GpuMultiThreadedRendering);
gpu[@"graphics_shader_level"] = _NSStringOrNil(GpuGraphicsShaderLevel);
[scope performSelector:@selector(setContextValue:forKey:) withObject:gpu withObject:@"gpu"];
NSMutableDictionary *unity = [[NSMutableDictionary alloc] init];
unity[@"editor_version"] = _NSStringOrNil(EditorVersion);
unity[@"install_mode"] = _NSStringOrNil(UnityInstallMode);
unity[@"target_frame_rate"] = _NSStringOrNil(UnityTargetFrameRate);
unity[@"copy_texture_support"] = _NSStringOrNil(UnityCopyTextureSupport);
unity[@"rendering_threading_mode"] = _NSStringOrNil(UnityRenderingThreadingMode);
[scope performSelector:@selector(setContextValue:forKey:)
withObject:unity
withObject:@"unity"];
}];
}
NS_ASSUME_NONNULL_END