Files
go-libp2p-kad-dht/dht_logger.go

47 lines
757 B
Go
Raw Normal View History

2014-08-14 08:32:17 -07:00
package dht
import (
"encoding/json"
"fmt"
2014-08-14 08:32:17 -07:00
"time"
)
2014-08-16 23:03:36 -07:00
type logDhtRPC struct {
2014-08-14 08:32:17 -07:00
Type string
Start time.Time
End time.Time
Duration time.Duration
2014-08-16 23:03:36 -07:00
RPCCount int
2014-08-14 08:32:17 -07:00
Success bool
}
2014-08-16 23:03:36 -07:00
func startNewRPC(name string) *logDhtRPC {
r := new(logDhtRPC)
2014-08-14 08:32:17 -07:00
r.Type = name
r.Start = time.Now()
return r
}
2014-08-16 23:03:36 -07:00
func (l *logDhtRPC) EndLog() {
2014-08-14 08:32:17 -07:00
l.End = time.Now()
l.Duration = l.End.Sub(l.Start)
}
2014-08-16 23:03:36 -07:00
func (l *logDhtRPC) Print() {
2014-08-14 08:32:17 -07:00
b, err := json.Marshal(l)
if err != nil {
log.Debugf("Error marshaling logDhtRPC object: %s", err)
2014-08-14 08:32:17 -07:00
} else {
log.Debug(string(b))
2014-08-14 08:32:17 -07:00
}
}
2014-09-17 07:19:40 -07:00
func (l *logDhtRPC) String() string {
return fmt.Sprintf("DHT RPC: %s took %s, success = %v", l.Type, l.Duration, l.Success)
}
2014-09-17 07:19:40 -07:00
func (l *logDhtRPC) EndAndPrint() {
l.EndLog()
l.Print()
}