From 1adaba56b9e020c63d8497590519c5c63e25867e Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Mon, 3 Jul 2023 22:08:56 +0200 Subject: [PATCH] feat: use own cImport --- build.zig | 15 +++++++++++++++ src/main.zig | 21 ++++++++++++--------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/build.zig b/build.zig index 8591d40..32b9bf5 100644 --- a/build.zig +++ b/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); } diff --git a/src/main.zig b/src/main.zig index c4cff53..d9c5909 100644 --- a/src/main.zig +++ b/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", .{}); }