Skip to content

Commit

Permalink
Better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
orsinium authored and deadprogram committed Feb 26, 2024
1 parent d62439d commit 24f5130
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 17 deletions.
12 changes: 8 additions & 4 deletions engine/engine.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package engine

import "errors"
import (
"errors"

"github.com/hybridgroup/mechanoid"
)

var (
ErrInvalidEngine = errors.New("engine: invalid engine")
Expand Down Expand Up @@ -40,19 +44,19 @@ func (e *Engine) Init() error {
return ErrNoInterpreter
}

println("Initializing interpreter...")
mechanoid.Log("Initializing interpreter...")
if err := e.Interpreter.Init(); err != nil {
return err
}

if e.FileStore != nil {
println("Initializing file store...")
mechanoid.Log("Initializing file store...")
if err := e.FileStore.Init(); err != nil {
return err
}
}

println("Initializing devices...")
mechanoid.Log("Initializing devices...")
for _, d := range e.Devices {
if err := d.Init(); err != nil {
return err
Expand Down
3 changes: 0 additions & 3 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ func (i *mockInterpreter) DefineFunc(modulename, funcname string, f interface{})
return nil
}

func (i *mockInterpreter) Log(msg string) {
}

func (i *mockInterpreter) MemoryData(ptr, sz uint32) ([]byte, error) {
return nil, nil
}
2 changes: 0 additions & 2 deletions engine/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ type Interpreter interface {
Halt() error
// DefineFunc defines a function in the host module.
DefineFunc(module, name string, f interface{}) error
// Log logs a message.
Log(msg string)
// MemoryData returns a slice of memory data from the memory managed by the host.
MemoryData(ptr, sz uint32) ([]byte, error)
}
7 changes: 4 additions & 3 deletions filestore/flash/flash.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"os"

"github.com/hybridgroup/mechanoid"
"github.com/hybridgroup/mechanoid/engine"
"tinygo.org/x/tinyfs"
"tinygo.org/x/tinyfs/littlefs"
Expand Down Expand Up @@ -57,15 +58,15 @@ func (fs *FileStore) Init() error {
fs.lfs = lfs

// Mount the filesystem
println("Mounting filesystem...")
mechanoid.Log("Mounting filesystem...")
if err := fs.lfs.Mount(); err != nil {
// if the filesystem cannot be mounted, try to format it
println("Formatting new filesystem...")
mechanoid.Log("Formatting new filesystem...")
if err := fs.lfs.Format(); err != nil {
return err
}

println("Mounting new filesystem...")
mechanoid.Log("Mounting new filesystem...")
// if the format was successful, try to mount again
if err := fs.lfs.Mount(); err != nil {
return err
Expand Down
7 changes: 2 additions & 5 deletions interp/wasman/interp.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wasman

import (
"github.com/hybridgroup/mechanoid"
"github.com/hybridgroup/mechanoid/engine"

wasmaneng "github.com/hybridgroup/wasman"
Expand Down Expand Up @@ -39,7 +40,7 @@ func (i *Interpreter) Init() error {
func (i *Interpreter) Load(code []byte) error {
conf := config.ModuleConfig{
Recover: true,
Logger: i.Log,
Logger: mechanoid.Log,
}

var err error
Expand Down Expand Up @@ -137,10 +138,6 @@ func (i *Interpreter) DefineFunc(moduleName, funcName string, f interface{}) err
}
}

func (i *Interpreter) Log(msg string) {
println(msg)
}

func (i *Interpreter) MemoryData(ptr, sz uint32) ([]byte, error) {
if i.instance.Memory == nil {
return nil, engine.ErrMemoryNotDefined
Expand Down
8 changes: 8 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package mechanoid

// Log a message into terminal if debug mode is enabled
func Log(msg string) {
if debug {
println(msg)
}
}

0 comments on commit 24f5130

Please sign in to comment.