Implement a more better logging solution

This commit is contained in:
2025-03-27 17:47:39 +01:00
parent 81d8259dfc
commit 9cea103042
7 changed files with 579 additions and 78 deletions

View File

@@ -2,13 +2,14 @@ package processor
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/antchfx/xmlquery"
lua "github.com/yuin/gopher-lua"
"modify/logger"
)
// Processor defines the interface for all file processors
@@ -42,14 +43,18 @@ func NewLuaState() (*lua.LState, error) {
// defer L.Close()
// Load math library
logger.Debug("Loading Lua math library")
L.Push(L.GetGlobal("require"))
L.Push(lua.LString("math"))
if err := L.PCall(1, 1, nil); err != nil {
logger.Error("Failed to load Lua math library: %v", err)
return nil, fmt.Errorf("error loading Lua math library: %v", err)
}
// Initialize helper functions
logger.Debug("Initializing Lua helper functions")
if err := InitLuaHelpers(L); err != nil {
logger.Error("Failed to initialize Lua helper functions: %v", err)
return nil, err
}
@@ -60,29 +65,40 @@ func Process(p Processor, filename string, pattern string, luaExpr string) (int,
// Read file content
cwd, err := os.Getwd()
if err != nil {
logger.Error("Failed to get current working directory: %v", err)
return 0, 0, fmt.Errorf("error getting current working directory: %v", err)
}
fullPath := filepath.Join(cwd, filename)
logger.Trace("Reading file content from: %s", fullPath)
content, err := os.ReadFile(fullPath)
if err != nil {
logger.Error("Failed to read file %s: %v", fullPath, err)
return 0, 0, fmt.Errorf("error reading file: %v", err)
}
fileContent := string(content)
logger.Trace("File %s read successfully, size: %d bytes", fullPath, len(content))
// Process the content
logger.Debug("Processing content for file: %s", filename)
modifiedContent, modCount, matchCount, err := p.ProcessContent(fileContent, pattern, luaExpr)
if err != nil {
logger.Error("Error processing content for file %s: %v", filename, err)
return 0, 0, err
}
// If we made modifications, save the file
if modCount > 0 {
logger.Info("Writing %d modifications to file: %s", modCount, filename)
err = os.WriteFile(fullPath, []byte(modifiedContent), 0644)
if err != nil {
logger.Error("Failed to write to file %s: %v", fullPath, err)
return 0, 0, fmt.Errorf("error writing file: %v", err)
}
logger.Debug("File %s written successfully", filename)
} else {
logger.Debug("No modifications to write for file: %s", filename)
}
return modCount, matchCount, nil
@@ -174,25 +190,28 @@ func FromLua(L *lua.LState, luaValue lua.LValue) (interface{}, error) {
}
func IsLuaTableArray(L *lua.LState, v *lua.LTable) (bool, error) {
logger.Trace("Checking if Lua table is an array")
L.SetGlobal("table_to_check", v)
// Use our predefined helper function from InitLuaHelpers
err := L.DoString(`is_array = isArray(table_to_check)`)
if err != nil {
logger.Error("Error determining if table is an array: %v", err)
return false, fmt.Errorf("error determining if table is array: %w", err)
}
// Check the result of our Lua function
isArray := L.GetGlobal("is_array")
// LVIsFalse returns true if a given LValue is a nil or false otherwise false.
if !lua.LVIsFalse(isArray) {
return true, nil
}
return false, nil
result := !lua.LVIsFalse(isArray)
logger.Trace("Lua table is array: %v", result)
return result, nil
}
// InitLuaHelpers initializes common Lua helper functions
func InitLuaHelpers(L *lua.LState) error {
logger.Debug("Loading Lua helper functions")
helperScript := `
-- Custom Lua helpers for math operations
function min(a, b) return math.min(a, b) end
@@ -239,9 +258,11 @@ end
modified = false
`
if err := L.DoString(helperScript); err != nil {
logger.Error("Failed to load Lua helper functions: %v", err)
return fmt.Errorf("error loading helper functions: %v", err)
}
logger.Debug("Setting up Lua print function to Go")
L.SetGlobal("print", L.NewFunction(printToGo))
return nil
}
@@ -280,6 +301,8 @@ func PrependLuaAssignment(luaExpr string) string {
// BuildLuaScript prepares a Lua expression from shorthand notation
func BuildLuaScript(luaExpr string) string {
logger.Debug("Building Lua script from expression: %s", luaExpr)
luaExpr = PrependLuaAssignment(luaExpr)
// This allows the user to specify whether or not they modified a value
@@ -300,17 +323,14 @@ func BuildLuaScript(luaExpr string) string {
}
func printToGo(L *lua.LState) int {
// Get the number of arguments passed to the Lua print function
n := L.GetTop()
// Create a slice to hold the arguments
args := make([]interface{}, n)
for i := 1; i <= n; i++ {
args[i-1] = L.Get(i) // Get the argument from Lua stack
top := L.GetTop()
args := make([]interface{}, top)
for i := 1; i <= top; i++ {
args[i-1] = L.Get(i)
}
// Print the arguments to Go's stdout
log.Print("Lua: ")
log.Println(args...)
return 0 // No return values
message := fmt.Sprint(args...)
logger.Info("[Lua] %s", message)
return 0
}
// Max returns the maximum of two integers