1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00

releasable log

This commit is contained in:
v2ray 2016-04-29 13:38:40 +02:00
parent cef5386a21
commit dd8ce6f164
3 changed files with 27 additions and 7 deletions

View File

@ -23,6 +23,12 @@ type accessLog struct {
Reason serial.String Reason serial.String
} }
func (this *accessLog) Release() {
this.From = nil
this.To = nil
this.Reason = nil
}
func (this *accessLog) String() string { func (this *accessLog) String() string {
return this.From.String() + " " + string(this.Status) + " " + this.To.String() + " " + this.Reason.String() return this.From.String() + " " + string(this.Status) + " " + this.To.String() + " " + this.Reason.String()
} }

View File

@ -3,6 +3,7 @@ package log
import ( import (
"fmt" "fmt"
"github.com/v2ray/v2ray-core/common"
"github.com/v2ray/v2ray-core/common/serial" "github.com/v2ray/v2ray-core/common/serial"
) )
@ -14,11 +15,23 @@ const (
NoneLevel = LogLevel(999) NoneLevel = LogLevel(999)
) )
type LogEntry interface {
common.Releasable
serial.String
}
type errorLog struct { type errorLog struct {
prefix string prefix string
values []interface{} values []interface{}
} }
func (this *errorLog) Release() {
for index := range this.values {
this.values[index] = nil
}
this.values = nil
}
func (this *errorLog) String() string { func (this *errorLog) String() string {
data := "" data := ""
for _, value := range this.values { for _, value := range this.values {

View File

@ -6,7 +6,6 @@ import (
"os" "os"
"github.com/v2ray/v2ray-core/common/platform" "github.com/v2ray/v2ray-core/common/platform"
"github.com/v2ray/v2ray-core/common/serial"
) )
func createLogger(writer io.Writer) *log.Logger { func createLogger(writer io.Writer) *log.Logger {
@ -14,13 +13,13 @@ func createLogger(writer io.Writer) *log.Logger {
} }
type logWriter interface { type logWriter interface {
Log(serial.String) Log(LogEntry)
} }
type noOpLogWriter struct { type noOpLogWriter struct {
} }
func (this *noOpLogWriter) Log(serial.String) { func (this *noOpLogWriter) Log(LogEntry) {
// Swallow // 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()) this.logger.Print(log.String() + platform.LineSeparator())
log.Release()
} }
type fileLogWriter struct { type fileLogWriter struct {
queue chan serial.String queue chan LogEntry
logger *log.Logger logger *log.Logger
file *os.File file *os.File
} }
func (this *fileLogWriter) Log(log serial.String) { func (this *fileLogWriter) Log(log LogEntry) {
select { select {
case this.queue <- log: case this.queue <- log:
default: default:
@ -55,6 +55,7 @@ func (this *fileLogWriter) Log(log serial.String) {
func (this *fileLogWriter) run() { func (this *fileLogWriter) run() {
for entry := range this.queue { for entry := range this.queue {
this.logger.Print(entry.String() + platform.LineSeparator()) this.logger.Print(entry.String() + platform.LineSeparator())
entry.Release()
} }
} }
@ -68,7 +69,7 @@ func newFileLogWriter(path string) (*fileLogWriter, error) {
return nil, err return nil, err
} }
logger := &fileLogWriter{ logger := &fileLogWriter{
queue: make(chan serial.String, 16), queue: make(chan LogEntry, 16),
logger: log.New(file, "", log.Ldate|log.Ltime), logger: log.New(file, "", log.Ldate|log.Ltime),
file: file, file: file,
} }