pkg/sentry: add more Zig APIs

This commit is contained in:
Mitchell Hashimoto
2024-08-27 13:31:59 -07:00
parent 2793cf8112
commit 1070068090
4 changed files with 70 additions and 0 deletions

8
pkg/sentry/level.zig Normal file
View File

@ -0,0 +1,8 @@
/// sentry_level_t
pub const Level = enum(c_int) {
debug = -1,
info = 0,
warning = 1,
err = 2,
fatal = 3,
};

View File

@ -1,5 +1,15 @@
pub const c = @import("c.zig").c;
pub const Level = @import("level.zig").Level;
pub const Value = @import("value.zig").Value;
pub const UUID = @import("uuid.zig").UUID;
pub fn captureEvent(value: Value) ?UUID {
const uuid: UUID = .{ .value = c.sentry_capture_event(value.value) };
if (uuid.isNil()) return null;
return uuid;
}
test {
@import("std").testing.refAllDecls(@This());
}

18
pkg/sentry/uuid.zig Normal file
View File

@ -0,0 +1,18 @@
const std = @import("std");
const assert = std.debug.assert;
const c = @import("c.zig").c;
/// sentry_uuid_t
pub const UUID = struct {
value: c.sentry_uuid_t,
pub fn isNil(self: UUID) bool {
return c.sentry_uuid_is_nil(&self.value) != 0;
}
pub fn string(self: UUID) [37]u8 {
var buf: [37]u8 = undefined;
c.sentry_uuid_as_string(&self.value, &buf);
return buf;
}
};

34
pkg/sentry/value.zig Normal file
View File

@ -0,0 +1,34 @@
const std = @import("std");
const assert = std.debug.assert;
const c = @import("c.zig").c;
const Level = @import("level.zig").Level;
/// sentry_value_t
pub const Value = struct {
/// The underlying value. This is a union that could be represented with
/// an extern union but I don't want to risk C ABI issues so we wrap it
/// in a struct.
value: c.sentry_value_t,
pub fn initMessageEvent(
level: Level,
logger: ?[]const u8,
message: []const u8,
) Value {
return .{ .value = c.sentry_value_new_message_event_n(
@intFromEnum(level),
if (logger) |v| v.ptr else null,
if (logger) |v| v.len else 0,
message.ptr,
message.len,
) };
}
pub fn decref(self: Value) void {
c.sentry_value_decref(self.value);
}
pub fn incref(self: Value) Value {
c.sentry_value_incref(self.value);
}
};