mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-08 10:10:12 +08:00
Fix(log): default level may race
Rename global variables in uber’s style.
This commit is contained in:
10
log/event.go
10
log/event.go
@@ -8,8 +8,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
logCh = make(chan interface{})
|
||||
source = observable.NewObservable(logCh)
|
||||
_logCh = make(chan interface{})
|
||||
_source = observable.NewObservable(_logCh)
|
||||
)
|
||||
|
||||
type Event struct {
|
||||
@@ -24,16 +24,16 @@ func newEvent(level Level, format string, args ...interface{}) *Event {
|
||||
Time: time.Now(),
|
||||
Message: fmt.Sprintf(format, args...),
|
||||
}
|
||||
logCh <- event /* send all events to logCh */
|
||||
_logCh <- event /* send all events to logCh */
|
||||
|
||||
return event
|
||||
}
|
||||
|
||||
func Subscribe() observable.Subscription {
|
||||
sub, _ := source.Subscribe()
|
||||
sub, _ := _source.Subscribe()
|
||||
return sub
|
||||
}
|
||||
|
||||
func UnSubscribe(sub observable.Subscription) {
|
||||
source.UnSubscribe(sub)
|
||||
_source.UnSubscribe(sub)
|
||||
}
|
||||
|
10
log/log.go
10
log/log.go
@@ -2,14 +2,14 @@ package log
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"go.uber.org/atomic"
|
||||
)
|
||||
|
||||
var (
|
||||
// defaultLevel is package default logging level.
|
||||
defaultLevel = InfoLevel
|
||||
// _defaultLevel is package default logging level.
|
||||
_defaultLevel = atomic.NewUint32(uint32(InfoLevel))
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -18,7 +18,7 @@ func init() {
|
||||
}
|
||||
|
||||
func SetLevel(level Level) {
|
||||
atomic.StoreUint32((*uint32)(&defaultLevel), uint32(level))
|
||||
_defaultLevel.Store(uint32(level))
|
||||
}
|
||||
|
||||
func Debugf(format string, args ...interface{}) {
|
||||
@@ -43,7 +43,7 @@ func Fatalf(format string, args ...interface{}) {
|
||||
|
||||
func logf(level Level, format string, args ...interface{}) {
|
||||
event := newEvent(level, format, args...)
|
||||
if event.Level > defaultLevel {
|
||||
if uint32(event.Level) > _defaultLevel.Load() {
|
||||
return
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user