Make little better logging
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/antchfx/xmlquery"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
@@ -62,6 +64,8 @@ func NewLuaState() (*lua.LState, error) {
|
||||
}
|
||||
|
||||
func Process(p Processor, filename string, pattern string, luaExpr string) (int, int, error) {
|
||||
logger.Debug("Processing file %q with pattern %q", filename, pattern)
|
||||
|
||||
// Read file content
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
@@ -70,7 +74,15 @@ func Process(p Processor, filename string, pattern string, luaExpr string) (int,
|
||||
}
|
||||
|
||||
fullPath := filepath.Join(cwd, filename)
|
||||
logger.Trace("Reading file content from: %s", fullPath)
|
||||
logger.Trace("Reading file from: %s", fullPath)
|
||||
|
||||
stat, err := os.Stat(fullPath)
|
||||
if err != nil {
|
||||
logger.Error("Failed to stat file %s: %v", fullPath, err)
|
||||
return 0, 0, fmt.Errorf("error getting file info: %v", err)
|
||||
}
|
||||
logger.Debug("File size: %d bytes, modified: %s", stat.Size(), stat.ModTime().Format(time.RFC3339))
|
||||
|
||||
content, err := os.ReadFile(fullPath)
|
||||
if err != nil {
|
||||
logger.Error("Failed to read file %s: %v", fullPath, err)
|
||||
@@ -78,32 +90,108 @@ func Process(p Processor, filename string, pattern string, luaExpr string) (int,
|
||||
}
|
||||
|
||||
fileContent := string(content)
|
||||
logger.Trace("File %s read successfully, size: %d bytes", fullPath, len(content))
|
||||
logger.Trace("File read successfully: %d bytes, hash: %x", len(content), md5sum(content))
|
||||
|
||||
// Detect and log file type
|
||||
fileType := detectFileType(filename, fileContent)
|
||||
if fileType != "" {
|
||||
logger.Debug("Detected file type: %s", fileType)
|
||||
}
|
||||
|
||||
// Process the content
|
||||
logger.Debug("Processing content for file: %s", filename)
|
||||
logger.Debug("Starting content processing with %s processor", getProcessorType(p))
|
||||
modifiedContent, modCount, matchCount, err := p.ProcessContent(fileContent, pattern, luaExpr)
|
||||
if err != nil {
|
||||
logger.Error("Error processing content for file %s: %v", filename, err)
|
||||
logger.Error("Processing error: %v", err)
|
||||
return 0, 0, err
|
||||
}
|
||||
|
||||
logger.Debug("Processing results: %d matches, %d modifications", matchCount, modCount)
|
||||
|
||||
// If we made modifications, save the file
|
||||
if modCount > 0 {
|
||||
logger.Info("Writing %d modifications to file: %s", modCount, filename)
|
||||
// Calculate changes summary
|
||||
changePercent := float64(len(modifiedContent)) / float64(len(fileContent)) * 100
|
||||
logger.Info("File size change: %d → %d bytes (%.1f%%)",
|
||||
len(fileContent), len(modifiedContent), changePercent)
|
||||
|
||||
logger.Debug("Writing modified content to %s", fullPath)
|
||||
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)
|
||||
logger.Debug("File written successfully, new hash: %x", md5sum([]byte(modifiedContent)))
|
||||
} else if matchCount > 0 {
|
||||
logger.Debug("No content modifications needed for %d matches", matchCount)
|
||||
} else {
|
||||
logger.Debug("No modifications to write for file: %s", filename)
|
||||
logger.Debug("No matches found in file")
|
||||
}
|
||||
|
||||
return modCount, matchCount, nil
|
||||
}
|
||||
|
||||
// Helper function to get a short MD5 hash of content for logging
|
||||
func md5sum(data []byte) []byte {
|
||||
h := md5.New()
|
||||
h.Write(data)
|
||||
return h.Sum(nil)[:4] // Just use first 4 bytes for brevity
|
||||
}
|
||||
|
||||
// Helper function to detect basic file type from extension and content
|
||||
func detectFileType(filename string, content string) string {
|
||||
ext := strings.ToLower(filepath.Ext(filename))
|
||||
|
||||
switch ext {
|
||||
case ".xml":
|
||||
return "XML"
|
||||
case ".json":
|
||||
return "JSON"
|
||||
case ".html", ".htm":
|
||||
return "HTML"
|
||||
case ".txt":
|
||||
return "Text"
|
||||
case ".go":
|
||||
return "Go"
|
||||
case ".js":
|
||||
return "JavaScript"
|
||||
case ".py":
|
||||
return "Python"
|
||||
case ".java":
|
||||
return "Java"
|
||||
case ".c", ".cpp", ".h":
|
||||
return "C/C++"
|
||||
default:
|
||||
// Try content-based detection for common formats
|
||||
if strings.HasPrefix(strings.TrimSpace(content), "<?xml") {
|
||||
return "XML"
|
||||
}
|
||||
if strings.HasPrefix(strings.TrimSpace(content), "{") ||
|
||||
strings.HasPrefix(strings.TrimSpace(content), "[") {
|
||||
return "JSON"
|
||||
}
|
||||
if strings.HasPrefix(strings.TrimSpace(content), "<!DOCTYPE html") ||
|
||||
strings.HasPrefix(strings.TrimSpace(content), "<html") {
|
||||
return "HTML"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to get processor type name
|
||||
func getProcessorType(p Processor) string {
|
||||
switch p.(type) {
|
||||
case *RegexProcessor:
|
||||
return "Regex"
|
||||
case *XMLProcessor:
|
||||
return "XML"
|
||||
case *JSONProcessor:
|
||||
return "JSON"
|
||||
default:
|
||||
return "Unknown"
|
||||
}
|
||||
}
|
||||
|
||||
// ToLua converts a struct or map to a Lua table recursively
|
||||
func ToLua(L *lua.LState, data interface{}) (lua.LValue, error) {
|
||||
switch v := data.(type) {
|
||||
|
Reference in New Issue
Block a user