Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features

- Add support for script context and variables on Apple platforms ([#306](https://github.com/getsentry/sentry-godot/pull/306))
- Add SentryEvent.to_json() ([#329](https://github.com/getsentry/sentry-godot/pull/329))

### Improvements

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.util.Log
import io.sentry.Attachment
import io.sentry.Breadcrumb
import io.sentry.Hint
import io.sentry.ISerializer
import io.sentry.Sentry
import io.sentry.SentryEvent
import io.sentry.SentryLevel
Expand All @@ -20,6 +21,7 @@ import org.godotengine.godot.plugin.GodotPlugin
import org.godotengine.godot.plugin.UsedByGodot
import org.godotengine.godot.variant.Callable
import java.io.File
import java.io.StringWriter
import kotlin.random.Random


Expand Down Expand Up @@ -381,6 +383,15 @@ class SentryAndroidGodotPlugin(godot: Godot) : GodotPlugin(godot) {
return getEvent(eventHandle)?.isCrashed == true
}

@UsedByGodot
fun eventToJson(eventHandle: Int): String {
val event = getEvent(eventHandle) ?: return ""
val serializer: ISerializer = Sentry.getCurrentScopes().options.serializer
val writer = StringWriter()
serializer.serialize(event, writer)
return writer.toString()
}

@UsedByGodot
fun createException(type: String, value: String): Int {
val exception = SentryException()
Expand Down
6 changes: 6 additions & 0 deletions doc_classes/SentryEvent.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
Sets a tag with the specified [param key] to the given [param value].
</description>
</method>
<method name="to_json" qualifiers="const">
<return type="String" />
<description>
Serializes the event to JSON string.
</description>
</method>
</methods>
<members>
<member name="dist" type="String" setter="set_dist" getter="get_dist">
Expand Down
5 changes: 5 additions & 0 deletions src/sentry/android/android_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ bool AndroidEvent::is_crash() const {
return android_plugin->call(ANDROID_SN(eventIsCrash), event_handle);
}

String AndroidEvent::to_json() const {
ERR_FAIL_NULL_V(android_plugin, String());
return android_plugin->call(ANDROID_SN(eventToJson), event_handle);
}

AndroidEvent::AndroidEvent(Object *p_android_plugin, int32_t p_event_handle) {
android_plugin = p_android_plugin;
event_handle = p_event_handle;
Expand Down
2 changes: 2 additions & 0 deletions src/sentry/android/android_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class AndroidEvent : public SentryEvent {

virtual bool is_crash() const override;

virtual String to_json() const override;

void set_as_borrowed() { is_borrowed = true; }

AndroidEvent() {}
Expand Down
1 change: 1 addition & 0 deletions src/sentry/android/android_string_names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ AndroidStringNames::AndroidStringNames() {
eventGetTag = StringName("eventGetTag");
eventMergeContext = StringName("eventMergeContext");
eventIsCrash = StringName("eventIsCrash");
eventToJson = StringName("eventToJson");

// Exceptions.
createException = StringName("createException");
Expand Down
1 change: 1 addition & 0 deletions src/sentry/android/android_string_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class AndroidStringNames {
StringName eventGetTag;
StringName eventMergeContext;
StringName eventIsCrash;
StringName eventToJson;

// Exceptions.
StringName createException;
Expand Down
2 changes: 2 additions & 0 deletions src/sentry/cocoa/cocoa_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class CocoaEvent : public sentry::SentryEvent {

virtual bool is_crash() const override;

virtual String to_json() const override;

CocoaEvent(objc::SentryEvent *p_cocoa_event);
CocoaEvent();
virtual ~CocoaEvent() override;
Expand Down
14 changes: 14 additions & 0 deletions src/sentry/cocoa/cocoa_event.mm
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,20 @@
return cocoa_event.error != nil;
}

String CocoaEvent::to_json() const {
ERR_FAIL_NULL_V(cocoa_event, String());

NSDictionary *event_dict = [cocoa_event serialize];

NSError *error = nil;
NSData *json_data = [NSJSONSerialization dataWithJSONObject:event_dict
options:0
error:&error];
ERR_FAIL_COND_V(error != nil, "Sentry: Failed to serialize event to JSON: " + string_from_objc(error.localizedDescription));

return String::utf8((const char *)json_data.bytes, json_data.length);
}

CocoaEvent::CocoaEvent() :
cocoa_event([[objc::SentryEvent alloc] init]) {
}
Expand Down
2 changes: 2 additions & 0 deletions src/sentry/disabled/disabled_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class DisabledEvent : public SentryEvent {
virtual void add_exception(const Exception &p_exception) override {}

virtual bool is_crash() const override { return false; }

virtual String to_json() const override { return ""; }
};

} // namespace sentry
Expand Down
7 changes: 7 additions & 0 deletions src/sentry/native/native_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ bool NativeEvent::is_crash() const {
return _is_crash;
}

String NativeEvent::to_json() const {
char *json_value = sentry_value_to_json(native_event);
String json_str = String::utf8(json_value);
sentry_free(json_value);
return json_str;
}

NativeEvent::NativeEvent(sentry_value_t p_native_event, bool p_is_crash) :
_is_crash(p_is_crash) {
if (sentry_value_refcount(p_native_event) > 0) {
Expand Down
2 changes: 2 additions & 0 deletions src/sentry/native/native_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class NativeEvent : public SentryEvent {

virtual bool is_crash() const override;

virtual String to_json() const override;

NativeEvent(sentry_value_t p_event, bool p_is_crash);
NativeEvent();
virtual ~NativeEvent() override;
Expand Down
1 change: 1 addition & 0 deletions src/sentry/sentry_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void SentryEvent::_bind_methods() {
ClassDB::bind_method(D_METHOD("remove_tag", "key"), &SentryEvent::remove_tag);
ClassDB::bind_method(D_METHOD("get_tag", "key"), &SentryEvent::get_tag);
ClassDB::bind_method(D_METHOD("is_crash"), &SentryEvent::is_crash);
ClassDB::bind_method(D_METHOD("to_json"), &SentryEvent::to_json);

ADD_PROPERTY(PropertyInfo(Variant::STRING, "id"), "", "get_id");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "message"), "set_message", "get_message");
Expand Down
2 changes: 2 additions & 0 deletions src/sentry/sentry_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class SentryEvent : public RefCounted {

virtual bool is_crash() const = 0;

virtual String to_json() const = 0;

virtual ~SentryEvent() = default;
};

Expand Down
Loading