Fix up the logs a little

This commit is contained in:
2025-03-27 23:35:39 +01:00
parent 5d10178bf9
commit ba7ac07001
4 changed files with 78 additions and 7 deletions

View File

@@ -5,6 +5,7 @@ import (
"regexp"
"strconv"
"strings"
"time"
lua "github.com/yuin/gopher-lua"
@@ -23,16 +24,22 @@ type CaptureGroup struct {
func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceCommand, error) {
var commands []utils.ReplaceCommand
logger.Trace("Processing regex: %q", command.Pattern)
// Start timing the regex processing
startTime := time.Now()
// We don't HAVE to do this multiple times for a pattern
// But it's quick enough for us to not care
pattern := resolveRegexPlaceholders(command.Pattern)
logger.Debug("Compiling regex pattern: %s", pattern)
patternCompileStart := time.Now()
compiledPattern, err := regexp.Compile(pattern)
if err != nil {
logger.Error("Error compiling pattern: %v", err)
return commands, fmt.Errorf("error compiling pattern: %v", err)
}
logger.Debug("Compiled pattern successfully: %s", pattern)
logger.Debug("Compiled pattern successfully in %v: %s", time.Since(patternCompileStart), pattern)
// Same here, it's just string concatenation, it won't kill us
// More important is that we don't fuck up the command
@@ -42,10 +49,20 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC
logger.Debug("Transformed Lua expression: %q → %q", previous, luaExpr)
// Process all regex matches
matchFindStart := time.Now()
indices := compiledPattern.FindAllStringSubmatchIndex(content, -1)
logger.Debug("Found %d matches in content of length %d", len(indices), len(content))
matchFindDuration := time.Since(matchFindStart)
logger.Debug("Found %d matches in content of length %d (search took %v)",
len(indices), len(content), matchFindDuration)
// Log pattern complexity metrics
patternComplexity := estimatePatternComplexity(pattern)
logger.Debug("Pattern complexity estimate: %d", patternComplexity)
if len(indices) == 0 {
logger.Warning("No matches found for regex: %q", pattern)
logger.Debug("Total regex processing time: %v", time.Since(startTime))
return commands, nil
}
@@ -184,7 +201,7 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC
modifiedGroups++
}
}
logger.Info("%d of %d capture groups were modified", modifiedGroups, len(captureGroups))
logger.Info("%d of %d capture groups identified for modification", modifiedGroups, len(captureGroups))
for _, capture := range captureGroups {
if capture.Value == capture.Updated {
@@ -193,7 +210,7 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC
}
// Log what changed with context
logger.Debug("Modifying group %s: %q → %q",
logger.Debug("Capture group %s scheduled for modification: %q → %q",
capture.Name, capture.Value, capture.Updated)
// Indices of the group are relative to content
@@ -214,6 +231,7 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC
}
}
logger.Debug("Total regex processing time: %v", time.Since(startTime))
return commands, nil
}
@@ -344,3 +362,21 @@ func fromLua(L *lua.LState, captureGroups []*CaptureGroup) ([]*CaptureGroup, err
return captureGroups, nil
}
// estimatePatternComplexity gives a rough estimate of regex pattern complexity
// This can help identify potentially problematic patterns
func estimatePatternComplexity(pattern string) int {
complexity := len(pattern)
// Add complexity for potentially expensive operations
complexity += strings.Count(pattern, ".*") * 10 // Greedy wildcard
complexity += strings.Count(pattern, ".*?") * 5 // Non-greedy wildcard
complexity += strings.Count(pattern, "[^") * 3 // Negated character class
complexity += strings.Count(pattern, "\\b") * 2 // Word boundary
complexity += strings.Count(pattern, "(") * 2 // Capture groups
complexity += strings.Count(pattern, "(?:") * 1 // Non-capture groups
complexity += strings.Count(pattern, "\\1") * 3 // Backreferences
complexity += strings.Count(pattern, "{") * 2 // Counted repetition
return complexity
}