From 8b23731451ba39727c3f7634caecb07c9c0e2649 Mon Sep 17 00:00:00 2001 From: mappu Date: Sat, 16 Apr 2016 18:21:38 +1200 Subject: [PATCH] nmdc-log-service: prevent logging the same connection-state message repeatedly --- main.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index c9a614d..bb99c4b 100644 --- a/main.go +++ b/main.go @@ -51,7 +51,11 @@ func LogMessage(hub, message string) { } } -func HubWorker(addr, nick, password string) { +type HubWorker struct { + LastConnectionStateMessage string +} + +func (this *HubWorker) MainLoop(addr, nick, password string) { opts := libnmdc.HubConnectionOptions{ Address: libnmdc.HubAddress(addr), @@ -71,11 +75,16 @@ func HubWorker(addr, nick, password string) { switch event.EventType { case libnmdc.EVENT_CONNECTION_STATE_CHANGED: if LogConnectionState { + str := "* " + event.StateChange.Format() if len(event.Message) > 0 { - LogMessage(addr, "* "+event.StateChange.Format()+" ("+event.Message+")") - } else { - LogMessage(addr, "* "+event.StateChange.Format()) + str += " (" + event.Message + ")" } + + // Prevent logging the same message repeatedly + if str != this.LastConnectionStateMessage { + LogMessage(addr, str) + } + this.LastConnectionStateMessage = str } case libnmdc.EVENT_PUBLIC: @@ -135,7 +144,8 @@ func main() { os.MkdirAll(GetDirectoryNameForHub(hubaddr), 0755) // Launch logger - go HubWorker(hubaddr, *nick, *password) + hw := HubWorker{} + go hw.MainLoop(hubaddr, *nick, *password) launch_ct++ }