handler_go13.go 511 B

123456789101112131415161718192021222324252627
  1. // +build !go1.4
  2. package log
  3. import (
  4. "sync/atomic"
  5. "unsafe"
  6. )
  7. // swapHandler wraps another handler that may be swapped out
  8. // dynamically at runtime in a thread-safe fashion.
  9. type swapHandler struct {
  10. handler unsafe.Pointer
  11. }
  12. func (h *swapHandler) Log(r *Record) error {
  13. return h.Get().Log(r)
  14. }
  15. func (h *swapHandler) Get() Handler {
  16. return *(*Handler)(atomic.LoadPointer(&h.handler))
  17. }
  18. func (h *swapHandler) Swap(newHandler Handler) {
  19. atomic.StorePointer(&h.handler, unsafe.Pointer(&newHandler))
  20. }