Refactor logging messages for clarity and add unit tests for core functionalities

This commit is contained in:
2025-08-15 12:31:58 -04:00
parent cd6934049e
commit 9b2343c867
2 changed files with 105 additions and 6 deletions

11
main.go
View File

@ -205,7 +205,7 @@ func (d *twoToneDetector) stepWindow(pcms []int16, t0 time.Time) (event string,
now := t0
if r < d.rmsThresh {
slog.Debug("Window at %s: RMS %.2f below threshold %.2f, resetting", now.Format(time.RFC3339), r, d.rmsThresh)
slog.Debug("Window RMS below threshold, resetting", "at", now.Format(time.RFC3339), "RMS", r, "threshold", d.rmsThresh)
d.reset()
return "", 0, 0, 0, 0
}
@ -222,7 +222,7 @@ func (d *twoToneDetector) stepWindow(pcms []int16, t0 time.Time) (event string,
}
ratio := bestPow / (total + 1e-12)
if ratio < d.ratioThresh {
slog.Debug("Window at %s: ratio %.3f below threshold %.3f, resetting", now.Format(time.RFC3339), ratio, d.ratioThresh)
slog.Debug("Window ratio below threshold, resetting", "at", now.Format(time.RFC3339), "ratio", ratio, "threshold", d.ratioThresh)
d.reset()
return "", 0, 0, 0, 0
}
@ -244,7 +244,7 @@ func (d *twoToneDetector) stepWindow(pcms []int16, t0 time.Time) (event string,
d.gapRemainMs = d.gapMaxMs
}
} else {
slog.Debug("Window at %s: freq %.1f Hz differs from Tone A %.1f Hz, resetting", now.Format(time.RFC3339), freq, d.aFreq)
slog.Debug("Window freq differs from Tone A, resetting A", "at", now.Format(time.RFC3339), "freq (Hz)", freq, "Tone A (Hz)", d.aFreq)
d.reset()
}
} else if d.waitingB {
@ -258,7 +258,7 @@ func (d *twoToneDetector) stepWindow(pcms []int16, t0 time.Time) (event string,
d.bFreq = freq
d.bStart = now
} else if math.Abs(freq-d.bFreq) > 10.0 {
slog.Debug("Window at %s: freq %.1f Hz differs from Tone B %.1f Hz, resetting B", now.Format(time.RFC3339), freq, d.bFreq)
slog.Debug("Window freq differs from Tone B, resetting B", "at", now.Format(time.RFC3339), "freq (Hz)", freq, "Tone B (Hz)", d.bFreq)
d.bFreq = freq
d.bAccumMs = 0
d.bStart = now
@ -426,8 +426,7 @@ func main() {
if det.recording {
if !det.bEnd.IsZero() && !chunkTime.Before(det.bEnd.Add(100*time.Millisecond)) {
r := rmsPCM(chunk)
slog.Info("Chunk at %s: RMS=%.2f, silenceThresh=%.2f, silenceAccum=%d ms, recordBuf=%d samples",
chunkTime.Format(time.RFC3339), r, *silenceThresh, det.silenceAccumMs, len(det.recordBuf))
slog.Info("chunk", "at", chunkTime.Format(time.RFC3339), "RMS", r, "silenceThresh", *silenceThresh, "silenceAccum (ms)", det.silenceAccumMs, "record buffer (samples)", len(det.recordBuf))
if r < *silenceThresh {
det.silenceAccumMs += int(time.Duration(len(chunk)) * time.Second / time.Duration(fs) / time.Millisecond)
if det.silenceAccumMs >= *silenceDurMs && len(det.recordBuf) > 0 {