v2ray-core/common/log/internal/log_entry.go

73 lines
1.2 KiB
Go
Raw Normal View History

2016-05-12 06:24:41 +00:00
package internal
import (
"fmt"
2016-07-15 19:15:41 +00:00
"strings"
2016-05-12 06:24:41 +00:00
2016-08-20 18:55:45 +00:00
"v2ray.com/core/common"
"v2ray.com/core/common/serial"
2016-05-12 06:24:41 +00:00
)
2016-05-24 20:41:51 +00:00
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:
2016-11-27 07:47:11 +00:00
return fmt.Sprintf("%+v", value)
2016-05-24 20:41:51 +00:00
}
}
2016-05-12 06:24:41 +00:00
type LogEntry interface {
common.Releasable
2016-05-24 19:55:46 +00:00
fmt.Stringer
2016-05-12 06:24:41 +00:00
}
type ErrorLog struct {
Prefix string
Values []interface{}
}
2016-11-27 20:39:09 +00:00
func (v *ErrorLog) Release() {
for index := range v.Values {
v.Values[index] = nil
2016-05-12 06:24:41 +00:00
}
2016-11-27 20:39:09 +00:00
v.Values = nil
2016-05-12 06:24:41 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *ErrorLog) String() string {
values := make([]string, len(v.Values)+1)
values[0] = v.Prefix
for i, value := range v.Values {
2016-07-15 19:15:41 +00:00
values[i+1] = InterfaceToString(value)
2016-05-12 06:24:41 +00:00
}
2016-07-15 19:15:41 +00:00
return strings.Join(values, "")
2016-05-12 06:24:41 +00:00
}
type AccessLog struct {
2016-05-24 20:41:51 +00:00
From interface{}
To interface{}
2016-05-12 06:24:41 +00:00
Status string
2016-05-24 20:41:51 +00:00
Reason interface{}
2016-05-12 06:24:41 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *AccessLog) Release() {
v.From = nil
v.To = nil
v.Reason = nil
2016-05-12 06:24:41 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *AccessLog) String() string {
return strings.Join([]string{InterfaceToString(v.From), v.Status, InterfaceToString(v.To), InterfaceToString(v.Reason)}, " ")
2016-05-12 06:24:41 +00:00
}