-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathandroid_sdk.cpp
More file actions
413 lines (336 loc) · 13.9 KB
/
Copy pathandroid_sdk.cpp
File metadata and controls
413 lines (336 loc) · 13.9 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include "android_sdk.h"
#include "android_breadcrumb.h"
#include "android_event.h"
#include "android_log.h"
#include "android_metric.h"
#include "android_string_names.h"
#include "android_util.h"
#include "sentry/common_defs.h"
#include "sentry/logging/print.h"
#include "sentry/processing/process_event.h"
#include "sentry/processing/process_log.h"
#include "sentry/processing/process_metric.h"
#include "sentry/sentry_attachment.h"
#include "sentry/sentry_sdk.h"
#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/project_settings.hpp>
#include <godot_cpp/variant/callable.hpp>
using namespace godot;
namespace {
inline Variant _as_attribute(const Variant &p_value) {
Variant::Type type = p_value.get_type();
return (type < Variant::BOOL || type > Variant::STRING) ? (Variant)p_value.stringify() : p_value;
}
Dictionary _sanitize_attributes(const Dictionary &p_attributes) {
Dictionary attributes;
if (!p_attributes.is_empty()) {
const Array &keys = p_attributes.keys();
for (int i = 0; i < keys.size(); i++) {
const Variant &key = keys[i];
attributes[key.stringify()] = _as_attribute(p_attributes[key]);
}
}
return attributes;
}
} // unnamed namespace
namespace sentry::android {
// *** SentryAndroidBeforeSendHandler
void SentryAndroidBeforeSendHandler::_initialize(Object *p_android_plugin) {
android_plugin = p_android_plugin;
}
void SentryAndroidBeforeSendHandler::_before_send(int32_t p_event_handle) {
sentry::logging::print_debug("handling before_send: ", p_event_handle);
Ref<AndroidEvent> event_obj = memnew(AndroidEvent(android_plugin, p_event_handle));
event_obj->set_as_borrowed();
Ref<AndroidEvent> processed = sentry::process_event(event_obj);
if (processed.is_null()) {
// Discard event.
android_plugin->call(ANDROID_SN(releaseEvent), p_event_handle);
}
}
void SentryAndroidBeforeSendHandler::_bind_methods() {
ClassDB::bind_method(D_METHOD("before_send"), &SentryAndroidBeforeSendHandler::_before_send);
}
// *** SentryAndroidBeforeSendLogHandler
void SentryAndroidBeforeSendLogHandler::_initialize(Object *p_android_plugin) {
android_plugin = p_android_plugin;
}
void SentryAndroidBeforeSendLogHandler::_before_send_log(int32_t p_handle) {
Ref<AndroidLog> log_obj = memnew(AndroidLog(android_plugin, p_handle));
log_obj->set_as_borrowed();
Ref<AndroidLog> processed = sentry::process_log(log_obj);
if (processed.is_null()) {
// Discard log.
android_plugin->call(ANDROID_SN(releaseLog), p_handle);
}
}
void SentryAndroidBeforeSendLogHandler::_bind_methods() {
ClassDB::bind_method(D_METHOD("before_send_log"), &SentryAndroidBeforeSendLogHandler::_before_send_log);
}
// ** SentryAndroidBeforeSendMetricHandler
void SentryAndroidBeforeSendMetricHandler::_before_send_metric(int32_t p_metric_handle) {
Ref<AndroidMetric> metric_obj = memnew(AndroidMetric(android_plugin, p_metric_handle));
metric_obj->set_as_borrowed();
Ref<AndroidMetric> processed = sentry::process_metric(metric_obj);
if (processed.is_null()) {
// Discard metric.
android_plugin->call(ANDROID_SN(releaseMetric), p_metric_handle);
}
}
void SentryAndroidBeforeSendMetricHandler::_bind_methods() {
ClassDB::bind_method(D_METHOD("before_send_metric"), &SentryAndroidBeforeSendMetricHandler::_before_send_metric);
}
// *** AndroidSDK
void AndroidSDK::set_context(const String &p_key, const Dictionary &p_value) {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL(android_plugin);
android_plugin->call(ANDROID_SN(setContext), p_key, sanitize_variant(p_value));
}
void AndroidSDK::remove_context(const String &p_key) {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL(android_plugin);
android_plugin->call(ANDROID_SN(removeContext), p_key);
}
void AndroidSDK::set_tag(const String &p_key, const String &p_value) {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL(android_plugin);
android_plugin->call(ANDROID_SN(setTag), p_key, p_value);
}
void AndroidSDK::remove_tag(const String &p_key) {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL(android_plugin);
android_plugin->call(ANDROID_SN(removeTag), p_key);
}
void AndroidSDK::set_user(const Ref<SentryUser> &p_user) {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL(android_plugin);
if (p_user.is_valid()) {
android_plugin->call(ANDROID_SN(setUser),
p_user->get_id(),
p_user->get_username(),
p_user->get_email(),
p_user->get_ip_address());
} else {
remove_user();
}
}
void AndroidSDK::remove_user() {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL(android_plugin);
android_plugin->call(ANDROID_SN(removeUser));
}
Ref<SentryBreadcrumb> AndroidSDK::create_breadcrumb() {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL_V(android_plugin, nullptr);
int32_t handle = android_plugin->call(ANDROID_SN(createBreadcrumb));
Ref<AndroidBreadcrumb> crumb = memnew(AndroidBreadcrumb(android_plugin, handle));
return crumb;
}
void AndroidSDK::add_breadcrumb(const Ref<SentryBreadcrumb> &p_breadcrumb) {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL(android_plugin);
Ref<AndroidBreadcrumb> crumb = p_breadcrumb;
ERR_FAIL_COND(crumb.is_null());
android_plugin->call(ANDROID_SN(addBreadcrumb), crumb->get_handle());
}
void AndroidSDK::log(LogLevel p_level, const String &p_body, const Dictionary &p_attributes) {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL(android_plugin);
if (p_body.is_empty()) {
return;
}
android_plugin->call(ANDROID_SN(log), p_level, p_body, _sanitize_attributes(p_attributes));
}
String AndroidSDK::capture_message(const String &p_message, Level p_level) {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL_V(android_plugin, String());
return android_plugin->call(ANDROID_SN(captureMessage), p_message, p_level);
}
String AndroidSDK::get_last_event_id() {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL_V(android_plugin, String());
return android_plugin->call(ANDROID_SN(getLastEventId));
}
Ref<SentryEvent> AndroidSDK::create_event() {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL_V(android_plugin, nullptr);
int32_t event_handle = android_plugin->call(ANDROID_SN(createEvent));
Ref<AndroidEvent> event = memnew(AndroidEvent(android_plugin, event_handle));
return event;
}
String AndroidSDK::capture_event(const Ref<SentryEvent> &p_event) {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL_V(android_plugin, String());
ERR_FAIL_COND_V(p_event.is_null(), String());
Ref<AndroidEvent> android_event = p_event;
ERR_FAIL_COND_V(android_event.is_null(), String());
int32_t handle = android_event->get_handle();
android_plugin->call(ANDROID_SN(captureEvent), handle);
return android_event->get_id();
}
void AndroidSDK::capture_feedback(const Ref<SentryFeedback> &p_feedback) {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL(android_plugin);
ERR_FAIL_COND_MSG(p_feedback.is_null(), "Sentry: Can't capture feedback - feedback object is null.");
ERR_FAIL_COND_MSG(p_feedback->get_message().is_empty(), "Sentry: Can't capture feedback - feedback message is empty.");
android_plugin->call(ANDROID_SN(captureFeedback),
p_feedback->get_message(),
p_feedback->get_contact_email(),
p_feedback->get_name(),
p_feedback->get_associated_event_id());
}
void AndroidSDK::add_attachment(const Ref<SentryAttachment> &p_attachment) {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL(android_plugin);
ERR_FAIL_COND(p_attachment.is_null());
if (!p_attachment->get_path().is_empty()) {
// File attachment
String absolute_path = p_attachment->get_globalized_path();
sentry::logging::print_debug("attaching file: ", absolute_path);
android_plugin->call(ANDROID_SN(addFileAttachment),
absolute_path,
p_attachment->get_effective_filename(),
p_attachment->get_content_type(),
p_attachment->get_attachment_type());
} else {
// Bytes attachment
ERR_FAIL_COND_MSG(p_attachment->get_filename().is_empty(), "Sentry: Can't add bytes attachment without filename.");
sentry::logging::print_debug("attaching bytes with filename: ", p_attachment->get_filename());
android_plugin->call(ANDROID_SN(addBytesAttachment),
p_attachment->get_bytes(),
p_attachment->get_filename(),
p_attachment->get_content_type(),
p_attachment->get_attachment_type());
}
}
void AndroidSDK::metrics_add_count(const String &p_name, int64_t p_value, const Dictionary &p_attributes) {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL(android_plugin);
android_plugin->call(ANDROID_SN(metricsAddCount),
p_name, p_value, _sanitize_attributes(p_attributes));
}
void AndroidSDK::metrics_add_gauge(const String &p_name, double p_value, const String &p_unit, const Dictionary &p_attributes) {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL(android_plugin);
android_plugin->call(ANDROID_SN(metricsAddGauge),
p_name, p_value, p_unit, _sanitize_attributes(p_attributes));
}
void AndroidSDK::metrics_add_distribution(const String &p_name, double p_value, const String &p_unit, const Dictionary &p_attributes) {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL(android_plugin);
android_plugin->call(ANDROID_SN(metricsAddDistribution),
p_name, p_value, p_unit, _sanitize_attributes(p_attributes));
}
void AndroidSDK::set_attribute(const String &p_name, const Variant &p_value) {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL(android_plugin);
switch (p_value.get_type()) {
case Variant::Type::BOOL: {
android_plugin->call(ANDROID_SN(setAttributeBool), p_name, p_value);
} break;
case Variant::Type::INT: {
android_plugin->call(ANDROID_SN(setAttributeLong), p_name, p_value);
} break;
case Variant::Type::FLOAT: {
android_plugin->call(ANDROID_SN(setAttributeDouble), p_name, p_value);
} break;
case Variant::Type::STRING: {
android_plugin->call(ANDROID_SN(setAttributeString), p_name, p_value);
} break;
default: {
android_plugin->call(ANDROID_SN(setAttributeString), p_name, p_value.stringify());
} break;
}
}
void AndroidSDK::remove_attribute(const String &p_name) {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL(android_plugin);
android_plugin->call(ANDROID_SN(removeAttribute), p_name);
}
void AndroidSDK::set_trace(const String &p_trace_id, const String &p_parent_span_id) {
ERR_FAIL_COND(p_trace_id.is_empty());
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL(android_plugin);
android_plugin->call(ANDROID_SN(setTrace), p_trace_id, p_parent_span_id);
}
void AndroidSDK::_add_default_attachments() {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL(android_plugin);
for (const Ref<SentryAttachment> &att : SENTRY_OPTIONS()->get_default_attachments()) {
android_plugin->call(ANDROID_SN(addFileAttachment),
att->get_globalized_path(),
att->get_effective_filename(),
att->get_content_type(),
att->get_attachment_type());
}
}
void AndroidSDK::clear_attachments() {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL(android_plugin);
android_plugin->call(ANDROID_SN(clearAttachments));
_add_default_attachments();
}
void AndroidSDK::init() {
Object *android_plugin = _get_android_plugin();
ERR_FAIL_NULL(android_plugin);
_add_default_attachments();
Dictionary optionsData;
optionsData["dsn"] = SENTRY_OPTIONS()->get_dsn();
optionsData["debug"] = SENTRY_OPTIONS()->is_debug_enabled();
optionsData["release"] = SENTRY_OPTIONS()->get_release();
optionsData["dist"] = SENTRY_OPTIONS()->get_dist();
optionsData["environment"] = SENTRY_OPTIONS()->get_environment();
optionsData["sample_rate"] = SENTRY_OPTIONS()->get_sample_rate();
optionsData["max_breadcrumbs"] = SENTRY_OPTIONS()->get_max_breadcrumbs();
optionsData["enable_logs"] = SENTRY_OPTIONS()->get_enable_logs();
optionsData["enable_metrics"] = SENTRY_OPTIONS()->get_enable_metrics();
optionsData["enable_anr_detection"] = SENTRY_OPTIONS()->get_android()->get_enable_anr_detection();
optionsData["anr_timeout_interval_ms"] = SENTRY_OPTIONS()->get_android()->get_anr_timeout_interval_ms();
optionsData["attach_anr_thread_dump"] = SENTRY_OPTIONS()->get_android()->get_attach_anr_thread_dump();
optionsData["shutdown_timeout_ms"] = SENTRY_OPTIONS()->get_shutdown_timeout_ms();
android_plugin->call(ANDROID_SN(init),
optionsData,
before_send_handler->get_instance_id(),
SENTRY_OPTIONS()->get_before_send_log().is_valid() ? before_send_log_handler->get_instance_id() : 0,
SENTRY_OPTIONS()->get_before_send_metric().is_valid() ? before_send_metric_handler->get_instance_id() : 0);
if (is_enabled()) {
set_user(SentryUser::create_default());
} else {
ERR_PRINT("Sentry: Failed to initialize Android SDK.");
}
}
void AndroidSDK::close() {
Object *android_plugin = _get_android_plugin();
if (android_plugin != nullptr) {
android_plugin->call(ANDROID_SN(close));
}
}
bool AndroidSDK::is_enabled() const {
Object *android_plugin = _get_android_plugin();
return android_plugin && android_plugin->call(ANDROID_SN(isEnabled));
}
AndroidSDK::AndroidSDK() {
AndroidStringNames::create_singleton();
Object *android_plugin = Engine::get_singleton()->get_singleton("SentryAndroidGodotPlugin");
ERR_FAIL_NULL_MSG(android_plugin, "Sentry: Unable to locate SentryAndroidGodotPlugin singleton.");
android_plugin_instance_id = android_plugin->get_instance_id();
before_send_handler = memnew(SentryAndroidBeforeSendHandler);
before_send_handler->_initialize(android_plugin);
before_send_log_handler = memnew(SentryAndroidBeforeSendLogHandler);
before_send_log_handler->_initialize(android_plugin);
before_send_metric_handler = memnew(SentryAndroidBeforeSendMetricHandler);
before_send_metric_handler->_initialize(android_plugin);
}
AndroidSDK::~AndroidSDK() {
AndroidStringNames::destroy_singleton();
if (before_send_handler) {
memdelete(before_send_handler);
}
if (before_send_log_handler) {
memdelete(before_send_log_handler);
}
if (before_send_metric_handler) {
memdelete(before_send_metric_handler);
}
}
} //namespace sentry::android