Skip to content

Commit

Permalink
chore: add trace logging (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko authored Oct 24, 2024
1 parent cf652fc commit 55cb612
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
14 changes: 10 additions & 4 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,31 @@ func extismHTTPStatusCode() int32
func extismHTTPHeaders() extismPointer

// 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

0 comments on commit 55cb612

Please sign in to comment.