OSDN Git Service

Don't init logrus in a seperate file (#361)
authorLiu-Cheng Xu <xuliuchengxlc@gmail.com>
Mon, 5 Feb 2018 01:33:45 +0000 (09:33 +0800)
committerGuanghua Guo <1536310027@qq.com>
Mon, 5 Feb 2018 01:33:45 +0000 (09:33 +0800)
The logging funcationality is broken when crossing compile the
executable file due to some unknown machnisms related to the main
package. So, move `log_hook.go` to `main.go`.

cmd/bytomd/log_hook.go [deleted file]
cmd/bytomd/main.go

diff --git a/cmd/bytomd/log_hook.go b/cmd/bytomd/log_hook.go
deleted file mode 100644 (file)
index 9505815..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-package main
-
-import (
-       log "github.com/sirupsen/logrus"
-       "os"
-       "path"
-       "runtime"
-       "strings"
-)
-
-type ContextHook struct{}
-
-func (hook ContextHook) Levels() []log.Level {
-       return log.AllLevels
-}
-
-func (hook ContextHook) Fire(entry *log.Entry) error {
-       pc := make([]uintptr, 3, 3)
-       cnt := runtime.Callers(6, pc)
-
-       for i := 0; i < cnt; i++ {
-               fu := runtime.FuncForPC(pc[i] - 1)
-               name := fu.Name()
-               if !strings.Contains(name, "github.com/Sirupsen/log") {
-                       file, line := fu.FileLine(pc[i] - 1)
-                       entry.Data["file"] = path.Base(file)
-                       entry.Data["func"] = path.Base(name)
-                       entry.Data["line"] = line
-                       break
-               }
-       }
-       return nil
-}
-
-func init() {
-       log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
-
-       // If environment variable BYTOM_DEBUG is not empty,
-       // then add the hook to logrus and set the log level to DEBUG
-       if os.Getenv("BYTOM_DEBUG") != "" {
-               log.AddHook(ContextHook{})
-               log.SetLevel(log.DebugLevel)
-       }
-}
index cf51c1d..3500898 100644 (file)
@@ -2,12 +2,54 @@ package main
 
 import (
        "os"
+       "path"
+       "runtime"
+       "strings"
 
+       log "github.com/sirupsen/logrus"
        "github.com/tendermint/tmlibs/cli"
 
        "github.com/bytom/cmd/bytomd/commands"
 )
 
+// ContextHook is a hook for logrus.
+type ContextHook struct{}
+
+// Levels returns the whole levels.
+func (hook ContextHook) Levels() []log.Level {
+       return log.AllLevels
+}
+
+// Fire helps logrus record the related file, function and line.
+func (hook ContextHook) Fire(entry *log.Entry) error {
+       pc := make([]uintptr, 3, 3)
+       cnt := runtime.Callers(6, pc)
+
+       for i := 0; i < cnt; i++ {
+               fu := runtime.FuncForPC(pc[i] - 1)
+               name := fu.Name()
+               if !strings.Contains(name, "github.com/Sirupsen/log") {
+                       file, line := fu.FileLine(pc[i] - 1)
+                       entry.Data["file"] = path.Base(file)
+                       entry.Data["func"] = path.Base(name)
+                       entry.Data["line"] = line
+                       break
+               }
+       }
+       return nil
+}
+
+func init() {
+       log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
+
+       // If environment variable BYTOM_DEBUG is not empty,
+       // then add the hook to logrus and set the log level to DEBUG
+       if os.Getenv("BYTOM_DEBUG") != "" {
+               log.AddHook(ContextHook{})
+               log.SetLevel(log.DebugLevel)
+       }
+}
+
 func main() {
        cmd := cli.PrepareBaseCmd(commands.RootCmd, "TM", os.ExpandEnv("./.bytomd"))
        cmd.Execute()