log caller

This commit is contained in:
Meo597
2026-09-26 15:42:57 +08:00
parent 219f758060
commit 70b8e9a61d
2 changed files with 34 additions and 11 deletions
+9 -3
View File
@@ -1,15 +1,13 @@
package log
import (
"path/filepath"
"strings"
lua "github.com/yuin/gopher-lua"
)
// RegisterLua makes xray.log available to require in an LState.
// Logging functions concatenate arguments using Lua's tostring semantics,
// except Go errors in userdata use Error(). Messages go through the current
// log handler.
func RegisterLua(L *lua.LState) {
L.PreloadModule("xray.log", func(L *lua.LState) int {
module := L.NewTable()
@@ -21,8 +19,16 @@ func RegisterLua(L *lua.LState) {
} {
module.RawSetString(name, L.NewFunction(func(L *lua.LState) int {
var content strings.Builder
// Prefix with the calling script's filename.
if caller, ok := L.GetStack(1); ok {
if _, err := L.GetInfo("S", caller, lua.LNil); err == nil && caller.Source != "" {
content.WriteString(filepath.Base(strings.TrimPrefix(caller.Source, "@")))
content.WriteString(": ")
}
}
for i := 1; i <= L.GetTop(); i++ {
value := L.Get(i)
// Use Error() for Go errors in userdata.
if ud, ok := value.(*lua.LUserData); ok {
if err, ok := ud.Value.(error); ok {
content.WriteString(err.Error())