Merge pull request #7796 from skepticfx:fix-gvisor-7795

PiperOrigin-RevId: 462248229
This commit is contained in:
gVisor bot
2022-07-20 15:51:18 -07:00
2 changed files with 13 additions and 4 deletions
+11 -4
View File
@@ -24,6 +24,7 @@ package context
import (
"context"
"sync"
"time"
"gvisor.dev/gvisor/pkg/log"
@@ -162,10 +163,8 @@ type logContext struct {
}
// bgContext is the context returned by context.Background.
var bgContext Context = &logContext{
Context: context.Background(),
Logger: log.Log(),
}
var bgContext Context
var bgOnce sync.Once
// Background returns an empty context using the default logger.
// Generally, one should use the Task as their context when available, or avoid
@@ -173,7 +172,15 @@ var bgContext Context = &logContext{
//
// Using a Background context for tests is fine, as long as no values are
// needed from the context in the tested code paths.
//
// The global log.SetTarget() must be called before context.Background()
func Background() Context {
bgOnce.Do(func() {
bgContext = &logContext{
Context: context.Background(),
Logger: log.Log(),
}
})
return bgContext
}
+2
View File
@@ -260,6 +260,8 @@ func Log() *BasicLogger {
//
// This is not thread safe and shouldn't be called concurrently with any
// logging calls.
//
// SetTarget should be called before any instances of log.Log() to avoid race conditions
func SetTarget(target Emitter) {
logMu.Lock()
defer logMu.Unlock()