From 3dc56ea6dbafb5a065a9c0af703ac9ab26756497 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Thu, 13 Jun 2024 16:56:49 +0200 Subject: [PATCH] fix(log): allow std.log calls outside of actor contexts --- src/log.zig | 11 +++++++++-- src/main.zig | 2 ++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/log.zig b/src/log.zig index e4c2ec3..1725d9e 100644 --- a/src/log.zig +++ b/src/log.zig @@ -181,18 +181,25 @@ pub fn unsubscribe() tp.result { return tp.env.get().proc("log").send(.{"unsubscribe"}); } +var std_log_pid: ?tp.pid_ref = null; + +pub fn set_std_log_pid(pid: ?tp.pid_ref) void { + std_log_pid = pid; +} + pub fn std_log_function( comptime level: std.log.Level, comptime scope: @TypeOf(.EnumLiteral), comptime format: []const u8, args: anytype, ) void { + const log_pid = std_log_pid orelse return; const prefix = "[" ++ comptime level.asText() ++ "] "; var buf: [max_log_message]u8 = undefined; const output = std.fmt.bufPrint(&buf, prefix ++ format, args) catch "MESSAGE TOO LARGE"; if (level == .err) { - tp.env.get().proc("log").send(.{ "log", "error", @tagName(scope), "std.log", "->", output }) catch {}; + log_pid.send(.{ "log", "error", @tagName(scope), "std.log", "->", output }) catch {}; } else { - tp.env.get().proc("log").send(.{ "log", @tagName(scope), output }) catch {}; + log_pid.send(.{ "log", @tagName(scope), output }) catch {}; } } diff --git a/src/main.zig b/src/main.zig index 600555a..c6a2c5d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -133,6 +133,8 @@ pub fn main() anyerror!void { const log_proc = try log.spawn(&ctx, a, &env); defer log_proc.deinit(); + log.set_std_log_pid(log_proc.ref()); + defer log.set_std_log_pid(null); env.set("restore-session", (res.args.@"restore-session" != 0)); env.set("no-alternate", (res.args.@"no-alternate" != 0));