From: wyjDoraemon <46176410+wyjDoraemon@users.noreply.github.com> Date: Tue, 30 Jul 2019 03:17:38 +0000 (+0800) Subject: add clear lock file (#364) X-Git-Tag: v1.0.5~68 X-Git-Url: http://git.osdn.net/view?p=bytom%2Fvapor.git;a=commitdiff_plain;h=4c30652bc333792725d18fba1f92493369b59e5a add clear lock file (#364) * add clear lock file * log.Fatal * fix * fix1 --- diff --git a/log/log.go b/log/log.go index 43bcc9ca..ebd979f4 100644 --- a/log/log.go +++ b/log/log.go @@ -1,7 +1,11 @@ package log import ( + "fmt" + "io/ioutil" + "os" "path/filepath" + "strings" "sync" "time" @@ -18,9 +22,17 @@ const ( var defaultFormatter = &logrus.TextFormatter{DisableColors: true} -func InitLogFile(config *config.Config) { - hook := newBtmHook(config.LogDir()) +func InitLogFile(config *config.Config) error { + logPath := config.LogDir() + if err := clearLockFiles(logPath); err != nil { + return err + } + + hook := newBtmHook(logPath) logrus.AddHook(hook) + logrus.SetOutput(ioutil.Discard)//控制台不输出 + fmt.Printf("all logs are output in the %s directory\n", logPath) + return nil } type BtmHook struct { @@ -60,6 +72,22 @@ func (hook *BtmHook) ioWrite(entry *logrus.Entry) error { return err } +func clearLockFiles(logPath string) error { + files, err := ioutil.ReadDir(logPath) + if err != nil { + return err + } + + for _, file := range files { + if ok := strings.HasSuffix(file.Name(), "_lock"); ok { + if err := os.Remove(filepath.Join(logPath, file.Name()));err!=nil{ + return err + } + } + } + return nil +} + func (hook *BtmHook) Fire(entry *logrus.Entry) error { hook.lock.Lock() defer hook.lock.Unlock() diff --git a/node/node.go b/node/node.go index beafcf03..c160f17c 100644 --- a/node/node.go +++ b/node/node.go @@ -66,7 +66,9 @@ func NewNode(config *cfg.Config) *Node { cmn.Exit(cmn.Fmt("Failed to load federated information:[%s]", err.Error())) } - vaporLog.InitLogFile(config) + if err:=vaporLog.InitLogFile(config);err!=nil{ + log.WithField("err",err).Fatalln("InitLogFile failed") + } log.WithFields(log.Fields{ "module": logModule, diff --git a/p2p/connection/connection.go b/p2p/connection/connection.go index 576be24b..af5f6450 100644 --- a/p2p/connection/connection.go +++ b/p2p/connection/connection.go @@ -38,7 +38,7 @@ const ( defaultRecvMessageCapacity = 22020096 // 21MB defaultRecvRate = int64(104857600) // 100MB/s defaultSendTimeout = 10 * time.Second - logModule = "p2p/conn" + logModule = "p2pConn" ) type receiveCbFunc func(chID byte, msgBytes []byte)