Skip to content

Commit

Permalink
add emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
xjasonlyu committed Aug 28, 2024
1 parent 2ea221c commit 42136de
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions log/emitter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package log

import (
"io"
"runtime"
"strings"
"time"

glog "gvisor.dev/gvisor/pkg/log"
)

func init() {
EnableStackLog(true)
}

func EnableStackLog(v bool) {
if v {
glog.SetTarget(&emitter{}) // built-in logger
} else {
glog.SetTarget(&glog.Writer{Next: io.Discard})
}
}

type emitter struct{}

func (*emitter) level(level glog.Level) Level {
return 1 - Level(level)
}

func (*emitter) prefix(format string) string {
return "[STACK] " + format
}

func (e *emitter) Emit(depth int, level glog.Level, _ time.Time, format string, args ...any) {
if _, file, line, ok := runtime.Caller(depth + 1); ok {
// Ignore: gvisor.dev/gvisor/pkg/tcpip/adapters/gonet/gonet.go:457
if line == 457 && strings.HasSuffix(file, "gonet/gonet.go") {
return
}
}
logf(e.level(level), e.prefix(format), args...)
}

0 comments on commit 42136de

Please sign in to comment.