diff --git a/.gitea/workflows/test.yaml b/.gitea/workflows/test.yaml new file mode 100644 index 0000000..7bed5ef --- /dev/null +++ b/.gitea/workflows/test.yaml @@ -0,0 +1,26 @@ +name: Run Tests +on: + push: + branches: [main] + pull_request: + types: [opened, synchronize, reopened] + +jobs: + test: + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/checkout@v4 + - uses: mlugg/setup-zig@v1 + with: + version: 0.13.0 + - run: zig build test + lint: + runs-on: ubuntu-latest + timeout-minutes: 3 + steps: + - uses: actions/checkout@v4 + - uses: mlugg/setup-zig@v1 + with: + version: 0.13.0 + - run: zig fmt --check . diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3389c86 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.zig-cache/ +zig-out/ diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..6667936 --- /dev/null +++ b/build.zig @@ -0,0 +1,77 @@ +const std = @import("std"); + +// Although this function looks imperative, note that its job is to +// declaratively construct a build graph that will be executed by an external +// runner. +pub fn build(b: *std.Build) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard optimization options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not + // set a preferred release mode, allowing the user to decide how to optimize. + const optimize = b.standardOptimizeOption(.{}); + + const zlog_module = b.addModule("zlog", .{ + // In this case the main source file is merely a path, however, in more + // complicated build scripts, this could be a generated file. + .root_source_file = b.path("src/zlog.zig"), + .target = target, + .optimize = optimize, + }); + + const exe = b.addExecutable(.{ + .name = "zlog", + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + }); + exe.root_module.addImport("zlog", zlog_module); + + // This declares intent for the executable to be installed into the + // standard location when the user invokes the "install" step (the default + // step when running `zig build`). + b.installArtifact(exe); + + // This *creates* a Run step in the build graph, to be executed when another + // step is evaluated that depends on it. The next line below will establish + // such a dependency. + const run_cmd = b.addRunArtifact(exe); + + // By making the run step depend on the install step, it will be run from the + // installation directory rather than directly from within the cache directory. + // This is not necessary, however, if the application depends on other installed + // files, this ensures they will be present and in the expected location. + run_cmd.step.dependOn(b.getInstallStep()); + + // This allows the user to pass arguments to the application in the build + // command itself, like this: `zig build run -- arg1 arg2 etc` + if (b.args) |args| { + run_cmd.addArgs(args); + } + + // This creates a build step. It will be visible in the `zig build --help` menu, + // and can be selected like this: `zig build run` + // This will evaluate the `run` step rather than the default, which is "install". + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); + + // Creates a step for unit testing. This only builds the test executable + // but does not run it. + const lib_unit_tests = b.addTest(.{ + .root_source_file = b.path("src/zlog.zig"), + .target = target, + .optimize = optimize, + }); + + const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests); + + // Similar to creating the run step earlier, this exposes a `test` step to + // the `zig build --help` menu, providing a way for the user to request + // running the unit tests. + const test_step = b.step("test", "Run unit tests"); + test_step.dependOn(&run_lib_unit_tests.step); +} diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..47fe730 --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,13 @@ +.{ + .name = "zlog", + // version name should match the zig version except for the last number, + // which stands for the version inside a given zig version + .version = "0.13.0", + .minimum_zig_version = "0.13.0", + .paths = .{ + "build.zig", + "build.zig.zon", + "src", + "LICENSE", + }, +} diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..fc04de1 --- /dev/null +++ b/src/main.zig @@ -0,0 +1,70 @@ +const std = @import("std"); +const zlog = @import("zlog"); + +const Options = enum { + a, + b, + c, +}; + +const Struct = struct { + a: usize = 42, + b: bool = true, + c: [5]u16 = .{ 1, 2, 3, 4, 5 }, + d: []const u8 = "string", + e: Options = Options.b, + + // pub fn format(value: Struct, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { + // // TODO: make the creation of this function comptime + // try writer.writeAll("main.Struct{ "); + // try writer.writeAll(".a: usize = "); + // try std.fmt.formatIntValue(value.a, fmt, options, writer); + // try writer.writeAll(", "); + + // try writer.writeAll(".b: bool = "); + // try std.fmt.formatBuf(if (value.b) "true" else "false", options, writer); + // try writer.writeAll(", "); + + // try writer.writeAll(".c: [5]u16 = "); + // try std.fmt.format(writer, "{any}", .{value.c}); + // try writer.writeAll(", "); + + // try writer.writeAll(".d: []const u8 = "); + // try std.fmt.format(writer, "\"{s}\"", .{value.d}); + // try writer.writeAll(", "); + + // try writer.writeAll(".e: main.Options = "); + // try std.fmt.format(writer, "{any}", .{value.e}); + + // try writer.writeAll(" }"); + // } + + // TODO: can this become comptime entirely? - i.e. create the entire struct through a helper function and provide the created one with the format function already attached? + pub fn format(value: Struct, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) anyerror!void { + try zlog.generic_format(value, fmt, options, writer); + } +}; + +pub const std_options = zlog.std_options; + +pub fn main() void { + // initialize zlog with the scope of `main` + const log = std.log.scoped(.main); + // NOTE: the scope of `default` will result in no scoping being printed in + // the resulting output + + // some variables to log + const int_var = 42; + const array_var = [_]i32{ 1, 2, 3, 4 }; + const string_var = "This is a test"; + const option_var = Options.a; + const struct_var: Struct = .{}; + + // NOTE: Depending on the optimization target some of these log messages + // will not show, which is inline with the behavior of `std.log`. + log.debug("Debug message {any}", .{int_var}); + log.info("Info message {any}", .{array_var}); + log.info("Info message \"{s}\"", .{string_var}); + log.warn("Warning message {any}", .{option_var}); + log.err("Error message {any}", .{struct_var}); +} diff --git a/src/zlog.zig b/src/zlog.zig new file mode 100644 index 0000000..af15bae --- /dev/null +++ b/src/zlog.zig @@ -0,0 +1,88 @@ +const std = @import("std"); + +const max_depth = 5; + +// pub fn format(value: ?, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void +// TODO: provide comptime helper function to add format function for user types: +// - pretty printing +// - compact printing +// - show types of struct members +// Maybe the corresponding configurations for the `format` function could also +// be effected by build time configurations + +pub const std_options: std.Options = .{ + .logFn = logFn, +}; + +// zlog defaultLog function replacement, which adjusts the surrounding contents of every std.log message. +fn logFn( + comptime message_level: std.log.Level, + comptime scope: @Type(.EnumLiteral), + comptime format: []const u8, + args: anytype, +) void { + // TODO: provide build time configuration to allow tweaking corresponding output + // - add timestamp to all log messages + // - change output file for writing messages to (default `stderr`) + + // TODO: improve formatting output, such that the resulting log has a good feel + // - should there be indenting? + // - should there be spacing? + // - should the messages be aligned (left, center, right)? + const level_txt = comptime message_level.asText(); + const prefix2 = if (scope == .default) ":\t" else "(" ++ @tagName(scope) ++ "):\t"; + const stderr = std.io.getStdErr().writer(); + var bw = std.io.bufferedWriter(stderr); + const writer = bw.writer(); + + std.debug.lockStdErr(); + defer std.debug.unlockStdErr(); + nosuspend { + writer.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return; + bw.flush() catch return; + } +} + +pub fn generic_format(object: anytype, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { + _ = fmt; + _ = options; + const Object = @TypeOf(object); + const object_info = @typeInfo(Object); + switch (object_info) { + .Struct => |s| { + try writer.writeAll(@typeName(Object)); + try writer.writeAll(" = {\n"); + inline for (s.fields) |field| { + try writer.writeAll("\t ."); + try writer.writeAll(field.name); + try writer.writeAll(" = "); + // TODO check corresponding type and try to adapt fmt accordingly! + try std.fmt.format(writer, "{any}", .{@field(object, field.name)}); + try writer.writeAll(",\n"); + } + try writer.writeAll("}"); + }, + else => {}, + } +} + +pub fn enhance_format(comptime object: type) type { + // TODO: append a function to the type if it is a user defined type + + // NOTE: this has the clear disadvantage that the type is not the same anymore! (but maybe this is still fine) + const object_info = @typeInfo(object); + switch (object_info) { + .Struct => |s| { + return @Type(.{ + .Struct = .{ + .layout = .auto, + .fields = s.fields, + .decls = s.decls, + .is_tuple = false, + }, + }); + }, + else => {}, + } + return object; +}