1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-30 05:56:54 -05:00

refine main thread to capture all logs

This commit is contained in:
v2ray 2016-07-10 23:11:42 +02:00
parent 636effbe38
commit 96c6012013
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
3 changed files with 47 additions and 18 deletions

View File

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

View File

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

View File

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