66 lines
1.2 KiB
Go
66 lines
1.2 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)
|
||
|
}
|
||
|
|
||
|
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
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
|