Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Add Logger package. #184

Merged
merged 5 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/inter/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/Fantom-foundation/go-lachesis/src/hash"
"github.com/Fantom-foundation/go-lachesis/src/logger"
)

// ParseEvents parses events from ASCII-scheme for test purpose.
Expand Down Expand Up @@ -47,7 +48,7 @@ func ParseEvents(asciiScheme string) (
break
default: // it is a event name
if _, ok := names[symbol]; ok {
panic(fmt.Errorf("event '%s' already exists", symbol))
logger.Log.Fatalf("event '%s' already exists", symbol)
}
nCreators = append(nCreators, current-1)
nNames = append(nNames, symbol)
Expand Down
7 changes: 4 additions & 3 deletions src/inter/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/Fantom-foundation/go-lachesis/src/crypto"
"github.com/Fantom-foundation/go-lachesis/src/hash"
"github.com/Fantom-foundation/go-lachesis/src/inter/wire"
"github.com/Fantom-foundation/go-lachesis/src/logger"
)

/*
Expand Down Expand Up @@ -44,7 +45,7 @@ func (e *Event) SignBy(priv *common.PrivateKey) error {
// Verify sign event by public key.
func (e *Event) Verify(pubKey *common.PublicKey) bool {
if pubKey == nil {
panic("can't verify")
logger.Log.Fatal("can't verify")
}

if e.Sign == "" {
Expand All @@ -54,7 +55,7 @@ func (e *Event) Verify(pubKey *common.PublicKey) bool {
hash := e.Hash()
r, s, err := crypto.DecodeSignature(string(e.Sign))
if err != nil {
panic(err)
logger.Log.Fatal(err)
}

return pubKey.Verify(hash.Bytes(), r, s)
Expand Down Expand Up @@ -112,7 +113,7 @@ func EventHashOf(e *Event) hash.Event {
w.Sign = ""
buf, err := proto.Marshal(w)
if err != nil {
panic(err)
logger.Log.Fatal(err)
}
return hash.Event(hash.Of(buf))
}
Expand Down
49 changes: 49 additions & 0 deletions src/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package logger

import (
"github.com/sirupsen/logrus"
)

/*
* global vars:
*/

var (
// Log - global logger
Log *logrus.Logger
)

func init() {
defaults := logrus.StandardLogger()
defaults.SetLevel(logrus.DebugLevel)
SetLogger(defaults)
}

// SetLogger sets logger for whole package.
func SetLogger(custom *logrus.Logger) {
if custom == nil {
panic("Nil-logger set")
}
Log = custom
}

// GetLevel return logrus.Level by string.
// Example: "debug" -> logrus.DebugLevel and etc.
func GetLevel(l string) logrus.Level {
switch l {
case "debug":
return logrus.DebugLevel
case "info":
return logrus.InfoLevel
case "warn":
return logrus.WarnLevel
case "error":
return logrus.ErrorLevel
case "fatal":
return logrus.FatalLevel
case "panic":
return logrus.PanicLevel
default:
return logrus.DebugLevel
}
}
6 changes: 4 additions & 2 deletions src/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package network
import (
"context"
"net"

"github.com/Fantom-foundation/go-lachesis/src/logger"
)

