diff --git a/common/log/access.go b/common/log/access.go index f41cace67..cdd3adc6d 100644 --- a/common/log/access.go +++ b/common/log/access.go @@ -23,6 +23,12 @@ type accessLog struct { Reason serial.String } +func (this *accessLog) Release() { + this.From = nil + this.To = nil + this.Reason = nil +} + func (this *accessLog) String() string { return this.From.String() + " " + string(this.Status) + " " + this.To.String() + " " + this.Reason.String() } diff --git a/common/log/log.go b/common/log/log.go index 44815ce7d..9276036a1 100644 --- a/common/log/log.go +++ b/common/log/log.go @@ -3,6 +3,7 @@ package log import ( "fmt" + "github.com/v2ray/v2ray-core/common" "github.com/v2ray/v2ray-core/common/serial" ) @@ -14,11 +15,23 @@ const ( NoneLevel = LogLevel(999) ) +type LogEntry interface { + common.Releasable + serial.String +} + type errorLog struct { prefix string values []interface{} } +func (this *errorLog) Release() { + for index := range this.values { + this.values[index] = nil + } + this.values = nil +} + func (this *errorLog) String() string { data := "" for _, value := range this.values { diff --git a/common/log/log_writer.go b/common/log/log_writer.go index 4f291c377..c4c8de772 100644 --- a/common/log/log_writer.go +++ b/common/log/log_writer.go @@ -6,7 +6,6 @@ import ( "os" "github.com/v2ray/v2ray-core/common/platform" - "github.com/v2ray/v2ray-core/common/serial" ) func createLogger(writer io.Writer) *log.Logger { @@ -14,13 +13,13 @@ func createLogger(writer io.Writer) *log.Logger { } type logWriter interface { - Log(serial.String) + Log(LogEntry) } type noOpLogWriter struct { } -func (this *noOpLogWriter) Log(serial.String) { +func (this *noOpLogWriter) Log(LogEntry) { // Swallow } @@ -34,17 +33,18 @@ func newStdOutLogWriter() logWriter { } } -func (this *stdOutLogWriter) Log(log serial.String) { +func (this *stdOutLogWriter) Log(log LogEntry) { this.logger.Print(log.String() + platform.LineSeparator()) + log.Release() } type fileLogWriter struct { - queue chan serial.String + queue chan LogEntry logger *log.Logger file *os.File } -func (this *fileLogWriter) Log(log serial.String) { +func (this *fileLogWriter) Log(log LogEntry) { select { case this.queue <- log: default: @@ -55,6 +55,7 @@ func (this *fileLogWriter) Log(log serial.String) { func (this *fileLogWriter) run() { for entry := range this.queue { this.logger.Print(entry.String() + platform.LineSeparator()) + entry.Release() } } @@ -68,7 +69,7 @@ func newFileLogWriter(path string) (*fileLogWriter, error) { return nil, err } logger := &fileLogWriter{ - queue: make(chan serial.String, 16), + queue: make(chan LogEntry, 16), logger: log.New(file, "", log.Ldate|log.Ltime), file: file, }