Update
This commit is contained in:
1 parent
3530afb854
commit
6903fcff27
2146 files changed
+112256
-190703
No files matched your search
+161
@@ -0,0 +1,161 @@
|
||||
// Copyright 2025 The gVisor Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"gvisor.dev/gvisor/pkg/sync"
|
||||
)
|
||||
|
||||
// This file contains helper functions analogous to the Linux kernel's WARN*
|
||||
// macros. Should be used for non-fatal errors that should be treated as bugs
|
||||
// none the less.
|
||||
|
||||
const (
|
||||
warnFmtStr = "WARNING: BUG on %s:%d\n"
|
||||
warnUnknownLineStr = "WARNING: BUG on unknown line\n"
|
||||
catchAllMagic = "runtime.Caller failed"
|
||||
)
|
||||
|
||||
//go:noinline
|
||||
func reportBugErr(caller int, err error) {
|
||||
reportBug(caller+1, err.Error(), nil)
|
||||
}
|
||||
|
||||
func reportBug(caller int, msg string, vars []any) {
|
||||
var b strings.Builder
|
||||
if _, file, line, ok := runtime.Caller(caller); ok {
|
||||
fmt.Fprintf(&b, warnFmtStr, file, line)
|
||||
} else {
|
||||
b.WriteString(warnUnknownLineStr)
|
||||
}
|
||||
b.WriteByte('\n')
|
||||
if len(msg) > 0 {
|
||||
if len(vars) > 0 {
|
||||
fmt.Fprintf(&b, msg, vars...)
|
||||
} else {
|
||||
b.WriteString(msg)
|
||||
}
|
||||
b.WriteByte('\n')
|
||||
}
|
||||
TracebackAll(b.String())
|
||||
}
|
||||
|
||||
var (
|
||||
// warnedMu protects the variables below.
|
||||
warnedMu sync.Mutex
|
||||
// warnedSet is used to keep track of which WarnOnOnce calls have fired.
|
||||
warnedSet map[string]struct{}
|
||||
)
|
||||
|
||||
//go:noinline
|
||||
func reportBugErrOnce(caller int, err error) {
|
||||
reportBugOnce(caller+1, err.Error(), nil)
|
||||
}
|
||||
|
||||
func reportBugOnce(caller int, msg string, vars []any) {
|
||||
var b strings.Builder
|
||||
if _, file, line, ok := runtime.Caller(caller); ok {
|
||||
key := fmt.Sprintf("%s:%d", file, line)
|
||||
|
||||
warnedMu.Lock()
|
||||
defer warnedMu.Unlock()
|
||||
|
||||
if _, ok = warnedSet[key]; !ok {
|
||||
fmt.Fprintf(&b, warnFmtStr, file, line)
|
||||
b.WriteByte('\n')
|
||||
if len(msg) > 0 {
|
||||
if len(vars) > 0 {
|
||||
fmt.Fprintf(&b, msg, vars...)
|
||||
} else {
|
||||
b.WriteString(msg)
|
||||
}
|
||||
b.WriteByte('\n')
|
||||
}
|
||||
|
||||
TracebackAll(b.String())
|
||||
warnedSet[key] = struct{}{}
|
||||
}
|
||||
} else {
|
||||
warnedMu.Lock()
|
||||
defer warnedMu.Unlock()
|
||||
|
||||
// Use const string as a catch-all when runtime.Caller fails,
|
||||
// so as to avoid log-spam since that's the point of WARN_ONCE.
|
||||
if _, ok := warnedSet[catchAllMagic]; !ok {
|
||||
b.WriteString(warnUnknownLineStr)
|
||||
b.WriteByte('\n')
|
||||
if len(msg) > 0 {
|
||||
if len(vars) > 0 {
|
||||
fmt.Fprintf(&b, msg, vars...)
|
||||
} else {
|
||||
b.WriteString(msg)
|
||||
}
|
||||
b.WriteByte('\n')
|
||||
}
|
||||
|
||||
TracebackAll(b.String())
|
||||
warnedSet[catchAllMagic] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// BugTraceback will report a bug with a traceback of all goroutines if the
|
||||
// error isn't nil. Use it for reporting abnormal bugs encountered at runtime
|
||||
// that should be fixed.
|
||||
//
|
||||
// Do not use this for bad user input. Errors reported by this function should
|
||||
// not be fatal.
|
||||
func BugTraceback(err error) {
|
||||
if err != nil {
|
||||
reportBugErr(2, err)
|
||||
}
|
||||
}
|
||||
|
||||
// BugTracebackf will report a bug with a traceback of all goroutines.
|
||||
// Use it for reporting abnormal bugs encountered at runtime that should be
|
||||
// fixed.
|
||||
//
|
||||
// Do not use this for bad user input. Errors reported by this function should
|
||||
// not be fatal.
|
||||
func BugTracebackf(s string, a ...any) {
|
||||
reportBug(2, s, a)
|
||||
}
|
||||
|
||||
// BugTracebackOnce will report a bug with a traceback of all goroutines if the
|
||||
// error isn't nil. Use it for reporting abnormal bugs encountered at runtime
|
||||
// that should be fixed. If called multiple time from same invocation, will only
|
||||
// print once.
|
||||
//
|
||||
// Do not use this for bad user input. Errors reported by this function should
|
||||
// not be fatal.
|
||||
func BugTracebackOnce(err error) {
|
||||
if err != nil {
|
||||
reportBugErrOnce(2, err)
|
||||
}
|
||||
}
|
||||
|
||||
// BugTracebackfOnce will report a bug with a traceback of all goroutines.
|
||||
// Use it for reporting abnormal bugs encountered at runtime that should be
|
||||
// fixed. If called multiple time from same invocation, will only print once.
|
||||
//
|
||||
// Do not use this for bad user input. Errors reported by this function should
|
||||
// not be fatal.
|
||||
func BugTracebackfOnce(s string, a ...any) {
|
||||
reportBugOnce(2, s, a)
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
// Copyright 2026 The gVisor Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// FileOpts contains options for creating a log file.
|
||||
type FileOpts interface {
|
||||
// Build constructs the log file path based on the given pattern.
|
||||
Build(logPattern string) string
|
||||
}
|
||||
|
||||
// DefaultFileOpts is the default implementation of FileOpts which supports no
|
||||
// variable substitution.
|
||||
type DefaultFileOpts struct{}
|
||||
|
||||
// Build implements FileOpts.Build.
|
||||
func (f *DefaultFileOpts) Build(logPattern string) string {
|
||||
return logPattern
|
||||
}
|
||||
|
||||
// OpenFile opens a log file using the specified flags. It uses `opts` to
|
||||
// construct the log file path based on the given `logPattern`.
|
||||
func OpenFile(logPattern string, flags int, opts FileOpts) (*os.File, error) {
|
||||
if len(logPattern) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Replace variables in the log pattern.
|
||||
logPath := opts.Build(logPattern)
|
||||
|
||||
// Create parent directory if it doesn't exist.
|
||||
dir := filepath.Dir(logPath)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return nil, fmt.Errorf("error creating dir %q: %v", dir, err)
|
||||
}
|
||||
|
||||
// Open file with the specified flags.
|
||||
f, err := os.OpenFile(logPath, flags, 0644)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error opening file %q: %v", logPath, err)
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
+3
-1
@@ -327,7 +327,7 @@ func Stacks(all bool) []byte {
|
||||
}
|
||||
|
||||
// stackRegexp matches one level within a stack trace.
|
||||
var stackRegexp = regexp.MustCompile("(?m)^\\S+\\(.*\\)$\\r?\\n^\\t\\S+:\\d+.*$\\r?\\n")
|
||||
var stackRegexp = regexp.MustCompile(`(?m)^\S+\(.*\)$\r?\n^\t\S+:\d+.*$\r?\n`)
|
||||
|
||||
// LocalStack returns the local goroutine stack, excluding the top N entries.
|
||||
// LocalStack's own entry is excluded by default and does not need to be counted in excludeTopN.
|
||||
@@ -396,4 +396,6 @@ func CopyStandardLogTo(l Level) error {
|
||||
func init() {
|
||||
// Store the initial value for the log.
|
||||
log.Store(&BasicLogger{Level: Info, Emitter: GoogleEmitter{&Writer{Next: os.Stderr}}})
|
||||
|
||||
warnedSet = make(map[string]struct{})
|
||||
}
|
||||
Reference in new issue
Block a user