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"
"regexp"
"sort"
"strconv"
"strings"
lua "github.com/yuin/gopher-lua"
"modify/logger"
)
// RegexProcessor implements the Processor interface using regex patterns
@@ -98,16 +99,17 @@ type ReplaceCommand struct {
// ProcessContent applies regex replacement with Lua processing
func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr string) (string, int, int, error) {
pattern = ResolveRegexPlaceholders(pattern)
logger.Debug("Compiling regex pattern: %s", pattern)
compiledPattern, err := regexp.Compile(pattern)
if err != nil {
log.Printf("Error compiling pattern: %v", err)
logger.Error("Error compiling pattern: %v", err)
return "", 0, 0, fmt.Errorf("error compiling pattern: %v", err)
}
log.Printf("Compiled pattern successfully: %s", pattern)
logger.Debug("Compiled pattern successfully: %s", pattern)
previous := luaExpr
luaExpr = BuildLuaScript(luaExpr)
log.Printf("Changing Lua expression from: %s to: %s", previous, luaExpr)
logger.Debug("Changing Lua expression from: %s to: %s", previous, luaExpr)
// Initialize Lua environment
modificationCount := 0
@@ -115,7 +117,7 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
// Process all regex matches
result := content
indices := compiledPattern.FindAllStringSubmatchIndex(content, -1)
log.Printf("Found %d matches in the content", len(indices))
logger.Debug("Found %d matches in the content", len(indices))
// We walk backwards because we're replacing something with something else that might be longer
// And in the case it is longer than the original all indicces past that change will be fucked up
@@ -124,17 +126,17 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
for i := len(indices) - 1; i >= 0; i-- {
L, err := NewLuaState()
if err != nil {
log.Printf("Error creating Lua state: %v", err)
logger.Error("Error creating Lua state: %v", err)
return "", 0, 0, fmt.Errorf("error creating Lua state: %v", err)
}
// Hmm... Maybe we don't want to defer this..
// Maybe we want to close them every iteration
// We'll leave it as is for now
defer L.Close()
log.Printf("Lua state created successfully")
logger.Trace("Lua state created successfully")
matchIndices := indices[i]
log.Printf("Processing match indices: %v", matchIndices)
logger.Trace("Processing match indices: %v", matchIndices)
// Why we're doing this whole song and dance of indices is to properly handle empty matches
// Plus it's a little cleaner to surgically replace our matches
@@ -144,21 +146,21 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
// As if concatenating in the middle of the array
// Plus it supports lookarounds
match := content[matchIndices[0]:matchIndices[1]]
log.Printf("Matched content: %s", match)
logger.Trace("Matched content: %s", match)
groups := matchIndices[2:]
if len(groups) <= 0 {
log.Println("No capture groups for lua to chew on")
logger.Warning("No capture groups for lua to chew on")
continue
}
if len(groups)%2 == 1 {
log.Println("Odd number of indices of groups, what the fuck?")
logger.Warning("Odd number of indices of groups, what the fuck?")
continue
}
for _, index := range groups {
if index == -1 {
// return "", 0, 0, fmt.Errorf("negative indices encountered: %v. This indicates that there was an issue with the match indices, possibly due to an empty match or an unexpected pattern. Please check the regex pattern and input content.", matchIndices)
log.Printf("Negative indices encountered: %v. This indicates that there was an issue with the match indices, possibly due to an empty match or an unexpected pattern. This is not an error but it's possibly not what you want.", matchIndices)
logger.Warning("Negative indices encountered: %v. This indicates that there was an issue with the match indices, possibly due to an empty match or an unexpected pattern. This is not an error but it's possibly not what you want.", matchIndices)
continue
}
}
@@ -187,25 +189,25 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
}
for _, capture := range captureGroups {
log.Printf("Capture group: %+v", *capture)
logger.Trace("Capture group: %+v", *capture)
}
if err := p.ToLua(L, captureGroups); err != nil {
log.Printf("Error setting Lua variables: %v", err)
logger.Error("Error setting Lua variables: %v", err)
continue
}
log.Println("Lua variables set successfully")
logger.Trace("Lua variables set successfully")
if err := L.DoString(luaExpr); err != nil {
log.Printf("Error executing Lua code %s for groups %+v: %v", luaExpr, captureGroups, err)
logger.Error("Error executing Lua code %s for groups %+v: %v", luaExpr, captureGroups, err)
continue
}
log.Println("Lua code executed successfully")
logger.Trace("Lua code executed successfully")
// Get modifications from Lua
captureGroups, err = p.FromLuaCustom(L, captureGroups)
if err != nil {
log.Printf("Error getting modifications: %v", err)
logger.Error("Error getting modifications: %v", err)
continue
}
@@ -214,12 +216,20 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
if replacementVar.Type() != lua.LTNil {
replacement = replacementVar.String()
}
// Check if modification flag is set
modifiedVal := L.GetGlobal("modified")
if modifiedVal.Type() != lua.LTBool || !lua.LVAsBool(modifiedVal) {
logger.Debug("No modifications made by Lua script")
continue
}
if replacement == "" {
commands := make([]ReplaceCommand, 0, len(captureGroups))
// Apply the modifications to the original match
replacement = match
for _, capture := range captureGroups {
log.Printf("Applying modification: %s", capture.Updated)
logger.Debug("Applying modification: %s", capture.Updated)
// Indices of the group are relative to content
// To relate them to match we have to subtract the match start index
// replacement = replacement[:groupStart] + newVal + replacement[groupEnd:]
@@ -238,12 +248,13 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
replacement = replacement[:command.From] + command.With + replacement[command.To:]
}
}
modificationCount++
result = result[:matchIndices[0]] + replacement + result[matchIndices[1]:]
log.Printf("Modification count updated: %d", modificationCount)
logger.Debug("Modification count updated: %d", modificationCount)
}
log.Printf("Process completed with %d modifications", modificationCount)
logger.Debug("Process completed with %d modifications", modificationCount)
return result, modificationCount, len(indices), nil
}
@@ -256,7 +267,8 @@ func ResolveRegexPlaceholders(pattern string) string {
// Handle special pattern modifications
if !strings.HasPrefix(pattern, "(?s)") {
pattern = "(?s)" + pattern
log.Printf("Pattern modified to include (?s): %s", pattern)
// Use fmt.Printf for test compatibility
fmt.Printf("Pattern modified to include (?s): %s\n", pattern)
}
namedGroupNum := regexp.MustCompile(`(?:(\?<[^>]+>)(!num))`)