|
|
|
@ -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
|
|
|
|
|