// ListenFunc returns addr listener.
Expand All @@ -13,7 +15,7 @@ type ListenFunc func(addr string) net.Listener
func TCPListener(addr string) net.Listener {
res, err := net.Listen("tcp", addr)
if err != nil {
panic(err)
logger.Log.Fatal(err)
}
return res
}
Expand All @@ -23,7 +25,7 @@ func TCPListener(addr string) net.Listener {
func FakeListener(addr string) net.Listener {
res, err := listenFreeAddr(Addr(addr))
if err != nil {
panic(err)
logger.Log.Fatal(err)
}

return res
Expand Down
5 changes: 3 additions & 2 deletions src/poslachesis/cli/command/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"

"github.com/Fantom-foundation/go-lachesis/src/hash"
"github.com/Fantom-foundation/go-lachesis/src/logger"
)

// Transfer makes a transaction for stake transfer.
Expand Down Expand Up @@ -44,9 +45,9 @@ func init() {
Transfer.Flags().Uint64("amount", 0, "transaction amount (required)")

if err := Transfer.MarkFlagRequired("receiver"); err != nil {
panic(err)
logger.Log.Fatal(err)
}
if err := Transfer.MarkFlagRequired("amount"); err != nil {
panic(err)
logger.Log.Fatal(err)
}
}
2 changes: 2 additions & 0 deletions src/poslachesis/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Config struct {
AppPort int
CtrlPort int
Node posnode.Config
LogLevel string
}

// DefaultConfig returns lachesis default config.
Expand All @@ -20,6 +21,7 @@ func DefaultConfig() *Config {
AppPort: 55556,
CtrlPort: 55557,
Node: *posnode.DefaultConfig(),
LogLevel: "debug",
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/poslachesis/lachesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package lachesis

import (
"github.com/dgraph-io/badger"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"

"github.com/Fantom-foundation/go-lachesis/src/common"
"github.com/Fantom-foundation/go-lachesis/src/hash"
"github.com/Fantom-foundation/go-lachesis/src/kvdb"
"github.com/Fantom-foundation/go-lachesis/src/logger"
"github.com/Fantom-foundation/go-lachesis/src/network"
"github.com/Fantom-foundation/go-lachesis/src/posnode"
"github.com/Fantom-foundation/go-lachesis/src/posposet"
Expand Down Expand Up @@ -37,6 +39,10 @@ func makeLachesis(db *badger.DB, host string, key *common.PrivateKey, conf *Conf
conf = DefaultConfig()
}

l := logrus.StandardLogger()
l.SetLevel(logger.GetLevel(conf.LogLevel))
logger.SetLogger(l)

c := posposet.New(cdb, ndb)
n := posnode.New(host, key, ndb, c, &conf.Node, listen, opts...)

Expand Down Expand Up @@ -75,7 +81,7 @@ func (l *Lachesis) AddPeers(hosts ...string) {

func (l *Lachesis) init(genesis map[hash.Peer]uint64) {
if err := l.consensusStore.ApplyGenesis(genesis); err != nil {
panic(err)
logger.Log.Fatal(err)
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/poslachesis/service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lachesis

import (
"github.com/Fantom-foundation/go-lachesis/src/logger"
"github.com/Fantom-foundation/go-lachesis/src/network"
"github.com/Fantom-foundation/go-lachesis/src/poset"
"github.com/Fantom-foundation/go-lachesis/src/posposet"
Expand All @@ -27,7 +28,7 @@ func (l *Lachesis) serviceStart() {
l.service.listen,
)
if err != nil {
panic(err)
logger.Log.Fatal(err)
}
defer ctrl.Close()

Expand All @@ -42,7 +43,7 @@ func (l *Lachesis) serviceStart() {
l.service.listen,
)
if err != nil {
panic(err)
logger.Log.Fatal(err)
}
defer app.Close()

Expand Down
7 changes: 4 additions & 3 deletions src/posnode/api/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/Fantom-foundation/go-lachesis/src/common"
"github.com/Fantom-foundation/go-lachesis/src/crypto"
"github.com/Fantom-foundation/go-lachesis/src/hash"
"github.com/Fantom-foundation/go-lachesis/src/logger"
)

// peerID is a internal key for context.Value().
Expand Down Expand Up @@ -86,7 +87,7 @@ func ServerAuth(key *common.PrivateKey) grpc.UnaryServerInterceptor {
sign := signData(resp, key)
md := metadata.Pairs("sign", sign, "pub", pub)
if e := grpc.SetTrailer(ctx, md); e != nil {
panic(err)
logger.Log.Fatal(err)
}

return resp, err
Expand Down Expand Up @@ -154,7 +155,7 @@ func verifyData(data interface{}, sign string, pub *common.PublicKey) error {
func hashOfData(data interface{}) hash.Hash {
d, ok := data.(proto.Message)
if !ok {
panic("data is not proto.Message")
logger.Log.Fatal("data is not proto.Message")
}

if IsProtoEmpty(&d) {
Expand All @@ -164,7 +165,7 @@ func hashOfData(data interface{}) hash.Hash {
var pbf proto.Buffer
pbf.SetDeterministic(true)
if err := pbf.Marshal(d); err != nil {
panic(err)
logger.Log.Fatal(err)
}

return hash.Of(pbf.Bytes())
Expand Down
7 changes: 4 additions & 3 deletions src/posnode/api/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/Fantom-foundation/go-lachesis/src/common"
"github.com/Fantom-foundation/go-lachesis/src/hash"
"github.com/Fantom-foundation/go-lachesis/src/logger"
"github.com/Fantom-foundation/go-lachesis/src/network"
)

Expand Down Expand Up @@ -48,13 +49,13 @@ func StartService(bind string, key *common.PrivateKey, svc NodeServer, log func(
func GrpcPeerHost(ctx context.Context) string {
p, ok := peer.FromContext(ctx)
if !ok {
panic("GrpcPeerHost should be called from gRPC handler only")
logger.Log.Fatal("GrpcPeerHost should be called from gRPC handler only")
}

addr := p.Addr.String()
host, _, err := net.SplitHostPort(addr)
if err != nil {
panic(err)
logger.Log.Fatal(err)
}
return host
}
Expand All @@ -63,7 +64,7 @@ func GrpcPeerHost(ctx context.Context) string {
func GrpcPeerID(ctx context.Context) hash.Peer {
id, ok := ctx.Value(peerID{}).(hash.Peer)
if !ok {
panic("GrpcPeerID should be called from gRPC handler only")
logger.Log.Fatal("GrpcPeerID should be called from gRPC handler only")
}

return id
Expand Down
4 changes: 3 additions & 1 deletion src/posnode/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package posnode

import (
"sync"

"github.com/Fantom-foundation/go-lachesis/src/logger"
)

// builtin is a set of some built in data.
Expand All @@ -19,7 +21,7 @@ func (n *Node) AddBuiltInPeers(hosts ...string) {

n.builtin.hosts = append(n.builtin.hosts, hosts...)

n.log.Debugf("built in peer hosts: %v", n.builtin.hosts)
logger.Log.Debugf("built in peer hosts: %v", n.builtin.hosts)
}

// NextBuiltInPeer returns next peer host or empty string.
Expand Down
5 changes: 3 additions & 2 deletions src/posnode/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/pkg/errors"
"google.golang.org/grpc"

"github.com/Fantom-foundation/go-lachesis/src/logger"
"github.com/Fantom-foundation/go-lachesis/src/posnode/api"
)

Expand Down Expand Up @@ -50,12 +51,12 @@ func (n *Node) initClient() {
// ConnectTo connects to other node service.
func (n *Node) ConnectTo(peer *Peer) (client api.NodeClient, free func(), fail func(error), err error) {
addr := n.NetAddrOf(peer.Host)
n.log.Debugf("connect to %s", addr)
logger.Log.Debugf("connect to %s", addr)

c, err := n.connPool.Get(addr)
if err != nil {
err = errors.Wrapf(err, "connect to: %s", addr)
n.log.Warn(err)
logger.Log.Warn(err)
return
}

Expand Down
13 changes: 7 additions & 6 deletions src/posnode/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"google.golang.org/grpc/status"

"github.com/Fantom-foundation/go-lachesis/src/hash"
"github.com/Fantom-foundation/go-lachesis/src/logger"
"github.com/Fantom-foundation/go-lachesis/src/posnode/api"
)

Expand Down Expand Up @@ -58,7 +59,7 @@ func (n *Node) StartDiscovery() {
}
}()

n.log.Info("discovery started")
logger.Log.Info("discovery started")
}

// StopDiscovery stops network discovery.
Expand All @@ -70,7 +71,7 @@ func (n *Node) StopDiscovery() {
close(n.discovery.done)
n.discovery.done = nil

n.log.Info("discovery stopped")
logger.Log.Info("discovery stopped")
}

// CheckPeerIsKnown queues peer checking for a late.
Expand All @@ -81,7 +82,7 @@ func (n *Node) CheckPeerIsKnown(host string, id *hash.Peer) {
unknown: id,
}:
default:
n.log.Warn("discovery.tasks queue is full, so skipped")
logger.Log.Warn("discovery.tasks queue is full, so skipped")
}
}

Expand All @@ -94,7 +95,7 @@ func (n *Node) AskPeerInfo(host string, id *hash.Peer) {
return
}

n.log.Debugf("ask %s about peer %s", host, id)
logger.Log.Debugf("ask %s about peer %s", host, id)

peer := &Peer{Host: host}

Expand All @@ -116,7 +117,7 @@ func (n *Node) AskPeerInfo(host string, id *hash.Peer) {
if id == nil {
n.ConnectFail(peer, fmt.Errorf("host %s knows nothing about self", host))
} else {
n.log.Warnf("peer %s (%s) knows nothing about %s", source.String(), host, id.String())
logger.Log.Warnf("peer %s (%s) knows nothing about %s", source.String(), host, id.String())
n.ConnectOK(peer)
}
return
Expand All @@ -136,7 +137,7 @@ func (n *Node) AskPeerInfo(host string, id *hash.Peer) {
info.Host = host
peer = WireToPeer(info)
n.store.SetWirePeer(peer.ID, info)
n.log.Debugf("discovered new peer %s with host %s", info.ID, info.Host)
logger.Log.Debugf("discovered new peer %s with host %s", info.ID, info.Host)
n.ConnectOK(peer)
}

Expand Down
Loading