mod: support appending through posix file handle
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m21s

This commit is contained in:
2025-11-01 00:01:50 +01:00
parent cd99e5b4d3
commit 411a6dc358

View File

@@ -18,17 +18,18 @@ fn logFn(
};
const complete_format = level_txt ++ prefix ++ format ++ "\n";
var buf: [128]u8 = undefined;
if (comptime build_options.file.len != 0) {
if (comptime build_options.file.len > 0) {
// TODO handle errors accordingly (i.e. panic?)
// NOTE with zig 0.13.0 there is currently no way to open files to append (except to use libc or talk directly to posix, which this lib should not have to do)
const file = std.fs.openFileAbsolute(build_options.file, .{
.mode = .read_write,
}) catch std.fs.createFileAbsolute(build_options.file, .{
.truncate = false,
}) catch @panic("Failed to open and/or create configured log file");
defer file.close();
// file, err := os.OpenFile("log", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
const fd = std.posix.open(build_options.file, .{
.CREAT = true,
.APPEND = true,
.ACCMODE = .WRONLY,
}, 0o600) catch @panic("Could not append to log file");
defer std.posix.close(fd);
var buffer = file.writer(&buf);
var buffer = std.fs.File.Writer.init(.{ .handle = fd }, &buf);
var writer = &buffer.interface;
defer writer.flush() catch {};