xgoprev/xmodules/logx/main.go

77 lines
1.4 KiB
Go

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)
}*/
str, err := tengo.Format(format, args...)
if err != nil {
return nil, err
}
log.Print(str)
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 {
var ok bool
v[i], ok = tengo.ToString(o)
if !ok {
v[i] = tengo.Undefined{}
}
}
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 {
var ok bool
v[i], ok = tengo.ToString(o)
if !ok {
v[i] = tengo.Undefined{}
}
}
log.Println(v...)
return nil, nil
},
},
}