2014-02-14 14:56:09 -08:00
|
|
|
package ethutil
|
|
|
|
|
|
|
|
import (
|
2014-02-21 16:53:09 -08:00
|
|
|
"fmt"
|
2014-02-14 14:56:09 -08:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path"
|
2014-03-17 04:08:16 -07:00
|
|
|
"runtime"
|
2014-02-14 14:56:09 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
type LogType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
LogTypeStdIn = 1
|
|
|
|
LogTypeFile = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config struct isn't exposed
|
|
|
|
type config struct {
|
|
|
|
Db Database
|
|
|
|
|
2014-03-17 04:08:16 -07:00
|
|
|
Log *Logger
|
|
|
|
ExecPath string
|
|
|
|
Debug bool
|
|
|
|
Ver string
|
|
|
|
ClientString string
|
|
|
|
Pubkey []byte
|
|
|
|
Seed bool
|
2014-02-14 14:56:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2014-02-19 02:35:17 -08: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) {
|
2014-02-19 02:40:02 -08:00
|
|
|
log.Printf("Debug logging directory %s doesn't exist, creating it\n", path)
|
2014-02-19 02:35:17 -08:00
|
|
|
os.Mkdir(path, 0777)
|
|
|
|
}
|
2014-02-14 14:56:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-28 07:45:46 -08:00
|
|
|
Config = &config{ExecPath: path, Debug: true, Ver: "0.3.1"}
|
2014-02-19 07:27:22 -08:00
|
|
|
Config.Log = NewLogger(LogFile|LogStd, LogLevelDebug)
|
2014-03-17 04:08:16 -07:00
|
|
|
Config.ClientString = fmt.Sprintf("/Ethereum(G) v%s/%s", Config.Ver, runtime.GOOS)
|
2014-02-14 14:56:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Config
|
|
|
|
}
|
|
|
|
|
|
|
|
type LoggerType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
LogFile = 0x1
|
|
|
|
LogStd = 0x2
|
|
|
|
)
|
|
|
|
|
2014-02-21 16:53:09 -08:00
|
|
|
type LogSystem interface {
|
|
|
|
Println(v ...interface{})
|
|
|
|
Printf(format string, v ...interface{})
|
|
|
|
}
|
|
|
|
|
2014-02-14 14:56:09 -08:00
|
|
|
type Logger struct {
|
2014-02-21 16:53:09 -08:00
|
|
|
logSys []LogSystem
|
2014-02-14 14:56:09 -08:00
|
|
|
logLevel int
|
|
|
|
}
|
|
|
|
|
2014-02-21 16:53:09 -08:00
|
|
|
func NewLogger(flag LoggerType, level int) *Logger {
|
|
|
|
var loggers []LogSystem
|
2014-02-14 14:56:09 -08:00
|
|
|
|
2014-02-19 07:27:22 -08:00
|
|
|
flags := log.LstdFlags
|
2014-02-14 14:56:09 -08: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 07:27:22 -08:00
|
|
|
log := log.New(file, "", flags)
|
2014-02-14 14:56:09 -08:00
|
|
|
|
|
|
|
loggers = append(loggers, log)
|
|
|
|
}
|
|
|
|
if flag&LogStd > 0 {
|
2014-02-19 07:27:22 -08:00
|
|
|
log := log.New(os.Stdout, "", flags)
|
2014-02-14 14:56:09 -08:00
|
|
|
loggers = append(loggers, log)
|
|
|
|
}
|
|
|
|
|
2014-02-21 16:53:09 -08:00
|
|
|
return &Logger{logSys: loggers, logLevel: level}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (log *Logger) AddLogSystem(logger LogSystem) {
|
|
|
|
log.logSys = append(log.logSys, logger)
|
2014-02-14 14:56:09 -08:00
|
|
|
}
|
|
|
|
|
2014-02-19 07:27:22 -08:00
|
|
|
const (
|
|
|
|
LogLevelDebug = iota
|
|
|
|
LogLevelInfo
|
|
|
|
)
|
|
|
|
|
2014-02-21 16:53:09 -08:00
|
|
|
func (log *Logger) Debugln(v ...interface{}) {
|
2014-02-19 07:27:22 -08:00
|
|
|
if log.logLevel != LogLevelDebug {
|
2014-02-14 14:56:09 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, logger := range log.logSys {
|
|
|
|
logger.Println(v...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-21 16:53:09 -08:00
|
|
|
func (log *Logger) Debugf(format string, v ...interface{}) {
|
2014-02-19 07:27:22 -08:00
|
|
|
if log.logLevel != LogLevelDebug {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, logger := range log.logSys {
|
|
|
|
logger.Printf(format, v...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-21 16:53:09 -08:00
|
|
|
func (log *Logger) Infoln(v ...interface{}) {
|
2014-02-19 07:27:22 -08:00
|
|
|
if log.logLevel > LogLevelInfo {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-02-21 16:53:09 -08:00
|
|
|
fmt.Println(len(log.logSys))
|
2014-02-19 07:27:22 -08:00
|
|
|
for _, logger := range log.logSys {
|
|
|
|
logger.Println(v...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-21 16:53:09 -08:00
|
|
|
func (log *Logger) Infof(format string, v ...interface{}) {
|
2014-02-19 07:27:22 -08:00
|
|
|
if log.logLevel > LogLevelInfo {
|
2014-02-14 14:56:09 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, logger := range log.logSys {
|
|
|
|
logger.Printf(format, v...)
|
|
|
|
}
|
|
|
|
}
|