logging

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Example (NewHostLoggingListenerFactory)

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

package main

import (
	"context"
	"log"
	"os"

	_ "embed"
	"wa-lang.org/wazero"
	"wa-lang.org/wazero/experimental"
	"wa-lang.org/wazero/experimental/logging"
	"wa-lang.org/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)
	<== ESUCCESS
	==> wasi_snapshot_preview1.random_get(buf=8,buf_len=4)
	<== ESUCCESS
<-- ()
Example (NewLoggingListenerFactory)

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

package main

import (
	"context"
	"log"
	"os"

	_ "embed"
	"wa-lang.org/wazero"
	"wa-lang.org/wazero/experimental"
	"wa-lang.org/wazero/experimental/logging"
	"wa-lang.org/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)
		<== ESUCCESS
		==> wasi_snapshot_preview1.random_get(buf=8,buf_len=4)
		<== ESUCCESS
	<-- ()
<-- ()

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHostLoggingListenerFactory

func NewHostLoggingListenerFactory(writer io.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 writer in order to provide minimal context needed to understand host calls such as "fd_open".

func NewLoggingListenerFactory

func NewLoggingListenerFactory(writer io.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

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL