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