Skip to content

Commit f456f4e

Browse files
committed
log: for atomic, underlying types must be same
but what the client code sends to SetConsole is out of pkg log's control and hence it must accomodate for changing underlying types of whatever implements the Console interface
1 parent 6d240f0 commit f456f4e

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

intra/log/logger.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,22 @@ func (a *atom[T]) get() (zz T) {
9696
return zz
9797
}
9898

99-
func (a *atom[T]) set(t T) {
99+
func (a *atom[T]) set(t T) (ok bool) {
100100
if a == nil {
101101
return
102102
}
103-
if IsNil(t) {
103+
if isNil(t) {
104104
zz := &atom[T]{}
105105
*a = *zz
106106
return
107107
}
108+
old := a.get()
109+
if !typeEq(old, t) {
110+
r := &atom[T]{}
111+
*a = *r
112+
}
108113
aa := (*atomic.Value)(a)
109-
aa.Store(t)
114+
return aa.CompareAndSwap(old, t)
110115
}
111116

112117
const pcbuckets = 512
@@ -304,7 +309,7 @@ func (l *simpleLogger) consoleDispatcher() {
304309
continue
305310
}
306311
load := (len(l.cmsgC) / cap(l.cmsgC) * 100) // load percentage
307-
if c := l.c.get(); c != nil && !IsNil(c) { // look for l.c on every msg
312+
if c := l.c.get(); c != nil && !isNil(c) { // look for l.c on every msg
308313
switch m.t {
309314
case NONE:
310315
// drop
@@ -405,7 +410,7 @@ func (l *simpleLogger) emitStack(at int, msgs ...string) {
405410
}
406411
if !sendtoconsole {
407412
l.err(at+nextframe, msg)
408-
} else if c != nil && !IsNil(c) {
413+
} else if c != nil && !isNil(c) {
409414
// c.Stack() on the same go routine, since
410415
// the caller (ex: core.Recover) may exit
411416
// immediately once simpleLogger.Stack() returns
@@ -687,8 +692,8 @@ top:
687692
}
688693

689694
// Cannot import pkg core here.
690-
// from: intra/core/typ.go:IsNil
691-
func IsNil(x any) bool {
695+
// from: intra/core/typ.go:isNil
696+
func isNil(x any) bool {
692697
// from: stackoverflow.com/a/76595928
693698
if x == nil {
694699
return true
@@ -701,3 +706,13 @@ func IsNil(x any) bool {
701706
}
702707
return false
703708
}
709+
710+
// from: intra/core/typ.go:typeEq
711+
func typeEq(a, b any) bool {
712+
if isNil(a) {
713+
return false
714+
} else if isNil(b) {
715+
return false
716+
}
717+
return reflect.TypeOf(a) == reflect.TypeOf(b)
718+
}

0 commit comments

Comments
 (0)