Skip to content

Commit

Permalink
allow setting min log level for logger
Browse files Browse the repository at this point in the history
  • Loading branch information
paulwe committed May 7, 2024
1 parent 5f0256e commit ce63ad8
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 64 deletions.
21 changes: 18 additions & 3 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ type ZapLogger interface {
Logger
ToZap() *zap.SugaredLogger
ComponentLeveler() ZapComponentLeveler
WithMinLevel(lvl zapcore.LevelEnabler) Logger
}

type zapLogger[T zaputil.Encoder[T]] struct {
Expand All @@ -198,6 +199,7 @@ type zapLogger[T zaputil.Encoder[T]] struct {
component string
deferred []*zaputil.Deferrer
sampler *zaputil.Sampler
minLevel zapcore.LevelEnabler
}

func FromZapLogger(log *zap.Logger, conf *Config, opts ...ZapLoggerOption) (ZapLogger, error) {
Expand Down Expand Up @@ -253,9 +255,15 @@ func newZapLogger[T zaputil.Encoder[T]](zap *zap.SugaredLogger, zc *zapConfig, e
}

func (l *zapLogger[T]) ToZap() *zap.SugaredLogger {
console, _ := l.writeEnablers.LoadOrCompute(l.component, func() *zaputil.WriteEnabler {
return zaputil.NewWriteEnabler(os.Stderr, l.sc.ComponentLevel(l.component))
})
var console *zaputil.WriteEnabler
if l.minLevel == nil {
console, _ = l.writeEnablers.LoadOrCompute(l.component, func() *zaputil.WriteEnabler {
return zaputil.NewWriteEnabler(os.Stderr, l.sc.ComponentLevel(l.component))
})
} else {
enab := zaputil.OrLevelEnabler{l.minLevel, l.sc.ComponentLevel(l.component)}
console = zaputil.NewWriteEnabler(os.Stderr, enab)
}

c := l.enc.Core(console, l.tap)
for i := range l.deferred {
Expand Down Expand Up @@ -291,6 +299,13 @@ func (l *zapLogger[T]) Debugw(msg string, keysAndValues ...interface{}) {
l.zap.Debugw(msg, keysAndValues...)
}

func (l *zapLogger[T]) WithMinLevel(lvl zapcore.LevelEnabler) Logger {
dup := *l
dup.minLevel = lvl
dup.zap = dup.ToZap()
return &dup
}

func (l *zapLogger[T]) Infow(msg string, keysAndValues ...interface{}) {
l.zap.Infow(msg, keysAndValues...)
}
Expand Down
2 changes: 1 addition & 1 deletion logger/pionlogger/logadapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
// implements webrtc.LeveledLogger
type logAdapter struct {
logger logger.Logger
ignoredPrefixes prefixSet
ignoredPrefixes *prefixSet
}

func (l *logAdapter) Trace(msg string) {
Expand Down
10 changes: 7 additions & 3 deletions logger/pionlogger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

var (
pionIgnoredPrefixes = map[string]prefixSet{
pionIgnoredPrefixes = map[string]*prefixSet{
"ice": {
"pingAllCandidates called with no candidate pairs",
"failed to send packet: io: read/write on closed pipe",
Expand Down Expand Up @@ -52,8 +52,12 @@ var (

type prefixSet []string

func (s prefixSet) Match(msg string) bool {
for _, prefix := range s {
func (s *prefixSet) Match(msg string) bool {
if s == nil {
return false
}

for _, prefix := range *s {
if strings.HasPrefix(msg, prefix) {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion logger/pionlogger/zaplogadapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type zapLogAdapter struct {
logger logger.Logger
level zapcore.LevelEnabler
scope string
ignoredPrefixes prefixSet
ignoredPrefixes *prefixSet
}

func (l *zapLogAdapter) init() {
Expand Down
2 changes: 1 addition & 1 deletion utils/hedge.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type HedgeParams[T any] struct {
Func func(context.Context) (T, error)
}

// race retries if the function takes to long to return
// race retries if the function takes too long to return
// |---------------- attempt 1 ----------------|
// | delay |--------- attempt 2 ---------|
func HedgeCall[T any](ctx context.Context, params HedgeParams[T]) (v T, err error) {
Expand Down
85 changes: 30 additions & 55 deletions utils/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@ package utils
import (
"crypto/rand"
"crypto/sha1"
"encoding/binary"
"fmt"
mrand "math/rand/v2"
"os"
"sync"
"time"

"github.com/jxskiss/base62"
"github.com/lithammer/shortuuid/v4"

"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/utils/must"
)

const GuidSize = 12
const (
GuidSize = 12
guidScratchSize = GuidSize + 10
)

const (
RoomPrefix = "RM_"
Expand All @@ -51,10 +53,16 @@ const (
AgentJobPrefix = "AJ_"
)

var defaultGuidGenerator = newGuidGenerator(4096, GuidSize+10)
var guidGeneratorPool = sync.Pool{
New: func() any {
return must.Get(newGuidGenerator(guidScratchSize))
},
}

func NewGuid(prefix string) string {
return defaultGuidGenerator.NewGuid(prefix)
g := guidGeneratorPool.Get().(*guidGenerator)
defer guidGeneratorPool.Put(g)
return g.NewGuid(prefix)
}

// HashedID creates a hashed ID from a unique string
Expand Down Expand Up @@ -86,57 +94,27 @@ func newB57Index() [256]byte {
}

type guidGenerator struct {
pool sync.Pool
mu sync.Mutex
buf []byte
pos int
scratch []byte
rng *mrand.ChaCha8
}

func newGuidGenerator(bufSize, scratchSize int) *guidGenerator {
return &guidGenerator{
pool: sync.Pool{
New: func() any {
b := make([]byte, scratchSize)
return &b
},
},
buf: make([]byte, bufSize),
pos: bufSize,
func newGuidGenerator(scratchSize int) (*guidGenerator, error) {
var seed [32]byte
if _, err := rand.Read(seed[:]); err != nil {
return nil, err
}
}

func (g *guidGenerator) refillBuf() {
for {
_, err := rand.Read(g.buf)
if err == nil {
return
}

logger.Errorw("unable to refill guid buffer", err)
time.Sleep(time.Millisecond)
}
}

func (g *guidGenerator) uint32() uint32 {
if g.pos == len(g.buf) {
g.refillBuf()
g.pos = 0
}

n := binary.BigEndian.Uint32(g.buf[g.pos:])
g.pos += 4

return n
return &guidGenerator{
scratch: make([]byte, scratchSize),
rng: mrand.NewChaCha8(seed),
}, nil
}

func (g *guidGenerator) readIDChars(b []byte) {
g.mu.Lock()
defer g.mu.Unlock()

var n int
for {
r := g.uint32()
for i := 0; i < 5; i++ {
r := g.rng.Uint64()
for i := 0; i < 10; i++ {
if int(r&0x3f) < len(b57Chars) {
b[n] = b57Chars[r&0x3f]
n++
Expand All @@ -150,13 +128,10 @@ func (g *guidGenerator) readIDChars(b []byte) {
}

func (g *guidGenerator) NewGuid(prefix string) string {
b := g.pool.Get().(*[]byte)
defer g.pool.Put(b)

*b = append((*b)[:0], make([]byte, len(prefix)+GuidSize)...)
copy(*b, prefix)
g.readIDChars((*b)[len(prefix):])
return string(*b)
s := append(g.scratch[:0], make([]byte, len(prefix)+GuidSize)...)
copy(s, prefix)
g.readIDChars(s[len(prefix):])
return string(s)
}

func guidPrefix[T livekit.Guid]() string {
Expand Down

0 comments on commit ce63ad8

Please sign in to comment.