mod: handle errors as @panic calls instead of silently ignoring them
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 1m25s
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 1m25s
Import order updated as well as `inline` inner private functions
This commit is contained in:
12
src/zlog.zig
12
src/zlog.zig
@@ -1,6 +1,6 @@
|
|||||||
const build_options = @import("build_options");
|
const build_options = @import("build_options");
|
||||||
const ztime = if (build_options.timestamp) @import("ztime") else null;
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const ztime = if (build_options.timestamp) @import("ztime") else null;
|
||||||
|
|
||||||
pub const std_options: std.Options = .{
|
pub const std_options: std.Options = .{
|
||||||
.logFn = logFn,
|
.logFn = logFn,
|
||||||
@@ -19,13 +19,14 @@ fn logFn(
|
|||||||
const level_txt = comptime message_level.asText();
|
const level_txt = comptime message_level.asText();
|
||||||
const prefix = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
|
const prefix = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
|
||||||
const fmt = level_txt ++ prefix ++ format ++ "\n";
|
const fmt = level_txt ++ prefix ++ format ++ "\n";
|
||||||
if (comptime build_options.file.len != 0) blk: {
|
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)
|
// 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, .{
|
const file = std.fs.openFileAbsolute(build_options.file, .{
|
||||||
.mode = .read_write,
|
.mode = .read_write,
|
||||||
}) catch std.fs.createFileAbsolute(build_options.file, .{
|
}) catch std.fs.createFileAbsolute(build_options.file, .{
|
||||||
.truncate = false,
|
.truncate = false,
|
||||||
}) catch break :blk;
|
}) catch @panic("Failed to open and/or create configured log file");
|
||||||
defer file.close();
|
defer file.close();
|
||||||
|
|
||||||
var buffer = std.io.bufferedWriter(file.writer());
|
var buffer = std.io.bufferedWriter(file.writer());
|
||||||
@@ -47,14 +48,14 @@ fn logFn(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn log_writing(writer: anytype, comptime fmt: []const u8, args: anytype) void {
|
inline fn log_writing(writer: anytype, comptime fmt: []const u8, args: anytype) void {
|
||||||
nosuspend {
|
nosuspend {
|
||||||
if (build_options.timestamp) log_timestamp(writer);
|
if (build_options.timestamp) log_timestamp(writer);
|
||||||
writer.print(fmt, args) catch return;
|
writer.print(fmt, args) catch return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn log_timestamp(writer: anytype) void {
|
inline fn log_timestamp(writer: anytype) void {
|
||||||
writer.print("[{any}] ", .{ztime.DateTime.now()}) catch return;
|
writer.print("[{any}] ", .{ztime.DateTime.now()}) catch return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,6 +64,7 @@ pub fn pretty_format(object: anytype, comptime fmt: []const u8, options: std.fmt
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn inner_format(object: anytype, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype, comptime depth: u8) !void {
|
fn inner_format(object: anytype, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype, comptime depth: u8) !void {
|
||||||
|
// TODO: here `std.meta` might be useful
|
||||||
const Object = @TypeOf(object);
|
const Object = @TypeOf(object);
|
||||||
const object_info = @typeInfo(Object);
|
const object_info = @typeInfo(Object);
|
||||||
switch (object_info) {
|
switch (object_info) {
|
||||||
|
|||||||
Reference in New Issue
Block a user