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"));
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);

View file

@ -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", .{});
}