From 800966349345a5bb5a8db1177e753d842184a804 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Mon, 4 Nov 2024 11:47:03 -0800 Subject: [PATCH 1/2] feat: make loglevel pub and only alloc when needed --- src/ffi.zig | 4 +++- src/main.zig | 17 +++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/ffi.zig b/src/ffi.zig index cb62859..815631a 100644 --- a/src/ffi.zig +++ b/src/ffi.zig @@ -17,7 +17,9 @@ pub extern "extism:host/env" fn store_u64(ExtismPointer, u64) void; pub extern "extism:host/env" fn load_u64(ExtismPointer) u64; pub extern "extism:host/env" fn http_request(ExtismPointer, ExtismPointer) ExtismPointer; pub extern "extism:host/env" fn http_status_code() i32; -pub extern "extism:host/env" fn log_info(ExtismPointer) void; +pub extern "extism:host/env" fn get_log_level() i32; +pub extern "extism:host/env" fn log_trace(ExtismPointer) void; pub extern "extism:host/env" fn log_debug(ExtismPointer) void; +pub extern "extism:host/env" fn log_info(ExtismPointer) void; pub extern "extism:host/env" fn log_warn(ExtismPointer) void; pub extern "extism:host/env" fn log_error(ExtismPointer) void; diff --git a/src/main.zig b/src/main.zig index bda94d2..90ce774 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3,7 +3,7 @@ const extism = @import("ffi.zig"); const Memory = @import("Memory.zig"); pub const http = @import("http.zig"); -const LogLevel = enum { Info, Debug, Warn, Error }; +pub const LogLevel = enum { Trace, Debug, Info, Warn, Error }; pub fn Json(comptime T: type) type { return struct { @@ -139,16 +139,25 @@ pub const Plugin = struct { pub fn logMemory(self: Plugin, level: LogLevel, memory: Memory) void { _ = self; // to make the interface consistent switch (level) { - .Info => extism.log_info(memory.offset), + .Trace => extism.log_trace(memory.offset), .Debug => extism.log_debug(memory.offset), + .Info => extism.log_info(memory.offset), .Warn => extism.log_warn(memory.offset), .Error => extism.log_error(memory.offset), } } pub fn log(self: Plugin, level: LogLevel, data: []const u8) void { - const mem = self.allocateBytes(data); - self.logMemory(level, mem); + const currentLevelInt = extism.get_log_level(); + if (currentLevelInt == std.math.maxInt(i32)) { + return; + } + + const levelInt = @intFromEnum(level); + if (levelInt >= currentLevelInt) { + const mem = self.allocateBytes(data); + self.logMemory(level, mem); + } } /// IMPORTANT: it's the caller's responsibility to free the returned string From edf29a504b0fc5605960adda6478487ec6413070 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Mon, 4 Nov 2024 11:58:12 -0800 Subject: [PATCH 2/2] feat: add fmt log option, also error --- src/main.zig | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/main.zig b/src/main.zig index 90ce774..e56c2ff 100644 --- a/src/main.zig +++ b/src/main.zig @@ -122,6 +122,11 @@ pub const Plugin = struct { extism.error_set(offset); } + pub fn setErrorFmt(self: Plugin, comptime fmt: []const u8, args: anytype) !void { + const data = try std.fmt.allocPrint(self.allocator, fmt, args); + self.setError(data); + } + /// IMPORTANT: it's the caller's responsibility to free the returned string pub fn getConfig(self: Plugin, key: []const u8) !?[]u8 { const key_mem = self.allocateBytes(key); @@ -148,18 +153,19 @@ pub const Plugin = struct { } pub fn log(self: Plugin, level: LogLevel, data: []const u8) void { - const currentLevelInt = extism.get_log_level(); - if (currentLevelInt == std.math.maxInt(i32)) { - return; - } - - const levelInt = @intFromEnum(level); - if (levelInt >= currentLevelInt) { + if (loggingEnabled(level)) { const mem = self.allocateBytes(data); self.logMemory(level, mem); } } + pub fn logFmt(self: Plugin, level: LogLevel, comptime fmt: []const u8, args: anytype) !void { + if (loggingEnabled(level)) { + const data = try std.fmt.allocPrint(self.allocator, fmt, args); + self.log(level, data); + } + } + /// IMPORTANT: it's the caller's responsibility to free the returned string pub fn getVar(self: Plugin, key: []const u8) !?[]u8 { const key_mem = self.allocateBytes(key); @@ -225,3 +231,17 @@ pub const Plugin = struct { }; } }; + +fn loggingEnabled(level: LogLevel) bool { + const currentLevelInt = extism.get_log_level(); + if (currentLevelInt == std.math.maxInt(i32)) { + return false; + } + + const levelInt = @intFromEnum(level); + if (levelInt >= currentLevelInt) { + return true; + } + + return false; +}