log_entry.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package internal
  2. import (
  3. "fmt"
  4. "github.com/v2ray/v2ray-core/common"
  5. "github.com/v2ray/v2ray-core/common/alloc"
  6. )
  7. type LogEntry interface {
  8. common.Releasable
  9. fmt.Stringer
  10. }
  11. type ErrorLog struct {
  12. Prefix string
  13. Values []interface{}
  14. }
  15. func (this *ErrorLog) Release() {
  16. for index := range this.Values {
  17. this.Values[index] = nil
  18. }
  19. this.Values = nil
  20. }
  21. func (this *ErrorLog) String() string {
  22. b := alloc.NewSmallBuffer().Clear()
  23. defer b.Release()
  24. b.AppendString(this.Prefix)
  25. for _, value := range this.Values {
  26. switch typedVal := value.(type) {
  27. case string:
  28. b.AppendString(typedVal)
  29. case *string:
  30. b.AppendString(*typedVal)
  31. case fmt.Stringer:
  32. b.AppendString(typedVal.String())
  33. case error:
  34. b.AppendString(typedVal.Error())
  35. default:
  36. b.AppendString(fmt.Sprint(value))
  37. }
  38. }
  39. return b.String()
  40. }
  41. type AccessLog struct {
  42. From fmt.Stringer
  43. To fmt.Stringer
  44. Status string
  45. Reason fmt.Stringer
  46. }
  47. func (this *AccessLog) Release() {
  48. this.From = nil
  49. this.To = nil
  50. this.Reason = nil
  51. }
  52. func (this *AccessLog) String() string {
  53. b := alloc.NewSmallBuffer().Clear()
  54. defer b.Release()
  55. return b.AppendString(this.From.String()).AppendString(" ").AppendString(this.Status).AppendString(" ").AppendString(this.To.String()).AppendString(" ").AppendString(this.Reason.String()).String()
  56. }