aboutsummaryrefslogtreecommitdiffstats
path: root/ethutil/config.go
blob: 607152b5b3cc938e3f66b8a465d2d91743d15ca3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package ethutil

import (
    "log"
    "os"
    "os/user"
    "path"
)

type LogType byte

const (
    LogTypeStdIn = 1
    LogTypeFile  = 2
)

// Config struct isn't exposed
type config struct {
    Db Database

    Log      Logger
    ExecPath string
    Debug    bool
    Ver      string
    Pubkey   []byte
    Seed     bool
}

var Config *config

// Read config doesn't read anything yet.
func ReadConfig(base string) *config {
    if Config == nil {
        usr, _ := user.Current()
        path := path.Join(usr.HomeDir, base)

        if len(base) > 0 {
            //Check if the logging directory already exists, create it if not
            _, err := os.Stat(path)
            if err != nil {
                if os.IsNotExist(err) {
                    log.Printf("Debug logging directory %s doesn't exist, creating it\n", path)
                    os.Mkdir(path, 0777)
                }
            }
        }

        Config = &config{ExecPath: path, Debug: true, Ver: "0.2.3"}
        Config.Log = NewLogger(LogFile|LogStd, LogLevelDebug)
    }

    return Config
}

type LoggerType byte

const (
    LogFile = 0x1
    LogStd  = 0x2
)

type Logger struct {
    logSys   []*log.Logger
    logLevel int
}

func NewLogger(flag LoggerType, level int) Logger {
    var loggers []*log.Logger

    flags := log.LstdFlags

    if flag&LogFile > 0 {
        file, err := os.OpenFile(path.Join(Config.ExecPath, "debug.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModePerm)
        if err != nil {
            log.Panic("unable to create file logger", err)
        }

        log := log.New(file, "", flags)

        loggers = append(loggers, log)
    }
    if flag&LogStd > 0 {
        log := log.New(os.Stdout, "", flags)
        loggers = append(loggers, log)
    }

    return Logger{logSys: loggers, logLevel: level}
}

const (
    LogLevelDebug = iota
    LogLevelInfo
)

func (log Logger) Debugln(v ...interface{}) {
    if log.logLevel != LogLevelDebug {
        return
    }

    for _, logger := range log.logSys {
        logger.Println(v...)
    }
}

func (log Logger) Debugf(format string, v ...interface{}) {
    if log.logLevel != LogLevelDebug {
        return
    }

    for _, logger := range log.logSys {
        logger.Printf(format, v...)
    }
}

func (log Logger) Infoln(v ...interface{}) {
    if log.logLevel > LogLevelInfo {
        return
    }

    for _, logger := range log.logSys {
        logger.Println(v...)
    }
}

func (log Logger) Infof(format string, v ...interface{}) {
    if log.logLevel > LogLevelInfo {
        return
    }

    for _, logger := range log.logSys {
        logger.Printf(format, v...)
    }
}