go-ethereum/ethutil/config.go

165 lines
2.9 KiB
Go
Raw Normal View History

2014-02-15 00:56:09 +02:00
package ethutil
import (
"fmt"
2014-02-15 00:56:09 +02:00
"log"
"os"
"os/user"
"path"
"runtime"
2014-02-15 00:56:09 +02:00
)
// Config struct
2014-02-15 00:56:09 +02:00
type config struct {
Db Database
Log *Logger
ExecPath string
Debug bool
Ver string
ClientString string
Pubkey []byte
Identifier string
2014-02-15 00:56:09 +02:00
}
var Config *config
// Read config
//
// Initialize the global Config variable with default settings
func ReadConfig(base string, logTypes LoggerType, id string) *config {
2014-02-15 00:56:09 +02:00
if Config == nil {
usr, _ := user.Current()
path := path.Join(usr.HomeDir, base)
2014-02-19 12:35:17 +02:00
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)
2014-02-19 12:35:17 +02:00
os.Mkdir(path, 0777)
}
2014-02-15 00:56:09 +02:00
}
}
2014-05-27 17:10:18 +03:00
Config = &config{ExecPath: path, Debug: true, Ver: "0.5.0 RC10"}
Config.Identifier = id
Config.Log = NewLogger(logTypes, LogLevelDebug)
2014-03-20 18:27:48 +02:00
Config.SetClientString("/Ethereum(G)")
2014-02-15 00:56:09 +02:00
}
return Config
}
// Set client string
//
2014-03-20 18:27:48 +02:00
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)
2014-03-20 18:27:48 +02:00
}
2014-02-15 00:56:09 +02:00
type LoggerType byte
const (
LogFile = 0x1
LogStd = 0x2
)
type LogSystem interface {
Println(v ...interface{})
Printf(format string, v ...interface{})
}
2014-02-15 00:56:09 +02:00
type Logger struct {
logSys []LogSystem
2014-02-15 00:56:09 +02:00
logLevel int
}
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)
}
return &Logger{logSys: loggers, logLevel: level}
}
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 (
LogLevelDebug = iota
LogLevelInfo
)
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...)
}
}
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...)
}
}
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...)
}
}
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)
}