Compare commits

...

2 Commits

Author SHA1 Message Date
411a6dc358 mod: support appending through posix file handle
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m21s
2025-11-01 00:01:50 +01:00
cd99e5b4d3 doc: correct alert blocks 2025-10-26 21:52:15 +01:00
2 changed files with 11 additions and 10 deletions

View File

@@ -2,7 +2,7 @@
Standard Library log wrapper. `zlog` provides adjusted `std.log` output and a pretty print function for easy overwriting of user defined types.
> [!CAUTION]
> [!caution]
> Only builds using the zig master version are tested to work.
## Usage
@@ -145,7 +145,7 @@ For more details about the output customization see the configuration options of
- _timestamp_ (default: `true`): Prepend the current timestamp before each log message.
- _stderr_ (default: `true`): Print log messages to stderr.
> [!CAUTION]
> [!caution]
> Currently not working as log output is not appended and only the last log message will be in the resulting log file! This is a not-yet-implemented feature of the standard library of zig! See this [issue](https://github.com/ziglang/zig/issues/14375) for more details.
> For now you should instead leave this option as it is and pipe the corresponding stderr outputs to a logfile instead.

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 {};