-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlogging_mutex.go
84 lines (69 loc) · 2.1 KB
/
logging_mutex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// +build mutexlog
package bchutil
import (
"os"
"sync"
"github.com/gcash/bchlog"
)
// Create a logger for all mutexes to use
var mutexLogger bchlog.Logger
func init() {
mutexLogger = bchlog.NewBackend(os.Stdout).Logger("MUTEX")
mutexLogger.SetLevel(bchlog.LevelInfo)
}
// Mutex wraps a sync.Mutex and adds logging around its actions
type Mutex struct {
name string
sync.Mutex
}
// NewMutex creates a new `Mutex` whose actions with be logged and labeled with
// the given name
func NewMutex(name string) Mutex {
return Mutex{name, sync.Mutex{}}
}
// Lock intercepts calls to lock this mutex and wraps logging around it
func (m *Mutex) Lock() {
mutexLogger.Info("Locking mutex:", m.name)
m.Mutex.Lock()
mutexLogger.Info("Locked mutex:", m.name)
}
// Unlock intercepts calls to unlock this mutex and wraps logging around it
func (m *Mutex) Unlock() {
mutexLogger.Info("Unlocking mutex:", m.name)
m.Mutex.Unlock()
mutexLogger.Info("Unlocked mutex:", m.name)
}
// RWMutex wraps a sync.RWMutex and adds logging around its actions
type RWMutex struct {
name string
sync.RWMutex
}
// NewRWMutex creates a new `RWMutex` whose actions with be logged and labeled
// with the given name
func NewRWMutex(name string) RWMutex {
return RWMutex{name, sync.RWMutex{}}
}
// Lock intercepts calls to lock this mutex and wraps logging around it
func (m *RWMutex) Lock() {
mutexLogger.Info("Locking mutex:", m.name)
m.RWMutex.Lock()
mutexLogger.Info("Locked mutex:", m.name)
}
// Unlock intercepts calls to unlock this mutex and wraps logging around it
func (m *RWMutex) Unlock() {
mutexLogger.Info("Unlocking mutex:", m.name)
m.RWMutex.Unlock()
mutexLogger.Info("Unlocked mutex:", m.name)
}
// RLock intercepts calls to rlock this mutex and wraps logging around it
func (m *RWMutex) RLock() {
mutexLogger.Info("RLocking mutex:", m.name)
m.RWMutex.RLock()
mutexLogger.Info("RLocked mutex:", m.name)
}
// RUnlock intercepts calls to runlock this mutex and wraps logging around it
func (m *RWMutex) RUnlock() {
mutexLogger.Info("RUnlocking mutex:", m.name)
m.RWMutex.RUnlock()
mutexLogger.Info("RUnlocked mutex:", m.name)
}