Add time, severity & category in logs

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion
2026-01-31 11:17:36 +01:00
parent 0c12008327
commit 2e04ea4d79
5 changed files with 95 additions and 83 deletions

View File

@@ -1,4 +1,5 @@
const std = @import("std");
const interop = @import("interop.zig");
const LogFile = @This();
@@ -19,10 +20,29 @@ pub fn reinit(self: *LogFile) !void {
}
pub fn deinit(self: *LogFile) void {
self.file_writer.interface.flush() catch {};
self.file.close();
}
pub fn info(self: *LogFile, category: []const u8, comptime message: []const u8, args: anytype) !void {
var buffer: [128:0]u8 = undefined;
const time = interop.timeAsString(&buffer, "%Y-%m-%d %H:%M:%S");
try self.file_writer.interface.print("{s} [info/{s}] ", .{ time, category });
try self.file_writer.interface.print(message, args);
try self.file_writer.interface.writeByte('\n');
try self.file_writer.interface.flush();
}
pub fn err(self: *LogFile, category: []const u8, comptime message: []const u8, args: anytype) !void {
var buffer: [128:0]u8 = undefined;
const time = interop.timeAsString(&buffer, "%Y-%m-%d %H:%M:%S");
try self.file_writer.interface.print("{s} [err/{s}] ", .{ time, category });
try self.file_writer.interface.print(message, args);
try self.file_writer.interface.writeByte('\n');
try self.file_writer.interface.flush();
}
fn openLogFile(path: []const u8, log_file: *LogFile) !bool {
var could_open_log_file = true;
open_log_file: {