xgoprev/xmodules/logx/main.go

66 lines
1.2 KiB
Go
Raw Normal View History

2024-06-06 15:00:39 +03:00
package logx
import "github.com/d5/tengo/v2"
import "log"
var Module = map[string]tengo.Object{
"printf": &tengo.BuiltinFunction{
Name: "printf",
Value: func(
args ...tengo.Object,
) (tengo.Object, error) {
if len(args) < 1 {
return nil, tengo.ErrWrongNumArguments
}
formatObject := args[0]
format, hasFormat := tengo.ToString(
formatObject,
)
if !hasFormat {
return nil, tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "string",
Found: formatObject.TypeName(),
}
}
v := make([]any, len(args[1:]))
for i, o := range args[1:] {
v[i] = tengo.ToInterface(o)
}
log.Printf(format, v...)
return nil, nil
},
},
"print": &tengo.BuiltinFunction{
Name: "print",
Value: func(
args ...tengo.Object,
) (tengo.Object, error){
v := make([]any, len(args))
for i, o := range args {
v[i] = tengo.ToInterface(o)
}
log.Print(v...)
return nil, nil
},
},
"println": &tengo.BuiltinFunction{
Name: "println",
Value: func(
args ...tengo.Object,
) (tengo.Object, error){
v := make([]any, len(args))
for i, o := range args {
v[i] = tengo.ToInterface(o)
}
log.Println(v...)
return nil, nil
},
},
}