Skip to content

Commit

Permalink
emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
xjasonlyu committed Aug 28, 2024
1 parent 42136de commit 0df5170
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
25 changes: 10 additions & 15 deletions log/emitter.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
package log

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

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

var _globalE = &emitter{}

func init() {
EnableStackLog(true)
glog.SetTarget(_globalE)
}

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

type emitter struct{}

func (*emitter) level(level glog.Level) Level {
return 1 - Level(level)
func (e *emitter) setLogger(logger *SugaredLogger) {
e.logger = logger.WithOptions(pkgCallerSkip)
}

func (*emitter) prefix(format string) string {
return "[STACK] " + format
func (e *emitter) logf(level glog.Level, format string, args ...any) {
e.logger.Logf(1-Level(level), "[STACK] "+format, args...)
}

func (e *emitter) Emit(depth int, level glog.Level, _ time.Time, format string, args ...any) {
Expand All @@ -38,5 +33,5 @@ func (e *emitter) Emit(depth int, level glog.Level, _ time.Time, format string,
return
}
}
logf(e.level(level), e.prefix(format), args...)
e.logf(level, format, args...)
}
1 change: 1 addition & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func SetLogger(logger *Logger) {
// apply pkgCallerSkip to global loggers.
_globalL = logger.WithOptions(pkgCallerSkip)
_globalS = _globalL.Sugar()
_globalE.setLogger(_globalS)
}

func logf(lvl Level, template string, args ...any) {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func init() {
flag.StringVar(&configFile, "config", "", "YAML format configuration file")
flag.StringVar(&key.Device, "device", "", "Use this device [driver://]name")
flag.StringVar(&key.Interface, "interface", "", "Use network INTERFACE (Linux/MacOS only)")
flag.StringVar(&key.LogLevel, "loglevel", "info", "Log level [debug|info|warning|error|silent]")
flag.StringVar(&key.LogLevel, "loglevel", "info", "Log level [debug|info|warn|error|silent]")
flag.StringVar(&key.Proxy, "proxy", "", "Use this proxy [protocol://]host[:port]")
flag.StringVar(&key.RestAPI, "restapi", "", "HTTP statistic server listen address")
flag.StringVar(&key.TCPSendBufferSize, "tcp-sndbuf", "", "Set TCP send buffer size for netstack")
Expand Down

0 comments on commit 0df5170

Please sign in to comment.