fix(log): allow std.log calls outside of actor contexts

This commit is contained in:
CJ van den Berg 2024-06-13 16:56:49 +02:00
parent 578b7bdbda
commit 3dc56ea6db
2 changed files with 11 additions and 2 deletions

View file

@ -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 {};
}
}

View file

@ -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));