feat: use own cImport
This commit is contained in:
parent
aaea1cd24e
commit
1adaba56b9
2 changed files with 27 additions and 9 deletions
15
build.zig
15
build.zig
|
@ -1,7 +1,22 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn build(b: *std.build.Builder) void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
_ = b.addModule("zig-glib-log", .{
|
||||
.source_file = .{ .path = "src/main.zig" },
|
||||
});
|
||||
|
||||
const test_exe = b.addTest(.{
|
||||
.root_source_file = .{ .path = "src/main.zig" },
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
test_exe.linkLibC();
|
||||
test_exe.linkSystemLibrary("glib-2.0");
|
||||
|
||||
const test_step = b.step("test", "run tests");
|
||||
test_step.dependOn(&b.addRunArtifact(test_exe).step);
|
||||
}
|
||||
|
|
21
src/main.zig
21
src/main.zig
|
@ -1,21 +1,20 @@
|
|||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
|
||||
const c = @cImport(@cInclude("glib.h"));
|
||||
|
||||
/// Returns the log function, which must be present in the root module.
|
||||
pub fn log(
|
||||
/// The value returned by @cImport.
|
||||
/// Must include the "glib.h" or "glib/gmessages.h" header.
|
||||
comptime c: type,
|
||||
/// The name of the module used for the log output.
|
||||
comptime module_name: []const u8,
|
||||
/// The length of the buffer used for the formatted output.
|
||||
/// This is the maximum length of log messages.
|
||||
comptime buf_size: usize,
|
||||
) @TypeOf(LogStruct(c, module_name, buf_size).log) {
|
||||
return LogStruct(c, module_name, buf_size).log;
|
||||
) @TypeOf(LogStruct(module_name, buf_size).log) {
|
||||
return LogStruct(module_name, buf_size).log;
|
||||
}
|
||||
|
||||
fn LogStruct(comptime c: type, comptime module_name: []const u8, comptime buf_size: usize) type {
|
||||
fn LogStruct(comptime module_name: []const u8, comptime buf_size: usize) type {
|
||||
return struct {
|
||||
threadlocal var fmt_buf: [buf_size]u8 = undefined;
|
||||
|
||||
|
@ -42,7 +41,6 @@ fn LogStruct(comptime c: type, comptime module_name: []const u8, comptime buf_si
|
|||
c.GLogField{
|
||||
.key = "GLIB_DOMAIN",
|
||||
.value = @ptrCast(
|
||||
*const anyopaque,
|
||||
// don't include scope name if not explicitly specified
|
||||
if (std.mem.eql(u8, @tagName(scope), "default"))
|
||||
module_name
|
||||
|
@ -53,7 +51,7 @@ fn LogStruct(comptime c: type, comptime module_name: []const u8, comptime buf_si
|
|||
},
|
||||
c.GLogField{
|
||||
.key = "MESSAGE",
|
||||
.value = @ptrCast(*const anyopaque, s),
|
||||
.value = @ptrCast(s),
|
||||
.length = -1,
|
||||
},
|
||||
};
|
||||
|
@ -68,5 +66,10 @@ fn LogStruct(comptime c: type, comptime module_name: []const u8, comptime buf_si
|
|||
}
|
||||
|
||||
test {
|
||||
std.testing.refAllDecls(@This());
|
||||
const logF = log("zig-glib-log", 1024 * 4);
|
||||
|
||||
logF(.debug, .default, "debug", .{});
|
||||
logF(.info, .default, "info", .{});
|
||||
logF(.warn, .default, "warn", .{});
|
||||
logF(.err, .default, "error", .{});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue