watchers 2

This commit is contained in:
Sky Johnson 2025-03-07 10:51:14 -06:00
parent 4a17388d83
commit 4f02f0e5bf
3 changed files with 38 additions and 20 deletions

View File

@ -21,7 +21,7 @@ func LogRequest(log *logger.Logger, statusCode int, r *http.Request, duration ti
statusColor := getStatusColor(statusCode)
// Use the logger's raw message writer to bypass the standard format
log.LogRaw("%s [%s%d%s] %s %s (%v)",
log.LogRaw("%s [ %s%d%s] %s %s (%v)",
time.Now().Format(log.TimeFormat()),
statusColor, statusCode, colorReset,
r.Method, r.URL.Path, duration)

View File

@ -34,11 +34,11 @@ var levelProps = map[int]struct {
tag string
color string
}{
LevelDebug: {"DBG", colorCyan},
LevelInfo: {"INF", colorBlue},
LevelWarning: {"WRN", colorYellow},
LevelError: {"ERR", colorRed},
LevelFatal: {"FTL", colorPurple},
LevelDebug: {" DBG", colorCyan},
LevelInfo: {"INFO", colorBlue},
LevelWarning: {"WARN", colorYellow},
LevelError: {" ERR", colorRed},
LevelFatal: {"FATL", colorPurple},
}
// Time format for log messages

View File

@ -1,6 +1,7 @@
package watchers
import (
"fmt"
"os"
"path/filepath"
"sync"
@ -120,9 +121,9 @@ func WatchDirectory(config WatcherConfig) (*Watcher, error) {
go w.debounceLoop()
if config.Adaptive {
config.Log.Debug("Started watching directory with adaptive polling: %s", config.Dir)
w.logDebug("Started watching with adaptive polling")
} else {
config.Log.Debug("Started watching directory: %s", config.Dir)
w.logDebug("Started watching with fixed polling interval: %v", pollInterval)
}
return w, nil
@ -147,7 +148,7 @@ func (w *Watcher) watchLoop() {
case <-ticker.C:
changed, err := w.checkForChanges()
if err != nil {
w.log.Error("Error checking for changes: %v", err)
w.logError("Error checking for changes: %v", err)
continue
}
@ -155,11 +156,10 @@ func (w *Watcher) watchLoop() {
// Update last change time
w.lastChangeTime = time.Now()
// Reset to base polling interval if we were using extended intervals
if w.adaptive && w.pollInterval > w.basePollInterval {
w.pollInterval = w.basePollInterval
ticker.Reset(w.pollInterval)
w.log.Debug("Reset to base polling interval: %v", w.pollInterval)
w.logDebug("Reset to base polling interval: %v", w.pollInterval)
}
// Try to send a change notification, non-blocking
@ -176,13 +176,13 @@ func (w *Watcher) watchLoop() {
// First extension
w.pollInterval = extendedPollInterval
ticker.Reset(w.pollInterval)
w.log.Debug("Extended polling interval to %v after %v of inactivity",
w.logDebug("Extended polling interval to: %v after %v of inactivity",
w.pollInterval, inactiveDuration.Round(time.Second))
} else if w.pollInterval == extendedPollInterval && inactiveDuration > secondExtendThreshold {
// Second extension
w.pollInterval = maxPollInterval
ticker.Reset(w.pollInterval)
w.log.Debug("Extended polling interval to %v after %v of inactivity",
w.logDebug("Extended polling interval to: %v after %v of inactivity",
w.pollInterval, inactiveDuration.Round(time.Second))
}
}
@ -209,7 +209,7 @@ func (w *Watcher) debounceLoop() {
// Start a new timer
timer = time.AfterFunc(w.debounceTime, func() {
if err := w.callback(); err != nil {
w.log.Error("Refresh callback error: %v", err)
w.logError("Refresh callback error: %v", err)
}
})
@ -229,7 +229,7 @@ func (w *Watcher) scanDirectory() error {
return filepath.Walk(w.dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
w.log.Warning("Error accessing path %s: %v", path, err)
w.logWarning("Error accessing path %s: %v", path, err)
return nil // Continue with other files
}
@ -248,7 +248,25 @@ func (w *Watcher) scanDirectory() error {
})
}
// checkForChanges compares current file state with the last scan
// logDebug logs a debug message with the watcher's directory prefix
func (w *Watcher) logDebug(format string, args ...any) {
w.log.Debug("[Watcher] [%s] %s", w.dir, fmt.Sprintf(format, args...))
}
// logInfo logs an info message with the watcher's directory prefix
func (w *Watcher) logInfo(format string, args ...any) {
w.log.Info("[Watcher] [%s] %s", w.dir, fmt.Sprintf(format, args...))
}
// logWarning logs a warning message with the watcher's directory prefix
func (w *Watcher) logWarning(format string, args ...any) {
w.log.Warning("[Watcher] [%s] %s", w.dir, fmt.Sprintf(format, args...))
}
// logError logs an error message with the watcher's directory prefix
func (w *Watcher) logError(format string, args ...any) {
w.log.Error("[Watcher] [%s] %s", w.dir, fmt.Sprintf(format, args...))
}
func (w *Watcher) checkForChanges() (bool, error) {
// Get current state
currentFiles := make(map[string]FileInfo)
@ -284,7 +302,7 @@ func (w *Watcher) checkForChanges() (bool, error) {
// Check for different file count (quick check)
if len(currentFiles) != len(previousFiles) {
w.log.Debug("File count changed: %d -> %d", len(previousFiles), len(currentFiles))
w.logDebug("File count changed: %d -> %d", len(previousFiles), len(currentFiles))
w.updateFiles(currentFiles)
return true, nil
}
@ -294,14 +312,14 @@ func (w *Watcher) checkForChanges() (bool, error) {
prevInfo, exists := previousFiles[path]
if !exists {
// New file
w.log.Debug("New file detected: %s", path)
w.logDebug("New file detected: %s", path)
w.updateFiles(currentFiles)
return true, nil
}
if currentInfo.ModTime != prevInfo.ModTime || currentInfo.Size != prevInfo.Size {
// File modified
w.log.Debug("File modified: %s", path)
w.logDebug("File modified: %s", path)
w.updateFiles(currentFiles)
return true, nil
}
@ -311,7 +329,7 @@ func (w *Watcher) checkForChanges() (bool, error) {
for path := range previousFiles {
if _, exists := currentFiles[path]; !exists {
// File deleted
w.log.Debug("File deleted: %s", path)
w.logDebug("File deleted: %s", path)
w.updateFiles(currentFiles)
return true, nil
}