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

chore: add trace logging #48

Merged
merged 1 commit into from
Oct 24, 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
14 changes: 10 additions & 4 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,25 +121,31 @@ func extismHTTPRequest(request, body extismPointer) extismPointer
func extismHTTPStatusCode() int32

// extismLogInfo logs an "info" string to the host from the previously-written UTF-8 string written to `offset`.
// Note that the memory at `offset` can be immediately freed because it is immediately logged.
//
//go:wasmimport extism:host/env log_info
func extismLogInfo(offset extismPointer)

// extismLogDebug logs a "debug" string to the host from the previously-written UTF-8 string written to `offset`.
// Note that the memory at `offset` can be immediately freed because it is immediately logged.
//
//go:wasmimport extism:host/env log_debug
func extismLogDebug(offset extismPointer)

// extismLogWarn logs a "warning" string to the host from the previously-written UTF-8 string written to `offset`.
// Note that the memory at `offset` can be immediately freed because it is immediately logged.
//
//go:wasmimport extism:host/env log_warn
func extismLogWarn(offset extismPointer)

// extismLogError logs an "error" string to the host from the previously-written UTF-8 string written to `offset`.
// Note that the memory at `offset` can be immediately freed because it is immediately logged.
//
//go:wasmimport extism:host/env log_error
func extismLogError(offset extismPointer)

// extismLogTrace logs an "error" string to the host from the previously-written UTF-8 string written to `offset`.
//
//go:wasmimport extism:host/env log_error
func extismLogTrace(offset extismPointer)

// extismGetLogLevel returns the configured log level
//
//go:wasmimport extism:host/env get_log_level
func extismGetLogLevel() int32
10 changes: 8 additions & 2 deletions extism_pdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ type Memory struct {
type LogLevel int

const (
LogInfo LogLevel = iota
LogTrace LogLevel = iota
LogDebug
LogInfo
LogWarn
LogError
LogTrace
)

func load(offset extismPointer, buf []byte) {
Expand Down Expand Up @@ -205,6 +205,10 @@ func GetConfig(key string) (string, bool) {

// LogMemory logs the `memory` block on the host using the provided log `level`.
func LogMemory(level LogLevel, memory Memory) {
configuredLevel := extismGetLogLevel()
if level < LogLevel(configuredLevel) {
return
}
switch level {
case LogInfo:
extismLogInfo(memory.offset)
Expand All @@ -214,6 +218,8 @@ func LogMemory(level LogLevel, memory Memory) {
extismLogWarn(memory.offset)
case LogError:
extismLogError(memory.offset)
case LogTrace:
extismLogTrace(memory.offset)
}
}

Expand Down
Loading