Skip to content

Commit

Permalink
Added lazy logging functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Djadih committed May 17, 2023
1 parent ae32e7d commit 85562a9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
28 changes: 28 additions & 0 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,34 @@ func Panic(msg string, args ...interface{}) {
Log.Panic(constructLogMessage(msg, args...))
}

func Lazy(fn func() string, logLevel string) {
level, err := logrus.ParseLevel(logLevel)
if err == nil && Log.IsLevelEnabled(level) {
callCorrectLevel(level, fn())
}
}

func callCorrectLevel(level logrus.Level, msg string, args ...interface{}) {
switch level {
case logrus.TraceLevel:
Trace(msg, args...)
case logrus.DebugLevel:
Debug(msg, args...)
case logrus.InfoLevel:
Info(msg, args...)
case logrus.WarnLevel:
Warn(msg, args...)
case logrus.ErrorLevel:
Error(msg, args...)
case logrus.FatalLevel:
Fatal(msg, args...)
case logrus.PanicLevel:
Panic(msg, args...)
default:
Error("Unknown log level: %v", level)
}
}

func constructLogMessage(msg string, fields ...interface{}) string {
var pairs []string

Expand Down
6 changes: 4 additions & 2 deletions p2p/discover/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ import (
mrand "math/rand"
"net"
"sort"
sync "github.com/sasha-s/go-deadlock"
"time"

sync "github.com/sasha-s/go-deadlock"

"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/log"
"github.com/dominant-strategies/go-quai/p2p/enode"
Expand Down Expand Up @@ -305,7 +306,8 @@ func (tab *Table) loadSeedNodes() {
seeds = append(seeds, tab.nursery...)
for i := range seeds {
seed := seeds[i]
tab.log.Trace("Found seed node in database", "id", seed.ID(), "addr", seed.addr())
log.Lazy(func() string { return time.Since(tab.db.LastPongReceived(seed.ID(), seed.IP())).String() }, "trace")
log.Trace("Found seed node in database", "id", seed.ID(), "addr", seed.addr())
tab.addSeenNode(seed)
}
}
Expand Down
7 changes: 6 additions & 1 deletion p2p/msgrate/msgrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package msgrate

import (
"encoding/json"
"errors"
"math"
"sort"
Expand Down Expand Up @@ -421,7 +422,11 @@ func (t *Trackers) tune() {
t.confidence = t.confidence + (1-t.confidence)/2

t.tuned = time.Now()
t.log.Debug("Recalculated msgrate QoS values", "rtt", t.roundtrip, "confidence", t.confidence, "ttl", t.targetTimeout(), "next", t.tuned.Add(t.roundtrip))
log.Debug("Recalculated msgrate QoS values", "rtt", t.roundtrip, "confidence", t.confidence, "ttl", t.targetTimeout(), "next", t.tuned.Add(t.roundtrip))
log.Lazy(func() string {
b, _ := json.Marshal(t.meanCapacities())
return string(b)
}, "trace")
}

// detune reduces the tracker's confidence in order to make fresh measurements
Expand Down

0 comments on commit 85562a9

Please sign in to comment.