Make shit work. AA.

This commit is contained in:
Lyssieth 2024-12-02 04:50:58 +02:00
parent 4ee3351dba
commit 7784de231a
Signed by untrusted user who does not match committer: lyssieth
GPG key ID: 200268854934CFAB
2 changed files with 24 additions and 20 deletions

View file

@ -17,22 +17,12 @@ pub fn build(b: *std.Build) void {
}); });
module.addImport("chameleon", chameleon.module("chameleon")); 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(.{ const libTests = b.addTest(.{
.root_source_file = libRoot, .root_source_file = libRoot,
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
libTests.root_module.addImport("chameleon", chameleon.module("chameleon"));
const libTestsRun = b.addRunArtifact(libTests); const libTestsRun = b.addRunArtifact(libTests);

View file

@ -50,6 +50,9 @@ const Globals = struct {
return .{ return .{
.allocator = allocator, .allocator = allocator,
.enableFileOutput = false,
.outputFile = null,
.additionalScopes = std.ArrayList(ScopeModifier).init(allocator), .additionalScopes = std.ArrayList(ScopeModifier).init(allocator),
}; };
} }
@ -110,6 +113,9 @@ pub const config = struct {
}; };
pub fn init(allocator: Allocator) !void { pub fn init(allocator: Allocator) !void {
if (core) |_| {
return error.AlreadyInitialized;
}
core = try Globals.init(allocator); core = try Globals.init(allocator);
} }
@ -129,19 +135,18 @@ pub fn logFn(comptime level: Level, comptime scope: Scope, comptime format: []co
}; };
} }
fn get() !*Globals { fn get() *Globals {
var cor = core; if (core) |*globals| {
if (cor) |*co| { return globals;
return co;
} }
unreachable; // logging is not initialized unreachable; // logging is not initialized
} }
fn logFnImpl(comptime level: Level, comptime scope: Scope, comptime format: []const u8, args: anytype) !void { 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(); var arena = globals.arena();
const c = cham.initRuntime(.{ var c = cham.initRuntime(.{
.allocator = arena.allocator(), .allocator = arena.allocator(),
}); });
defer { defer {
@ -154,7 +159,7 @@ fn logFnImpl(comptime level: Level, comptime scope: Scope, comptime format: []co
.default => "main", .default => "main",
.gpa => gpaBlock: { .gpa => gpaBlock: {
const gpa = "General Purpose Allocator"; const gpa = "General Purpose Allocator";
scopeLen = gpa; scopeLen = gpa.len;
break :gpaBlock try c.redBright().fmt("{s}", .{gpa}); 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) { const levelText = switch (level) {
.debug => try c.gray().fmt("{s: >5}", .{"DEBUG"}), .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); 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 file = try globals.initOrGetFile();
var writer = file.writer().any(); 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", .{});
}