Explorar o código

releasable log

v2ray %!s(int64=9) %!d(string=hai) anos
pai
achega
dd8ce6f164
Modificáronse 3 ficheiros con 27 adicións e 7 borrados
  1. 6 0
      common/log/access.go
  2. 13 0
      common/log/log.go
  3. 8 7
      common/log/log_writer.go

+ 6 - 0
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()
 }

+ 13 - 0
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 {

+ 8 - 7
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,
 	}