github.com/livekit/protocol@v1.16.1-0.20240517185851-47e4c6bba773/logger/zaputil/zaputil_test.go (about)

     1  // Copyright 2023 LiveKit, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package zaputil
    16  
    17  import (
    18  	"go.uber.org/atomic"
    19  	"go.uber.org/zap/zapcore"
    20  )
    21  
    22  type testStringer string
    23  
    24  func (s testStringer) String() string {
    25  	return string(s)
    26  }
    27  
    28  type testStringLike string
    29  
    30  type testCore struct {
    31  	zapcore.Core
    32  	writeCount *atomic.Int32
    33  }
    34  
    35  func (c *testCore) init() {
    36  	if c.writeCount == nil {
    37  		c.writeCount = &atomic.Int32{}
    38  	}
    39  }
    40  
    41  func (c *testCore) WriteCount() int {
    42  	c.init()
    43  	return int(c.writeCount.Load())
    44  }
    45  
    46  func (c *testCore) With(fields []zapcore.Field) zapcore.Core {
    47  	c.init()
    48  	return &testCore{
    49  		Core:       c.Core.With(fields),
    50  		writeCount: c.writeCount,
    51  	}
    52  }
    53  
    54  func (s *testCore) Write(entry zapcore.Entry, fields []zapcore.Field) error {
    55  	s.init()
    56  	s.writeCount.Inc()
    57  	return nil
    58  }