diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2018-11-29 15:57:23 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-29 15:57:23 +0800 |
commit | 9fbc3a068b228963e6f077e486dc962bf77714c1 (patch) | |
tree | f50a7f60c8cbe1a2c9101017d57724eb3bf686cf /simulation | |
parent | daf3bab93c323b173345811adc9a334dad4a7094 (diff) | |
download | tangerine-consensus-9fbc3a068b228963e6f077e486dc962bf77714c1.tar tangerine-consensus-9fbc3a068b228963e6f077e486dc962bf77714c1.tar.gz tangerine-consensus-9fbc3a068b228963e6f077e486dc962bf77714c1.tar.bz2 tangerine-consensus-9fbc3a068b228963e6f077e486dc962bf77714c1.tar.lz tangerine-consensus-9fbc3a068b228963e6f077e486dc962bf77714c1.tar.xz tangerine-consensus-9fbc3a068b228963e6f077e486dc962bf77714c1.tar.zst tangerine-consensus-9fbc3a068b228963e6f077e486dc962bf77714c1.zip |
simulation: -log to dump log to file as well (#348)
Diffstat (limited to 'simulation')
-rw-r--r-- | simulation/node.go | 7 | ||||
-rw-r--r-- | simulation/simulation.go | 33 |
2 files changed, 30 insertions, 10 deletions
diff --git a/simulation/node.go b/simulation/node.go index 8c37f65..cea0035 100644 --- a/simulation/node.go +++ b/simulation/node.go @@ -20,7 +20,6 @@ package simulation import ( "encoding/json" "fmt" - "os" "time" "github.com/dexon-foundation/dexon-consensus/common" @@ -30,7 +29,6 @@ import ( "github.com/dexon-foundation/dexon-consensus/core/test" "github.com/dexon-foundation/dexon-consensus/core/types" "github.com/dexon-foundation/dexon-consensus/simulation/config" - "github.com/dexon-foundation/dexon/log" ) type infoStatus string @@ -126,7 +124,8 @@ func (n *node) GetID() types.NodeID { } // run starts the node. -func (n *node) run(serverEndpoint interface{}, dMoment time.Time) { +func (n *node) run( + serverEndpoint interface{}, dMoment time.Time, logger common.Logger) { // Run network. if err := n.netModule.Setup(serverEndpoint); err != nil { panic(err) @@ -146,8 +145,6 @@ func (n *node) run(serverEndpoint interface{}, dMoment time.Time) { // Setup of governance is ready, can be switched to remote mode. n.gov.SwitchToRemoteMode(n.netModule) // Setup Consensus. - logger := log.New() - logger.SetHandler(log.StreamHandler(os.Stderr, log.LogfmtFormat())) n.consensus = core.NewConsensus( dMoment, n.app, diff --git a/simulation/simulation.go b/simulation/simulation.go index dcb7225..3a0e9bb 100644 --- a/simulation/simulation.go +++ b/simulation/simulation.go @@ -19,16 +19,21 @@ package simulation import ( "fmt" + "io" + "os" "sync" "time" + "github.com/dexon-foundation/dexon/log" + + "github.com/dexon-foundation/dexon-consensus/common" "github.com/dexon-foundation/dexon-consensus/core/crypto/ecdsa" "github.com/dexon-foundation/dexon-consensus/core/test" "github.com/dexon-foundation/dexon-consensus/simulation/config" ) // Run starts the simulation. -func Run(cfg *config.Config) { +func Run(cfg *config.Config, logPrefix string) { var ( networkType = cfg.Networking.Type server *PeerServer @@ -46,8 +51,22 @@ func Run(cfg *config.Config) { dMoment := time.Now().UTC() + newLogger := func(logPrefix string) common.Logger { + mw := io.Writer(os.Stderr) + if logPrefix != "" { + f, err := os.Create(logPrefix + ".log") + if err != nil { + panic(err) + } + mw = io.MultiWriter(os.Stderr, f) + } + logger := log.New() + logger.SetHandler(log.StreamHandler(mw, log.LogfmtFormat())) + return logger + } + // init is a function to init a node. - init := func(serverEndpoint interface{}) { + init := func(serverEndpoint interface{}, logger common.Logger) { prv, err := ecdsa.NewPrivateKey() if err != nil { panic(err) @@ -56,7 +75,7 @@ func Run(cfg *config.Config) { wg.Add(1) go func() { defer wg.Done() - v.run(serverEndpoint, dMoment) + v.run(serverEndpoint, dMoment, logger) }() } @@ -64,7 +83,7 @@ func Run(cfg *config.Config) { case test.NetworkTypeTCP: // Intialized a simulation on multiple remotely peers. // The peer-server would be initialized with another command. - init(nil) + init(nil, newLogger(logPrefix)) case test.NetworkTypeTCPLocal, test.NetworkTypeFake: // Initialize a local simulation with a peer server. var serverEndpoint interface{} @@ -79,7 +98,11 @@ func Run(cfg *config.Config) { }() // Initialize all nodes. for i := uint32(0); i < cfg.Node.Num; i++ { - init(serverEndpoint) + prefix := fmt.Sprintf("%s.%d", logPrefix, i) + if logPrefix == "" { + prefix = "" + } + init(serverEndpoint, newLogger(prefix)) } } wg.Wait() |