This commit is contained in:
LordMZTE 2022-07-18 15:21:01 +02:00
commit f76d8a3310
Signed by: LordMZTE
GPG Key ID: B64802DC33A64FF6
3 changed files with 91 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
zig-*

21
build.zig Normal file
View File

@ -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);
}

69
src/main.zig Normal file
View File

@ -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;
}