Skip to content

Commit

Permalink
feat: prevent log allocation unless needed
Browse files Browse the repository at this point in the history
  • Loading branch information
nilslice committed Aug 30, 2024
1 parent 49215e0 commit 2e03046
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
11 changes: 11 additions & 0 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ func extismHTTPRequest(request, body extismPointer) extismPointer
//go:wasmimport extism:host/env http_status_code
func extismHTTPStatusCode() int32

// extismGetLogLevel returns the current log level set in the Extism runtime.
//
//go:wasmimport extism:host/env get_log_level
func extismGetLogLevel() int32

// extismLogTrace logs a "trace" 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_trace
func extismLogTrace(offset 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.
//
Expand Down
45 changes: 28 additions & 17 deletions extism_pdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pdk
import (
"encoding/binary"
"encoding/json"
"math"
)

// Memory represents memory allocated by (and shared with) the host.
Expand All @@ -12,14 +13,16 @@ type Memory struct {
}

// LogLevel represents a logging level.
type LogLevel int
type LogLevel int32

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

LogOff LogLevel = math.MaxInt32
)

func load(offset extismPointer, buf []byte) {
Expand Down Expand Up @@ -205,27 +208,35 @@ 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) {
globalLogLevel := LogLevel(extismGetLogLevel())
if level > globalLogLevel {
return
}

}

// Log logs a message at the specified log level, but only if the current log level is high enough.
func Log(level LogLevel, message string) {
globalLogLevel := LogLevel(extismGetLogLevel())
if level < globalLogLevel {
return
}

mem := AllocateString(message)
switch level {
case LogInfo:
extismLogInfo(memory.offset)
case LogTrace:
extismLogTrace(mem.offset)
case LogDebug:
extismLogDebug(memory.offset)
extismLogDebug(mem.offset)
case LogInfo:
extismLogInfo(mem.offset)
case LogWarn:
extismLogWarn(memory.offset)
extismLogWarn(mem.offset)
case LogError:
extismLogError(memory.offset)
extismLogError(mem.offset)
}
}

// Log logs the provided UTF-8 string `s` on the host using the provided log `level`.
func Log(level LogLevel, s string) {
mem := AllocateString(s)
// TODO: coordinate replacement of call to free based on SDK alignment
// defer mem.Free()

LogMemory(level, mem)
}

// GetVar returns the byte slice (if any) associated with `key`.
func GetVar(key string) []byte {
mem := AllocateBytes([]byte(key))
Expand Down

0 comments on commit 2e03046

Please sign in to comment.