Commit b4af146f authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

p2p/net: better dial log.Event

parent 9dac5bb7
...@@ -28,6 +28,7 @@ func (d *Dialer) String() string { ...@@ -28,6 +28,7 @@ func (d *Dialer) String() string {
// Example: d.DialAddr(ctx, peer.Addresses()[0], peer) // Example: d.DialAddr(ctx, peer.Addresses()[0], peer)
func (d *Dialer) Dial(ctx context.Context, raddr ma.Multiaddr, remote peer.ID) (Conn, error) { func (d *Dialer) Dial(ctx context.Context, raddr ma.Multiaddr, remote peer.ID) (Conn, error) {
logdial := lgbl.Dial("conn", d.LocalPeer, remote, nil, raddr) logdial := lgbl.Dial("conn", d.LocalPeer, remote, nil, raddr)
logdial["encrypted"] = (d.PrivateKey != nil) // log wether this will be an encrypted dial or not.
defer log.EventBegin(ctx, "connDial", logdial).Done() defer log.EventBegin(ctx, "connDial", logdial).Done()
maconn, err := d.rawConnDial(ctx, raddr, remote) maconn, err := d.rawConnDial(ctx, raddr, remote)
...@@ -53,12 +54,10 @@ func (d *Dialer) Dial(ctx context.Context, raddr ma.Multiaddr, remote peer.ID) ( ...@@ -53,12 +54,10 @@ func (d *Dialer) Dial(ctx context.Context, raddr ma.Multiaddr, remote peer.ID) (
if d.PrivateKey == nil { if d.PrivateKey == nil {
log.Warning("dialer %s dialing INSECURELY %s at %s!", d, remote, raddr) log.Warning("dialer %s dialing INSECURELY %s at %s!", d, remote, raddr)
log.Event(ctx, "connDialInsecure", logdial)
connOut = c connOut = c
return return
} }
defer log.EventBegin(ctx, "connDialEncrypt", logdial).Done()
c2, err := newSecureConn(ctx, d.PrivateKey, c) c2, err := newSecureConn(ctx, d.PrivateKey, c)
if err != nil { if err != nil {
logdial["error"] = err logdial["error"] = err
......
...@@ -32,19 +32,41 @@ func Error(e error) log.Loggable { ...@@ -32,19 +32,41 @@ func Error(e error) log.Loggable {
} }
// Dial metadata is metadata for dial events // Dial metadata is metadata for dial events
func Dial(sys string, lid, rid peer.ID, laddr, raddr ma.Multiaddr) log.LoggableMap { func Dial(sys string, lid, rid peer.ID, laddr, raddr ma.Multiaddr) DeferredMap {
m := log.Metadata{"subsystem": sys} m := DeferredMap{}
m["subsystem"] = sys
if lid != "" { if lid != "" {
m["localPeer"] = lid.Pretty() m["localPeer"] = func() interface{} { return lid.Pretty() }
_ = m["localPeer"].(func() interface{})
} }
if laddr != nil { if laddr != nil {
m["localAddr"] = laddr.String() m["localAddr"] = func() interface{} { return laddr.String() }
} }
if rid != "" { if rid != "" {
m["remotePeer"] = rid.Pretty() m["remotePeer"] = func() interface{} { return rid.Pretty() }
} }
if raddr != nil { if raddr != nil {
m["remoteAddr"] = raddr.String() m["remoteAddr"] = func() interface{} { return raddr.String() }
} }
return log.LoggableMap(m) return m
}
// DeferredMap is a Loggable which may contained deffered values.
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
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment