loggables.go 2.1 KB
Newer Older
1 2 3
// Package loggables includes a bunch of translator functions for
// commonplace/stdlib objects. This is boilerplate code that shouldn't change
// much, and not sprinkled all over the place (i.e. gather it here).
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
4
//
5
// NOTE: it may make sense to put all stdlib Loggable functions in the eventlog
6
// package. Putting it here for now in case we don't want to pollute it.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7 8 9 10 11
package loggables

import (
	"net"

12
	uuid "gx/ipfs/QmcyaFHbyiZfoX5GTpcqqCPYmbjYNAhRDekXSJPFHdYNSV/go.uuid"
13

14
	ma "gx/ipfs/QmYzDkkgAEmrcNzFCiYo6L1dTX4EAG1gZkbtdbd9trL4vd/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15

16
	logging "gx/ipfs/QmaDNZ4QMdBdku1YZWBysufYyoQt1negQGNav6PLYarbY8/go-log"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17

18
	peer "gx/ipfs/QmbyvM8zRFDkbFdYyt1MnevUMJ62SiSGbfDFZ3Z8nkrzr4/go-libp2p-peer"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
19 20 21
)

// NetConn returns an eventlog.Metadata with the conn addresses
Jeromy's avatar
Jeromy committed
22 23
func NetConn(c net.Conn) logging.Loggable {
	return logging.Metadata{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
24 25 26 27 28 29
		"localAddr":  c.LocalAddr(),
		"remoteAddr": c.RemoteAddr(),
	}
}

// Error returns an eventlog.Metadata with an error
Jeromy's avatar
Jeromy committed
30 31
func Error(e error) logging.Loggable {
	return logging.Metadata{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32 33 34
		"error": e.Error(),
	}
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35

36 37 38 39 40 41
func Uuid(key string) logging.Metadata {
	return logging.Metadata{
		key: uuid.NewV4().String(),
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42
// Dial metadata is metadata for dial events
43 44 45
func Dial(sys string, lid, rid peer.ID, laddr, raddr ma.Multiaddr) DeferredMap {
	m := DeferredMap{}
	m["subsystem"] = sys
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46
	if lid != "" {
47
		m["localPeer"] = func() interface{} { return lid.Pretty() }
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
48 49
	}
	if laddr != nil {
50
		m["localAddr"] = func() interface{} { return laddr.String() }
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
51 52
	}
	if rid != "" {
53
		m["remotePeer"] = func() interface{} { return rid.Pretty() }
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
54 55
	}
	if raddr != nil {
56
		m["remoteAddr"] = func() interface{} { return raddr.String() }
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57
	}
58 59 60
	return m
}

61
// DeferredMap is a Loggable which may contain deferred values.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
type DeferredMap map[string]interface{}

// Loggable describes objects that can be marshalled into Metadata for logging
func (m DeferredMap) Loggable() map[string]interface{} {
	m2 := map[string]interface{}{}
	for k, v := range m {

		if vf, ok := v.(func() interface{}); ok {
			// if it's a DeferredVal, call it.
			m2[k] = vf()

		} else {
			// else use the value as is.
			m2[k] = v
		}
	}
	return m2
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
79
}