diff --git a/internal/web/api.go b/internal/web/api.go index 73ab84d..8752895 100644 --- a/internal/web/api.go +++ b/internal/web/api.go @@ -1,7 +1,7 @@ package web import ( - // "log" + "log/slog" "net/http" "github.com/gin-gonic/gin" @@ -49,6 +49,8 @@ func apiHostDel(c *gin.Context) { db.Delete(appConfig.DBPath, "now", host.ID) allHosts = db.Select(appConfig.DBPath, "now") + slog.Info("Deleting from DB", "host", host) + c.IndentedJSON(http.StatusOK, "OK") } diff --git a/internal/web/config.go b/internal/web/config.go index e92fe09..9ad4c26 100644 --- a/internal/web/config.go +++ b/internal/web/config.go @@ -47,7 +47,7 @@ func saveConfigHandler(c *gin.Context) { conf.Write(appConfig) - slog.Info("writing new config to " + appConfig.ConfPath) + slog.Info("Writing new config to " + appConfig.ConfPath) updateRoutines() // routines-upd.go @@ -76,7 +76,7 @@ func saveInfluxHandler(c *gin.Context) { conf.Write(appConfig) - slog.Info("writing new config to " + appConfig.ConfPath) + slog.Info("Writing new config to " + appConfig.ConfPath) c.Redirect(http.StatusFound, "/config") } diff --git a/internal/web/routines-upd.go b/internal/web/routines-upd.go index 0a35bb7..d8d6a24 100644 --- a/internal/web/routines-upd.go +++ b/internal/web/routines-upd.go @@ -7,6 +7,7 @@ import ( ) func updateRoutines() { + slog.Debug("Restarting go routines") close(quitScan) @@ -26,15 +27,20 @@ func setLogLevel() { switch appConfig.LogLevel { case "debug": + slog.Info("Log level=DEBUG") level = slog.LevelDebug case "info": + slog.Info("Log level=INFO") level = slog.LevelInfo case "warn": + slog.Info("Log level=WARN") level = slog.LevelWarn case "error": + slog.Info("Log level=ERROR") level = slog.LevelError default: - // invalid log level, handle error + slog.Error("Invalid log level. Setting default level INFO") + slog.SetLogLoggerLevel(slog.LevelInfo) } slog.SetLogLoggerLevel(level) } diff --git a/internal/web/scan-routine.go b/internal/web/scan-routine.go index 9e1786f..5f76c44 100644 --- a/internal/web/scan-routine.go +++ b/internal/web/scan-routine.go @@ -12,20 +12,25 @@ import ( "github.com/aceberg/WatchYourLAN/internal/notify" ) +var foundHostsMap map[string]models.Host +var aHost, fHost models.Host +var exists bool + func startScan(quit chan bool) { - var lastDate time.Time + var lastDate, nowDate, plusDate time.Time + var foundHosts []models.Host for { select { case <-quit: return default: - nowDate := time.Now() - plusDate := lastDate.Add(time.Duration(appConfig.Timeout) * time.Second) + nowDate = time.Now() + plusDate = lastDate.Add(time.Duration(appConfig.Timeout) * time.Second) if nowDate.After(plusDate) { - foundHosts := arp.Scan(appConfig.Ifaces) + foundHosts = arp.Scan(appConfig.Ifaces) compareHosts(foundHosts) allHosts = db.Select(appConfig.DBPath, "now") histHosts = db.Select(appConfig.DBPath, "history") @@ -41,14 +46,14 @@ func startScan(quit chan bool) { func compareHosts(foundHosts []models.Host) { // Make map of found hosts - foundHostsMap := make(map[string]models.Host) - for _, fHost := range foundHosts { + foundHostsMap = make(map[string]models.Host) + for _, fHost = range foundHosts { foundHostsMap[fHost.Mac] = fHost } - for _, aHost := range allHosts { + for _, aHost = range allHosts { - fHost, exists := foundHostsMap[aHost.Mac] + fHost, exists = foundHostsMap[aHost.Mac] if exists { aHost.Iface = fHost.Iface @@ -70,7 +75,7 @@ func compareHosts(foundHosts []models.Host) { } } - for _, fHost := range foundHostsMap { + for _, fHost = range foundHostsMap { msg := fmt.Sprintf("Unknown host IP: '%s', MAC: '%s', Hw: '%s', Iface: '%s'", fHost.IP, fHost.Mac, fHost.Hw, fHost.Iface) slog.Warn(msg)