From 957ebaa960bef4d92db1ac77c47299f07e8a2a6e Mon Sep 17 00:00:00 2001 From: devintegral2 Date: Tue, 21 May 2019 15:38:45 +0700 Subject: [PATCH 1/5] Setup logger level. Replace panic -> log.Fatal --- cmd/dummy/commands/root.go | 24 ++++-------------------- cmd/dummy_client/main.go | 26 ++++---------------------- cmd/lachesis/commands/run.go | 10 ++++++---- cmd/network/commands/run.go | 6 ++++-- src/lachesis/lachesis_config.go | 22 ++-------------------- src/poslachesis/config.go | 2 ++ src/poslachesis/lachesis.go | 4 ++-- src/posnode/emitter.go | 2 +- src/posnode/gossip.go | 10 +++++----- src/posnode/gossip_test.go | 2 +- src/posnode/log.go | 6 +++++- src/posnode/node.go | 4 ++-- src/posnode/node_test.go | 2 +- src/posnode/store.go | 24 ++++++++++++------------ src/posposet/common_test.go | 2 +- src/posposet/flag_table.go | 3 +-- src/posposet/frame.go | 4 +--- src/posposet/frames.go | 2 +- src/posposet/input.go | 2 +- src/posposet/poset.go | 11 +++++++++-- src/posposet/poset_atropos_test.go | 2 +- src/posposet/state.go | 4 ++-- src/posposet/store.go | 18 +++++++++--------- src/posposet/store_test.go | 2 +- src/utils/util.go | 23 +++++++++++++++++++++++ 25 files changed, 101 insertions(+), 116 deletions(-) diff --git a/cmd/dummy/commands/root.go b/cmd/dummy/commands/root.go index 911400155..fec9b24dd 100644 --- a/cmd/dummy/commands/root.go +++ b/cmd/dummy/commands/root.go @@ -6,11 +6,13 @@ import ( "io/ioutil" "os" - "github.com/Fantom-foundation/go-lachesis/src/dummy" "github.com/rifflock/lfshook" "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" + + "github.com/Fantom-foundation/go-lachesis/src/dummy" + "github.com/Fantom-foundation/go-lachesis/src/utils" ) var ( @@ -73,7 +75,7 @@ func loadConfig(cmd *cobra.Command, args []string) error { return err } logger = newLogger() - logger.Level = logLevel(config.LogLevel) + logger.Level = utils.GetLogLevel(config.LogLevel) logger.WithFields(logrus.Fields{ "name": config.Name, "client-listen": config.ClientAddr, @@ -117,21 +119,3 @@ func newLogger() *logrus.Logger { )) return logger } -func logLevel(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 - } -} diff --git a/cmd/dummy_client/main.go b/cmd/dummy_client/main.go index 32d86acba..5fc129f32 100644 --- a/cmd/dummy_client/main.go +++ b/cmd/dummy_client/main.go @@ -5,12 +5,13 @@ import ( "fmt" "io/ioutil" "os" - + "github.com/rifflock/lfshook" "github.com/sirupsen/logrus" "github.com/urfave/cli" - + "github.com/Fantom-foundation/go-lachesis/src/dummy" + "github.com/Fantom-foundation/go-lachesis/src/utils" ) var ( @@ -57,7 +58,7 @@ func main() { func run(c *cli.Context) error { logger := newLogger() - logger.Level = logLevel(c.String(LogLevelFlag.Name)) + logger.Level = utils.GetLogLevel(c.String(LogLevelFlag.Name)) name := c.String(NameFlag.Name) address := c.String(ProxyAddressFlag.Name) @@ -111,22 +112,3 @@ func newLogger() *logrus.Logger { )) return logger } - -func logLevel(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 - } -} diff --git a/cmd/lachesis/commands/run.go b/cmd/lachesis/commands/run.go index c88ca82cb..cbf19d4c9 100644 --- a/cmd/lachesis/commands/run.go +++ b/cmd/lachesis/commands/run.go @@ -7,14 +7,16 @@ import ( "runtime" "time" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "github.com/Fantom-foundation/go-lachesis/src/dummy" "github.com/Fantom-foundation/go-lachesis/src/lachesis" "github.com/Fantom-foundation/go-lachesis/src/log" aproxy "github.com/Fantom-foundation/go-lachesis/src/proxy" "github.com/Fantom-foundation/go-lachesis/tester" - "github.com/sirupsen/logrus" - "github.com/spf13/cobra" - "github.com/spf13/viper" + "github.com/Fantom-foundation/go-lachesis/src/utils" ) //NewRunCmd returns the command that starts a Lachesis node @@ -29,7 +31,7 @@ func NewRunCmd() *cobra.Command { } func runSingleLachesis(config *CLIConfig) error { - config.Lachesis.Logger.Level = lachesis.LogLevel(config.Lachesis.LogLevel) + config.Lachesis.Logger.Level = utils.GetLogLevel(config.Lachesis.LogLevel) config.Lachesis.NodeConfig.Logger = config.Lachesis.Logger if config.Log2file { f, err := os.OpenFile(fmt.Sprintf("lachesis_%v.log", config.Lachesis.BindAddr), diff --git a/cmd/network/commands/run.go b/cmd/network/commands/run.go index 1ef8415ff..42b02790c 100644 --- a/cmd/network/commands/run.go +++ b/cmd/network/commands/run.go @@ -12,9 +12,11 @@ import ( "syscall" "time" - "github.com/Fantom-foundation/go-lachesis/src/lachesis" "github.com/spf13/cobra" "github.com/spf13/viper" + + "github.com/Fantom-foundation/go-lachesis/src/lachesis" + "github.com/Fantom-foundation/go-lachesis/src/utils" ) //NewRunCmd returns the command that starts a Lachesis node @@ -204,7 +206,7 @@ func loadConfig(cmd *cobra.Command, args []string) error { return err } - config.Lachesis.Logger.Level = lachesis.LogLevel(config.Lachesis.LogLevel) + config.Lachesis.Logger.Level = utils.GetLogLevel(config.Lachesis.LogLevel) config.Lachesis.NodeConfig.Logger = config.Lachesis.Logger return nil diff --git a/src/lachesis/lachesis_config.go b/src/lachesis/lachesis_config.go index 8533b2101..0ea0c4db0 100644 --- a/src/lachesis/lachesis_config.go +++ b/src/lachesis/lachesis_config.go @@ -15,6 +15,7 @@ import ( "github.com/Fantom-foundation/go-lachesis/src/peer" "github.com/Fantom-foundation/go-lachesis/src/pos" "github.com/Fantom-foundation/go-lachesis/src/proxy" + "github.com/Fantom-foundation/go-lachesis/src/utils" ) type LachesisConfig struct { @@ -62,7 +63,7 @@ func NewDefaultConfig() *LachesisConfig { TestDelay: 1, } - config.Logger.Level = LogLevel(config.LogLevel) + config.Logger.Level = utils.GetLogLevel(config.LogLevel) lachesis_log.NewLocal(config.Logger, config.LogLevel) //config.Proxy = sproxy.NewInmemAppProxy(config.Logger) //config.Proxy, _ = sproxy.NewSocketAppProxy("127.0.0.1:1338", "127.0.0.1:1339", 1*time.Second, config.Logger) @@ -109,22 +110,3 @@ func HomeDir() string { } return "" } - -func LogLevel(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 - } -} diff --git a/src/poslachesis/config.go b/src/poslachesis/config.go index 7f9b3b643..da0ee36a1 100644 --- a/src/poslachesis/config.go +++ b/src/poslachesis/config.go @@ -12,6 +12,7 @@ type Config struct { AppPort int CtrlPort int Node posnode.Config + LogLevel string } // DefaultConfig returns lachesis default config. @@ -20,6 +21,7 @@ func DefaultConfig() *Config { AppPort: 55556, CtrlPort: 55557, Node: *posnode.DefaultConfig(), + LogLevel: "debug", } } diff --git a/src/poslachesis/lachesis.go b/src/poslachesis/lachesis.go index 2e78d66bb..381c530a8 100644 --- a/src/poslachesis/lachesis.go +++ b/src/poslachesis/lachesis.go @@ -37,8 +37,8 @@ func makeLachesis(db *badger.DB, host string, key *common.PrivateKey, conf *Conf conf = DefaultConfig() } - c := posposet.New(cdb, ndb) - n := posnode.New(host, key, ndb, c, &conf.Node, listen, opts...) + c := posposet.New(cdb, ndb, conf.LogLevel) + n := posnode.New(host, key, ndb, c, &conf.Node, conf.LogLevel, listen, opts...) return &Lachesis{ host: host, diff --git a/src/posnode/emitter.go b/src/posnode/emitter.go index 4fa523ed0..549e82fe7 100644 --- a/src/posnode/emitter.go +++ b/src/posnode/emitter.go @@ -128,7 +128,7 @@ func (n *Node) EmitEvent() *inter.Event { ExternalTransactions: externalTxns, } if err := event.SignBy(n.key); err != nil { - panic(err) + n.log.Fatal(err) } n.saveNewEvent(event, false) diff --git a/src/posnode/gossip.go b/src/posnode/gossip.go index 47cfbcd8b..d856a2f0e 100644 --- a/src/posnode/gossip.go +++ b/src/posnode/gossip.go @@ -144,7 +144,7 @@ func (n *Node) checkParents(client api.NodeClient, peer *Peer, parents hash.Even toDownload := n.lockNotDownloaded(parents) defer n.unlockDownloaded(toDownload) - n.log.Debugf("check Parents") + n.log.Info("check Parents") for e := range toDownload { if e == hash.ZeroEvent { @@ -156,11 +156,11 @@ func (n *Node) checkParents(client api.NodeClient, peer *Peer, parents hash.Even event, err := n.downloadEvent(client, peer, &req) if err != nil { - n.log.Debugf("download parent event error: %s", err.Error()) + n.log.Warnf("download parent event error: %s", err.Error()) } if event == nil { - n.log.Debugf("download parent event error: Event is nil") + n.log.Warn("download parent event error: Event is nil") } } } @@ -206,7 +206,7 @@ func (n *Node) downloadEvent(client api.NodeClient, peer *Peer, req *api.EventRe id, ctx := api.ServerPeerID(ctx) - n.log.Debugf("download event") + n.log.Info("download event") w, err := client.GetEvent(ctx, req) if err != nil { @@ -252,7 +252,7 @@ func (n *Node) downloadEvent(client api.NodeClient, peer *Peer, req *api.EventRe // Note: event should be last from its creator. // Note: we should not update Peer Height during save parent event. func (n *Node) saveNewEvent(e *inter.Event, isParent bool) { - n.log.Debugf("save new event") + n.log.Info("save new event") n.store.SetEvent(e) n.store.SetEventHash(e.Creator, e.Index, e.Hash()) diff --git a/src/posnode/gossip_test.go b/src/posnode/gossip_test.go index c0fa92da4..6f4db8d1b 100644 --- a/src/posnode/gossip_test.go +++ b/src/posnode/gossip_test.go @@ -155,7 +155,7 @@ func TestMissingParents(t *testing.T) { if err != nil { t.Fatal(err) } - + unknowns, err := node2.compareKnownEvents(client, peer) if err != nil { t.Fatal(err) diff --git a/src/posnode/log.go b/src/posnode/log.go index fc0f7ae0f..78220c7a5 100644 --- a/src/posnode/log.go +++ b/src/posnode/log.go @@ -2,13 +2,17 @@ package posnode import ( "github.com/sirupsen/logrus" + + "github.com/Fantom-foundation/go-lachesis/src/utils" ) type logger struct { log *logrus.Entry } -func newLogger(node string) (r logger) { +func newLogger(node, logLevel string) (r logger) { + log.SetLevel(utils.GetLogLevel(logLevel)) + if node != "" { r.log = log.WithField("node", node) } else { diff --git a/src/posnode/node.go b/src/posnode/node.go index 1ede50262..4616a0ebb 100644 --- a/src/posnode/node.go +++ b/src/posnode/node.go @@ -34,7 +34,7 @@ type Node struct { // New creates node. // It does not start any process. -func New(host string, key *common.PrivateKey, s *Store, c Consensus, conf *Config, listen network.ListenFunc, opts ...grpc.DialOption) *Node { +func New(host string, key *common.PrivateKey, s *Store, c Consensus, conf *Config, logLevel string, listen network.ListenFunc, opts ...grpc.DialOption) *Node { if key == nil { key = crypto.GenerateKey() } @@ -53,7 +53,7 @@ func New(host string, key *common.PrivateKey, s *Store, c Consensus, conf *Confi conf: *conf, service: service{listen, nil}, connPool: connPool{opts: opts}, - logger: newLogger(host), + logger: newLogger(host, logLevel), } return &n diff --git a/src/posnode/node_test.go b/src/posnode/node_test.go index 4e19652e4..3fd9a30f5 100644 --- a/src/posnode/node_test.go +++ b/src/posnode/node_test.go @@ -9,5 +9,5 @@ import ( // NewForTests creates node with fake network client. func NewForTests(host string, s *Store, c Consensus) *Node { - return New(host, nil, s, c, nil, network.FakeListener, FakeClient(host)) + return New(host, nil, s, c, nil, "debug", network.FakeListener, FakeClient(host)) } diff --git a/src/posnode/store.go b/src/posnode/store.go index 3a5ec5030..89b08aff1 100644 --- a/src/posnode/store.go +++ b/src/posnode/store.go @@ -79,7 +79,7 @@ func (s *Store) SetEventHash(creator hash.Peer, index uint64, hash hash.Event) { key := append(creator.Bytes(), intToBytes(index)...) if err := s.hashes.Put(key, hash.Bytes()); err != nil { - panic(err) + log.Fatal(err) } } @@ -89,7 +89,7 @@ func (s *Store) GetEventHash(creator hash.Peer, index uint64) *hash.Event { buf, err := s.hashes.Get(key) if err != nil { - panic(err) + log.Fatal(err) } if buf == nil { return nil @@ -148,16 +148,16 @@ func (s *Store) BootstrapPeers(peers ...*Peer) { var pbf proto.Buffer w := peer.ToWire() if err := pbf.Marshal(w); err != nil { - panic(err) + log.Fatal(err) } if err := batch.Put(peer.ID.Bytes(), pbf.Bytes()); err != nil { - panic(err) + log.Fatal(err) } ids = append(ids, peer.ID) } if err := batch.Write(); err != nil { - panic(err) + log.Fatal(err) } s.SetTopPeers(ids) @@ -180,7 +180,7 @@ func (s *Store) GetTopPeers() []hash.Peer { // SetPeerHeight stores last event index of peer. func (s *Store) SetPeerHeight(id hash.Peer, height uint64) { if err := s.peerHeights.Put(id.Bytes(), intToBytes(height)); err != nil { - panic(err) + log.Fatal(err) } } @@ -188,7 +188,7 @@ func (s *Store) SetPeerHeight(id hash.Peer, height uint64) { func (s *Store) GetPeerHeight(id hash.Peer) uint64 { buf, err := s.peerHeights.Get(id.Bytes()) if err != nil { - panic(err) + log.Fatal(err) } if buf == nil { return 0 @@ -205,18 +205,18 @@ func (s *Store) set(table kvdb.Database, key []byte, val proto.Message) { var pbf proto.Buffer if err := pbf.Marshal(val); err != nil { - panic(err) + log.Fatal(err) } if err := table.Put(key, pbf.Bytes()); err != nil { - panic(err) + log.Fatal(err) } } func (s *Store) get(table kvdb.Database, key []byte, to proto.Message) proto.Message { buf, err := table.Get(key) if err != nil { - panic(err) + log.Fatal(err) } if buf == nil { return nil @@ -224,7 +224,7 @@ func (s *Store) get(table kvdb.Database, key []byte, to proto.Message) proto.Mes err = proto.Unmarshal(buf, to) if err != nil { - panic(err) + log.Fatal(err) } return to } @@ -232,7 +232,7 @@ func (s *Store) get(table kvdb.Database, key []byte, to proto.Message) proto.Mes func (s *Store) has(table kvdb.Database, key []byte) bool { res, err := table.Has(key) if err != nil { - panic(err) + log.Fatal(err) } return res } diff --git a/src/posposet/common_test.go b/src/posposet/common_test.go index e5f751898..0704332fc 100644 --- a/src/posposet/common_test.go +++ b/src/posposet/common_test.go @@ -20,7 +20,7 @@ func FakePoset(nodes []hash.Peer) (*Poset, *Store, *EventStore) { input := NewEventStore(nil, false) - poset := New(store, input) + poset := New(store, input, "debug") poset.Bootstrap() return poset, store, input diff --git a/src/posposet/flag_table.go b/src/posposet/flag_table.go index bd4363b62..c4bbe4257 100644 --- a/src/posposet/flag_table.go +++ b/src/posposet/flag_table.go @@ -1,7 +1,6 @@ package posposet import ( - "fmt" "strings" "github.com/Fantom-foundation/go-lachesis/src/hash" @@ -160,7 +159,7 @@ func WireToEventsByPeer(arr []*wire.EventDescr) EventsByPeer { res[creator] = hash.Events{} } if !res[creator].Add(h) { - panic(fmt.Errorf("double value is detected")) + log.Fatal("double value is detected") } } diff --git a/src/posposet/frame.go b/src/posposet/frame.go index b5ecb26b3..99811279f 100644 --- a/src/posposet/frame.go +++ b/src/posposet/frame.go @@ -1,8 +1,6 @@ package posposet import ( - "fmt" - "github.com/Fantom-foundation/go-lachesis/src/hash" "github.com/Fantom-foundation/go-lachesis/src/inter" "github.com/Fantom-foundation/go-lachesis/src/posposet/wire" @@ -103,7 +101,7 @@ func (p *Poset) setFrameSaving(f *Frame) { if f.Index > p.state.LastFinishedFrameN { p.store.SetFrame(f) } else { - panic(fmt.Errorf("frame %d is finished and should not be changed", f.Index)) + log.Fatalf("frame %d is finished and should not be changed", f.Index) } } } diff --git a/src/posposet/frames.go b/src/posposet/frames.go index e2fa9686f..8c3064786 100644 --- a/src/posposet/frames.go +++ b/src/posposet/frames.go @@ -66,7 +66,7 @@ func (p *Poset) FrameOfEvent(event hash.Event) (frame *Frame, isRoot bool) { // frame finds or creates frame. func (p *Poset) frame(n uint64, orCreate bool) *Frame { if n < p.state.LastFinishedFrameN && orCreate { - panic(fmt.Errorf("too old frame%d is requested", n)) + log.Fatalf("too old frame %d is requested", n) } // return ephemeral if n == 0 { diff --git a/src/posposet/input.go b/src/posposet/input.go index 4d20da745..941f2193d 100644 --- a/src/posposet/input.go +++ b/src/posposet/input.go @@ -23,7 +23,7 @@ func (p *Poset) HasEvent(h hash.Event) bool { func (p *Poset) GetEvent(h hash.Event) *Event { e := p.input.GetEvent(h) if e == nil { - panic("got unsaved event") + log.Fatal("got unsaved event") } return &Event{ Event: *e, diff --git a/src/posposet/poset.go b/src/posposet/poset.go index 5d3bbd0fc..dbd443658 100644 --- a/src/posposet/poset.go +++ b/src/posposet/poset.go @@ -4,8 +4,11 @@ import ( "sort" "sync" + "github.com/sirupsen/logrus" + "github.com/Fantom-foundation/go-lachesis/src/hash" "github.com/Fantom-foundation/go-lachesis/src/inter" + "github.com/Fantom-foundation/go-lachesis/src/utils" ) // Poset processes events to get consensus. @@ -26,7 +29,7 @@ type Poset struct { // New creates Poset instance. // It does not start any process. -func New(store *Store, input EventSource) *Poset { +func New(store *Store, input EventSource, logLevel string) *Poset { const buffSize = 10 p := &Poset{ @@ -38,6 +41,10 @@ func New(store *Store, input EventSource) *Poset { incompleteEvents: make(map[hash.Event]*Event), } + log = logrus.StandardLogger() + log.SetLevel(utils.GetLogLevel(logLevel)) + SetLogger(log) + return p } @@ -188,7 +195,7 @@ func (p *Poset) consensus(e *Event) { applyRewards(state, ordered) balances, err := state.Commit(true) if err != nil { - panic(err) + log.Fatal(err) } if applyAt.SetBalances(balances) { p.reconsensusFromFrame(applyAt.Index) diff --git a/src/posposet/poset_atropos_test.go b/src/posposet/poset_atropos_test.go index 73a7a2afb..87c991cad 100644 --- a/src/posposet/poset_atropos_test.go +++ b/src/posposet/poset_atropos_test.go @@ -104,7 +104,7 @@ func testSpecialNamedAtropos(t *testing.T, tryRestoring bool, asciiScheme string n++ if tryRestoring && n == len(names)*2/3 { ee := p.incompleteEvents - p = New(store, input) + p = New(store, input, "debug") p.Bootstrap() for _, e := range ee { input.SetEvent(&e.Event) diff --git a/src/posposet/state.go b/src/posposet/state.go index 511e301f8..f2a7498e2 100644 --- a/src/posposet/state.go +++ b/src/posposet/state.go @@ -55,7 +55,7 @@ func (p *Poset) Bootstrap() { // restore state p.state = p.store.GetState() if p.state == nil { - panic("Apply genesis for store first") + log.Fatal("Apply genesis for store first") } // restore frames for n := p.state.LastFinishedFrameN; true; n++ { @@ -75,7 +75,7 @@ func GenesisHash(balances map[hash.Peer]uint64) hash.Hash { defer s.Close() if err := s.ApplyGenesis(balances); err != nil { - panic(err) + log.Fatal(err) } return s.GetState().Genesis diff --git a/src/posposet/store.go b/src/posposet/store.go index 2878ca1ba..8c3a04968 100644 --- a/src/posposet/store.go +++ b/src/posposet/store.go @@ -64,7 +64,7 @@ func (s *Store) initCache() { cache := func() *lru.Cache { c, err := lru.New(cacheSize) if err != nil { - panic(err) + log.Fatal(err) } return c } @@ -129,7 +129,7 @@ func (s *Store) SetEventFrame(e hash.Event, frame uint64) { key := e.Bytes() val := intToBytes(frame) if err := s.event2frame.Put(key, val); err != nil { - panic(err) + log.Fatal(err) } if s.event2frameCache != nil { @@ -149,7 +149,7 @@ func (s *Store) GetEventFrame(e hash.Event) *uint64 { key := e.Bytes() buf, err := s.event2frame.Get(key) if err != nil { - panic(err) + log.Fatal(err) } if buf == nil { return nil @@ -215,7 +215,7 @@ func (s *Store) GetBlock(n uint64) *Block { func (s *Store) StateDB(from hash.Hash) *state.DB { db, err := state.New(from, s.balances) if err != nil { - panic(err) + log.Fatal(err) } return db } @@ -228,18 +228,18 @@ func (s *Store) set(table kvdb.Database, key []byte, val proto.Message) { var pbf proto.Buffer if err := pbf.Marshal(val); err != nil { - panic(err) + log.Fatal(err) } if err := table.Put(key, pbf.Bytes()); err != nil { - panic(err) + log.Fatal(err) } } func (s *Store) get(table kvdb.Database, key []byte, to proto.Message) proto.Message { buf, err := table.Get(key) if err != nil { - panic(err) + log.Fatal(err) } if buf == nil { return nil @@ -247,7 +247,7 @@ func (s *Store) get(table kvdb.Database, key []byte, to proto.Message) proto.Mes err = proto.Unmarshal(buf, to) if err != nil { - panic(err) + log.Fatal(err) } return to } @@ -255,7 +255,7 @@ func (s *Store) get(table kvdb.Database, key []byte, to proto.Message) proto.Mes func (s *Store) has(table kvdb.Database, key []byte) bool { res, err := table.Has(key) if err != nil { - panic(err) + log.Fatal(err) } return res } diff --git a/src/posposet/store_test.go b/src/posposet/store_test.go index 514e22ce6..b45eb3eb5 100644 --- a/src/posposet/store_test.go +++ b/src/posposet/store_test.go @@ -84,7 +84,7 @@ func benchPoset(nodes []hash.Peer, input EventSource, store *Store, cached bool) panic(err) } - poset := New(store, input) + poset := New(store, input, "debug") poset.Bootstrap() return poset diff --git a/src/utils/util.go b/src/utils/util.go index c318fbbbe..78d580c95 100644 --- a/src/utils/util.go +++ b/src/utils/util.go @@ -6,6 +6,8 @@ import ( "strconv" "sync/atomic" "testing" + + "github.com/sirupsen/logrus" ) var startBase uint32 = 12000 @@ -75,3 +77,24 @@ func FreePort(network string) (port uint16) { }() return uint16(l.Addr().(*net.TCPAddr).Port) } + +// GetLogLevel return logrus.Level by string. +// Example: "debug" -> logrus.DebugLevel and etc. +func GetLogLevel(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 + } +} \ No newline at end of file From 047883d7042439d4cdbf451eefc9ac04f35600b6 Mon Sep 17 00:00:00 2001 From: devintegral2 Date: Tue, 21 May 2019 15:41:54 +0700 Subject: [PATCH 2/5] Fix fmt. --- cmd/dummy_client/main.go | 4 ++-- cmd/lachesis/commands/run.go | 2 +- src/utils/util.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/dummy_client/main.go b/cmd/dummy_client/main.go index 5fc129f32..5fa59420a 100644 --- a/cmd/dummy_client/main.go +++ b/cmd/dummy_client/main.go @@ -5,11 +5,11 @@ import ( "fmt" "io/ioutil" "os" - + "github.com/rifflock/lfshook" "github.com/sirupsen/logrus" "github.com/urfave/cli" - + "github.com/Fantom-foundation/go-lachesis/src/dummy" "github.com/Fantom-foundation/go-lachesis/src/utils" ) diff --git a/cmd/lachesis/commands/run.go b/cmd/lachesis/commands/run.go index cbf19d4c9..f8f057c1d 100644 --- a/cmd/lachesis/commands/run.go +++ b/cmd/lachesis/commands/run.go @@ -15,8 +15,8 @@ import ( "github.com/Fantom-foundation/go-lachesis/src/lachesis" "github.com/Fantom-foundation/go-lachesis/src/log" aproxy "github.com/Fantom-foundation/go-lachesis/src/proxy" - "github.com/Fantom-foundation/go-lachesis/tester" "github.com/Fantom-foundation/go-lachesis/src/utils" + "github.com/Fantom-foundation/go-lachesis/tester" ) //NewRunCmd returns the command that starts a Lachesis node diff --git a/src/utils/util.go b/src/utils/util.go index 78d580c95..7ad216fcb 100644 --- a/src/utils/util.go +++ b/src/utils/util.go @@ -97,4 +97,4 @@ func GetLogLevel(l string) logrus.Level { default: return logrus.DebugLevel } -} \ No newline at end of file +} From f8e6a1f5aee06f3ab3ae5d55f0640881e7960d49 Mon Sep 17 00:00:00 2001 From: devintegral2 Date: Tue, 21 May 2019 17:02:13 +0700 Subject: [PATCH 3/5] Create logger package. Revert prev changes. --- cmd/dummy/commands/root.go | 24 ++++++++++--- cmd/dummy_client/main.go | 22 ++++++++++-- cmd/lachesis/commands/run.go | 10 +++--- cmd/network/commands/run.go | 6 ++-- src/inter/common.go | 3 +- src/inter/event.go | 7 ++-- src/lachesis/lachesis_config.go | 22 ++++++++++-- src/logger/logger.go | 48 +++++++++++++++++++++++++ src/network/network.go | 6 ++-- src/poslachesis/cli/command/transfer.go | 5 +-- src/poslachesis/lachesis.go | 12 +++++-- src/poslachesis/service.go | 5 +-- src/posnode/api/security.go | 7 ++-- src/posnode/api/wire.go | 7 ++-- src/posnode/builtin.go | 4 ++- src/posnode/client.go | 5 +-- src/posnode/discovery.go | 13 +++---- src/posnode/emitter.go | 7 ++-- src/posnode/gossip.go | 17 ++++----- src/posnode/log.go | 44 ----------------------- src/posnode/main_test.go | 4 ++- src/posnode/node.go | 9 +++-- src/posnode/node_test.go | 2 +- src/posnode/peers.go | 3 +- src/posnode/service.go | 7 ++-- src/posnode/store.go | 25 ++++++------- src/posposet/common_test.go | 2 +- src/posposet/creators.go | 5 +-- src/posposet/flag_table.go | 3 +- src/posposet/frame.go | 3 +- src/posposet/frames.go | 3 +- src/posposet/input.go | 3 +- src/posposet/log.go | 22 ------------ src/posposet/poset.go | 16 +++------ src/posposet/poset_atropos_test.go | 2 +- src/posposet/state.go | 5 +-- src/posposet/store.go | 19 +++++----- src/posposet/store_test.go | 2 +- src/posposet/time.go | 5 +-- src/posposet/transaction.go | 3 +- src/utils/util.go | 23 ------------ 41 files changed, 237 insertions(+), 203 deletions(-) create mode 100644 src/logger/logger.go delete mode 100644 src/posnode/log.go delete mode 100644 src/posposet/log.go diff --git a/cmd/dummy/commands/root.go b/cmd/dummy/commands/root.go index fec9b24dd..3db62160f 100644 --- a/cmd/dummy/commands/root.go +++ b/cmd/dummy/commands/root.go @@ -6,13 +6,11 @@ import ( "io/ioutil" "os" + "github.com/Fantom-foundation/go-lachesis/src/dummy" "github.com/rifflock/lfshook" "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" - - "github.com/Fantom-foundation/go-lachesis/src/dummy" - "github.com/Fantom-foundation/go-lachesis/src/utils" ) var ( @@ -75,7 +73,7 @@ func loadConfig(cmd *cobra.Command, args []string) error { return err } logger = newLogger() - logger.Level = utils.GetLogLevel(config.LogLevel) + logger.Level = logLevel(config.LogLevel) logger.WithFields(logrus.Fields{ "name": config.Name, "client-listen": config.ClientAddr, @@ -119,3 +117,21 @@ func newLogger() *logrus.Logger { )) return logger } +func logLevel(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 + } +} \ No newline at end of file diff --git a/cmd/dummy_client/main.go b/cmd/dummy_client/main.go index 5fa59420a..618dce3a2 100644 --- a/cmd/dummy_client/main.go +++ b/cmd/dummy_client/main.go @@ -11,7 +11,6 @@ import ( "github.com/urfave/cli" "github.com/Fantom-foundation/go-lachesis/src/dummy" - "github.com/Fantom-foundation/go-lachesis/src/utils" ) var ( @@ -58,7 +57,7 @@ func main() { func run(c *cli.Context) error { logger := newLogger() - logger.Level = utils.GetLogLevel(c.String(LogLevelFlag.Name)) + logger.Level = logLevel(c.String(LogLevelFlag.Name)) name := c.String(NameFlag.Name) address := c.String(ProxyAddressFlag.Name) @@ -112,3 +111,22 @@ func newLogger() *logrus.Logger { )) return logger } + +func logLevel(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 + } +} \ No newline at end of file diff --git a/cmd/lachesis/commands/run.go b/cmd/lachesis/commands/run.go index f8f057c1d..c88ca82cb 100644 --- a/cmd/lachesis/commands/run.go +++ b/cmd/lachesis/commands/run.go @@ -7,16 +7,14 @@ import ( "runtime" "time" - "github.com/sirupsen/logrus" - "github.com/spf13/cobra" - "github.com/spf13/viper" - "github.com/Fantom-foundation/go-lachesis/src/dummy" "github.com/Fantom-foundation/go-lachesis/src/lachesis" "github.com/Fantom-foundation/go-lachesis/src/log" aproxy "github.com/Fantom-foundation/go-lachesis/src/proxy" - "github.com/Fantom-foundation/go-lachesis/src/utils" "github.com/Fantom-foundation/go-lachesis/tester" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "github.com/spf13/viper" ) //NewRunCmd returns the command that starts a Lachesis node @@ -31,7 +29,7 @@ func NewRunCmd() *cobra.Command { } func runSingleLachesis(config *CLIConfig) error { - config.Lachesis.Logger.Level = utils.GetLogLevel(config.Lachesis.LogLevel) + config.Lachesis.Logger.Level = lachesis.LogLevel(config.Lachesis.LogLevel) config.Lachesis.NodeConfig.Logger = config.Lachesis.Logger if config.Log2file { f, err := os.OpenFile(fmt.Sprintf("lachesis_%v.log", config.Lachesis.BindAddr), diff --git a/cmd/network/commands/run.go b/cmd/network/commands/run.go index 42b02790c..1ef8415ff 100644 --- a/cmd/network/commands/run.go +++ b/cmd/network/commands/run.go @@ -12,11 +12,9 @@ import ( "syscall" "time" + "github.com/Fantom-foundation/go-lachesis/src/lachesis" "github.com/spf13/cobra" "github.com/spf13/viper" - - "github.com/Fantom-foundation/go-lachesis/src/lachesis" - "github.com/Fantom-foundation/go-lachesis/src/utils" ) //NewRunCmd returns the command that starts a Lachesis node @@ -206,7 +204,7 @@ func loadConfig(cmd *cobra.Command, args []string) error { return err } - config.Lachesis.Logger.Level = utils.GetLogLevel(config.Lachesis.LogLevel) + config.Lachesis.Logger.Level = lachesis.LogLevel(config.Lachesis.LogLevel) config.Lachesis.NodeConfig.Logger = config.Lachesis.Logger return nil diff --git a/src/inter/common.go b/src/inter/common.go index 43e3e74b4..0e8519704 100644 --- a/src/inter/common.go +++ b/src/inter/common.go @@ -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. @@ -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) diff --git a/src/inter/event.go b/src/inter/event.go index 1b4635187..5eb2af638 100644 --- a/src/inter/event.go +++ b/src/inter/event.go @@ -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" ) /* @@ -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 == "" { @@ -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) @@ -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)) } diff --git a/src/lachesis/lachesis_config.go b/src/lachesis/lachesis_config.go index 0ea0c4db0..5d3aa987f 100644 --- a/src/lachesis/lachesis_config.go +++ b/src/lachesis/lachesis_config.go @@ -15,7 +15,6 @@ import ( "github.com/Fantom-foundation/go-lachesis/src/peer" "github.com/Fantom-foundation/go-lachesis/src/pos" "github.com/Fantom-foundation/go-lachesis/src/proxy" - "github.com/Fantom-foundation/go-lachesis/src/utils" ) type LachesisConfig struct { @@ -63,7 +62,7 @@ func NewDefaultConfig() *LachesisConfig { TestDelay: 1, } - config.Logger.Level = utils.GetLogLevel(config.LogLevel) + config.Logger.Level = LogLevel(config.LogLevel) lachesis_log.NewLocal(config.Logger, config.LogLevel) //config.Proxy = sproxy.NewInmemAppProxy(config.Logger) //config.Proxy, _ = sproxy.NewSocketAppProxy("127.0.0.1:1338", "127.0.0.1:1339", 1*time.Second, config.Logger) @@ -110,3 +109,22 @@ func HomeDir() string { } return "" } + +func LogLevel(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 + } +} \ No newline at end of file diff --git a/src/logger/logger.go b/src/logger/logger.go new file mode 100644 index 000000000..72e8af502 --- /dev/null +++ b/src/logger/logger.go @@ -0,0 +1,48 @@ +package logger + +import ( + "github.com/sirupsen/logrus" +) + +/* + * global vars: + */ + +var ( + 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 + } +} \ No newline at end of file diff --git a/src/network/network.go b/src/network/network.go index 094592c0e..ec0ca65e3 100644 --- a/src/network/network.go +++ b/src/network/network.go @@ -3,6 +3,8 @@ package network import ( "context" "net" + + "github.com/Fantom-foundation/go-lachesis/src/logger" ) // ListenFunc returns addr listener. @@ -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 } @@ -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 diff --git a/src/poslachesis/cli/command/transfer.go b/src/poslachesis/cli/command/transfer.go index a7925f913..eba5c0ea5 100644 --- a/src/poslachesis/cli/command/transfer.go +++ b/src/poslachesis/cli/command/transfer.go @@ -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. @@ -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) } } diff --git a/src/poslachesis/lachesis.go b/src/poslachesis/lachesis.go index 381c530a8..78574d07d 100644 --- a/src/poslachesis/lachesis.go +++ b/src/poslachesis/lachesis.go @@ -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" @@ -37,8 +39,12 @@ func makeLachesis(db *badger.DB, host string, key *common.PrivateKey, conf *Conf conf = DefaultConfig() } - c := posposet.New(cdb, ndb, conf.LogLevel) - n := posnode.New(host, key, ndb, c, &conf.Node, conf.LogLevel, listen, opts...) + 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...) return &Lachesis{ host: host, @@ -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) } } diff --git a/src/poslachesis/service.go b/src/poslachesis/service.go index 94b5f883f..f811d03e9 100644 --- a/src/poslachesis/service.go +++ b/src/poslachesis/service.go @@ -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" @@ -27,7 +28,7 @@ func (l *Lachesis) serviceStart() { l.service.listen, ) if err != nil { - panic(err) + logger.Log.Fatal(err) } defer ctrl.Close() @@ -42,7 +43,7 @@ func (l *Lachesis) serviceStart() { l.service.listen, ) if err != nil { - panic(err) + logger.Log.Fatal(err) } defer app.Close() diff --git a/src/posnode/api/security.go b/src/posnode/api/security.go index 146347179..4eefdee4d 100644 --- a/src/posnode/api/security.go +++ b/src/posnode/api/security.go @@ -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(). @@ -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 @@ -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) { @@ -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()) diff --git a/src/posnode/api/wire.go b/src/posnode/api/wire.go index 66c9db14f..562540521 100644 --- a/src/posnode/api/wire.go +++ b/src/posnode/api/wire.go @@ -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" ) @@ -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 } @@ -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 diff --git a/src/posnode/builtin.go b/src/posnode/builtin.go index 6ce5ba8f6..df3237ec3 100644 --- a/src/posnode/builtin.go +++ b/src/posnode/builtin.go @@ -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. @@ -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. diff --git a/src/posnode/client.go b/src/posnode/client.go index b80b39934..f62a8b5c6 100644 --- a/src/posnode/client.go +++ b/src/posnode/client.go @@ -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" ) @@ -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 } diff --git a/src/posnode/discovery.go b/src/posnode/discovery.go index 440f538ec..e137bddcb 100644 --- a/src/posnode/discovery.go +++ b/src/posnode/discovery.go @@ -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" ) @@ -58,7 +59,7 @@ func (n *Node) StartDiscovery() { } }() - n.log.Info("discovery started") + logger.Log.Info("discovery started") } // StopDiscovery stops network discovery. @@ -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. @@ -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") } } @@ -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} @@ -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 @@ -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) } diff --git a/src/posnode/emitter.go b/src/posnode/emitter.go index 549e82fe7..67358d67c 100644 --- a/src/posnode/emitter.go +++ b/src/posnode/emitter.go @@ -6,6 +6,7 @@ import ( "github.com/Fantom-foundation/go-lachesis/src/hash" "github.com/Fantom-foundation/go-lachesis/src/inter" + "github.com/Fantom-foundation/go-lachesis/src/logger" ) // emitter creates events from external transactions. @@ -80,7 +81,7 @@ func (n *Node) EmitEvent() *inter.Event { n.emitter.Lock() defer n.emitter.Unlock() - n.log.Debugf("emiting event") + logger.Log.Debugf("emiting event") var ( index uint64 @@ -128,11 +129,11 @@ func (n *Node) EmitEvent() *inter.Event { ExternalTransactions: externalTxns, } if err := event.SignBy(n.key); err != nil { - n.log.Fatal(err) + logger.Log.Fatal(err) } n.saveNewEvent(event, false) - n.log.Debugf("new event emited %s", event) + logger.Log.Debugf("new event emited %s", event) return event } diff --git a/src/posnode/gossip.go b/src/posnode/gossip.go index d856a2f0e..2062bbe11 100644 --- a/src/posnode/gossip.go +++ b/src/posnode/gossip.go @@ -8,6 +8,7 @@ import ( "github.com/Fantom-foundation/go-lachesis/src/hash" "github.com/Fantom-foundation/go-lachesis/src/inter" + "github.com/Fantom-foundation/go-lachesis/src/logger" "github.com/Fantom-foundation/go-lachesis/src/posnode/api" ) @@ -49,7 +50,7 @@ func (n *Node) StartGossip(threads int) { go n.gossiping(n.gossip.tickets) - n.log.Info("gossip started") + logger.Log.Info("gossip started") } // StopGossip stops gossiping. @@ -64,7 +65,7 @@ func (n *Node) StopGossip() { close(n.gossip.tickets) n.gossip.tickets = nil - n.log.Info("gossip stopped") + logger.Log.Info("gossip stopped") } // gossiping is a infinity gossip process. @@ -77,7 +78,7 @@ func (n *Node) gossiping(tickets chan struct{}) { defer n.FreePeer(peer) n.syncWithPeer(peer) } else { - n.log.Warn("no candidate for gossip") + logger.Log.Warn("no candidate for gossip") } time.Sleep(gossipIdle) }() @@ -144,7 +145,7 @@ func (n *Node) checkParents(client api.NodeClient, peer *Peer, parents hash.Even toDownload := n.lockNotDownloaded(parents) defer n.unlockDownloaded(toDownload) - n.log.Info("check Parents") + logger.Log.Info("check Parents") for e := range toDownload { if e == hash.ZeroEvent { @@ -156,11 +157,11 @@ func (n *Node) checkParents(client api.NodeClient, peer *Peer, parents hash.Even event, err := n.downloadEvent(client, peer, &req) if err != nil { - n.log.Warnf("download parent event error: %s", err.Error()) + logger.Log.Warnf("download parent event error: %s", err.Error()) } if event == nil { - n.log.Warn("download parent event error: Event is nil") + logger.Log.Warn("download parent event error: Event is nil") } } } @@ -206,7 +207,7 @@ func (n *Node) downloadEvent(client api.NodeClient, peer *Peer, req *api.EventRe id, ctx := api.ServerPeerID(ctx) - n.log.Info("download event") + logger.Log.Info("download event") w, err := client.GetEvent(ctx, req) if err != nil { @@ -252,7 +253,7 @@ func (n *Node) downloadEvent(client api.NodeClient, peer *Peer, req *api.EventRe // Note: event should be last from its creator. // Note: we should not update Peer Height during save parent event. func (n *Node) saveNewEvent(e *inter.Event, isParent bool) { - n.log.Info("save new event") + logger.Log.Info("save new event") n.store.SetEvent(e) n.store.SetEventHash(e.Creator, e.Index, e.Hash()) diff --git a/src/posnode/log.go b/src/posnode/log.go deleted file mode 100644 index 78220c7a5..000000000 --- a/src/posnode/log.go +++ /dev/null @@ -1,44 +0,0 @@ -package posnode - -import ( - "github.com/sirupsen/logrus" - - "github.com/Fantom-foundation/go-lachesis/src/utils" -) - -type logger struct { - log *logrus.Entry -} - -func newLogger(node, logLevel string) (r logger) { - log.SetLevel(utils.GetLogLevel(logLevel)) - - if node != "" { - r.log = log.WithField("node", node) - } else { - r.log = logrus.NewEntry(log) - } - return -} - -/* - * global vars: - */ - -var ( - 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 -} diff --git a/src/posnode/main_test.go b/src/posnode/main_test.go index d5f0997e2..bd5244020 100644 --- a/src/posnode/main_test.go +++ b/src/posnode/main_test.go @@ -4,6 +4,8 @@ import ( "io/ioutil" "os" "testing" + + "github.com/Fantom-foundation/go-lachesis/src/logger" ) const ( @@ -14,7 +16,7 @@ const ( // TestMain is for test mode settings func TestMain(m *testing.M) { if noLogOutput { - log.Out = ioutil.Discard + logger.Log.Out = ioutil.Discard } os.Exit(m.Run()) diff --git a/src/posnode/node.go b/src/posnode/node.go index 4616a0ebb..7a37db5d9 100644 --- a/src/posnode/node.go +++ b/src/posnode/node.go @@ -7,6 +7,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" + "github.com/Fantom-foundation/go-lachesis/src/logger" "github.com/Fantom-foundation/go-lachesis/src/network" ) @@ -29,12 +30,11 @@ type Node struct { downloads discovery builtin - logger } // New creates node. // It does not start any process. -func New(host string, key *common.PrivateKey, s *Store, c Consensus, conf *Config, logLevel string, listen network.ListenFunc, opts ...grpc.DialOption) *Node { +func New(host string, key *common.PrivateKey, s *Store, c Consensus, conf *Config, listen network.ListenFunc, opts ...grpc.DialOption) *Node { if key == nil { key = crypto.GenerateKey() } @@ -53,7 +53,6 @@ func New(host string, key *common.PrivateKey, s *Store, c Consensus, conf *Confi conf: *conf, service: service{listen, nil}, connPool: connPool{opts: opts}, - logger: newLogger(host, logLevel), } return &n @@ -109,13 +108,13 @@ func (n *Node) LastEventOf(peer hash.Peer) *inter.Event { func (n *Node) EventOf(peer hash.Peer, i uint64) *inter.Event { h := n.store.GetEventHash(peer, i) if h == nil { - n.log.Errorf("no event hash for (%s,%d) in store", peer.String(), i) + logger.Log.Errorf("no event hash for (%s,%d) in store", peer.String(), i) return nil } e := n.store.GetEvent(*h) if e == nil { - n.log.Errorf("no event in store of %d", i) + logger.Log.Errorf("no event in store of %d", i) } return e diff --git a/src/posnode/node_test.go b/src/posnode/node_test.go index 3fd9a30f5..4e19652e4 100644 --- a/src/posnode/node_test.go +++ b/src/posnode/node_test.go @@ -9,5 +9,5 @@ import ( // NewForTests creates node with fake network client. func NewForTests(host string, s *Store, c Consensus) *Node { - return New(host, nil, s, c, nil, "debug", network.FakeListener, FakeClient(host)) + return New(host, nil, s, c, nil, network.FakeListener, FakeClient(host)) } diff --git a/src/posnode/peers.go b/src/posnode/peers.go index c60b70199..c20831cac 100644 --- a/src/posnode/peers.go +++ b/src/posnode/peers.go @@ -6,6 +6,7 @@ import ( "time" "github.com/Fantom-foundation/go-lachesis/src/hash" + "github.com/Fantom-foundation/go-lachesis/src/logger" ) // peers manages node peer list. @@ -128,7 +129,7 @@ func (n *Node) ConnectOK(p *Peer) { // ConnectFail counts unsuccessful connections to peer. func (n *Node) ConnectFail(p *Peer, err error) { - n.log.Warn(err) + logger.Log.Warn(err) n.peers.Lock() defer n.peers.Unlock() diff --git a/src/posnode/service.go b/src/posnode/service.go index e1a386feb..1a1a65d84 100644 --- a/src/posnode/service.go +++ b/src/posnode/service.go @@ -10,6 +10,7 @@ import ( "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" "github.com/Fantom-foundation/go-lachesis/src/network" "github.com/Fantom-foundation/go-lachesis/src/posnode/api" ) @@ -30,9 +31,9 @@ func (n *Node) StartService() { } bind := n.NetAddrOf(n.host) - n.server, _ = api.StartService(bind, n.key, n, n.log.Infof, n.service.listen) + n.server, _ = api.StartService(bind, n.key, n, logger.Log.Infof, n.service.listen) - n.log.Info("service started") + logger.Log.Info("service started") } // StopService stops node service. @@ -43,7 +44,7 @@ func (n *Node) StopService() { n.server.GracefulStop() n.server = nil - n.log.Info("service stopped") + logger.Log.Info("service stopped") } /* diff --git a/src/posnode/store.go b/src/posnode/store.go index 89b08aff1..4f2a1c478 100644 --- a/src/posnode/store.go +++ b/src/posnode/store.go @@ -7,6 +7,7 @@ import ( "github.com/Fantom-foundation/go-lachesis/src/inter" "github.com/Fantom-foundation/go-lachesis/src/inter/wire" "github.com/Fantom-foundation/go-lachesis/src/kvdb" + "github.com/Fantom-foundation/go-lachesis/src/logger" "github.com/Fantom-foundation/go-lachesis/src/posnode/api" ) @@ -79,7 +80,7 @@ func (s *Store) SetEventHash(creator hash.Peer, index uint64, hash hash.Event) { key := append(creator.Bytes(), intToBytes(index)...) if err := s.hashes.Put(key, hash.Bytes()); err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } } @@ -89,7 +90,7 @@ func (s *Store) GetEventHash(creator hash.Peer, index uint64) *hash.Event { buf, err := s.hashes.Get(key) if err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } if buf == nil { return nil @@ -148,16 +149,16 @@ func (s *Store) BootstrapPeers(peers ...*Peer) { var pbf proto.Buffer w := peer.ToWire() if err := pbf.Marshal(w); err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } if err := batch.Put(peer.ID.Bytes(), pbf.Bytes()); err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } ids = append(ids, peer.ID) } if err := batch.Write(); err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } s.SetTopPeers(ids) @@ -180,7 +181,7 @@ func (s *Store) GetTopPeers() []hash.Peer { // SetPeerHeight stores last event index of peer. func (s *Store) SetPeerHeight(id hash.Peer, height uint64) { if err := s.peerHeights.Put(id.Bytes(), intToBytes(height)); err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } } @@ -188,7 +189,7 @@ func (s *Store) SetPeerHeight(id hash.Peer, height uint64) { func (s *Store) GetPeerHeight(id hash.Peer) uint64 { buf, err := s.peerHeights.Get(id.Bytes()) if err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } if buf == nil { return 0 @@ -205,18 +206,18 @@ func (s *Store) set(table kvdb.Database, key []byte, val proto.Message) { var pbf proto.Buffer if err := pbf.Marshal(val); err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } if err := table.Put(key, pbf.Bytes()); err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } } func (s *Store) get(table kvdb.Database, key []byte, to proto.Message) proto.Message { buf, err := table.Get(key) if err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } if buf == nil { return nil @@ -224,7 +225,7 @@ func (s *Store) get(table kvdb.Database, key []byte, to proto.Message) proto.Mes err = proto.Unmarshal(buf, to) if err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } return to } @@ -232,7 +233,7 @@ func (s *Store) get(table kvdb.Database, key []byte, to proto.Message) proto.Mes func (s *Store) has(table kvdb.Database, key []byte) bool { res, err := table.Has(key) if err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } return res } diff --git a/src/posposet/common_test.go b/src/posposet/common_test.go index 0704332fc..e5f751898 100644 --- a/src/posposet/common_test.go +++ b/src/posposet/common_test.go @@ -20,7 +20,7 @@ func FakePoset(nodes []hash.Peer) (*Poset, *Store, *EventStore) { input := NewEventStore(nil, false) - poset := New(store, input, "debug") + poset := New(store, input) poset.Bootstrap() return poset, store, input diff --git a/src/posposet/creators.go b/src/posposet/creators.go index 19f17929b..1bc3baf75 100644 --- a/src/posposet/creators.go +++ b/src/posposet/creators.go @@ -2,6 +2,7 @@ package posposet import ( "github.com/Fantom-foundation/go-lachesis/src/hash" + "github.com/Fantom-foundation/go-lachesis/src/logger" ) /* @@ -23,7 +24,7 @@ func newParentsValidator(e *Event) *parentsValidator { func (v *parentsValidator) IsParentUnique(node hash.Peer) bool { if _, ok := v.nodes[node]; ok { - log.Warnf("Event %s has double refer to node %s, so rejected", + logger.Log.Warnf("Event %s has double refer to node %s, so rejected", v.event.Hash().String(), node.String()) return false @@ -35,7 +36,7 @@ func (v *parentsValidator) IsParentUnique(node hash.Peer) bool { func (v *parentsValidator) HasSelfParent() bool { if _, ok := v.nodes[v.event.Creator]; !ok { - log.Warnf("Event %s has no refer to self-node %s, so rejected", + logger.Log.Warnf("Event %s has no refer to self-node %s, so rejected", v.event.Hash().String(), v.event.Creator.String()) return false diff --git a/src/posposet/flag_table.go b/src/posposet/flag_table.go index c4bbe4257..ec1359bb9 100644 --- a/src/posposet/flag_table.go +++ b/src/posposet/flag_table.go @@ -4,6 +4,7 @@ import ( "strings" "github.com/Fantom-foundation/go-lachesis/src/hash" + "github.com/Fantom-foundation/go-lachesis/src/logger" "github.com/Fantom-foundation/go-lachesis/src/posposet/wire" ) @@ -159,7 +160,7 @@ func WireToEventsByPeer(arr []*wire.EventDescr) EventsByPeer { res[creator] = hash.Events{} } if !res[creator].Add(h) { - log.Fatal("double value is detected") + logger.Log.Fatal("double value is detected") } } diff --git a/src/posposet/frame.go b/src/posposet/frame.go index 99811279f..7d24bb20b 100644 --- a/src/posposet/frame.go +++ b/src/posposet/frame.go @@ -3,6 +3,7 @@ package posposet import ( "github.com/Fantom-foundation/go-lachesis/src/hash" "github.com/Fantom-foundation/go-lachesis/src/inter" + "github.com/Fantom-foundation/go-lachesis/src/logger" "github.com/Fantom-foundation/go-lachesis/src/posposet/wire" ) @@ -101,7 +102,7 @@ func (p *Poset) setFrameSaving(f *Frame) { if f.Index > p.state.LastFinishedFrameN { p.store.SetFrame(f) } else { - log.Fatalf("frame %d is finished and should not be changed", f.Index) + logger.Log.Fatalf("frame %d is finished and should not be changed", f.Index) } } } diff --git a/src/posposet/frames.go b/src/posposet/frames.go index 8c3064786..d35c16ad4 100644 --- a/src/posposet/frames.go +++ b/src/posposet/frames.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/Fantom-foundation/go-lachesis/src/hash" + "github.com/Fantom-foundation/go-lachesis/src/logger" ) // eventsByFrame maps frame num --> roots. @@ -66,7 +67,7 @@ func (p *Poset) FrameOfEvent(event hash.Event) (frame *Frame, isRoot bool) { // frame finds or creates frame. func (p *Poset) frame(n uint64, orCreate bool) *Frame { if n < p.state.LastFinishedFrameN && orCreate { - log.Fatalf("too old frame %d is requested", n) + logger.Log.Fatalf("too old frame %d is requested", n) } // return ephemeral if n == 0 { diff --git a/src/posposet/input.go b/src/posposet/input.go index 941f2193d..f618e39f6 100644 --- a/src/posposet/input.go +++ b/src/posposet/input.go @@ -3,6 +3,7 @@ package posposet import ( "github.com/Fantom-foundation/go-lachesis/src/hash" "github.com/Fantom-foundation/go-lachesis/src/inter" + "github.com/Fantom-foundation/go-lachesis/src/logger" ) type EventSource interface { @@ -23,7 +24,7 @@ func (p *Poset) HasEvent(h hash.Event) bool { func (p *Poset) GetEvent(h hash.Event) *Event { e := p.input.GetEvent(h) if e == nil { - log.Fatal("got unsaved event") + logger.Log.Fatal("got unsaved event") } return &Event{ Event: *e, diff --git a/src/posposet/log.go b/src/posposet/log.go deleted file mode 100644 index 085a1041c..000000000 --- a/src/posposet/log.go +++ /dev/null @@ -1,22 +0,0 @@ -package posposet - -import ( - "github.com/sirupsen/logrus" -) - -var ( - log *logrus.Logger -) - -func init() { - log = logrus.StandardLogger() - log.SetLevel(logrus.DebugLevel) -} - -// SetLogger sets logger for whole package. -func SetLogger(custom *logrus.Logger) { - if custom == nil { - panic("Nil-logger set") - } - log = custom -} diff --git a/src/posposet/poset.go b/src/posposet/poset.go index dbd443658..0d6e412ce 100644 --- a/src/posposet/poset.go +++ b/src/posposet/poset.go @@ -4,11 +4,9 @@ import ( "sort" "sync" - "github.com/sirupsen/logrus" - "github.com/Fantom-foundation/go-lachesis/src/hash" "github.com/Fantom-foundation/go-lachesis/src/inter" - "github.com/Fantom-foundation/go-lachesis/src/utils" + "github.com/Fantom-foundation/go-lachesis/src/logger" ) // Poset processes events to get consensus. @@ -29,7 +27,7 @@ type Poset struct { // New creates Poset instance. // It does not start any process. -func New(store *Store, input EventSource, logLevel string) *Poset { +func New(store *Store, input EventSource) *Poset { const buffSize = 10 p := &Poset{ @@ -41,10 +39,6 @@ func New(store *Store, input EventSource, logLevel string) *Poset { incompleteEvents: make(map[hash.Event]*Event), } - log = logrus.StandardLogger() - log.SetLevel(utils.GetLogLevel(logLevel)) - SetLogger(log) - return p } @@ -92,7 +86,7 @@ func (p *Poset) PushEvent(e hash.Event) { // onNewEvent runs consensus calc from new event. It is not safe for concurrent use. func (p *Poset) onNewEvent(e *Event) { if p.store.GetEventFrame(e.Hash()) != nil { - log.WithField("event", e).Warnf("Event had received already") + logger.Log.WithField("event", e).Warnf("Event had received already") return } @@ -195,7 +189,7 @@ func (p *Poset) consensus(e *Event) { applyRewards(state, ordered) balances, err := state.Commit(true) if err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } if applyAt.SetBalances(balances) { p.reconsensusFromFrame(applyAt.Index) @@ -226,7 +220,7 @@ func (p *Poset) checkIfRoot(e *Event) *Frame { if !parent.IsZero() { frame, isRoot := p.FrameOfEvent(parent) if frame == nil || frame.Index <= p.state.LastFinishedFrameN { - log.Warnf("Parent %s of %s is too old. Skipped", parent.String(), e.String()) + logger.Log.Warnf("Parent %s of %s is too old. Skipped", parent.String(), e.String()) // NOTE: is it possible some participants got this event before parent outdated? continue } diff --git a/src/posposet/poset_atropos_test.go b/src/posposet/poset_atropos_test.go index 87c991cad..73a7a2afb 100644 --- a/src/posposet/poset_atropos_test.go +++ b/src/posposet/poset_atropos_test.go @@ -104,7 +104,7 @@ func testSpecialNamedAtropos(t *testing.T, tryRestoring bool, asciiScheme string n++ if tryRestoring && n == len(names)*2/3 { ee := p.incompleteEvents - p = New(store, input, "debug") + p = New(store, input) p.Bootstrap() for _, e := range ee { input.SetEvent(&e.Event) diff --git a/src/posposet/state.go b/src/posposet/state.go index f2a7498e2..b58ec8c17 100644 --- a/src/posposet/state.go +++ b/src/posposet/state.go @@ -2,6 +2,7 @@ package posposet import ( "github.com/Fantom-foundation/go-lachesis/src/hash" + "github.com/Fantom-foundation/go-lachesis/src/logger" "github.com/Fantom-foundation/go-lachesis/src/posposet/wire" ) @@ -55,7 +56,7 @@ func (p *Poset) Bootstrap() { // restore state p.state = p.store.GetState() if p.state == nil { - log.Fatal("Apply genesis for store first") + logger.Log.Fatal("Apply genesis for store first") } // restore frames for n := p.state.LastFinishedFrameN; true; n++ { @@ -75,7 +76,7 @@ func GenesisHash(balances map[hash.Peer]uint64) hash.Hash { defer s.Close() if err := s.ApplyGenesis(balances); err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } return s.GetState().Genesis diff --git a/src/posposet/store.go b/src/posposet/store.go index 8c3a04968..29a7a906c 100644 --- a/src/posposet/store.go +++ b/src/posposet/store.go @@ -8,6 +8,7 @@ import ( "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/posposet/wire" "github.com/Fantom-foundation/go-lachesis/src/state" ) @@ -64,7 +65,7 @@ func (s *Store) initCache() { cache := func() *lru.Cache { c, err := lru.New(cacheSize) if err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } return c } @@ -129,7 +130,7 @@ func (s *Store) SetEventFrame(e hash.Event, frame uint64) { key := e.Bytes() val := intToBytes(frame) if err := s.event2frame.Put(key, val); err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } if s.event2frameCache != nil { @@ -149,7 +150,7 @@ func (s *Store) GetEventFrame(e hash.Event) *uint64 { key := e.Bytes() buf, err := s.event2frame.Get(key) if err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } if buf == nil { return nil @@ -215,7 +216,7 @@ func (s *Store) GetBlock(n uint64) *Block { func (s *Store) StateDB(from hash.Hash) *state.DB { db, err := state.New(from, s.balances) if err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } return db } @@ -228,18 +229,18 @@ func (s *Store) set(table kvdb.Database, key []byte, val proto.Message) { var pbf proto.Buffer if err := pbf.Marshal(val); err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } if err := table.Put(key, pbf.Bytes()); err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } } func (s *Store) get(table kvdb.Database, key []byte, to proto.Message) proto.Message { buf, err := table.Get(key) if err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } if buf == nil { return nil @@ -247,7 +248,7 @@ func (s *Store) get(table kvdb.Database, key []byte, to proto.Message) proto.Mes err = proto.Unmarshal(buf, to) if err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } return to } @@ -255,7 +256,7 @@ func (s *Store) get(table kvdb.Database, key []byte, to proto.Message) proto.Mes func (s *Store) has(table kvdb.Database, key []byte) bool { res, err := table.Has(key) if err != nil { - log.Fatal(err) + logger.Log.Fatal(err) } return res } diff --git a/src/posposet/store_test.go b/src/posposet/store_test.go index b45eb3eb5..514e22ce6 100644 --- a/src/posposet/store_test.go +++ b/src/posposet/store_test.go @@ -84,7 +84,7 @@ func benchPoset(nodes []hash.Peer, input EventSource, store *Store, cached bool) panic(err) } - poset := New(store, input, "debug") + poset := New(store, input) poset.Bootstrap() return poset diff --git a/src/posposet/time.go b/src/posposet/time.go index 6e82d93cc..05d8fdef0 100644 --- a/src/posposet/time.go +++ b/src/posposet/time.go @@ -5,6 +5,7 @@ import ( "github.com/Fantom-foundation/go-lachesis/src/hash" "github.com/Fantom-foundation/go-lachesis/src/inter" + "github.com/Fantom-foundation/go-lachesis/src/logger" ) type ( @@ -85,7 +86,7 @@ func newLamportTimeValidator(e *Event) *lamportTimeValidator { func (v *lamportTimeValidator) IsGreaterThan(time inter.Timestamp) bool { if v.event.LamportTime <= time { - log.Warnf("Event %s has lamport time %d. It isn't next of parents, so rejected", + logger.Log.Warnf("Event %s has lamport time %d. It isn't next of parents, so rejected", v.event.Hash().String(), v.event.LamportTime) return false @@ -98,7 +99,7 @@ func (v *lamportTimeValidator) IsGreaterThan(time inter.Timestamp) bool { func (v *lamportTimeValidator) IsSequential() bool { if v.event.LamportTime != v.maxTime+1 { - log.Warnf("Event %s has lamport time %d. It is too far from parents, so rejected", + logger.Log.Warnf("Event %s has lamport time %d. It is too far from parents, so rejected", v.event.Hash().String(), v.event.LamportTime) return false diff --git a/src/posposet/transaction.go b/src/posposet/transaction.go index bf7382a2b..3b87aa82f 100644 --- a/src/posposet/transaction.go +++ b/src/posposet/transaction.go @@ -9,6 +9,7 @@ and the payment of all rewards. */ import ( + "github.com/Fantom-foundation/go-lachesis/src/logger" "github.com/Fantom-foundation/go-lachesis/src/state" ) @@ -32,7 +33,7 @@ func applyTransactions(db *state.DB, ordered Events) { receiver := tx.Receiver if db.FreeBalance(sender) < tx.Amount { - log.Warnf("Cannot send %d from %s to %s: balance is insufficient, skipped", tx.Amount, sender.String(), receiver.String()) + logger.Log.Warnf("Cannot send %d from %s to %s: balance is insufficient, skipped", tx.Amount, sender.String(), receiver.String()) continue } diff --git a/src/utils/util.go b/src/utils/util.go index 7ad216fcb..c318fbbbe 100644 --- a/src/utils/util.go +++ b/src/utils/util.go @@ -6,8 +6,6 @@ import ( "strconv" "sync/atomic" "testing" - - "github.com/sirupsen/logrus" ) var startBase uint32 = 12000 @@ -77,24 +75,3 @@ func FreePort(network string) (port uint16) { }() return uint16(l.Addr().(*net.TCPAddr).Port) } - -// GetLogLevel return logrus.Level by string. -// Example: "debug" -> logrus.DebugLevel and etc. -func GetLogLevel(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 - } -} From e00476da9168aee2129fd7a068b0c770b155b43f Mon Sep 17 00:00:00 2001 From: devintegral2 Date: Tue, 21 May 2019 17:08:35 +0700 Subject: [PATCH 4/5] Fix fmt. --- cmd/dummy/commands/root.go | 2 +- cmd/dummy_client/main.go | 2 +- src/lachesis/lachesis_config.go | 2 +- src/logger/logger.go | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/dummy/commands/root.go b/cmd/dummy/commands/root.go index 3db62160f..911400155 100644 --- a/cmd/dummy/commands/root.go +++ b/cmd/dummy/commands/root.go @@ -134,4 +134,4 @@ func logLevel(l string) logrus.Level { default: return logrus.DebugLevel } -} \ No newline at end of file +} diff --git a/cmd/dummy_client/main.go b/cmd/dummy_client/main.go index 618dce3a2..32d86acba 100644 --- a/cmd/dummy_client/main.go +++ b/cmd/dummy_client/main.go @@ -129,4 +129,4 @@ func logLevel(l string) logrus.Level { default: return logrus.DebugLevel } -} \ No newline at end of file +} diff --git a/src/lachesis/lachesis_config.go b/src/lachesis/lachesis_config.go index 5d3aa987f..8533b2101 100644 --- a/src/lachesis/lachesis_config.go +++ b/src/lachesis/lachesis_config.go @@ -127,4 +127,4 @@ func LogLevel(l string) logrus.Level { default: return logrus.DebugLevel } -} \ No newline at end of file +} diff --git a/src/logger/logger.go b/src/logger/logger.go index 72e8af502..a680bbb71 100644 --- a/src/logger/logger.go +++ b/src/logger/logger.go @@ -45,4 +45,4 @@ func GetLevel(l string) logrus.Level { default: return logrus.DebugLevel } -} \ No newline at end of file +} From 50af942f198189cb789fb1d12061e17036a89856 Mon Sep 17 00:00:00 2001 From: devintegral2 Date: Thu, 23 May 2019 09:55:28 +0700 Subject: [PATCH 5/5] Add desc. --- src/logger/logger.go | 1 + 1 file changed, 1 insertion(+) diff --git a/src/logger/logger.go b/src/logger/logger.go index a680bbb71..200018282 100644 --- a/src/logger/logger.go +++ b/src/logger/logger.go @@ -9,6 +9,7 @@ import ( */ var ( + // Log - global logger Log *logrus.Logger )