Skip to content
This repository has been archived by the owner on Oct 4, 2019. It is now read-only.

Commit

Permalink
problem: update logging to better manage severity, verbosity, and use…
Browse files Browse the repository at this point in the history
… display

solution: makes quite a few log lines better aware of verbosity and severity, so
errors use .Error, more important logs have verbosities Warn, etc.
add glog.D log lines for configuration notices, cmd progress reports, and other important HUD events
  • Loading branch information
whilei committed Dec 6, 2017
1 parent 38f69bd commit 425f94d
Show file tree
Hide file tree
Showing 21 changed files with 262 additions and 185 deletions.
2 changes: 1 addition & 1 deletion accounts/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (w *watcher) loop() {

err := notify.Watch(w.ac.getKeydir(), w.ev, notify.All)
if err != nil {
glog.V(logger.Detail).Infof("can't watch %s: %v", w.ac.getKeydir(), err)
glog.V(logger.Warn).Errorf("can't watch %s: %v", w.ac.getKeydir(), err)
return
}
defer notify.Stop(w.ev)
Expand Down
4 changes: 3 additions & 1 deletion cmd/geth/accountcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func accountIndex(ctx *cli.Context) error {
if len(errs) > 0 {
for _, e := range errs {
if e != nil {
glog.V(logger.Error).Infof("init cache db err: %v", e)
glog.V(logger.Error).Errorf("init cache db err: %v", e)
}
}
}
Expand Down Expand Up @@ -236,10 +236,12 @@ func unlockAccount(ctx *cli.Context, accman *accounts.Manager, address string, i
err = accman.Unlock(account, password)
if err == nil {
glog.V(logger.Info).Infof("Unlocked account %x", account.Address)
glog.D(logger.Error).Infof("Unlocked account %x", account.Address)
return account, password
}
if err, ok := err.(*accounts.AmbiguousAddrError); ok {
glog.V(logger.Info).Infof("Unlocked account %x", account.Address)
glog.D(logger.Error).Infof("Unlocked account %x", account.Address)
return ambiguousAddrRecovery(accman, err, password), password
}
if err != accounts.ErrDecrypt {
Expand Down
99 changes: 46 additions & 53 deletions cmd/geth/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ func StartNode(stack *node.Node) {
signal.Notify(sigc, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(sigc)
sig := <-sigc
glog.Infof("Got %v, shutting down...", sig)
glog.V(logger.Warn).Warnf("Got %v, shutting down...", sig)

fails := make(chan error, 1)
go func(fs chan error) {
for {
select {
case e := <-fs:
if e != nil {
glog.V(logger.Error).Infof("node stop failure: %v", e)
glog.V(logger.Error).Errorf("node stop failure: %v", e)
}
}
}
Expand Down Expand Up @@ -127,7 +127,7 @@ func ImportChain(chain *core.BlockChain, fn string) error {
defer close(interrupt)
go func() {
if _, ok := <-interrupt; ok {
glog.Info("caught interrupt during import, will stop at next batch")
glog.D(logger.Warn).Warnln("caught interrupt during import, will stop at next batch")
}
close(stop)
}()
Expand All @@ -140,7 +140,7 @@ func ImportChain(chain *core.BlockChain, fn string) error {
}
}

glog.Infoln("Importing blockchain ", fn)
glog.D(logger.Error).Infoln("Importing blockchain ", fn)
fh, err := os.Open(fn)
if err != nil {
return err
Expand Down Expand Up @@ -180,7 +180,7 @@ func ImportChain(chain *core.BlockChain, fn string) error {
return fmt.Errorf("interrupted")
}
if hasAllBlocks(chain, blocks[:i]) {
glog.Infof("skipping batch %d, all blocks present [%x / %x]",
glog.D(logger.Warn).Warnf("skipping batch %d, all blocks present [%x / %x]",
batch, blocks[0].Hash().Bytes()[:4], blocks[i-1].Hash().Bytes()[:4])
continue
}
Expand All @@ -202,7 +202,7 @@ func hasAllBlocks(chain *core.BlockChain, bs []*types.Block) bool {
}

func ExportChain(blockchain *core.BlockChain, fn string) error {
glog.Infoln("Exporting blockchain to ", fn)
glog.D(logger.Warn).Infoln("Exporting blockchain to ", fn)
fh, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
if err != nil {
return err
Expand All @@ -211,12 +211,12 @@ func ExportChain(blockchain *core.BlockChain, fn string) error {
if err := blockchain.Export(fh); err != nil {
return err
}
glog.Infoln("Exported blockchain to ", fn)
glog.D(logger.Error).Infoln("Exported blockchain to ", fn)
return nil
}

func ExportAppendChain(blockchain *core.BlockChain, fn string, first uint64, last uint64) error {
glog.Infoln("Exporting blockchain to ", fn)
glog.D(logger.Warn).Infoln("Exporting blockchain to ", fn)
// TODO verify mode perms
fh, err := os.OpenFile(fn, os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.ModePerm)
if err != nil {
Expand All @@ -226,40 +226,32 @@ func ExportAppendChain(blockchain *core.BlockChain, fn string, first uint64, las
if err := blockchain.ExportN(fh, first, last); err != nil {
return err
}
glog.Infoln("Exported blockchain to ", fn)
glog.D(logger.Error).Infoln("Exported blockchain to ", fn)
return nil
}

func withLineBreak(s string) string {
return s + "\n"
}

func colorGreen(s interface{}) string {
return fmt.Sprintf("\x1b[32m%v\x1b[39m", s)
}

func colorBlue(s interface{}) string {
return fmt.Sprintf("\x1b[36m%v\x1b[39m", s)
}

func formatStatusKeyValue(prefix string, ss ...interface{}) (s string) {

s = ""
// Single arg; category? ie Forks?
if len(ss) == 1 {
s += colorBlue(ss[0])
s += logger.ColorBlue(fmt.Sprintf("%v", ss[0]))
}
if len(ss) == 2 {
if ss[0] == "" {
s += fmt.Sprintf("%v", colorGreen(ss[1]))
s += fmt.Sprintf("%v", logger.ColorGreen(fmt.Sprintf("%v", ss[1])))
} else {
s += fmt.Sprintf("%v: %v", ss[0], colorGreen(ss[1]))
s += fmt.Sprintf("%v: %v", ss[0], logger.ColorGreen(fmt.Sprintf("%v", ss[1])))
}
}
if len(ss) > 2 {
s += fmt.Sprintf("%v:", ss[0])
for i := 2; i < len(ss); i++ {
s += withLineBreak(fmt.Sprintf(" %v", colorGreen(ss[i])))
s += withLineBreak(fmt.Sprintf(" %v", logger.ColorGreen(fmt.Sprintf("%v", ss[i]))))
}
}

Expand Down Expand Up @@ -529,16 +521,16 @@ func status(ctx *cli.Context) error {
for _, p := range printme {
s += withLineBreak(sep)
// right align category title
s += withLineBreak(strings.Repeat(" ", len(sep)-len(p.title)) + colorBlue(p.title))
s += withLineBreak(strings.Repeat(" ", len(sep)-len(p.title)) + logger.ColorBlue(p.title))
for _, v := range p.keyVals {
s += v
}
}
glog.V(logger.Info).Info(s)
glog.D(logger.Info).Infoln(s)

// Return here if database has not been initialized.
if !shouldUseExisting {
glog.V(logger.Info).Info("Geth has not been initialized; no database information available yet.")
glog.D(logger.Info).Infoln("Geth has not been initialized; no database information available yet.")
return nil
}

Expand All @@ -547,11 +539,11 @@ func status(ctx *cli.Context) error {
s = "\n"
s += withLineBreak(sep)
title := "Chain database status"
s += withLineBreak(strings.Repeat(" ", len(sep)-len(title)) + colorBlue(title))
s += withLineBreak(strings.Repeat(" ", len(sep)-len(title)) + logger.ColorBlue(title))
for _, v := range formatChainDataPretty(datadir, chaindata) {
s += v
}
glog.V(logger.Info).Info(s)
glog.D(logger.Info).Infoln(s)

return nil
}
Expand All @@ -572,10 +564,10 @@ func rollback(ctx *cli.Context) error {
bc, chainDB := MakeChain(ctx)
defer chainDB.Close()

glog.Warning("Rolling back blockchain...")
glog.D(logger.Warn).Infoln("Rolling back blockchain...")

if err := bc.SetHead(blockIndex); err != nil {
glog.V(logger.Warn).Infof("error setting head: %v", err)
glog.D(logger.Error).Errorf("error setting head: %v", err)
}

// Check if *neither* block nor fastblock numbers match desired head number
Expand All @@ -584,7 +576,7 @@ func rollback(ctx *cli.Context) error {
if nowCurrentHead != blockIndex && nowCurrentFastHead != blockIndex {
glog.Fatalf("ERROR: Wanted rollback to set head to: %v, instead current head is: %v", blockIndex, nowCurrentHead)
}
glog.Infof("SUCCESS: Head block set to: %v", nowCurrentHead)
glog.D(logger.Error).Infof("Success. Head block set to: %v", nowCurrentHead)
return nil
}

Expand All @@ -598,7 +590,7 @@ func dumpChainConfig(ctx *cli.Context) error {
glog.Fatal("Dump config should only be used with default chain configurations (mainnet or morden).")
}

glog.V(logger.Info).Infof("Dumping configuration for: %v", chainIdentity)
glog.D(logger.Warn).Infof("Dumping configuration for: %v", chainIdentity)

chainConfigFilePath := ctx.Args().First()
chainConfigFilePath = filepath.Clean(chainConfigFilePath)
Expand All @@ -618,7 +610,7 @@ func dumpChainConfig(ctx *cli.Context) error {
}
di, _ = os.Stat(fb) // update var with new dir info
} else {
glog.V(logger.Error).Infof("err: %v (at '%v')", de, fb)
glog.V(logger.Error).Errorf("err: %v (at '%v')", de, fb)
}
}
if !di.IsDir() {
Expand Down Expand Up @@ -658,7 +650,7 @@ func dumpChainConfig(ctx *cli.Context) error {
return writeError
}

glog.V(logger.Info).Info(fmt.Sprintf("Wrote chain config file to \x1b[32m%s\x1b[39m.", chainConfigFilePath))
glog.D(logger.Error).Infoln(fmt.Sprintf("Wrote chain config file to \x1b[32m%s\x1b[39m.", chainConfigFilePath))
return nil
}

Expand Down Expand Up @@ -706,6 +698,7 @@ func makedag(ctx *cli.Context) error {
glog.Fatal("Can't find dir")
}
glog.V(logger.Info).Infoln("making DAG, this could take awhile...")
glog.D(logger.Warn).Infoln("making DAG, this could take awhile...")
ethash.MakeDAG(blockNum, dir)
}
default:
Expand Down Expand Up @@ -1207,7 +1200,7 @@ func recoverChaindata(ctx *cli.Context) error {
if !ctx.GlobalBool(aliasableName(FakePoWFlag.Name, ctx)) {
pow = ethash.New()
} else {
glog.V(logger.Info).Info("Consensus: fake")
glog.V(logger.Warn).Info("Consensus: fake")
}

bc, err := core.NewBlockChainDryrun(bcdb, sconf.ChainConfig, pow, new(event.TypeMux))
Expand All @@ -1216,42 +1209,42 @@ func recoverChaindata(ctx *cli.Context) error {
}

if blockchainLoadError := bc.LoadLastState(true); blockchainLoadError != nil {
glog.V(logger.Error).Infof("! Found error while loading blockchain: %v", blockchainLoadError)
glog.V(logger.Error).Errorf("Error while loading blockchain: %v", blockchainLoadError)
// but do not return
}

header := bc.CurrentHeader()
currentBlock := bc.CurrentBlock()
currentFastBlock := bc.CurrentFastBlock()

glog.V(logger.Error).Infoln("Current status (before recovery attempt):")
glog.D(logger.Error).Infoln("Current status (before recovery attempt):")
if header != nil {
glog.V(logger.Error).Infof("Last header: #%d\n", header.Number.Uint64())
glog.D(logger.Error).Infof("Last header: #%d\n", header.Number.Uint64())
if currentBlock != nil {
glog.V(logger.Error).Infof("Last block: #%d\n", currentBlock.Number())
glog.D(logger.Error).Infof("Last block: #%d\n", currentBlock.Number())
} else {
glog.V(logger.Error).Infoln("! Last block: nil")
glog.D(logger.Error).Errorf("! Last block: nil")
}
if currentFastBlock != nil {
glog.V(logger.Error).Infof("Last fast block: #%d\n", currentFastBlock.Number())
glog.D(logger.Error).Infof("Last fast block: #%d\n", currentFastBlock.Number())
} else {
glog.V(logger.Error).Infoln("! Last fast block: nil")
glog.D(logger.Error).Errorln("! Last fast block: nil")
}
} else {
glog.V(logger.Error).Infoln("! Last header: nil")
glog.D(logger.Error).Errorln("! Last header: nil")
}

glog.V(logger.Error).Infoln(glog.Separator("-"))
glog.D(logger.Error).Infoln(glog.Separator("-"))

glog.V(logger.Error).Infoln("Checking db validity and recoverable data...")
glog.D(logger.Error).Infoln("Checking db validity and recoverable data...")
checkpoint := bc.Recovery(1, 2048)
glog.V(logger.Error).Infof("Found last recoverable checkpoint=#%d\n", checkpoint)
glog.D(logger.Error).Infof("Found last recoverable checkpoint=#%d\n", checkpoint)

glog.V(logger.Error).Infoln(glog.Separator("-"))
glog.D(logger.Error).Infoln(glog.Separator("-"))

glog.V(logger.Error).Infoln("Setting blockchain db head to last safe checkpoint...")
glog.D(logger.Error).Infoln("Setting blockchain db head to last safe checkpoint...")
if setHeadErr := bc.SetHead(checkpoint); setHeadErr != nil {
glog.V(logger.Error).Infof("Error setting head: %v\n", setHeadErr)
glog.D(logger.Error).Errorf("Error setting head: %v\n", setHeadErr)
return setHeadErr
}
return nil
Expand All @@ -1268,7 +1261,7 @@ func askForConfirmation(s string) bool {
reader := bufio.NewReader(os.Stdin)

for {
glog.V(logger.Error).Infof("%s [y/n]: ", s)
glog.D(logger.Error).Warnf("%s [y/n]: ", s)

response, err := reader.ReadString('\n')
if err != nil {
Expand All @@ -1282,9 +1275,9 @@ func askForConfirmation(s string) bool {
} else if response == "n" || response == "no" {
return false
} else {
glog.V(logger.Error).Infoln(glog.Separator("*"))
glog.V(logger.Error).Infoln("* INVALID RESPONSE: Please respond with [y|yes] or [n|no], or use CTRL-C to abort.")
glog.V(logger.Error).Infoln(glog.Separator("*"))
glog.D(logger.Error).Warnln(glog.Separator("*"))
glog.D(logger.Error).Errorln("* INVALID RESPONSE: Please respond with [y|yes] or [n|no], or use CTRL-C to abort.")
glog.D(logger.Error).Warnln(glog.Separator("*"))
}
}
}
Expand All @@ -1300,9 +1293,9 @@ func resetChaindata(ctx *cli.Context) error {
if err := os.RemoveAll(dir); err != nil {
return err
}
glog.V(logger.Error).Infof("Successfully removed chaindata directory: '%s'\n", dir)
glog.D(logger.Error).Infof("Successfully removed chaindata directory: '%s'\n", dir)
} else {
glog.V(logger.Error).Infoln("Leaving chaindata untouched. As you were.")
glog.D(logger.Error).Infoln("Leaving chaindata untouched. As you were.")
}
return nil
}
Loading

0 comments on commit 425f94d

Please sign in to comment.