Skip to content

Commit

Permalink
feat: make loglevel pub and only alloc when needed (#38)
Browse files Browse the repository at this point in the history
* feat: make loglevel pub and only alloc when needed

* feat: add fmt log option, also error
  • Loading branch information
nilslice authored Nov 4, 2024
1 parent 634cd5f commit d25f594
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/ffi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
37 changes: 33 additions & 4 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -139,16 +144,26 @@ 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);
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
Expand Down Expand Up @@ -216,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;
}

0 comments on commit d25f594

Please sign in to comment.