1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-10 06:16:53 -05:00
v2fly/common/log/internal/log_entry.go
2016-05-24 22:41:51 +02:00

83 lines
1.5 KiB
Go

package internal
import (
"fmt"
"github.com/v2ray/v2ray-core/common"
"github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/common/serial"
)
func InterfaceToString(value interface{}) string {
if value == nil {
return " "
}
switch value := value.(type) {
case string:
return value
case *string:
return *value
case fmt.Stringer:
return value.String()
case error:
return value.Error()
case []byte:
return serial.BytesToHexString(value)
default:
return fmt.Sprint(value)
}
}
type LogEntry interface {
common.Releasable
fmt.Stringer
}
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 {
b := alloc.NewSmallBuffer().Clear()
defer b.Release()
b.AppendString(this.Prefix)
for _, value := range this.Values {
b.AppendString(InterfaceToString(value))
}
return b.String()
}
type AccessLog struct {
From interface{}
To interface{}
Status string
Reason interface{}
}
func (this *AccessLog) Release() {
this.From = nil
this.To = nil
this.Reason = nil
}
func (this *AccessLog) String() string {
b := alloc.NewSmallBuffer().Clear()
defer b.Release()
b.AppendString(InterfaceToString(this.From)).AppendString(" ")
b.AppendString(this.Status).AppendString(" ")
b.AppendString(InterfaceToString(this.To)).AppendString(" ")
b.AppendString(InterfaceToString(this.Reason))
return b.String()
}