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