xgoprev/xmodules/logx/main.go

78 lines
1.4 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:]))
2024-06-06 15:00:39 +03:00
for i, o := range args[1:] {
v[i] = tengo.ToInterface(o)
}*/
2024-06-06 15:00:39 +03:00
str, err := tengo.Format(format, args...)
if err != nil {
return nil, err
}
log.Print(str)
2024-06-06 15:00:39 +03:00
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{}
}
2024-06-06 15:00:39 +03:00
}
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{}
}
2024-06-06 15:00:39 +03:00
}
log.Println(v...)
return nil, nil
},
},
}