Skip to content

Commit 87a1c2a

Browse files
committed
log: adapt Console to backend.Gomsg instead of string
1 parent 73f3a0b commit 87a1c2a

4 files changed

Lines changed: 27 additions & 27 deletions

File tree

intra/log/log.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,12 @@ const (
4343
callerat = 2
4444
)
4545

46-
// String wrapped in struct Logmsg is a workaround for:
47-
// github.com/golang/go/issues/46893
48-
type Logmsg struct {
49-
S string
50-
}
51-
52-
func (m *Logmsg) String() string {
53-
if m == nil {
54-
return ""
55-
}
56-
return m.S
57-
}
46+
type Logmsg string
5847

5948
// Console is an external logger.
6049
type Console interface {
6150
// Log logs a multi-line msg.
62-
Log(level int32, msg *Logmsg)
51+
Log(level int32, msg Logmsg)
6352
}
6453

6554
type conMsg struct {

intra/log/logger.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func (l *simpleLogger) incrStCount(id string) (c uint32) {
283283
// Must be called once from a goroutine.
284284
func (l *simpleLogger) consoleDispatcher() {
285285
for m := range l.cmsgC {
286-
if m == nil || len(m.m.S) <= 0 { // no msg
286+
if m == nil || len(m.m) <= 0 { // no msg
287287
continue
288288
}
289289
load := (len(l.cmsgC) / cap(l.cmsgC) * 100) // load percentage
@@ -293,24 +293,24 @@ func (l *simpleLogger) consoleDispatcher() {
293293
// drop
294294
case VVERBOSE, VERBOSE, DEBUG, INFO:
295295
if load < 50 {
296-
c.Log(int32(m.t), &m.m)
296+
c.Log(int32(m.t), m.m)
297297
continue
298298
} // drop
299299
case WARN, ERROR:
300300
if load < 5 {
301301
if d := l.cskips.Swap(0); d > 0 {
302-
c.Log(int32(WARN), &Logmsg{l.msgstr(WARN, "backpressure... dropped %d msgs", d)})
302+
c.Log(int32(WARN), Logmsg(l.msgstr(WARN, "backpressure... dropped %d msgs", d)))
303303
}
304304
}
305305
if load < 80 {
306-
c.Log(int32(m.t), &m.m)
306+
c.Log(int32(m.t), m.m)
307307
continue
308308
} // drop
309309
case STACKTRACE:
310-
c.Log(int32(m.t), &m.m)
310+
c.Log(int32(m.t), m.m)
311311
continue
312312
case USR:
313-
c.Log(int32(m.t), &m.m)
313+
c.Log(int32(m.t), m.m)
314314
continue
315315
}
316316
} // dropped
@@ -331,7 +331,7 @@ func (l *simpleLogger) Usr(msg string) {
331331
if count := l.incrStCount(msg); count > similarUsrMsgThreshold {
332332
return
333333
}
334-
l.consoleQueue(&conMsg{Logmsg{msg}, USR})
334+
l.consoleQueue(&conMsg{Logmsg(msg), USR})
335335
}
336336
}
337337

@@ -392,7 +392,7 @@ func (l *simpleLogger) emitStack(at int, msgs ...string) {
392392
// c.Stack() on the same go routine, since
393393
// the caller (ex: core.Recover) may exit
394394
// immediately once simpleLogger.Stack() returns
395-
c.Log(int32(STACKTRACE), &Logmsg{msg})
395+
c.Log(int32(STACKTRACE), Logmsg(msg))
396396
} else {
397397
// msg, which is unsafely type-coerced from []byte,
398398
// is pooled; but the caller owns []byte and so it
@@ -551,7 +551,7 @@ func (l *simpleLogger) writelog(lvl LogLevel, at int, msg string, args ...any) {
551551
}
552552
// print spammsg only if spamming is not allowed
553553
if cc && !spamConsole {
554-
l.consoleQueue(&conMsg{Logmsg{spammsg}, lvl})
554+
l.consoleQueue(&conMsg{Logmsg(spammsg), lvl})
555555
}
556556
}
557557
}
@@ -576,7 +576,7 @@ func (l *simpleLogger) writelog(lvl LogLevel, at int, msg string, args ...any) {
576576
l.out(msg)
577577
}
578578
if cc && (!isspam || spamConsole) {
579-
l.consoleQueue(&conMsg{Logmsg{msg}, lvl})
579+
l.consoleQueue(&conMsg{Logmsg(msg), lvl})
580580
}
581581
}
582582
}

intra/tun2socks.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ import (
4141
// pkg.go.dev/runtime#hdr-Environment_Variables
4242
type traceout string
4343

44-
type Console = log.Console
45-
type Logmsg = log.Logmsg
46-
type Controller x.Controller
44+
type Console interface {
45+
Log(int32, *x.Gomsg)
46+
}
4747

48+
type Controller x.Controller
4849
type ProxyListener x.ProxyListener
4950
type DNSListener x.DNSListener
5051
type ServerListener rnet.ServerListener

intra/tunnel.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func NewTunnel(fd, mtu int, fakedns string, dtr DefaultDNS, bdg Bridge) (t Tunne
143143

144144
const dualstack = settings.IP46
145145

146-
log.SetConsole(bdg)
146+
log.SetConsole(&clogAdapter{bdg})
147147
natpt := x64.NewNatPt()
148148
proxies := ipn.NewProxifier(ctx, dualstack, mtu, bdg, bdg)
149149
services := rnet.NewServices(ctx, proxies, bdg, bdg)
@@ -417,3 +417,13 @@ func fetchDNSInfo(r dnsx.Resolver, id string) string {
417417
return rerr.Error()
418418
}
419419
}
420+
421+
type clogAdapter struct {
422+
b Bridge
423+
}
424+
425+
var _ log.Console = (*clogAdapter)(nil)
426+
427+
func (l *clogAdapter) Log(lvl int32, msg log.Logmsg) {
428+
l.b.Log(lvl, x.MsgOf(string(msg))) // adopt the log message
429+
}

0 commit comments

Comments
 (0)