access.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package log
  2. import (
  3. "github.com/v2ray/v2ray-core/common/alloc"
  4. "github.com/v2ray/v2ray-core/common/serial"
  5. )
  6. // AccessStatus is the status of an access request from clients.
  7. type AccessStatus string
  8. const (
  9. AccessAccepted = AccessStatus("accepted")
  10. AccessRejected = AccessStatus("rejected")
  11. )
  12. var (
  13. accessLoggerInstance logWriter = &noOpLogWriter{}
  14. )
  15. type accessLog struct {
  16. From serial.String
  17. To serial.String
  18. Status AccessStatus
  19. Reason serial.String
  20. }
  21. func (this *accessLog) Release() {
  22. this.From = nil
  23. this.To = nil
  24. this.Reason = nil
  25. }
  26. func (this *accessLog) String() string {
  27. b := alloc.NewSmallBuffer().Clear()
  28. defer b.Release()
  29. return b.AppendString(this.From.String()).AppendString(" ").AppendString(string(this.Status)).AppendString(" ").AppendString(this.To.String()).AppendString(" ").AppendString(this.Reason.String()).String()
  30. }
  31. // InitAccessLogger initializes the access logger to write into the give file.
  32. func InitAccessLogger(file string) error {
  33. logger, err := newFileLogWriter(file)
  34. if err != nil {
  35. Error("Failed to create access logger on file (", file, "): ", file, err)
  36. return err
  37. }
  38. accessLoggerInstance = logger
  39. return nil
  40. }
  41. // Access writes an access log.
  42. func Access(from, to serial.String, status AccessStatus, reason serial.String) {
  43. accessLoggerInstance.Log(&accessLog{
  44. From: from,
  45. To: to,
  46. Status: status,
  47. Reason: reason,
  48. })
  49. }