2017-06-29 08:03:43 -07:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2017-07-03 09:10:46 -07:00
|
|
|
"github.com/tendermint/tmlibs/log"
|
|
|
|
|
2017-06-29 08:03:43 -07:00
|
|
|
"github.com/tendermint/basecoin"
|
|
|
|
"github.com/tendermint/basecoin/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
NameLogger = "lggr"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Logger catches any panics and returns them as errors instead
|
|
|
|
type Logger struct{}
|
|
|
|
|
|
|
|
func (_ Logger) Name() string {
|
|
|
|
return NameLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Middleware = Logger{}
|
|
|
|
|
|
|
|
func (_ Logger) CheckTx(ctx basecoin.Context, store types.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
|
|
|
|
start := time.Now()
|
|
|
|
res, err = next.CheckTx(ctx, store, tx)
|
|
|
|
delta := time.Now().Sub(start)
|
|
|
|
// TODO: log some info on the tx itself?
|
|
|
|
l := ctx.With("duration", micros(delta))
|
|
|
|
if err == nil {
|
|
|
|
l.Debug("CheckTx", "log", res.Log)
|
|
|
|
} else {
|
|
|
|
l.Info("CheckTx", "err", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (_ Logger) DeliverTx(ctx basecoin.Context, store types.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
|
|
|
|
start := time.Now()
|
|
|
|
res, err = next.DeliverTx(ctx, store, tx)
|
|
|
|
delta := time.Now().Sub(start)
|
|
|
|
// TODO: log some info on the tx itself?
|
|
|
|
l := ctx.With("duration", micros(delta))
|
|
|
|
if err == nil {
|
|
|
|
l.Info("DeliverTx", "log", res.Log)
|
|
|
|
} else {
|
|
|
|
l.Error("DeliverTx", "err", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-04 03:22:06 -07:00
|
|
|
func (_ Logger) SetOption(l log.Logger, store types.KVStore, module, key, value string, next basecoin.SetOptioner) (string, error) {
|
2017-07-03 09:10:46 -07:00
|
|
|
start := time.Now()
|
2017-07-04 03:22:06 -07:00
|
|
|
res, err := next.SetOption(l, store, module, key, value)
|
2017-07-03 09:10:46 -07:00
|
|
|
delta := time.Now().Sub(start)
|
|
|
|
// TODO: log the value being set also?
|
2017-07-04 03:22:06 -07:00
|
|
|
l = l.With("duration", micros(delta)).With("mod", module).With("key", key)
|
2017-07-03 09:10:46 -07:00
|
|
|
if err == nil {
|
|
|
|
l.Info("SetOption", "log", res)
|
|
|
|
} else {
|
|
|
|
l.Error("SetOption", "err", err)
|
|
|
|
}
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2017-06-29 08:03:43 -07:00
|
|
|
// micros returns how many microseconds passed in a call
|
|
|
|
func micros(d time.Duration) int {
|
|
|
|
return int(d.Seconds() * 1000000)
|
|
|
|
}
|