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