log.go 838 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package log
  2. import (
  3. "sync/atomic"
  4. "unsafe"
  5. "v2ray.com/core/common/serial"
  6. )
  7. type Message interface {
  8. String() string
  9. }
  10. type Handler interface {
  11. Handle(msg Message)
  12. }
  13. type noOpHandler byte
  14. func (noOpHandler) Handle(msg Message) {}
  15. type GeneralMessage struct {
  16. Severity Severity
  17. Content interface{}
  18. }
  19. func (m *GeneralMessage) String() string {
  20. return serial.Concat("[", m.Severity, "]: ", m.Content)
  21. }
  22. func (s Severity) SevererThan(another Severity) bool {
  23. return s <= another
  24. }
  25. func Record(msg Message) {
  26. h := (*Handler)(atomic.LoadPointer(&logHandler))
  27. (*h).Handle(msg)
  28. }
  29. var (
  30. logHandler unsafe.Pointer
  31. )
  32. func RegisterHandler(handler Handler) {
  33. if handler == nil {
  34. panic("Log handler is nil")
  35. }
  36. atomic.StorePointer(&logHandler, unsafe.Pointer(&handler))
  37. }
  38. func init() {
  39. RegisterHandler(noOpHandler(0))
  40. }