Skip to content

Commit

Permalink
fixes #252 Add logging to ease debugging connection reattempts
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Krieger committed Nov 13, 2016
1 parent 68cfb1b commit 94028f4
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ wenfang <[email protected]>
Anner van Hardenbroek <[email protected]>
Gerrit Renker <[email protected]>
Rene Kaufmann <[email protected]>
Ben Krieger <[email protected]>
1 change: 1 addition & 0 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ func (d *dialer) dialer() {
case <-d.sock.closeq: // exit if parent socket closed
return
case <-time.After(rtime):
log.Debug(err)
if rtmax > 0 {
rtime *= 2
if rtime > rtmax {
Expand Down
71 changes: 71 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package mangos

import "strings"

// Logger is an interface that describes a very basic logger.
// It only needs INFO and DEBUG levels, because errors are
// returned by the library code and warnings are just ignored
// errors (which is a bad idea). See:
// http://dave.cheney.net/2015/11/05/lets-talk-about-logging
type Logger interface {
Infof(string, ...interface{})
Debugf(string, ...interface{})
}

type loggers struct {
logs []Logger
level int
}

var log loggers

// RegisterLogger appends a new logger to the list of loggers.
// The same logger can be registered multiple times, but will
// then be used multiple times.
func RegisterLogger(l Logger) {
log.logs = append(log.logs, l)
}

// SetLogLevel understands the following levels:
// 0 - No output
// 1 - Only INFO level
// 2 - Both INFO and DEBUG level
func SetLogLevel(i int) {
log.level = i
}

// Infof calls Infof on all registered loggers if the log level
// is set to 1 or greater
func (l *loggers) Infof(fmt string, v ...interface{}) {
if l.level < 1 {
return
}
for _, log := range l.logs {
log.Infof(fmt, v...)
}
}

// Info logs the values space-separated on all registered
// loggers if the log level is set to 1 or greater
func (l *loggers) Info(v ...interface{}) {
fmt := strings.Repeat(" %v", len(v))
l.Infof(fmt[1:], v...)
}

// Debugf calls Debugf on all registered loggers if the log level
// is set to 2 or greater
func (l *loggers) Debugf(fmt string, v ...interface{}) {
if l.level < 2 {
return
}
for _, log := range l.logs {
log.Debugf(fmt, v...)
}
}

// Debug logs the values space-separated on all registered
// loggers if the log level is set to 2 or greater
func (l *loggers) Debug(v ...interface{}) {
fmt := strings.Repeat(" %v", len(v))
l.Debugf(fmt[1:], v...)
}
21 changes: 21 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package log

import (
"log"

"github.com/go-mangos/mangos"
)

func init() {
mangos.RegisterLogger(&logger{})
}

type logger struct{}

func (l *logger) Infof(fmt string, v ...interface{}) {
log.Printf("[INFO] "+fmt, v...)
}

func (l *logger) Debugf(fmt string, v ...interface{}) {
log.Printf("[DEBUG] "+fmt, v...)
}

0 comments on commit 94028f4

Please sign in to comment.