edge/pkg/module/console.go

45 lines
853 B
Go

package module
import (
"context"
"fmt"
"strings"
"gitlab.com/wpetit/goweb/logger"
"forge.cadoles.com/arcad/edge/pkg/app"
"github.com/dop251/goja"
"github.com/pkg/errors"
)
type ConsoleModule struct{}
func (m *ConsoleModule) Name() string {
return "console"
}
func (m *ConsoleModule) log(call goja.FunctionCall) goja.Value {
var sb strings.Builder
for _, arg := range call.Arguments {
sb.WriteString(fmt.Sprintf("%+v", arg.Export()))
sb.WriteString(" ")
}
logger.Debug(context.Background(), sb.String())
return nil
}
func (m *ConsoleModule) Export(export *goja.Object) {
if err := export.Set("log", m.log); err != nil {
panic(errors.Wrap(err, "could not set 'log' function"))
}
}
func ConsoleModuleFactory() app.BackendModuleFactory {
return func(backend *app.Backend) app.BackendModule {
return &ConsoleModule{}
}
}