aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJimmy Hu <jimmy.hu@dexon.org>2018-11-29 15:57:23 +0800
committerGitHub <noreply@github.com>2018-11-29 15:57:23 +0800
commit9fbc3a068b228963e6f077e486dc962bf77714c1 (patch)
treef50a7f60c8cbe1a2c9101017d57724eb3bf686cf
parentdaf3bab93c323b173345811adc9a334dad4a7094 (diff)
downloaddexon-consensus-9fbc3a068b228963e6f077e486dc962bf77714c1.tar
dexon-consensus-9fbc3a068b228963e6f077e486dc962bf77714c1.tar.gz
dexon-consensus-9fbc3a068b228963e6f077e486dc962bf77714c1.tar.bz2
dexon-consensus-9fbc3a068b228963e6f077e486dc962bf77714c1.tar.lz
dexon-consensus-9fbc3a068b228963e6f077e486dc962bf77714c1.tar.xz
dexon-consensus-9fbc3a068b228963e6f077e486dc962bf77714c1.tar.zst
dexon-consensus-9fbc3a068b228963e6f077e486dc962bf77714c1.zip
simulation: -log to dump log to file as well (#348)
-rw-r--r--cmd/dexcon-simulation/main.go14
-rw-r--r--simulation/node.go7
-rw-r--r--simulation/simulation.go33
3 files changed, 32 insertions, 22 deletions
diff --git a/cmd/dexcon-simulation/main.go b/cmd/dexcon-simulation/main.go
index e34d6c1..e45dbce 100644
--- a/cmd/dexcon-simulation/main.go
+++ b/cmd/dexcon-simulation/main.go
@@ -20,7 +20,6 @@ package main
import (
"flag"
"fmt"
- "io"
"log"
"math/rand"
"net/http"
@@ -38,7 +37,7 @@ var initialize = flag.Bool("init", false, "initialize config file")
var configFile = flag.String("config", "", "path to simulation config file")
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
-var logfile = flag.String("log", "", "write log to `file`")
+var logfile = flag.String("log", "", "write log to `file`.log")
func main() {
flag.Parse()
@@ -70,20 +69,11 @@ func main() {
defer pprof.StopCPUProfile()
}
- if *logfile != "" {
- f, err := os.Create(*logfile)
- if err != nil {
- log.Fatal("could not create log file: ", err)
- }
- mw := io.MultiWriter(os.Stdout, f)
- log.SetOutput(mw)
- }
-
cfg, err := config.Read(*configFile)
if err != nil {
panic(err)
}
- simulation.Run(cfg)
+ simulation.Run(cfg, *logfile)
if *memprofile != "" {
f, err := os.Create(*memprofile)
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()