2014-02-15 00:56:09 +02:00
|
|
|
package ethutil
|
|
|
|
|
|
|
|
import (
|
2014-05-30 20:51:19 +03:00
|
|
|
"flag"
|
2014-02-22 02:53:09 +02:00
|
|
|
"fmt"
|
2014-05-30 20:51:19 +03:00
|
|
|
"github.com/rakyll/globalconf"
|
2014-02-15 00:56:09 +02:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path"
|
2014-03-17 13:08:16 +02:00
|
|
|
"runtime"
|
2014-02-15 00:56:09 +02:00
|
|
|
)
|
|
|
|
|
2014-04-27 18:15:44 +03:00
|
|
|
// Config struct
|
2014-02-15 00:56:09 +02:00
|
|
|
type config struct {
|
|
|
|
Db Database
|
|
|
|
|
2014-03-17 13:08:16 +02:00
|
|
|
Log *Logger
|
|
|
|
ExecPath string
|
|
|
|
Debug bool
|
|
|
|
Ver string
|
|
|
|
ClientString string
|
|
|
|
Pubkey []byte
|
2014-05-22 18:35:26 +03:00
|
|
|
Identifier string
|
2014-05-30 20:51:19 +03:00
|
|
|
|
|
|
|
conf *globalconf.GlobalConf
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
|
|
|
|
2014-05-30 17:57:58 +03:00
|
|
|
const defaultConf = `
|
|
|
|
id = ""
|
|
|
|
port = 30303
|
|
|
|
upnp = true
|
|
|
|
maxpeer = 10
|
|
|
|
rpc = false
|
|
|
|
rpcport = 8080
|
|
|
|
`
|
|
|
|
|
2014-02-15 00:56:09 +02:00
|
|
|
var Config *config
|
|
|
|
|
2014-05-30 17:57:58 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-04-27 18:15:44 +03:00
|
|
|
// Read config
|
|
|
|
//
|
|
|
|
// Initialize the global Config variable with default settings
|
2014-05-30 20:51:19 +03:00
|
|
|
func ReadConfig(base string, logTypes LoggerType, g *globalconf.GlobalConf, id string) *config {
|
2014-02-15 00:56:09 +02:00
|
|
|
if Config == nil {
|
2014-05-30 17:57:58 +03:00
|
|
|
path := ApplicationFolder(base)
|
2014-02-15 00:56:09 +02:00
|
|
|
|
2014-06-20 01:47:50 +03:00
|
|
|
Config = &config{ExecPath: path, Debug: true, Ver: "0.5.14"}
|
2014-05-30 20:51:19 +03:00
|
|
|
Config.conf = g
|
2014-05-22 18:35:26 +03:00
|
|
|
Config.Identifier = id
|
2014-05-17 15:07:52 +03:00
|
|
|
Config.Log = NewLogger(logTypes, LogLevelDebug)
|
2014-06-12 11:07:27 +03:00
|
|
|
Config.SetClientString("Ethereum(G)")
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Config
|
|
|
|
}
|
|
|
|
|
2014-04-27 18:15:44 +03:00
|
|
|
// Set client string
|
|
|
|
//
|
2014-03-20 18:27:48 +02:00
|
|
|
func (c *config) SetClientString(str string) {
|
2014-06-12 11:07:27 +03:00
|
|
|
os := runtime.GOOS
|
|
|
|
cust := c.Identifier
|
|
|
|
Config.ClientString = fmt.Sprintf("%s/v%s/%s/%s/Go", str, c.Ver, cust, os)
|
2014-03-20 18:27:48 +02:00
|
|
|
}
|
|
|
|
|
2014-05-30 20:51:19 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-02-15 00:56:09 +02:00
|
|
|
type LoggerType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
LogFile = 0x1
|
|
|
|
LogStd = 0x2
|
|
|
|
)
|
|
|
|
|
2014-02-22 02:53:09 +02:00
|
|
|
type LogSystem interface {
|
|
|
|
Println(v ...interface{})
|
|
|
|
Printf(format string, v ...interface{})
|
|
|
|
}
|
|
|
|
|
2014-02-15 00:56:09 +02:00
|
|
|
type Logger struct {
|
2014-02-22 02:53:09 +02:00
|
|
|
logSys []LogSystem
|
2014-02-15 00:56:09 +02:00
|
|
|
logLevel int
|
|
|
|
}
|
|
|
|
|
2014-02-22 02:53:09 +02:00
|
|
|
func NewLogger(flag LoggerType, level int) *Logger {
|
|
|
|
var loggers []LogSystem
|
2014-02-15 00:56:09 +02:00
|
|
|
|
2014-02-19 17:27:22 +02:00
|
|
|
flags := log.LstdFlags
|
2014-02-15 00:56:09 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-02-19 17:27:22 +02:00
|
|
|
log := log.New(file, "", flags)
|
2014-02-15 00:56:09 +02:00
|
|
|
|
|
|
|
loggers = append(loggers, log)
|
|
|
|
}
|
|
|
|
if flag&LogStd > 0 {
|
2014-02-19 17:27:22 +02:00
|
|
|
log := log.New(os.Stdout, "", flags)
|
2014-02-15 00:56:09 +02:00
|
|
|
loggers = append(loggers, log)
|
|
|
|
}
|
|
|
|
|
2014-02-22 02:53:09 +02:00
|
|
|
return &Logger{logSys: loggers, logLevel: level}
|
|
|
|
}
|
|
|
|
|
2014-06-14 16:44:13 +03:00
|
|
|
func (self *Logger) SetLevel(level int) {
|
|
|
|
self.logLevel = level
|
|
|
|
}
|
|
|
|
|
2014-02-22 02:53:09 +02:00
|
|
|
func (log *Logger) AddLogSystem(logger LogSystem) {
|
|
|
|
log.logSys = append(log.logSys, logger)
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
|
|
|
|
2014-02-19 17:27:22 +02:00
|
|
|
const (
|
2014-06-14 16:44:13 +03:00
|
|
|
LogLevelSystem = iota
|
|
|
|
LogLevelDebug
|
2014-02-19 17:27:22 +02:00
|
|
|
LogLevelInfo
|
|
|
|
)
|
|
|
|
|
2014-02-22 02:53:09 +02:00
|
|
|
func (log *Logger) Debugln(v ...interface{}) {
|
2014-02-19 17:27:22 +02:00
|
|
|
if log.logLevel != LogLevelDebug {
|
2014-02-15 00:56:09 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, logger := range log.logSys {
|
|
|
|
logger.Println(v...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-22 02:53:09 +02:00
|
|
|
func (log *Logger) Debugf(format string, v ...interface{}) {
|
2014-02-19 17:27:22 +02:00
|
|
|
if log.logLevel != LogLevelDebug {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, logger := range log.logSys {
|
|
|
|
logger.Printf(format, v...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-22 02:53:09 +02:00
|
|
|
func (log *Logger) Infoln(v ...interface{}) {
|
2014-02-19 17:27:22 +02:00
|
|
|
if log.logLevel > LogLevelInfo {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, logger := range log.logSys {
|
|
|
|
logger.Println(v...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-22 02:53:09 +02:00
|
|
|
func (log *Logger) Infof(format string, v ...interface{}) {
|
2014-02-19 17:27:22 +02:00
|
|
|
if log.logLevel > LogLevelInfo {
|
2014-02-15 00:56:09 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, logger := range log.logSys {
|
|
|
|
logger.Printf(format, v...)
|
|
|
|
}
|
|
|
|
}
|
2014-05-19 18:02:16 +03:00
|
|
|
|
|
|
|
func (log *Logger) Fatal(v ...interface{}) {
|
|
|
|
if log.logLevel > LogLevelInfo {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, logger := range log.logSys {
|
|
|
|
logger.Println(v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2014-05-30 20:51:19 +03:00
|
|
|
|
2014-06-14 16:44:13 +03:00
|
|
|
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...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 20:51:19 +03:00
|
|
|
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 }
|