github.com/jimmyx0x/go-ethereum@v1.10.28/log/handler_go13.go (about)

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