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: prevent log allocation unless needed #46

Closed
wants to merge 1 commit into from
Closed
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
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
50 changes: 33 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,40 @@ 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
}

logMemory(level, memory)
}

// 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)
logMemory(level, mem)
}

func logMemory(level LogLevel, mem Memory) {
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
Loading