log_entry.go 1.4 KB

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