From f76d8a3310633669e189152975c3124cddd6b4ef Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Mon, 18 Jul 2022 15:21:01 +0200 Subject: [PATCH] init --- .gitignore | 1 + build.zig | 21 ++++++++++++++++ src/main.zig | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 .gitignore create mode 100644 build.zig create mode 100644 src/main.zig diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c80a22 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +zig-* diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..4e2ac67 --- /dev/null +++ b/build.zig @@ -0,0 +1,21 @@ +const std = @import("std"); + +pub fn build(b: *std.build.Builder) void { + // Standard release options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. + const mode = b.standardReleaseOptions(); + + const lib = b.addStaticLibrary("zig-glib-log", "src/main.zig"); + lib.setBuildMode(mode); + + lib.linkLibC(); + lib.linkSystemLibrary("glib-2.0"); + + lib.install(); + + const main_tests = b.addTest("src/main.zig"); + main_tests.setBuildMode(mode); + + const test_step = b.step("test", "Run library tests"); + test_step.dependOn(&main_tests.step); +} diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..6d91588 --- /dev/null +++ b/src/main.zig @@ -0,0 +1,69 @@ +const std = @import("std"); +const testing = std.testing; + +/// 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. + c: anytype, + /// 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, +) fn ( + comptime std.log.Level, + comptime @TypeOf(.EnumLiteral), + comptime format: []const u8, + anytype, +) void { + return struct { + pub fn log( + comptime level: std.log.Level, + comptime scope: @TypeOf(.EnumLiteral), + comptime format: []const u8, + args: anytype, + ) void { + const Buf = struct { + pub threadlocal var fmt_buf: [buf_size]u8 = undefined; + }; + + const g_level = switch (level) { + .err => c.G_LOG_LEVEL_ERROR, + .warn => c.G_LOG_LEVEL_WARNING, + .info => c.G_LOG_LEVEL_INFO, + .debug => c.G_LOG_LEVEL_DEBUG, + }; + + const s = std.fmt.bufPrintZ( + &Buf.fmt_buf, + format, + args, + ) catch return; + + var fields = [_]c.GLogField{ + c.GLogField{ + .key = "GLIB_DOMAIN", + .value = module_name ++ "-" ++ @tagName(scope), + .length = -1, + }, + c.GLogField{ + .key = "MESSAGE", + .value = @ptrCast(*const anyopaque, s), + .length = -1, + }, + }; + + c.g_log_structured_array( + g_level, + &fields, + fields.len, + ); + } + }.log; +} + +export fn add(a: i32, b: i32) i32 { + return a + b; +} +