Official Go implementation of the Ethereum protocol
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-ethereum/ethutil/config.go

215 lines
4.0 KiB

11 years ago
package ethutil
import (
11 years ago
"flag"
"fmt"
11 years ago
"github.com/rakyll/globalconf"
11 years ago
"log"
"os"
"os/user"
"path"
"runtime"
11 years ago
)
// Config struct
11 years ago
type config struct {
Db Database
Log *Logger
ExecPath string
Debug bool
Ver string
ClientString string
Pubkey []byte
Identifier string
11 years ago
conf *globalconf.GlobalConf
11 years ago
}
const defaultConf = `
id = ""
port = 30303
upnp = true
maxpeer = 10
rpc = false
rpcport = 8080
`
11 years ago
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
11 years ago
func ReadConfig(base string, logTypes LoggerType, g *globalconf.GlobalConf, id string) *config {
11 years ago
if Config == nil {
path := ApplicationFolder(base)
11 years ago
11 years ago
Config = &config{ExecPath: path, Debug: true, Ver: "0.5.0 RC12"}
11 years ago
Config.conf = g
Config.Identifier = id
Config.Log = NewLogger(logTypes, LogLevelDebug)
Config.SetClientString("/Ethereum(G)")
11 years ago
}
return Config
}
// Set client string
//
func (c *config) SetClientString(str string) {
id := runtime.GOOS
if len(c.Identifier) > 0 {
id = c.Identifier
}
Config.ClientString = fmt.Sprintf("%s nv%s/%s", str, c.Ver, id)
}
11 years ago
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)
}
11 years ago
type LoggerType byte
const (
LogFile = 0x1
LogStd = 0x2
)
type LogSystem interface {
Println(v ...interface{})
Printf(format string, v ...interface{})
}
11 years ago
type Logger struct {
logSys []LogSystem
11 years ago
logLevel int
}
func NewLogger(flag LoggerType, level int) *Logger {
var loggers []LogSystem
11 years ago
flags := log.LstdFlags
11 years ago
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)
11 years ago
loggers = append(loggers, log)
}
if flag&LogStd > 0 {
log := log.New(os.Stdout, "", flags)
11 years ago
loggers = append(loggers, log)
}
return &Logger{logSys: loggers, logLevel: level}
}
func (log *Logger) AddLogSystem(logger LogSystem) {
log.logSys = append(log.logSys, logger)
11 years ago
}
const (
LogLevelDebug = iota
LogLevelInfo
)
func (log *Logger) Debugln(v ...interface{}) {
if log.logLevel != LogLevelDebug {
11 years ago
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 {
11 years ago
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)
}
11 years ago
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 }