Compare commits
2 Commits
ab0898f9c4
...
411a6dc358
| Author | SHA1 | Date | |
|---|---|---|---|
|
411a6dc358
|
|||
|
cd99e5b4d3
|
@@ -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.
|
||||
|
||||
|
||||
17
src/root.zig
17
src/root.zig
@@ -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 {};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user