6
6
7
7
NS_ASSUME_NONNULL_BEGIN
8
8
9
+ static NSDateFormatter *_Nullable sentry_cachedISO8601Formatter (void ) {
10
+ static NSDateFormatter *formatter = nil ;
11
+ static dispatch_once_t onceToken;
12
+ dispatch_once (&onceToken, ^{
13
+ formatter = [[NSDateFormatter alloc ] init ];
14
+ formatter.dateFormat = @" yyyy-MM-dd'T'HH:mm:ss'Z'" ;
15
+ formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation: @" UTC" ];
16
+ formatter.locale = [NSLocale localeWithLocaleIdentifier: @" en_US_POSIX" ];
17
+ });
18
+ return formatter;
19
+ }
20
+
21
+ static inline NSString *_NSStringOrNil (const char *value)
22
+ {
23
+ return value ? [NSString stringWithUTF8String: value] : nil ;
24
+ }
25
+
26
+ static inline NSNumber *_NSNumberOrNil (int32_t value)
27
+ {
28
+ return value == 0 ? nil : @(value);
29
+ }
30
+
31
+ static inline NSNumber *_NSBoolOrNil (int8_t value)
32
+ {
33
+ if (value == 0 ) {
34
+ return @NO ;
35
+ }
36
+ if (value == 1 ) {
37
+ return @YES ;
38
+ }
39
+ return nil ;
40
+ }
41
+
9
42
// macOS only
10
43
// On iOS, the SDK is linked statically so we don't need to dlopen() it.
11
44
int SentryNativeBridgeLoadLibrary () { return 1 ; }
@@ -64,24 +97,26 @@ void SentryNativeBridgeAddBreadcrumb(
64
97
return ;
65
98
}
66
99
100
+ NSString *timestampString = _NSStringOrNil (timestamp);
101
+ NSString *messageString = _NSStringOrNil (message);
102
+ NSString *typeString = _NSStringOrNil (type);
103
+ NSString *categoryString = _NSStringOrNil (category) ?: @" default" ; // Category cannot be nil
104
+
67
105
[SentrySDK configureScope: ^(SentryScope *scope) {
68
106
SentryBreadcrumb *breadcrumb = [[SentryBreadcrumb alloc ]
69
107
initWithLevel: level
70
- category: (category ? [ NSString stringWithUTF8String: category] : nil ) ];
108
+ category: categoryString ];
71
109
72
- if (timestamp != NULL ) {
73
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc ] init ];
74
- [dateFormatter setDateFormat: NSCalendarIdentifierISO8601 ];
75
- breadcrumb.timestamp =
76
- [dateFormatter dateFromString: [NSString stringWithUTF8String: timestamp]];
110
+ if (timestampString != nil && timestampString.length > 0 ) {
111
+ breadcrumb.timestamp = [sentry_cachedISO8601Formatter () dateFromString: timestampString];
77
112
}
78
113
79
- if (message != NULL ) {
80
- breadcrumb.message = [ NSString stringWithUTF8String: message] ;
114
+ if (messageString != nil ) {
115
+ breadcrumb.message = messageString ;
81
116
}
82
117
83
- if (type != NULL ) {
84
- breadcrumb.type = [ NSString stringWithUTF8String: type] ;
118
+ if (typeString != nil ) {
119
+ breadcrumb.type = typeString ;
85
120
}
86
121
87
122
[scope addBreadcrumb: breadcrumb];
@@ -94,13 +129,11 @@ void SentryNativeBridgeSetExtra(const char *key, const char *value)
94
129
return ;
95
130
}
96
131
132
+ NSString *keyString = [NSString stringWithUTF8String: key];
133
+ NSString *valueString = _NSStringOrNil (value);
134
+
97
135
[SentrySDK configureScope: ^(SentryScope *scope) {
98
- if (value != NULL ) {
99
- [scope setExtraValue: [NSString stringWithUTF8String: value]
100
- forKey: [NSString stringWithUTF8String: key]];
101
- } else {
102
- [scope removeExtraForKey: [NSString stringWithUTF8String: key]];
103
- }
136
+ [scope setExtraValue: valueString forKey: keyString];
104
137
}];
105
138
}
106
139
@@ -110,13 +143,11 @@ void SentryNativeBridgeSetTag(const char *key, const char *value)
110
143
return ;
111
144
}
112
145
146
+ NSString *keyString = [NSString stringWithUTF8String: key];
147
+ NSString *valueString = _NSStringOrNil (value);
148
+
113
149
[SentrySDK configureScope: ^(SentryScope *scope) {
114
- if (value != NULL ) {
115
- [scope setTagValue: [NSString stringWithUTF8String: value]
116
- forKey: [NSString stringWithUTF8String: key]];
117
- } else {
118
- [scope removeTagForKey: [NSString stringWithUTF8String: key]];
119
- }
150
+ [scope setTagValue: valueString forKey: keyString];
120
151
}];
121
152
}
122
153
@@ -126,35 +157,28 @@ void SentryNativeBridgeUnsetTag(const char *key)
126
157
return ;
127
158
}
128
159
129
- [SentrySDK configureScope: ^(
130
- SentryScope *scope) { [scope removeTagForKey: [NSString stringWithUTF8String: key]]; }];
160
+ NSString *keyString = [NSString stringWithUTF8String: key];
161
+
162
+ [SentrySDK configureScope: ^(SentryScope *scope) {
163
+ [scope removeTagForKey: keyString];
164
+ }];
131
165
}
132
166
133
167
void SentryNativeBridgeSetUser (
134
168
const char *email, const char *userId, const char *ipAddress, const char *username)
135
169
{
136
- if (email == NULL && userId == NULL && ipAddress == NULL && username == NULL ) {
137
- return ;
138
- }
139
-
170
+ NSString *emailString = _NSStringOrNil (email);
171
+ NSString *userIdString = _NSStringOrNil (userId);
172
+ NSString *ipAddressString = _NSStringOrNil (ipAddress);
173
+ NSString *usernameString = _NSStringOrNil (username);
174
+
140
175
[SentrySDK configureScope: ^(SentryScope *scope) {
141
176
SentryUser *user = [[SentryUser alloc ] init ];
142
177
143
- if (email != NULL ) {
144
- user.email = [NSString stringWithUTF8String: email];
145
- }
146
-
147
- if (userId != NULL ) {
148
- user.userId = [NSString stringWithUTF8String: userId];
149
- }
150
-
151
- if (ipAddress != NULL ) {
152
- user.ipAddress = [NSString stringWithUTF8String: ipAddress];
153
- }
154
-
155
- if (username != NULL ) {
156
- user.username = [NSString stringWithUTF8String: username];
157
- }
178
+ user.email = emailString;
179
+ user.userId = userIdString;
180
+ user.ipAddress = ipAddressString;
181
+ user.username = usernameString;
158
182
159
183
[scope setUser: user];
160
184
}];
@@ -184,42 +208,21 @@ void SentryNativeBridgeSetTrace(const char *traceId, const char *spanId)
184
208
185
209
NSString *traceIdStr = [NSString stringWithUTF8String: traceId];
186
210
NSString *spanIdStr = [NSString stringWithUTF8String: spanId];
187
-
211
+
188
212
// This is a workaround to deal with SentryId living inside the Swift header
189
213
Class sentryIdClass = NSClassFromString (@" _TtC6Sentry8SentryId" );
190
214
Class sentrySpanIdClass = NSClassFromString (@" SentrySpanId" );
191
-
215
+
192
216
if (sentryIdClass && sentrySpanIdClass) {
193
217
id sentryTraceId = [[sentryIdClass alloc ] initWithUUIDString: traceIdStr];
194
218
id sentrySpanId = [[sentrySpanIdClass alloc ] initWithValue: spanIdStr];
195
-
219
+
196
220
if (sentryTraceId && sentrySpanId) {
197
221
[PrivateSentrySDKOnly setTrace: sentryTraceId spanId: sentrySpanId];
198
222
}
199
223
}
200
224
}
201
225
202
- static inline NSString *_NSStringOrNil (const char *value)
203
- {
204
- return value ? [NSString stringWithUTF8String: value] : nil ;
205
- }
206
-
207
- static inline NSNumber *_NSNumberOrNil (int32_t value)
208
- {
209
- return value == 0 ? nil : @(value);
210
- }
211
-
212
- static inline NSNumber *_NSBoolOrNil (int8_t value)
213
- {
214
- if (value == 0 ) {
215
- return @NO ;
216
- }
217
- if (value == 1 ) {
218
- return @YES ;
219
- }
220
- return nil ;
221
- }
222
-
223
226
void SentryNativeBridgeWriteScope ( // clang-format off
224
227
// // const char *AppStartTime,
225
228
// const char *AppBuildType,
@@ -288,4 +291,4 @@ void SentryNativeBridgeWriteScope( // clang-format off
288
291
}];
289
292
}
290
293
291
- NS_ASSUME_NONNULL_END
294
+ NS_ASSUME_NONNULL_END
0 commit comments