refine main thread to capture all logs

pull/215/head v1.19
v2ray 9 years ago
parent 636effbe38
commit 96c6012013
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

@ -3,12 +3,15 @@ package internal
import ( import (
"log" "log"
"os" "os"
"time"
"github.com/v2ray/v2ray-core/common/platform" "github.com/v2ray/v2ray-core/common/platform"
"github.com/v2ray/v2ray-core/common/signal"
) )
type LogWriter interface { type LogWriter interface {
Log(LogEntry) Log(LogEntry)
Close()
} }
type NoOpLogWriter struct { type NoOpLogWriter struct {
@ -18,13 +21,18 @@ func (this *NoOpLogWriter) Log(entry LogEntry) {
entry.Release() entry.Release()
} }
func (this *NoOpLogWriter) Close() {
}
type StdOutLogWriter struct { type StdOutLogWriter struct {
logger *log.Logger logger *log.Logger
cancel *signal.CancelSignal
} }
func NewStdOutLogWriter() LogWriter { func NewStdOutLogWriter() LogWriter {
return &StdOutLogWriter{ return &StdOutLogWriter{
logger: log.New(os.Stdout, "", log.Ldate|log.Ltime), logger: log.New(os.Stdout, "", log.Ldate|log.Ltime),
cancel: signal.NewCloseSignal(),
} }
} }
@ -33,10 +41,15 @@ func (this *StdOutLogWriter) Log(log LogEntry) {
log.Release() log.Release()
} }
func (this *StdOutLogWriter) Close() {
time.Sleep(500 * time.Millisecond)
}
type FileLogWriter struct { type FileLogWriter struct {
queue chan string queue chan string
logger *log.Logger logger *log.Logger
file *os.File file *os.File
cancel *signal.CancelSignal
} }
func (this *FileLogWriter) Log(log LogEntry) { func (this *FileLogWriter) Log(log LogEntry) {
@ -56,9 +69,12 @@ func (this *FileLogWriter) run() {
} }
this.logger.Print(entry + platform.LineSeparator()) this.logger.Print(entry + platform.LineSeparator())
} }
this.cancel.Done()
} }
func (this *FileLogWriter) Close() { func (this *FileLogWriter) Close() {
close(this.queue)
<-this.cancel.WaitForDone()
this.file.Close() this.file.Close()
} }
@ -71,6 +87,7 @@ func NewFileLogWriter(path string) (*FileLogWriter, error) {
queue: make(chan string, 16), queue: make(chan string, 16),
logger: log.New(file, "", log.Ldate|log.Ltime), logger: log.New(file, "", log.Ldate|log.Ltime),
file: file, file: file,
cancel: signal.NewCloseSignal(),
} }
go logger.run() go logger.run()
return logger, nil return logger, nil

@ -90,3 +90,8 @@ func Error(v ...interface{}) {
Values: v, Values: v,
}) })
} }
func Close() {
streamLoggerInstance.Close()
accessLoggerInstance.Close()
}

@ -43,15 +43,7 @@ func init() {
flag.StringVar(&configFile, "config", defaultConfigFile, "Config file for this Point server.") flag.StringVar(&configFile, "config", defaultConfigFile, "Config file for this Point server.")
} }
func main() { func startV2Ray() *point.Point {
flag.Parse()
core.PrintVersion()
if *version {
return
}
switch *logLevel { switch *logLevel {
case "debug": case "debug":
log.SetLogLevel(log.DebugLevel) log.SetLogLevel(log.DebugLevel)
@ -63,17 +55,17 @@ func main() {
log.SetLogLevel(log.ErrorLevel) log.SetLogLevel(log.ErrorLevel)
default: default:
fmt.Println("Unknown log level: " + *logLevel) fmt.Println("Unknown log level: " + *logLevel)
return return nil
} }
if len(configFile) == 0 { if len(configFile) == 0 {
log.Error("Config file is not set.") log.Error("Config file is not set.")
return return nil
} }
config, err := point.LoadConfig(configFile) config, err := point.LoadConfig(configFile)
if err != nil { if err != nil {
log.Error("Failed to read config file (", configFile, "): ", configFile, err) log.Error("Failed to read config file (", configFile, "): ", configFile, err)
return return nil
} }
if config.LogConfig != nil && len(config.LogConfig.AccessLog) > 0 { if config.LogConfig != nil && len(config.LogConfig.AccessLog) > 0 {
@ -83,23 +75,38 @@ func main() {
vPoint, err := point.NewPoint(config) vPoint, err := point.NewPoint(config)
if err != nil { if err != nil {
log.Error("Failed to create Point server: ", err) log.Error("Failed to create Point server: ", err)
return return nil
} }
if *test { if *test {
fmt.Println("Configuration OK.") fmt.Println("Configuration OK.")
return return nil
} }
err = vPoint.Start() err = vPoint.Start()
if err != nil { if err != nil {
log.Error("Error starting Point server: ", err) log.Error("Error starting Point server: ", err)
return nil
}
return vPoint
}
func main() {
flag.Parse()
core.PrintVersion()
if *version {
return return
} }
if point := startV2Ray(); point != nil {
osSignals := make(chan os.Signal, 1) osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, os.Interrupt, os.Kill) signal.Notify(osSignals, os.Interrupt, os.Kill)
<-osSignals <-osSignals
vPoint.Close() point.Close()
}
log.Close()
} }

Loading…
Cancel
Save