dht_logger.go 556 Bytes
Newer Older
Jeromy's avatar
Jeromy committed
1 2 3 4 5 6 7 8 9
package dht

import (
	"encoding/json"
	"time"

	u "github.com/jbenet/go-ipfs/util"
)

10
type logDhtRPC struct {
Jeromy's avatar
Jeromy committed
11 12 13 14
	Type     string
	Start    time.Time
	End      time.Time
	Duration time.Duration
15
	RPCCount int
Jeromy's avatar
Jeromy committed
16 17 18
	Success  bool
}

19 20
func startNewRPC(name string) *logDhtRPC {
	r := new(logDhtRPC)
Jeromy's avatar
Jeromy committed
21 22 23 24 25
	r.Type = name
	r.Start = time.Now()
	return r
}

26
func (l *logDhtRPC) EndLog() {
Jeromy's avatar
Jeromy committed
27 28 29 30
	l.End = time.Now()
	l.Duration = l.End.Sub(l.Start)
}

31
func (l *logDhtRPC) Print() {
Jeromy's avatar
Jeromy committed
32 33
	b, err := json.Marshal(l)
	if err != nil {
34
		u.DOut(err.Error())
Jeromy's avatar
Jeromy committed
35
	} else {
36
		u.DOut(string(b))
Jeromy's avatar
Jeromy committed
37 38
	}
}