Skip to content

Commit c22c779

Browse files
committed
Add user attachments
1 parent d0fdc80 commit c22c779

12 files changed

+178
-1
lines changed

src/register_types.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "runtime_config.h"
2+
#include "sentry/disabled_attachment.h"
23
#include "sentry/disabled_event.h"
34
#include "sentry/util/print.h"
5+
#include "sentry_attachment.h"
46
#include "sentry_configuration.h"
57
#include "sentry_event.h"
68
#include "sentry_logger.h"
@@ -13,6 +15,7 @@
1315
#include <godot_cpp/classes/window.hpp>
1416

1517
#ifdef NATIVE_SDK
18+
#include "sentry/native/native_attachment.h"
1619
#include "sentry/native/native_event.h"
1720
#endif // NATIVE_SDK
1821

@@ -52,10 +55,13 @@ void initialize_module(ModuleInitializationLevel p_level) {
5255
GDREGISTER_CLASS(SentryConfiguration);
5356
GDREGISTER_CLASS(SentryUser);
5457
GDREGISTER_CLASS(SentrySDK);
58+
GDREGISTER_ABSTRACT_CLASS(SentryAttachment);
5559
GDREGISTER_ABSTRACT_CLASS(SentryEvent);
60+
GDREGISTER_INTERNAL_CLASS(DisabledAttachment);
5661
GDREGISTER_INTERNAL_CLASS(DisabledEvent);
5762
GDREGISTER_INTERNAL_CLASS(SentryLogger);
5863
#ifdef NATIVE_SDK
64+
GDREGISTER_INTERNAL_CLASS(NativeAttachment);
5965
GDREGISTER_INTERNAL_CLASS(NativeEvent);
6066
#endif // NATIVE_SDK
6167

src/sentry/disabled_attachment.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef DISABLED_ATTACHMENT_H
2+
#define DISABLED_ATTACHMENT_H
3+
4+
#include "sentry_attachment.h"
5+
6+
// Attachment class that does nothing (for unsupported platforms or disabled SDK).
7+
class DisabledAttachment : public SentryAttachment {
8+
GDCLASS(DisabledAttachment, SentryAttachment);
9+
10+
protected:
11+
static void _bind_methods() {}
12+
13+
public:
14+
virtual ~DisabledAttachment() override = default;
15+
};
16+
17+
#endif // DISABLED_ATTACHMENT_H

src/sentry/disabled_sdk.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class DisabledSDK : public InternalSDK {
2626
virtual Ref<SentryEvent> create_event() override { return memnew(DisabledEvent); }
2727
virtual String capture_event(const Ref<SentryEvent> &p_event) override { return ""; }
2828

29+
virtual void add_attachment(const Ref<SentryAttachment> &p_attachment) override {}
30+
virtual void remove_attachment(const Ref<SentryAttachment> &p_attachment) override {}
31+
2932
virtual void initialize() override {}
3033
};
3134

src/sentry/internal_sdk.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <godot_cpp/variant/packed_string_array.hpp>
1010
#include <godot_cpp/variant/string.hpp>
1111

12+
class SentryAttachment;
13+
1214
using namespace godot;
1315

1416
namespace sentry {
@@ -34,6 +36,9 @@ class InternalSDK {
3436
virtual Ref<SentryEvent> create_event() = 0;
3537
virtual String capture_event(const Ref<SentryEvent> &p_event) = 0;
3638

39+
virtual void add_attachment(const Ref<SentryAttachment> &p_attachment) = 0;
40+
virtual void remove_attachment(const Ref<SentryAttachment> &p_attachment) = 0;
41+
3742
virtual void initialize() = 0;
3843

3944
virtual ~InternalSDK() = default;

src/sentry/native/native_attachment.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef NATIVE_ATTACHMENT_H
2+
#define NATIVE_ATTACHMENT_H
3+
4+
#include "sentry_attachment.h"
5+
6+
#include <sentry.h>
7+
8+
// Attachment class that is used with the NativeSDK.
9+
class NativeAttachment : public SentryAttachment {
10+
GDCLASS(NativeAttachment, SentryAttachment);
11+
12+
private:
13+
sentry_attachment_t *native_attachment = nullptr;
14+
15+
protected:
16+
static void _bind_methods() {}
17+
18+
public:
19+
sentry_attachment_t *get_native_attachment() const { return native_attachment; }
20+
void set_native_attachment(sentry_attachment_t *p_native_attachment) { native_attachment = p_native_attachment; }
21+
};
22+
23+
#endif // NATIVE_ATTACHMENT_H

src/sentry/native/native_sdk.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "sentry.h"
44
#include "sentry/contexts.h"
55
#include "sentry/level.h"
6+
#include "sentry/native/native_attachment.h"
67
#include "sentry/native/native_event.h"
78
#include "sentry/native/native_util.h"
89
#include "sentry/util/print.h"
@@ -340,6 +341,45 @@ String NativeSDK::capture_event(const Ref<SentryEvent> &p_event) {
340341
return _uuid_as_string(uuid);
341342
}
342343

344+
void NativeSDK::add_attachment(const Ref<SentryAttachment> &p_attachment) {
345+
ERR_FAIL_COND_MSG(p_attachment.is_null(), "Sentry: Can't add null attachment.");
346+
ERR_FAIL_COND_MSG(p_attachment->get_file_path().is_empty(), "Sentry: Can't add attachment with empty path.");
347+
348+
NativeAttachment *native_attachment_wrapper = Object::cast_to<NativeAttachment>(p_attachment.ptr());
349+
ERR_FAIL_NULL(native_attachment_wrapper); // Sanity check - this should never happen.
350+
351+
ERR_FAIL_NULL(ProjectSettings::get_singleton());
352+
String absolute_path = ProjectSettings::get_singleton()->globalize_path(p_attachment->get_file_path());
353+
sentry_attachment_t *native_attachment = sentry_attach_file(absolute_path.utf8().get_data());
354+
if (!native_attachment) {
355+
ERR_FAIL_MSG(vformat("Sentry: Failed to attach file: %s", absolute_path));
356+
}
357+
358+
if (!p_attachment->get_content_type().is_empty()) {
359+
sentry_attachment_set_content_type(native_attachment, p_attachment->get_content_type().utf8().get_data());
360+
}
361+
362+
native_attachment_wrapper->set_native_attachment(native_attachment);
363+
364+
sentry::util::print_debug(vformat("attached file: %s", absolute_path));
365+
}
366+
367+
void NativeSDK::remove_attachment(const Ref<SentryAttachment> &p_attachment) {
368+
ERR_FAIL_COND_MSG(p_attachment.is_null(), "Sentry: Can't remove null attachment.");
369+
370+
NativeAttachment *native_attachment_wrapper = Object::cast_to<NativeAttachment>(p_attachment.ptr());
371+
ERR_FAIL_NULL(native_attachment_wrapper); // Sanity check - this should never.
372+
373+
sentry_attachment_t *native_attachment = native_attachment_wrapper->get_native_attachment();
374+
if (native_attachment) {
375+
sentry_remove_attachment(native_attachment);
376+
native_attachment_wrapper->set_native_attachment(nullptr);
377+
sentry::util::print_debug(vformat("removed attachment: %s", p_attachment->get_file_path()));
378+
} else {
379+
sentry::util::print_warning(vformat("attempted to remove attachment that was not added: %s", p_attachment->get_file_path()));
380+
}
381+
}
382+
343383
void NativeSDK::initialize() {
344384
ERR_FAIL_NULL(OS::get_singleton());
345385
ERR_FAIL_NULL(ProjectSettings::get_singleton());

src/sentry/native/native_sdk.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class NativeSDK : public InternalSDK {
3333
virtual Ref<SentryEvent> create_event() override;
3434
virtual String capture_event(const Ref<SentryEvent> &p_event) override;
3535

36+
virtual void add_attachment(const Ref<SentryAttachment> &p_attachment) override;
37+
virtual void remove_attachment(const Ref<SentryAttachment> &p_attachment) override;
38+
3639
virtual void initialize() override;
3740

3841
virtual ~NativeSDK() override;

src/sentry_attachment.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "sentry_attachment.h"
2+
3+
#include "sentry/simple_bind.h"
4+
5+
#include <godot_cpp/core/class_db.hpp>
6+
7+
#ifdef NATIVE_SDK
8+
#include "sentry/native/native_attachment.h"
9+
#else
10+
#include "sentry/disabled_attachment.h"
11+
#endif
12+
13+
void SentryAttachment::_bind_methods() {
14+
ClassDB::bind_static_method("SentryAttachment", D_METHOD("create_with_path", "path", "content_type"), &SentryAttachment::create_with_path, DEFVAL(""));
15+
16+
BIND_PROPERTY(SentryAttachment, PropertyInfo(Variant::STRING, "file_path"), set_file_path, get_file_path);
17+
BIND_PROPERTY(SentryAttachment, PropertyInfo(Variant::STRING, "content_type"), set_content_type, get_content_type);
18+
}
19+
20+
Ref<SentryAttachment> SentryAttachment::create_with_path(const String &p_path, const String &p_content_type) {
21+
#ifdef NATIVE_SDK
22+
Ref<SentryAttachment> attachment = memnew(NativeAttachment);
23+
#else
24+
Ref<SentryAttachment> attachment = memnew(DisabledAttachment);
25+
#endif
26+
attachment->set_file_path(p_path);
27+
attachment->set_content_type(p_content_type);
28+
return attachment;
29+
}

src/sentry_attachment.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef SENTRY_ATTACHMENT_H
2+
#define SENTRY_ATTACHMENT_H
3+
4+
#include <godot_cpp/classes/ref_counted.hpp>
5+
#include <godot_cpp/variant/string.hpp>
6+
7+
using namespace godot;
8+
9+
// Base class for attachment objects in the public API.
10+
class SentryAttachment : public RefCounted {
11+
GDCLASS(SentryAttachment, RefCounted);
12+
13+
private:
14+
String file_path;
15+
String content_type;
16+
17+
protected:
18+
static void _bind_methods();
19+
20+
public:
21+
void set_file_path(const String &p_path) { file_path = p_path; }
22+
String get_file_path() const { return file_path; }
23+
24+
void set_content_type(const String &p_content_type) { content_type = p_content_type; }
25+
String get_content_type() const { return content_type; }
26+
27+
static Ref<SentryAttachment> create_with_path(const String &p_path, const String &p_content_type = "");
28+
29+
virtual ~SentryAttachment() = default;
30+
};
31+
32+
#endif // SENTRY_ATTACHMENT_H

0 commit comments

Comments
 (0)