godns/main.go
2013-07-25 12:32:57 +08:00

67 lines
1002 B
Go

package main
import (
"log"
"os"
"os/signal"
"runtime"
"time"
)
var (
logger *log.Logger
)
func main() {
logger = initLogger(settings.Log.File)
server := &Server{
host: settings.Server.Host,
port: settings.Server.Port,
rTimeout: 5 * time.Second,
wTimeout: 5 * time.Second,
}
server.Run()
logger.Printf("godns %s start", settings.Version)
sig := make(chan os.Signal)
signal.Notify(sig, os.Interrupt)
forever:
for {
select {
case <-sig:
logger.Printf("signal received, stopping")
break forever
}
}
}
func Debug(format string, v ...interface{}) {
if settings.Debug {
logger.Printf(format, v...)
}
}
func initLogger(log_file string) (logger *log.Logger) {
if log_file != "" {
f, err := os.Create(log_file)
if err != nil {
os.Exit(1)
}
logger = log.New(f, "[godns]", log.Ldate|log.Ltime)
} else {
logger = log.New(os.Stdout, "[godns]", log.Ldate|log.Ltime)
}
return logger
}
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
}