aboutsummaryrefslogtreecommitdiffstats
path: root/ethutil/config.go
blob: a573e108bc77caeb0cea3f16a5c7fe2ca19114c5 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package ethutil

import (
    "flag"
    "fmt"
    "github.com/rakyll/globalconf"
    "log"
    "os"
    "os/user"
    "path"
    "runtime"
)

// Config struct
type config struct {
    Db Database

    Log          *Logger
    ExecPath     string
    Debug        bool
    Ver          string
    ClientString string
    Pubkey       []byte
    Identifier   string

    conf *globalconf.GlobalConf
}

const defaultConf = `
id = ""
port = 30303
upnp = true
maxpeer = 10
rpc = false
rpcport = 8080
`

var Config *config

func ApplicationFolder(base string) string {
    usr, _ := user.Current()
    p := path.Join(usr.HomeDir, base)

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

            }
        }

        iniFilePath := path.Join(p, "conf.ini")
        _, err = os.Stat(iniFilePath)
        if err != nil && os.IsNotExist(err) {
            file, err := os.Create(iniFilePath)
            if err != nil {
                fmt.Println(err)
            } else {
                assetPath := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "ethereal", "assets")
                file.Write([]byte(defaultConf + "\nasset_path = " + assetPath))
            }
        }
    }

    return p
}

// Read config
//
// Initialize the global Config variable with default settings
func ReadConfig(base string, logTypes LoggerType, g *globalconf.GlobalConf, id string) *config {
    if Config == nil {
        path := ApplicationFolder(base)

        Config = &config{ExecPath: path, Debug: true, Ver: "0.5.14"}
        Config.conf = g
        Config.Identifier = id
        Config.Log = NewLogger(logTypes, LogLevelDebug)
        Config.SetClientString("Ethereum(G)")
    }

    return Config
}

// Set client string
//
func (c *config) SetClientString(str string) {
    os := runtime.GOOS
    cust := c.Identifier
    Config.ClientString = fmt.Sprintf("%s/v%s/%s/%s/Go", str, c.Ver, cust, os)
}

func (c *config) SetIdentifier(id string) {
    c.Identifier = id
    c.Set("id", id)
}

func (c *config) Set(key, value string) {
    f := &flag.Flag{Name: key, Value: &confValue{value}}
    c.conf.Set("", f)
}

type LoggerType byte

const (
    LogFile = 0x1
    LogStd  = 0x2
)

type LogSystem interface {
    Println(v ...interface{})
    Printf(format string, v ...interface{})
}

type Logger struct {
    logSys   []LogSystem
    logLevel int
}

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

    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}
}

func (self *Logger) SetLevel(level int) {
    self.logLevel = level
}

func (log *Logger) AddLogSystem(logger LogSystem) {
    log.logSys = append(log.logSys, logger)
}

const (
    LogLevelSystem = iota
    LogLevelDebug
    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...)
    }
}

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

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

    os.Exit(1)
}

func (log *Logger) Println(level int, v ...interface{}) {
    if log.logLevel > level {
        return
    }

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

func (log *Logger) Printf(level int, format string, v ...interface{}) {
    if log.logLevel > level {
        return
    }

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

type confValue struct {
    value string
}

func (self confValue) String() string     { return self.value }
func (self confValue) Set(s string) error { self.value = s; return nil }