package logging

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

Example (NewHostLoggingListenerFactory)

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

Code:play 

package main

import (
	"context"
	"log"
	"os"

	_ "embed"
	"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

func main() {
	// Set context to one that has an experimental listener
	ctx := context.WithValue(context.Background(), experimental.FunctionListenerFactoryKey{}, logging.NewHostLoggingListenerFactory(os.Stdout))

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

	wasi_snapshot_preview1.MustInstantiate(ctx, r)

	// Compile the WebAssembly module using the default configuration.
	code, err := r.CompileModule(ctx, listenerWasm)
	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)
	<== errno=ESUCCESS
	==> wasi_snapshot_preview1.random_get(buf=8,buf_len=4)
	<== errno=ESUCCESS
<--
Example (NewLoggingListenerFactory)

This example shows how to see all function calls, including between host functions.

Code:play 

package main

import (
	"context"
	"log"
	"os"

	_ "embed"
	"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

func main() {
	// Set context to one that has an experimental listener
	ctx := context.WithValue(context.Background(), experimental.FunctionListenerFactoryKey{}, logging.NewLoggingListenerFactory(os.Stdout))

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

	wasi_snapshot_preview1.MustInstantiate(ctx, r)

	// Compile the WebAssembly module using the default configuration.
	code, err := r.CompileModule(ctx, listenerWasm)
	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)
	--> listener.wasi_rand(len=4)
		==> wasi_snapshot_preview1.random_get(buf=4,buf_len=4)
		<== errno=ESUCCESS
		==> wasi_snapshot_preview1.random_get(buf=8,buf_len=4)
		<== errno=ESUCCESS
	<--
<--

Index

Examples

Functions

func NewFilesystemLoggingListenerFactory

func NewFilesystemLoggingListenerFactory(w Writer) experimental.FunctionListenerFactory

NewFilesystemLoggingListenerFactory is an experimental.FunctionListenerFactory that logs exported filesystem functions to the writer.

This is an alternative to NewHostLoggingListenerFactory.

func NewHostLoggingListenerFactory

func NewHostLoggingListenerFactory(w Writer) experimental.FunctionListenerFactory

NewHostLoggingListenerFactory is an experimental.FunctionListenerFactory that logs exported and host functions to the writer.

This is an alternative to NewLoggingListenerFactory, and would weed out guest defined functions such as those implementing garbage collection.

For example, "_start" is defined by the guest, but exported, so would be written to the w in order to provide minimal context needed to understand host calls such as "args_get".

func NewLoggingListenerFactory

func NewLoggingListenerFactory(w Writer) experimental.FunctionListenerFactory

NewLoggingListenerFactory is an experimental.FunctionListenerFactory that logs all functions that have a name to the writer.

Use NewHostLoggingListenerFactory if only interested in host interactions.

Types

type Writer

type Writer interface {
	io.Writer
	io.StringWriter
}

Source Files

log_listener.go

Version
v1.0.0-pre.6
Published
Dec 31, 2022
Platform
linux/amd64
Imports
9 packages
Last checked
1 hour ago

Tools for package owners.