diff --git a/src/root.zig b/src/root.zig index 97e54c5..4ba5cd7 100644 --- a/src/root.zig +++ b/src/root.zig @@ -1,11 +1,15 @@ /// *zlog* defaultLog function replacement, which adjusts the surrounding contents of every `std.log` message. fn logFn( comptime message_level: log.Level, - comptime scope: @Type(.enum_literal), + comptime scope: @EnumLiteral(), comptime format: []const u8, args: anytype, ) void { var buf: [128]u8 = undefined; + const io = std.Options.debug_io; + const prev = io.swapCancelProtection(.blocked); + defer _ = io.swapCancelProtection(prev); + if (comptime build_options.file.len > 0) { const prefix = if (scope == .default) " " else "(" ++ @tagName(scope) ++ ") "; const level_txt = switch (comptime message_level) { @@ -25,12 +29,12 @@ fn logFn( }, 0o600) catch @panic("Could not append to log file"); defer std.posix.close(fd); - var file: fs.File = .{ .handle = fd }; - var buffer = file.writer(&buf); + var file: Io.File = .{ .handle = fd }; + var buffer = file.writer(io, &buf); var writer = &buffer.interface; defer writer.flush() catch unreachable; - log_writing(writer, complete_format, false, args); + log_writing(io, writer, complete_format, false, args); } if (comptime build_options.stderr) { @@ -43,20 +47,17 @@ fn logFn( }; const complete_format = level_txt ++ prefix ++ format ++ "\n"; - std.debug.lockStdErr(); - defer std.debug.unlockStdErr(); - - var buffer = fs.File.stderr().writer(&buf); + var buffer = Io.File.stderr().writer(io, &buf); var writer = &buffer.interface; defer writer.flush() catch unreachable; - log_writing(writer, complete_format, true, args); + log_writing(io, writer, complete_format, true, args); } } -inline fn log_writing(writer: *std.Io.Writer, comptime format: []const u8, comptime ansi: bool, args: anytype) void { +inline fn log_writing(io: std.Io, writer: *std.Io.Writer, comptime format: []const u8, comptime ansi: bool, args: anytype) void { nosuspend { - if (build_options.timestamp) log_timestamp(writer, ansi); + if (build_options.timestamp) log_timestamp(io, writer, ansi); writer.print(format, args) catch return; } } @@ -65,9 +66,10 @@ inline fn log_writing(writer: *std.Io.Writer, comptime format: []const u8, compt /// `// :: ` /// /// NOTE all information are displayed using numbers -inline fn log_timestamp(writer: anytype, comptime ansi: bool) void { - const time = std.posix.clock_gettime(.REALTIME) catch @panic("Cannot request timestamp"); - const epoch_secs: std.time.epoch.EpochSeconds = .{ .secs = @intCast(time.sec) }; +/// NOTE the timestamp is printed in UTC time regardless of the machine's configuration +inline fn log_timestamp(io: std.Io, writer: anytype, comptime ansi: bool) void { + const time_ms: u64 = @intCast(std.Io.Timestamp.now(io, .real).toMilliseconds()); + const epoch_secs: std.time.epoch.EpochSeconds = .{ .secs = @intCast(time_ms / std.time.ms_per_s) }; const day_secs = epoch_secs.getDaySeconds(); const year_and_day = epoch_secs.getEpochDay().calculateYearDay(); const month_and_day = year_and_day.calculateMonthDay(); @@ -222,7 +224,7 @@ pub const std_options: std.Options = .{ }; const std = @import("std"); -const fs = std.fs; +const Io = std.Io; const log = std.log; const fmt = std.fmt; const build_options = @import("build_options");