github.com/turingchain2020/turingchain@v1.1.21/common/log/log15/handler_go13.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build !go1.4
     6  
     7  package log15
     8  
     9  import (
    10  	"sync/atomic"
    11  	"unsafe"
    12  )
    13  
    14  // swapHandler wraps another handler that may be swapped out
    15  // dynamically at runtime in a thread-safe fashion.
    16  type swapHandler struct {
    17  	handler unsafe.Pointer
    18  }
    19  
    20  func (h *swapHandler) Log(r *Record) error {
    21  	return h.Get().Log(r)
    22  }
    23  
    24  func (h *swapHandler) MaxLevel() int {
    25  	return h.Get().MaxLevel()
    26  }
    27  
    28  func (h *swapHandler) SetMaxLevel(maxLevel int) {
    29  	h.Get().SetMaxLevel(maxLevel)
    30  }
    31  
    32  func (h *swapHandler) Get() Handler {
    33  	return *(*Handler)(atomic.LoadPointer(&h.handler))
    34  }
    35  
    36  func (h *swapHandler) Swap(newHandler Handler) {
    37  	atomic.StorePointer(&h.handler, unsafe.Pointer(&newHandler))
    38  }