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 27, 2024
1 parent 49215e0 commit afd28f2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 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
1 change: 1 addition & 0 deletions example/countvowels/tiny_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func countVowels() int32 {
}

output := `{"count": ` + strconv.Itoa(count) + `, "config": "` + thing + `", "a": "` + string(varA) + `"}`
pdk.Log(pdk.LogWarn, "WE GOT LOGS: "+output)
mem := pdk.AllocateString(output)

// zero-copy output to host
Expand Down
29 changes: 20 additions & 9 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
LogInfo
LogDebug
LogWarn
LogError
LogTrace
)

func load(offset extismPointer, buf []byte) {
Expand Down Expand Up @@ -217,13 +217,24 @@ func LogMemory(level LogLevel, memory Memory) {
}
}

// 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)
// 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) {
currentLevel := LogLevel(extismGetLogLevel())
if currentLevel <= level {
mem := AllocateString(message)
switch level {
case LogInfo:
extismLogInfo(mem.offset)
case LogDebug:
extismLogDebug(mem.offset)
case LogWarn:
extismLogWarn(mem.offset)
case LogError:
extismLogError(mem.offset)
case LogTrace:
extismLogTrace(mem.offset)
}
}
}

// GetVar returns the byte slice (if any) associated with `key`.
Expand Down

0 comments on commit afd28f2

Please sign in to comment.