1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-25 17:05:23 +00:00

reduce memory usage in log

This commit is contained in:
v2ray 2016-04-29 23:40:28 +02:00
parent dd8ce6f164
commit 5390e8efff
4 changed files with 29 additions and 10 deletions

View File

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

View File

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

View File

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

View File

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