github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/logger/implementation/zap/logger_unsafe.go (about)

     1  // Copyright 2022 Meta Platforms, Inc. and affiliates.
     2  //
     3  // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
     4  //
     5  // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
     6  //
     7  // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
     8  //
     9  // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    10  //
    11  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    12  
    13  package zap
    14  
    15  import (
    16  	"math"
    17  	"sync/atomic"
    18  	"unsafe"
    19  
    20  	"github.com/facebookincubator/go-belt/pkg/field"
    21  	"go.uber.org/zap"
    22  	"go.uber.org/zap/zapcore"
    23  )
    24  
    25  func fieldsToZap(length int, forEachField func(callback func(f *field.Field) bool) bool) *[]zap.Field {
    26  	if length == 0 {
    27  		return nil
    28  	}
    29  	result := acquireZapFields()
    30  	if cap(*result) < length {
    31  		*result = make([]zap.Field, 0, length)
    32  	}
    33  	if len(*result) != 0 {
    34  		panic(len(*result))
    35  	}
    36  	count := 0
    37  	appendToResult := func(f *field.Field) bool {
    38  		count++
    39  		if count > length {
    40  			return false
    41  		}
    42  		zapField := zap.Field{
    43  			Key: f.Key,
    44  		}
    45  		switch value := f.Value.(type) {
    46  		case string:
    47  			zapField.Type = zapcore.StringType
    48  			zapField.String = value
    49  		case int:
    50  			zapField.Type = zapcore.Int64Type
    51  			zapField.Integer = int64(value)
    52  		case float64:
    53  			zapField.Type = zapcore.Float64Type
    54  			zapField.Integer = int64(math.Float64bits(value))
    55  		case error:
    56  			zapField.Type = zapcore.ErrorType
    57  			zapField.Interface = f.Value
    58  		default:
    59  			zapField.Type = zapcore.ReflectType
    60  			zapField.Interface = f.Value
    61  		}
    62  		*result = append(*result, zapField)
    63  		return true
    64  	}
    65  
    66  	// See the assumption described in the "WARNING" message of field.ForEachFieldser.
    67  	forEachField(*(*func(f *field.Field) bool)(noescape(unsafe.Pointer(&appendToResult))))
    68  
    69  	if len(*result) > length {
    70  		panic("should not happen")
    71  	}
    72  	return result
    73  }
    74  
    75  func (l *Emitter) setZapLogger(logger *zap.Logger) {
    76  	atomic.StorePointer((*unsafe.Pointer)((unsafe.Pointer)(&l.ZapLogger)), (unsafe.Pointer)(logger))
    77  }
    78  
    79  func (l *Emitter) getZapLogger() *zap.Logger {
    80  	return (*zap.Logger)(atomic.LoadPointer((*unsafe.Pointer)(noescape((unsafe.Pointer)(&l.ZapLogger)))))
    81  }