diff --git a/common/alloc/buffer.go b/common/alloc/buffer.go index 7615a04a6..111d3fa16 100644 --- a/common/alloc/buffer.go +++ b/common/alloc/buffer.go @@ -49,6 +49,11 @@ func (b *Buffer) Append(data []byte) *Buffer { return b } +func (b *Buffer) AppendString(s string) *Buffer { + b.Value = append(b.Value, s...) + return b +} + // Prepend prepends bytes in front of the buffer. Caller must ensure total bytes prepended is // no more than 16 bytes. func (b *Buffer) Prepend(data []byte) *Buffer { @@ -131,6 +136,10 @@ func (b *Buffer) FillFrom(reader io.Reader) (int, error) { return nBytes, err } +func (b *Buffer) String() string { + return string(b.Value) +} + // NewSmallBuffer creates a Buffer with 1K bytes of arbitrary content. func NewSmallBuffer() *Buffer { return smallPool.Allocate() diff --git a/common/log/access.go b/common/log/access.go index cdd3adc6d..f858edfbf 100644 --- a/common/log/access.go +++ b/common/log/access.go @@ -1,6 +1,7 @@ package log import ( + "github.com/v2ray/v2ray-core/common/alloc" "github.com/v2ray/v2ray-core/common/serial" ) @@ -30,7 +31,10 @@ func (this *accessLog) Release() { } func (this *accessLog) String() string { - return this.From.String() + " " + string(this.Status) + " " + this.To.String() + " " + this.Reason.String() + b := alloc.NewSmallBuffer().Clear() + defer b.Release() + + return b.AppendString(this.From.String()).AppendString(" ").AppendString(string(this.Status)).AppendString(" ").AppendString(this.To.String()).AppendString(" ").AppendString(this.Reason.String()).String() } // InitAccessLogger initializes the access logger to write into the give file. diff --git a/common/log/log.go b/common/log/log.go index 9276036a1..48a9b2e3d 100644 --- a/common/log/log.go +++ b/common/log/log.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/v2ray/v2ray-core/common" + "github.com/v2ray/v2ray-core/common/alloc" "github.com/v2ray/v2ray-core/common/serial" ) @@ -33,22 +34,26 @@ func (this *errorLog) Release() { } func (this *errorLog) String() string { - data := "" + b := alloc.NewSmallBuffer().Clear() + defer b.Release() + + b.AppendString(this.prefix) + for _, value := range this.values { switch typedVal := value.(type) { case string: - data += typedVal + b.AppendString(typedVal) case *string: - data += *typedVal + b.AppendString(*typedVal) case serial.String: - data += typedVal.String() + b.AppendString(typedVal.String()) case error: - data += typedVal.Error() + b.AppendString(typedVal.Error()) default: - data += fmt.Sprintf("%v", value) + b.AppendString(fmt.Sprint(value)) } } - return this.prefix + data + return b.String() } var ( diff --git a/common/log/log_writer.go b/common/log/log_writer.go index c4c8de772..fe7aa1521 100644 --- a/common/log/log_writer.go +++ b/common/log/log_writer.go @@ -19,8 +19,8 @@ type logWriter interface { type noOpLogWriter struct { } -func (this *noOpLogWriter) Log(LogEntry) { - // Swallow +func (this *noOpLogWriter) Log(entry LogEntry) { + entry.Release() } type stdOutLogWriter struct { @@ -48,6 +48,7 @@ func (this *fileLogWriter) Log(log LogEntry) { select { case this.queue <- log: default: + log.Release() // We don't expect this to happen, but don't want to block main thread as well. } }