Files
ghostty/pkg/macos/os/log.zig
Mitchell Hashimoto eccd07f009 pkg: replace @cImport with addTranslateC in pkg/
@cImport is going to disappear in Zig 0.17. Its deprecated in Zig 0.16.
Let's remove it now.

Replace @cImport with addTranslateC across pkg/ packages. Each
package now has a c_import.h header that is translated at build
time via addTranslateC and exposed as a "cimport" module import.

Converted packages:
- dcimgui
- fontconfig
- freetype
- glslang
- harfbuzz
- macos
- oniguruma
- opengl
- sentry
- spirv-cross
- wuffs

Omitted:
- gtk4-layer-shell - This has a bit more complexity with how it
  interacts with GTK headers, so I need to consider this a bit more.
- src/ - It'll be cleaner to do this separately.
2026-04-16 21:35:51 -07:00

66 lines
1.6 KiB
Zig

const std = @import("std");
const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const c = @import("c");
pub const Log = opaque {
pub fn create(
subsystem: [:0]const u8,
category: [:0]const u8,
) *Log {
return @ptrCast(c.os_log_create(
subsystem.ptr,
category.ptr,
).?);
}
pub fn release(self: *Log) void {
c.os_release(self);
}
pub fn typeEnabled(self: *Log, typ: LogType) bool {
return c.os_log_type_enabled(
@ptrCast(self),
@intFromEnum(typ),
);
}
pub fn log(
self: *Log,
alloc: Allocator,
typ: LogType,
comptime format: []const u8,
args: anytype,
) void {
const str = nosuspend std.fmt.allocPrintSentinel(
alloc,
format,
args,
0,
) catch return;
defer alloc.free(str);
zig_os_log_with_type(self, typ, str.ptr);
}
extern "c" fn zig_os_log_with_type(*Log, LogType, [*c]const u8) void;
};
/// https://developer.apple.com/documentation/os/os_log_type_t?language=objc
pub const LogType = enum(c.os_log_type_t) {
default = c.OS_LOG_TYPE_DEFAULT,
debug = c.OS_LOG_TYPE_DEBUG,
info = c.OS_LOG_TYPE_INFO,
err = c.OS_LOG_TYPE_ERROR,
fault = c.OS_LOG_TYPE_FAULT,
};
test {
const testing = std.testing;
const log = Log.create("com.mitchellh.ghostty", "test");
defer log.release();
try testing.expect(log.typeEnabled(.fault));
log.log(testing.allocator, .default, "hello {d}", .{12});
}