package logging

import "github.com/tetratelabs/wazero/experimental/logging"

Example (NewLoggingListenerFactory)

This is a very basic integration of listener. The main goal is to show how it is configured.

Code:play 

package main

import (
	"context"
	_ "embed"
	"log"
	"os"

	"github.com/tetratelabs/wazero"
	"github.com/tetratelabs/wazero/experimental"
	"github.com/tetratelabs/wazero/experimental/logging"
	"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)

// listenerWasm was generated by the following:
//
//	cd testdata; wat2wasm --debug-names listener.wat
//
//go:embed testdata/listener.wasm
var listenerWasm []byte

// This is a very basic integration of listener. The main goal is to show how it is configured.
func main() {
	// Set context to one that has an experimental listener
	ctx := context.WithValue(context.Background(), experimental.FunctionListenerFactoryKey{}, logging.NewLoggingListenerFactory(os.Stdout))

	r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigInterpreter())
	defer r.Close(ctx) // This closes everything this Runtime created.

	if _, err := wasi_snapshot_preview1.Instantiate(ctx, r); err != nil {
		log.Panicln(err)
	}

	// Compile the WebAssembly module using the default configuration.
	code, err := r.CompileModule(ctx, listenerWasm, wazero.NewCompileConfig())
	if err != nil {
		log.Panicln(err)
	}

	mod, err := r.InstantiateModule(ctx, code, wazero.NewModuleConfig().WithStdout(os.Stdout))
	if err != nil {
		log.Panicln(err)
	}

	_, err = mod.ExportedFunction("rand").Call(ctx, 4)
	if err != nil {
		log.Panicln(err)
	}

	// We should see the same function called twice: directly and indirectly.

}

Output:

--> listener.rand(len=4)
	==> wasi_snapshot_preview1.random_get(buf=4,buf_len=4)
	<== ESUCCESS
	==> wasi_snapshot_preview1.random_get(buf=8,buf_len=4)
	<== ESUCCESS
<-- ()

Index

Examples

Functions

func NewLoggingListenerFactory

func NewLoggingListenerFactory(writer io.Writer) experimental.FunctionListenerFactory

NewLoggingListenerFactory implements FunctionListenerFactory to log all functions that have a name to the writer.

Source Files

log_listener.go

Version
v1.0.0-beta.2
Published
Aug 31, 2022
Platform
linux/amd64
Imports
8 packages
Last checked
2 hours ago

Tools for package owners.