From: Diogo Franco (Kovensky) Date: Tue, 16 Jul 2013 16:46:40 +0000 (-0300) Subject: anidb: Log UDP traffic X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=d32928667949b2815adea455690fd71fb747400f;p=go-anidb.git anidb: Log UDP traffic But only the first line of replies. ReAuth and Logout need special handling since they don't use the sendQueue. --- diff --git a/auth.go b/auth.go index 029d5a1..3d346e4 100644 --- a/auth.go +++ b/auth.go @@ -92,11 +92,13 @@ func (udp *udpWrap) ReAuth() udpapi.APIReply { defer udp.credLock.Unlock() if c := udp.credentials; c != nil { + logRequest(paramSet{cmd: "AUTH", params: paramMap{"user": decrypt(c.username)}}) r := udp.AniDBUDP.Auth( decrypt(c.username), decrypt(c.password), decrypt(c.udpKey)) runtime.GC() // any better way to clean the plaintexts? + logReply(r) err := r.Error() @@ -160,6 +162,7 @@ func (adb *AniDB) Logout() error { if adb.udp.connected { adb.udp.connected = false + logRequest(paramSet{cmd: "LOGOUT"}) return adb.udp.Logout() } return nil diff --git a/udp.go b/udp.go index 372aead..ec8f83d 100644 --- a/udp.go +++ b/udp.go @@ -99,6 +99,19 @@ func (r *bannedAPIReply) Error() error { var bannedReply udpapi.APIReply = &bannedAPIReply{} +func logRequest(set paramSet) { + switch set.cmd { + case "AUTH": + log.Printf("UDP>>> AUTH user=%s\n", set.params["user"]) + default: + log.Printf("UDP>>> %s %s\n", set.cmd, udpapi.ParamMap(set.params).String()) + } +} + +func logReply(reply udpapi.APIReply) { + log.Printf("UDP<<< %d %s\n", reply.Code(), reply.Text()) +} + func (udp *udpWrap) sendQueue() { initialWait := 6 * time.Second wait := initialWait @@ -110,6 +123,7 @@ func (udp *udpWrap) sendQueue() { continue } + logRequest(set) reply := <-udp.AniDBUDP.SendRecv(set.cmd, udpapi.ParamMap(set.params)) if reply.Error() == udpapi.TimeoutError { @@ -118,10 +132,13 @@ func (udp *udpWrap) sendQueue() { if wait > time.Minute { wait = time.Minute } + log.Printf("UDP--- Timeout; waiting %s before retry", wait) time.Sleep(wait) goto Retry } + logReply(reply) + wait = initialWait switch reply.Code() {