From 7784de231afc8ab246388c015a88648ef62611d5 Mon Sep 17 00:00:00 2001 From: Lyssieth Date: Mon, 2 Dec 2024 04:50:58 +0200 Subject: [PATCH] Make shit work. AA. --- build.zig | 12 +----------- src/log/logging.zig | 32 +++++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/build.zig b/build.zig index bf4020c..402e1d5 100644 --- a/build.zig +++ b/build.zig @@ -17,22 +17,12 @@ pub fn build(b: *std.Build) void { }); module.addImport("chameleon", chameleon.module("chameleon")); - const lib = b.addStaticLibrary(.{ - .name = "lys", - .root_source_file = libRoot, - .target = target, - .optimize = optimize, - }); - lib.root_module.addImport("lys", module); - lib.root_module.addImport("chameleon", chameleon.module("chameleon")); - - b.installArtifact(lib); - const libTests = b.addTest(.{ .root_source_file = libRoot, .target = target, .optimize = optimize, }); + libTests.root_module.addImport("chameleon", chameleon.module("chameleon")); const libTestsRun = b.addRunArtifact(libTests); diff --git a/src/log/logging.zig b/src/log/logging.zig index b6ec156..ebd2005 100644 --- a/src/log/logging.zig +++ b/src/log/logging.zig @@ -50,6 +50,9 @@ const Globals = struct { return .{ .allocator = allocator, + .enableFileOutput = false, + .outputFile = null, + .additionalScopes = std.ArrayList(ScopeModifier).init(allocator), }; } @@ -110,6 +113,9 @@ pub const config = struct { }; pub fn init(allocator: Allocator) !void { + if (core) |_| { + return error.AlreadyInitialized; + } core = try Globals.init(allocator); } @@ -129,19 +135,18 @@ pub fn logFn(comptime level: Level, comptime scope: Scope, comptime format: []co }; } -fn get() !*Globals { - var cor = core; - if (cor) |*co| { - return co; +fn get() *Globals { + if (core) |*globals| { + return globals; } unreachable; // logging is not initialized } fn logFnImpl(comptime level: Level, comptime scope: Scope, comptime format: []const u8, args: anytype) !void { - const globals = try get(); + const globals = get(); var arena = globals.arena(); - const c = cham.initRuntime(.{ + var c = cham.initRuntime(.{ .allocator = arena.allocator(), }); defer { @@ -154,7 +159,7 @@ fn logFnImpl(comptime level: Level, comptime scope: Scope, comptime format: []co .default => "main", .gpa => gpaBlock: { const gpa = "General Purpose Allocator"; - scopeLen = gpa; + scopeLen = gpa.len; break :gpaBlock try c.redBright().fmt("{s}", .{gpa}); }, @@ -191,7 +196,8 @@ fn logFnImpl(comptime level: Level, comptime scope: Scope, comptime format: []co }, }; - longestScopeYet = std.math.max(longestScopeYet, scopeLen); + if (scopeLen > longestScopeYet) + longestScopeYet = scopeLen; const levelText = switch (level) { .debug => try c.gray().fmt("{s: >5}", .{"DEBUG"}), @@ -213,7 +219,7 @@ fn logFnImpl(comptime level: Level, comptime scope: Scope, comptime format: []co const message = try std.fmt.allocPrint(arena.allocator(), format, args); - if (globals.enableFileOutput) { + if (globals.enableFileOutput and globals.outputFile != null) { var file = try globals.initOrGetFile(); var writer = file.writer().any(); @@ -228,3 +234,11 @@ fn logFnImpl(comptime level: Level, comptime scope: Scope, comptime format: []co }); } } + +test "logFn does something" { + const t = std.testing; + try init(t.allocator); + defer deinit(); + + try logFnImpl(.info, .default, "hello world", .{}); +}