github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/libkb/vlock.go (about)

     1  package libkb
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  )
     7  
     8  type VerboseLockRelease = func()
     9  
    10  type VerboseLock struct {
    11  	mu    sync.Mutex
    12  	level VDebugLevel
    13  	name  string
    14  }
    15  
    16  func NewVerboseLock(level VDebugLevel, name string) *VerboseLock {
    17  	return &VerboseLock{
    18  		level: level,
    19  		name:  name,
    20  	}
    21  }
    22  
    23  func (l *VerboseLock) Acquire(mctx MetaContext, reasonFormat string, args ...interface{}) (release VerboseLockRelease) {
    24  	reason := fmt.Sprintf(reasonFormat, args...)
    25  	log := func(symbol string, word string) {
    26  		mctx.VLogf(l.level, "%v VerboseLock [%v] %v: %v", symbol, l.name, word, reason)
    27  	}
    28  	log("+", "acquiring")
    29  	l.mu.Lock()
    30  	log("|", "acquired")
    31  	return func() {
    32  		l.mu.Unlock()
    33  		log("-", "released")
    34  	}
    35  }