mod: support appending through posix file handle
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m21s
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m21s
This commit is contained in:
17
src/root.zig
17
src/root.zig
@@ -18,17 +18,18 @@ fn logFn(
|
|||||||
};
|
};
|
||||||
const complete_format = level_txt ++ prefix ++ format ++ "\n";
|
const complete_format = level_txt ++ prefix ++ format ++ "\n";
|
||||||
var buf: [128]u8 = undefined;
|
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?)
|
// 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)
|
// 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, .{
|
// file, err := os.OpenFile("log", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
|
||||||
.mode = .read_write,
|
const fd = std.posix.open(build_options.file, .{
|
||||||
}) catch std.fs.createFileAbsolute(build_options.file, .{
|
.CREAT = true,
|
||||||
.truncate = false,
|
.APPEND = true,
|
||||||
}) catch @panic("Failed to open and/or create configured log file");
|
.ACCMODE = .WRONLY,
|
||||||
defer file.close();
|
}, 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;
|
var writer = &buffer.interface;
|
||||||
defer writer.flush() catch {};
|
defer writer.flush() catch {};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user