Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make loglevel pub and only alloc when needed #38

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quality of life improvement (relates to #32)


/// 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);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quality of life improvement (relates to #32)

}

/// 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;
}
Comment on lines +235 to +247
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zshipko, just a second set of eyes on the logic here would be helpful :)

Loading