log_entry.go 1.3 KB

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