mod(options): build options to log to stderr and/or a file
All checks were successful
Run Tests / test (push) Successful in 43s
Run Tests / lint (push) Successful in 39s

NOTE: file logging will not work correctly yet due to opening files
appending using the std is currently not implemented (as of zig version
0.13.0)!
This commit is contained in:
2024-08-29 16:36:58 +02:00
parent b03c770b59
commit 7889a6ec7a
3 changed files with 46 additions and 15 deletions

View File

@@ -15,26 +15,52 @@ fn logFn(
) void {
// TODO: provide build time configuration to allow tweaking corresponding output
// - change output file for writing messages to (default `stderr`)
// write into own file for each level?
const level_txt = comptime message_level.asText();
const prefix = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
const stderr = std.io.getStdErr().writer();
var bw = std.io.bufferedWriter(stderr);
const writer = bw.writer();
const fmt = level_txt ++ prefix ++ format ++ "\n";
if (comptime build_options.file.len != 0) blk: {
// 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 break :blk;
defer file.close();
std.debug.lockStdErr();
defer std.debug.unlockStdErr();
nosuspend {
if (build_options.timestamp) {
const curtime = c_time.time(null);
const tm = c_time.localtime(&curtime);
var buffer = std.io.bufferedWriter(file.writer());
defer buffer.flush() catch {};
var buf: [32]u8 = undefined;
_ = c_time.strftime(@ptrCast(&buf), 32, "%F %R", tm);
writer.print("[{s}] ", .{buf}) catch return;
}
writer.print(level_txt ++ prefix ++ format ++ "\n", args) catch return;
bw.flush() catch return;
const writer = buffer.writer();
log_writing(writer, fmt, args);
}
if (comptime build_options.stderr) {
var buffer = std.io.bufferedWriter(std.io.getStdErr().writer());
defer buffer.flush() catch {};
std.debug.lockStdErr();
defer std.debug.unlockStdErr();
const writer = buffer.writer();
log_writing(writer, fmt, args);
}
}
fn log_writing(writer: anytype, comptime fmt: []const u8, args: anytype) void {
nosuspend {
if (build_options.timestamp) log_timestamp(writer);
writer.print(fmt, args) catch return;
}
}
fn log_timestamp(writer: anytype) void {
const curtime = c_time.time(null);
const tm = c_time.localtime(&curtime);
var buffer: [32]u8 = undefined;
_ = c_time.strftime(@ptrCast(&buffer), 32, "%F %R", tm);
writer.print("[{s}] ", .{buffer}) catch return;
}
pub fn pretty_format(object: anytype, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {