OSDN Git Service

new repo
[bytom/vapor.git] / cmd / vapor / main.go
1 package main
2
3 import (
4         "os"
5         "path"
6         "runtime"
7         "strings"
8
9         log "github.com/sirupsen/logrus"
10         "github.com/tendermint/tmlibs/cli"
11
12         "github.com/vapor/cmd/vapor/commands"
13         "github.com/vapor/config"
14 )
15
16 // ContextHook is a hook for logrus.
17 type ContextHook struct{}
18
19 // Levels returns the whole levels.
20 func (hook ContextHook) Levels() []log.Level {
21         return log.AllLevels
22 }
23
24 // Fire helps logrus record the related file, function and line.
25 func (hook ContextHook) Fire(entry *log.Entry) error {
26         pc := make([]uintptr, 3, 3)
27         cnt := runtime.Callers(6, pc)
28
29         for i := 0; i < cnt; i++ {
30                 fu := runtime.FuncForPC(pc[i] - 1)
31                 name := fu.Name()
32                 if !strings.Contains(name, "github.com/Sirupsen/log") {
33                         file, line := fu.FileLine(pc[i] - 1)
34                         entry.Data["file"] = path.Base(file)
35                         entry.Data["func"] = path.Base(name)
36                         entry.Data["line"] = line
37                         break
38                 }
39         }
40         return nil
41 }
42
43 func init() {
44         log.SetFormatter(&log.TextFormatter{FullTimestamp: true, DisableColors: true})
45
46         // If environment variable BYTOM_DEBUG is not empty,
47         // then add the hook to logrus and set the log level to DEBUG
48         if os.Getenv("BYTOM_DEBUG") != "" {
49                 log.AddHook(ContextHook{})
50         }
51 }
52
53 func main() {
54         cmd := cli.PrepareBaseCmd(commands.RootCmd, "TM", os.ExpandEnv(config.DefaultDataDir()))
55         cmd.Execute()
56 }